View Single Post
  #4   Spotlight this post!  
Unread 25-10-2006, 02:25
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 Josh Siegel
Hi everyone,
I have been reading about CCP pins and interrupts, and am at a complete loss as to what the -simplest- way of timing an input pulse is. Here are the specifics of my application:
-The signal varies from 1mS to 36.99mS
-There is a 65mS low signal between pulses
-I am currently using, essentially, the default code (added sensor capture and filtering for sonar, but the framework is the same).
-It would be best if lag during capture could be avoided (so that the proc. doesn't wait for a signal when it could be filtering, etc.)
-I will, obviously, need access to the up-to-date value as stored in a variable.
-My PWM generator specifies down to 10uS readability; 100uS would be fine for my purposes.
-The signal is 0V or 5V, through a sensor powered by the analog pins.

Now, I know interrupts can be used like the sonar code, tracking rising and falling edges of the square wave. I also gather that CCP can do this with one pin, and appears like it will not lag the program.

How can I impliment something like this? What code do I need to add to the default code? What pins do I use?

Thank you,
Josh
Not really a fan of CCP I am.. well, assuming you know the key registers, try the following:
1. find an available timer, 8 or 16 bit, and init it to count such that it counts to max within ur 37ms. Or around that time. Do not set timer overflow to activate an interrupt. Do not start the timer running.
2. take one of ur interrupt pins and set it to activate on to-high and to-low transitions. in the interrupt, read back the pin to differentiate between to-high and to-low events.
3. during to-high event, init timer to 0 and start the timer.
4. during to-low event, stop the timer and copy the 16it value to a 16bit variable
5. now let the pulses come in, have the output printf-ed and see if anything goes wrong..

this is fully pulse-activated interrupt based so you go about doing ur normal routines leaving this to run by its own , reading the 16bit variable to check for the last pulse recorded.

edit: about disabling interrupts when modifying values.. in this scenario u probably wont need to. But you may want to avoid reading the 16bit variable while it is in the midst of being updated... I'm not sure if 16 bit read/write are single instruction (but longer clocks) or multi instruction. Probably multi I guess.. get someone to verify this.
__________________
| jamesyong.net |
FVC2007, FTC2008

Last edited by yongkimleng : 25-10-2006 at 03:01.