|
Re: Help needed timing a pulse (2004 Robovation)
So, like others have suggested, I'd look at Kevin's code. But I'd specifically look at the 2005 frc_interrupts.zip file. That's a really good interrupt handler, AND it shows you how to set up the timers. As far as actualy implementation:
Use an 1:8 prescale on a 16-bit Timer, anything else is too short. Start the timer, leave it running, it'll save you the bother of starting and stopping it needlessly. Disable the Timer's Interrupt, you don't need it.
Use one of the KBI interrupts, handled by Int_3_Handler through Int_6_Handler. These interrupt on rise and fall. The value passed by the Int Handler function tells you what the state of the pin is.
On the rising edge, when the value passed is 1, clear the timer. If it's in 16-bit Read Mode, write 0 to TMRxH THEN write 0 to TMRxL.
On the falling edge, when the value passed is 0, read TMRxL to a temp variable, read TMRxH to your actual 16-bit variable, shift 8 bits to the left, and add your temp variable. You're done.
Stopping the timer is pointless, you have to clear it no matter what, so just clear it at the start and you're fine.
Your program should copy the value of the variable this routine saves to to a new variable and work with the new variable, as the routine might change the value halfway through your computation otherwise. I'm pretty sure you don't have to disable interrupts while in the interrupt handler itself in this case.
Using the CCPs is a good bit more complicated, but you'll get a much more accurate time measure, as the CCP handles saving the Timer value the instant the of the edge trigger, without the delay of entering the interrupt routine, etc.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.
Lone Star Regional Troubleshooter
|