Page 1 of 1
PIC CCP Assistance
Posted: Wed Feb 09, 2011 11:10 pm
by thesaxmachine
Does anyone have experience using the input capture on the PIC micro?
Re: PIC CCP Assistance
Posted: Thu Feb 10, 2011 1:23 pm
by SolTurboLove
I try and avoid it if possible...
what are you trying to do?
Re: PIC CCP Assistance
Posted: Thu Feb 10, 2011 1:28 pm
by thesaxmachine
Time period measurement. There is a sensor (si1120) that outputs PWM based on position.
Re: PIC CCP Assistance
Posted: Thu Feb 10, 2011 3:41 pm
by SolTurboLove
Oh, i see...hmm...
CCP is probably what you should use...
but if it were me i'd just put the PWM input to an external interrupt pin...and start/stop a timer in the interrupt service routine.
the output of the timer will be equal to the pulse width.
Kinda brute-force method...but it'll work

Re: PIC CCP Assistance
Posted: Thu Feb 10, 2011 6:04 pm
by JamesCooper
It's been several years since I've looked at a PIC, but I can probably help you out.
I assume you just need to know the pulse width, not the duty cycle, right?
First, make sure you have a timer available. It can be shared with something else, as long as the timer won't be stopped or reset at any time during the capture; it must be free-running. Make sure the timer period is at least as long as your pulse width (if possible; it's somewhat more complicated if we don't do this). Setup two unsigned, 16 bit variables: one for the time of the rise and one for the time of the fall and one boolean as a flag to tell you when the pulse width has been captured. You'll need to write an interrupt handler that checks what edge it's detecting and handle them separately. For the rising edge, it should disable the interrupt, save the captured value to the "rise time" variable, change the edge detection to falling edge, and re-enable the interrupt. For the falling edge, it should disable the interrupt, save the captured value to the "fall time" variable, and set the flag that says the pulse width has been captured. In the rest of your code, to start the thing you set the CCP to detect the rising edge and enable the interrupt. Then spin and wait or do other things until the "pulse width captured" flag is set. You get the pulse width by subtracting the rise time from the fall time (make sure everything is unsigned so that a rollover is handled properly). You end up with the number of timer ticks, which you can use as is or convert to time units.
Let me know if you have any problems or any specific questions. This is kind of a general approach and might need to be tweaked depending on what needs are.