View previous topic :: View next topic |
Author |
Message |
CCS_neophite Guest
|
ISR global variable change |
Posted: Wed Feb 02, 2005 1:42 pm |
|
|
I need to modify a variable down in an ISR routine. Do I need to use a pointer to allow the routine to access the variable? Thanks. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Feb 02, 2005 2:10 pm |
|
|
Short answer is no.
Ex
#include...
#fuses...
int8 myownglobal;
#int_xxx
myownisr()
{
myownglobal=99;
}
main()
{
while(1);
} |
|
|
valemike Guest
|
one caveat of global variables and ISRs... |
Posted: Wed Feb 02, 2005 4:16 pm |
|
|
Suppose we had an "unsigned long", i.e. anything bigger than a byte.
I would find it safer to disable interrupts when reading that variable, or especially changing it, while not in an ISR. Reason being, an interrupt might occur right in the middle of that multi-byte manipulation operation.
Maybe i'm being paranoid, but this practice arose back in my days of RTOSes, semaphores, globals, tasks accessing the same variable, etc. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Feb 03, 2005 9:34 am |
|
|
valemike is right with the caution about reading or writing variables that are more than int8 in length that are assigned within an isr. A possible method to mitigate this is to have the globals that are updated by isr code read inside a function
Ex template
int16 myisrglobalreader()
{
int16 temp;
disable_interrupts(the interrupt that writes to global);
temp=myglobalvar;
enable_interrupts(the interrupt that writes to global);
return temp;
}
main()
{
int16 value;
value=myisrglobalreader();
while(1);
} |
|
|
|