Pulse & Frequency meter using CCP FC6
Posted: Sat Apr 26, 2014 6:39 pm
Attached is a flowchart that is created with Flowocde V6
Flowchart is not all my work so credit to Sean who use to work at Matrix.
The objective is to use CCP (Capture Compare & Pulse width modulation) function of a 16F1937.
This should also work for a majority of microcontrollers that have a 16bit timer 1 interrupt and CCP.
Eblocks used:
Eb006 v9 Programmer with J18&J19 set to A6&A7
Eb005 2x16 LCD connected to portD
EB070USB Test pod
Target device = 16F1937
Oscillator mode : Internal @ 32MHz
Pulse/ Frequency input connected to CCP1 (port C2)
Pulse / Frequency range = 7143ms/70Hz to 5uS/100000Hz
For an example, connected the USB Test Pod to port C and set frequency going to pin C2 at 50KHz: The LCD displayed: Accuracy was very good for the range stated above.
ICT was also displaying correct pulse duration of 10uS: For any users not yet got Flowcode V6, you can down download a 30day trial From here
How CCP works to measure pulse with is:
when CCP interrupt is first triggered (by a positive edge going to port C2) then Timer1 is enabed (t1con = 0x01;) and timer1 is set for falling edge (ccp1con = 0x04; ).
When falling edge occurs, timer1 CCP interrupt service routine is again accessed, the values stored in ccpr1h & ccpr1l 8 bit CCP registers are converted in to 16bit unsigned integer that is supported by Flowcode6
The secret of converting to a pulse width is using the value from the CCP register and diving by 8 this works because with prescaler set to 1:1 each count in duration is 1/(32MHz/4) = 125nS then x 8 = 1uS = 8 time longer.
Therefore after the value of ccpr1h & ccpr1l registers are divided by 8 (ControlVal = PulseWidth >> 3 ) the result is directly in microseconds.
Hope this helps if trying to use CCP interrupt to measure pulse width or frequencies.
Martin
Flowchart is not all my work so credit to Sean who use to work at Matrix.
The objective is to use CCP (Capture Compare & Pulse width modulation) function of a 16F1937.
This should also work for a majority of microcontrollers that have a 16bit timer 1 interrupt and CCP.
Eblocks used:
Eb006 v9 Programmer with J18&J19 set to A6&A7
Eb005 2x16 LCD connected to portD
EB070USB Test pod
Target device = 16F1937
Oscillator mode : Internal @ 32MHz
Pulse/ Frequency input connected to CCP1 (port C2)
Pulse / Frequency range = 7143ms/70Hz to 5uS/100000Hz
For an example, connected the USB Test Pod to port C and set frequency going to pin C2 at 50KHz: The LCD displayed: Accuracy was very good for the range stated above.
ICT was also displaying correct pulse duration of 10uS: For any users not yet got Flowcode V6, you can down download a 30day trial From here
How CCP works to measure pulse with is:
when CCP interrupt is first triggered (by a positive edge going to port C2) then Timer1 is enabed (t1con = 0x01;) and timer1 is set for falling edge (ccp1con = 0x04; ).
When falling edge occurs, timer1 CCP interrupt service routine is again accessed, the values stored in ccpr1h & ccpr1l 8 bit CCP registers are converted in to 16bit unsigned integer that is supported by Flowcode6
The secret of converting to a pulse width is using the value from the CCP register and diving by 8 this works because with prescaler set to 1:1 each count in duration is 1/(32MHz/4) = 125nS then x 8 = 1uS = 8 time longer.
Therefore after the value of ccpr1h & ccpr1l registers are divided by 8 (ControlVal = PulseWidth >> 3 ) the result is directly in microseconds.
Hope this helps if trying to use CCP interrupt to measure pulse width or frequencies.
Martin