Jump to content

Wrong Condition Accessed !


Recommended Posts

Bug description:

When using a switch/case structure or if/ else if structure, the code seems to be executed regardless of the inputs.

 

Steps to reproduce:

 

Here is the code source. A simple program that performs a LED flashing on port A according to the word presented on port B.

 

#include <system.h>

char read;

 

void main()

{

trisb = 0xFF; //configure port B

trisa = 0x00;//configure port A

spbrg = 25; // Asuart config for future apps.

txsta = 0x26;

rcsta = 0x90;

 

 

 

#ifdef _PIC16

porta = 0x00; //clear port A

 

 

 

#endif

 

 

while(1) //endless loop

{

read=(portb&&0x03); //bit mask for the two LSB

switch (read)

{

 

case 0x00 : //flash led with long period

porta=0x01;

delay_s(1);

porta=0x00;

delay_s(1);

break;

 

case 0x01 : //flash LED with short period

porta=0x01;

delay_ms(50);

porta=0x00;

delay_ms(50);

break;

 

 

case 0x03 : //flash LED with intermediate periode

porta=0x01;

delay_ms(200);

porta=0x00;

delay_ms(200);

break;

 

 

 

}

}

}

 

Expected behaviour:

It is obvious that the program should be considering the two LSB only. However, the case 0x01 is true even if the word 0x04 is presented ! When 0x03 is presented, the case 0x01 is true and therefore the break instruction sends the PC to the while instruction.

 

Is the problem 100% reproduceable:

This problem reproduces every time, even after reinstalling the software or restarting computer. The debugger AND the PIC are behaving in the same way.

 

IDE version: SourceBoost IDE version 6.20

Compiler: Boost C

Compiler version: Compiler version 6.20

Target device: PIC16F873

OS: WinXP

 

Comments:

When using PIC16F873 as the target device, the LED block linked to port A does not copy the port A state. Moreover, the port A register does not change value (0x00 remains) even if the instruction "porta=0x01" had been executed.

 

When using another target device, the port A (register and associated led block) is behaving normally but the condition testing problem remains

Link to post
Share on other sites

jetster94,

 

This looks like a bug in your code!

 

Try correcting your programming error.

 

Change:

read=(portb && 0x03); //bit mask for the two LSB

To Read:

read=(portb & 0x03); //bit mask for the two LSB

 

Then things may work as you expect :)

 

Regards

Dave

Link to post
Share on other sites

Thanks a lot Dave for your advice. This had solved the condition testing problem. I used to write && for bit masking in LabWindows CVI, such differences may arise when using different compilers, this will be an excellent thing to bear in mind in the future. :)

 

Anyway, I maintain the bug concerning portA register and led block failure in debugger mode.

Link to post
Share on other sites

jetster94,

 

Anyway, I maintain the bug concerning portA register and led block failure in debugger mode.

 

Please indicate if you tried this on the actual hardware and it worked, but it failed when using the debugger/simulator.

 

Many devices that have ADCs by default have PORTA configured for analog input. You have to turn off the ADC function on pins you want to use for digital I/O. Maybe this is your problem.

 

Regards

Dave

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...