|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
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? 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++;
}
}
|
|
#2
|
|||||
|
|||||
|
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. |
|
#3
|
|||||
|
|||||
|
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?
|
|
#4
|
|||||
|
|||||
|
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.
|
|
#5
|
|||
|
|||
|
Re: Timer0 Interrupt Behaviour
Quote:
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 Last edited by eugenebrooks : 29-01-2006 at 02:35. |
|
#6
|
||||
|
||||
|
Re: Timer0 Interrupt Behaviour
Quote:
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 |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining ADC, Serial, and Camera_s codes | kaszeta | Programming | 6 | 22-01-2006 20:23 |
| TTL port to a serial port on a demo board | ImmortalAres | Programming | 16 | 09-07-2005 23:44 |
| Accelerometer code | ImmortalAres | Programming | 28 | 04-06-2005 01:02 |
| heres the code. y this not working | omega | Programming | 16 | 31-03-2004 15:18 |
| Interrupts Questions | mightywombat | Programming | 0 | 03-01-2004 14:50 |