Log in

View Full Version : Timer0 Interrupt Behaviour


Andrew Blair
28-01-2006, 22:50
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?:rolleyes:

Thanks, interrupts are scary.


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++;
}
}

Kevin Sevcik
28-01-2006, 23:07
Erm. The internal clock of the PIC is 10 MHz. So, sadly, your whole plan goes up in smoke because you'd need to interrupt the processor every cycle, which is pretty much impossible.

If you choose a slower sampling frequency, then I recommend the IFI Timer White Paper (http://www.ifirobotics.com/docs/timers_white_paper_2004-jan-14.pdf)

It's pretty simple and informative.

Andrew Blair
28-01-2006, 23:17
Ahh. That might be important. I can easily work around it; I chose it more for it's ease of conversion than the need for precision. That many interrupts is probably a bad thing. But, a question: why would there be a timer capable of 10mhz operation, if it can't be used? Is it just easily synced with the PIC timer, so they decided to throw it in there?

Kevin Sevcik
28-01-2006, 23:29
The timer gets incremented at 10 MHz. However, if it's an 8 bit timer, it will overflow and interrupt after 256 counts, assuming no prescale. If you want it to interrupt faster than this, you preload it with some value everytime it overflows. If you load it with 127, then it would overflow after only 128 more counts. So if you wanted it to interrupt at 10MHz, you'd preload it with 255. Except you'd have to take time to get in and out of the interrupt handler.... Probably, you could get in and out of the handler fast enough to have 100 kHz interrupts, and have your processor doing absolutely nothing else and giving you the dreaded Red Light of Death.

eugenebrooks
29-01-2006, 02:32
But, a question: why would there be a timer capable of 10mhz operation, if it can't be used?

You can use it to accurately measure the time that it takes a high
speed shaft, or wheel, to go around. I can't imagine why you
would want to do that, however... You can also use it to measure
the time of flight of a ball between two light beams, but that is probably
only of interest to technical inspectors at competitions.

Eugene

Mike Betts
30-01-2006, 20:04
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?:rolleyes:

Thanks, interrupts are scary.


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