AnsweredAssumed Answered

Why does this simple conditional misbehave on a MC56F8006?

Question asked by Lance Fisher on Feb 17, 2014
Latest reply on May 21, 2014 by Shaun James

I have the following code in a Processor Expert project (CW 10.5) that get's executed once every 10ms:


    if (timer1 < 255) timer1++;


The timer1 variable is declared as an unsigned 8 bit integer (UInt8) initialized to zero and is set to zero under certain conditions and tested for being greater than some other value elsewhere E.G..


     if (condition1) timer1 = 0;

     else if (timer1 > value1) doSomethin();


The intent is to execute "doSomethin" if condition1 is false for 10 * value1  consecutive milliseconds.


The behavior I'm seeing is that when timer1 reaches 255 is rolls over to zero.  I've tried changing the "255" limit to 250 but it still misbehaves the same way (always increments).

I did a project wide text search for the "timer1" variable which didn't turn up any other references to timer1.  I also changed the initialization and (conditional) reset value from 0 to 2 and confirmed that it still goes from 255 to 0 rather than staying at 255.


I looked at the disassembly  listing and find that it appears the compiler has generated the code correctly:


moveu.bp X:0x000244,A

cmp.b #255,A

bcc Fmain+0x5a3 (0x1627)



inc.bp X:0x000244



I did some instruction step debugging and found that if I break on the line after the last line of that assembly code and set timer1 to 254 then run to the breakpoint again, CPU register A is zero but RAM location 0x244 still contains 255.  The only way I can see that happening would be if an interrupt occurred right after the move from 0x255 and the interrupt routine cleared register A.  I did verify that all PE components that have interrupts enabled are configured with "Interrupt preserve registers" set to yes.


I did find that I get the expected behavior  if I modified the code thusly:

if (timer1 < 255) timer1++;

else timer1 = 255;


But that extra line shouldn't be necessary and I'm concerned about what else might be affected in a similar manner.


I'm at a loss WRT how to proceed from here.  Any help would be greatly appreciated.