Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Timer0 Interrupt Behaviour (http://www.chiefdelphi.com/forums/showthread.php?t=42879)

Andrew Blair 28-01-2006 22:50

Timer0 Interrupt Behaviour
 
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.

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


Kevin Sevcik 28-01-2006 23:07

Re: Timer0 Interrupt Behaviour
 
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

It's pretty simple and informative.

Andrew Blair 28-01-2006 23:17

Re: Timer0 Interrupt Behaviour
 
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

Re: Timer0 Interrupt Behaviour
 
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

Re: Timer0 Interrupt Behaviour
 
Quote:

Originally Posted by Andrew Blair
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

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?:rolleyes:

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


All times are GMT -5. The time now is 01:04.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi