|
Re: Timer0 Interrupt Behaviour
Quote:
|
Originally Posted by Andrew Blair
I'm trying to write some code (actually, modifying Kevin's) with a defunct MPlab, so unfortunately I can't check myself with a test build this weekend. I'm using timer0 as a timer, and have it configured for 10mHz (Or 100 ns). I intend to simply, every 100ns, poll some variables. I have three questions:
1. The interrupt routine is called after the timer overflows. Does it overflow at it's frequency (10mHz)?
2. Do I have to turn off the timer, before I do anything in the interrupt? And will, when executed, the code just enter back into the timer, or do I have to somehow reset it?
3. Did I screw anything else up?
Thanks, interrupts are scary.
Code:
void Timer_0_Int_Handler(void)
{
// this function will be called when a timer 0 interrupt occurs
unsigned char tenth_micro_seconds;
unsigned int micro_seconds;
tenth_micro_seconds++;
if (tenth_micro_seconds>=10)
{
micro_seconds++;
}
}
|
]
Andrew,
The biggest problem that jumps out at me is that your variables (tenth_micro_seconds and micro_seconds) are local to your interrupt handler.
They must be staic, volatile and initialized properly.
See section 1.10 of K&R and 2.9.2.1 in the C18 Compiler User's Guide.
Mike
__________________
Mike Betts
Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013
As easy as 355/113...
|