Accessing variables from ISRs
Is it safe to access global variables that are used by both ISRs and tasks by simply enclosing the access to the variable(s) with enter/exit critical calls? (see method 1)
Or is the proper way to use the method found in tasks.c to get the TickCount? (like method 2)
(Using a PIC – ask me if i’m lovin it – NOT)
Thanks!
Ron.
-———– Method 1 ————-
interrrupts.c
volatile unsigned char ucPeriodDivider = 1;
#pragma interruptlow vIRQfunction save=PRODH, PRODL, section(".tmpdata")
void vIRQfunction (void)
{
….
OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_EDGE_RISE & ucPeriodDivider);
….
}
monitor.c
extern volatile unsigned char ucPeriodDivider;
void vChangeThePeriod(void)
{
taskENTER_CRITICAL();
ucPeriodDivider = 3;
taskEXIT_CRITICAL();
}
-————- Method 2 ————-
interrrupts.c
static volatile unsigned char ucPeriodDivider = 1;
#pragma interruptlow vIRQfunction save=PRODH, PRODL, section(".tmpdata")
void vIRQfunction (void)
{
….
OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_EDGE_RISE & ucPeriodDivider);
….
}
void vChangeThePeriod(volatile unsigned char ucPeriod)
{
taskENTER_CRITICAL();
ucPeriodDivider = ucPeriod;
taskEXIT_CRITICAL();
}
monitor.c
extern volatile unsigned char ucPeriodDivider;
void vMonitor(void)
{
….
vChangeThePeriod (3);
….
}
Accessing variables from ISRs
Method 1 is not dangerous from a mutual exclusion point of view. As long as you are in the critical region the interrupt function cannot touch the variable.