3 CCP Module
3 CCP Module
3 CCP Module
DUTY CYCLE MEASUREMENT. The duty cycle is the percentage of time that the signal is high within
a period in a periodic digital signaL The measurement of duty cycle is illustrated in Figure
Scenerio: Time Period Measurment of a pulse
Problem:Use the CCP channel 1 in capture mode to measure the period of an unknown signal, assuming
that the PIC18 microcontroller is running with a 10-MHz crystal oscillator. The period of the unknown
signal is shorter than 65536 clock cycles.
SOLUTION: The user needs to capture either two consecutive rising or falling edges and
calculate their difference in order to measure the period.
void main (void) while(1)
{ {
unsigned int period; CCPR1L=0; //initialize CCP1 regs
CCP1CON = 0x05; // capture on every CCPR1H=0;
rising edge TMR1H=0; //initialize timer value
T3CON = 0x00; //Timer1 for capture TMR1L=0;
TlCON = 0x00; //enable 16-bit timer1, PIR1bits.CCP11F = 0; //optional but its good to do it
prescaler =1, Clk internal while (PIR1bits.CCP11F==0); // wait for 1st rising edge
TRISCbits.TRISC2 = 1; // configure CCP1 pin period = CCPR1; //save the first edge time
for input (CCPR1 is accessed as a 16-bit value)
PIR1bits.CCP11F = 0; //clr CCP for next edge
T1CONbits.TMR1ON=1; //start timer 1
while (PIR1bits.CCP11F)); //wait for the 2nd rising edge
T1CONbits.TMR1ON=0; //stop timer 1
period = CCPR1 - period; //second edge time – first edge time
CCP1CON = 0x00; //disable CCP1 capture
//write a line here to get period in seconds or micro seconds
Write Duty cycle measurement code of a pulse
using CCP1