Quote:
|
Originally Posted by Astronouth7303
I have had problems with interupts.c and timers. I use Timer 2 (and/or 1) to increment a variable and if that variable overflows, it increments another one. The problem is that I can't ever seem to get either to change. Help?
|
Sounds like the timers haven't been enabled in your initialization code.
Here's a sample initialization of Timer 4 to cause a 4ms interrupt.
Code:
OpenTimer4(TIMER_INT_ON &
T4_PS_1_16 &
T4_POST_1_10);
WriteTimer4(6); /* Preload timer to overflow after 4ms */
Here's the corrsponding user_routines_fast.c interrupt code.
Code:
unsigned long Clockms; // 1 millisecond clock
unsigned long Clockcs; // 100 millisecond
unsigned long Clocksec; // 1 second clock
static unsigned char t100ms=0;
static unsigned int t1sec=0;
...
else if (PIR3bits.TMR4IF) /* TIMER 4 INTERRUPT */
{
/** This provides us with a clock for timing events **/
PIR3bits.TMR4IF = 0; /* Clear Timer interrupt flag */
WriteTimer4(6); /* Reset Timer to overflow at 4ms */
Clockms += 4; /* milliseconds */
if (++t100ms >= 25)
{
t100ms = 0;
Clockcs++; /* tenths of seconds */
if (++t1sec >= 10)
{
t1sec = 0;
Clocksec++; /* seconds */
}
}
}