Why does this simple conditional misbehave on a MC56F8006?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Why does this simple conditional misbehave on a MC56F8006?

1,392件の閲覧回数
lancefisher
Contributor III

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)

nop

nop

inc.bp X:0x000244

(0x1627)

 

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.

ラベル(1)
タグ(2)
0 件の賞賛
返信
2 返答(返信)

1,096件の閲覧回数
yibbidy
Contributor V

Hi Lance,

I've been racking my memory for an answer to this is as I've had exactly the same thing happen but I can't remember how I resolved it.  Try changing timer1 to an unsigned short.  I know that doesn’t answer the question but there is something in my memory about it :smileyhappy:  If that works we can go from there.

Shaun

0 件の賞賛
返信

1,096件の閲覧回数
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Iance,

I tried replicating the issue but could not succeed.

Can you share your code so that I can analyse it.

Thanks and Regards

Arpita Agarwal

0 件の賞賛
返信