View previous topic :: View next topic |
Author |
Message |
AnthonyF Guest
|
Interrupts changing variables as they are being used |
Posted: Sat Nov 10, 2007 6:42 pm |
|
|
Hi,
I am doing a comparison using an if statement on a 32-bit variable. There is a hardware interrupt that will change this variable. Is it possible for the interrupt to happen in the middle of the comparison? Could this be a problem?
Code: |
int32 counter = 0;
#int_ext
void ext_isr(){
counter++;
}
void main(){
while(TRUE){
if(counter > 50000)
run_task();
}
}
|
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Nov 10, 2007 9:01 pm |
|
|
Yes, that is a common problem with multi-byte comparisons and interrupts. The solution is to disable relevant interrupts while the comparison is made. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Sun Nov 11, 2007 8:32 am |
|
|
Better yet, disable ints, copy the variable to a temp, enable ints, then do the compare (in the interest of keeping ints off as short a time as possible)
Ken |
|
|
|