I have written a simple program by GCC-AVR to detect key press. I found the debounce loop cannot be break.
In file main.c:
extern unsigned char timerDebounce=0;
: : c=pressKey(); if(c==-1) { } else { timerDebounce=5; while(timerDebounce!=0);//cannot break here col=pressKey(); : :
In file timer.c:
unsigned char timerDebounce=0;
void SIG_OVERFLOW0 (void) __attribute__ ((signal));
void SIG_OVERFLOW0 (void) { timerCounter25ms=(timerCounter25ms+1)%50; if(timerCounter25ms==0) { if(timerDebounce==0) { } else timerDebounce--;
: :
Then I disasmbler the program and found:
timerDebounce=5;
+0000006D: E085 LDI R24,0x05 Load immediate +0000006E: 93800071 STS 0x0071,R24 Store direct to data spacewhile(timerDebounce!=0);
+00000070: 2388 TST R24 Logical AND +00000071: F7F1 BRNE -0x02 Branch if not equalObviously the "timerDebounce" is saved at SRAM location 0x71. I have traced the program and found the value at the location is 0 during looping , and the looping is caused by the program continues test the register R24 rather than the SRAM location 0x71 for branching if zero.
Is it a bug in GCC-AVR? Thanks!