View Single Post
  #7   Spotlight this post!  
Unread 25-10-2006, 13:51
yongkimleng yongkimleng is offline
deus ex programmeur
AKA: James Yong
FTC #0747
Team Role: Mentor
 
Join Date: Aug 2006
Rookie Year: 2004
Location: Singapore, West
Posts: 134
yongkimleng is a jewel in the roughyongkimleng is a jewel in the roughyongkimleng is a jewel in the rough
Send a message via MSN to yongkimleng
Re: Help needed timing a pulse (2004 Robovation)

Quote:
Originally Posted by Kevin Sevcik
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.
May I ask, is there a possibility where during the copy of the first 8 bits, the value is something like 255, and when it proceeds to copy the next 8 its, it increments by 1 (due to the overflow of the first 8 bits when the timer ticks a few more times) before it gets copied, resulting in a larger value than expected?..
__________________
| jamesyong.net |
FVC2007, FTC2008