Professional Documents
Culture Documents
4 CCP Module
4 CCP Module
4 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, PIR1.CCP11F = 0; //optional but its good to do it
prescaler =1, Clk internal while (PIR1.CCP11F==0); // wait for 1st rising edge
TRISC.TRISC2 = 1; // configure CCP1 pin period = CCPR1; //save the first edge time
for input (CCPR1 is accessed as a 16-bit value)
PIR1.CCP11F = 0; //clr CCP for next edge
T1CON.TMR1ON=1; //start timer 1
while (!PIR1.CCP11F); //wait for the 2nd rising edge
T1CON.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 CCP2 and display binary of that %age duty
cycle on PORTB
PWM MODE
Generate a waveform with certain frequency and duty cycle using 8-bit
Timer2.
The PWM duty cycle is specified by using in total of 10 bits: eight MSbs of the CCPRxL register
and two additional LSbs of the CCPxCON register (DCxB1:DCxB0). The result is a 10-bit number
contained in the formula:
Pulse Width = (CCPRxL:DCxB1,DCxB0) * Tosc * TMR2 Prescale
Calculate PR2 reg value from formula and then Choose CCPRxL as percentage of PR2
register.
Do By Yourself:
From code determine
frequency and starting
duty cycle of PWM??
void main() //Mikro C code Try to understand yourself
{ if (!RD1_bit) // button on RD1 pressed
short current_duty_1 = 16; // initial value for current_duty_1 {
Delay_ms(40);
short current_duty_2 = 16; // initial value for current_duty_2 current_duty_1--; // decrement current_duty_1
TRISD = 0xFF; // PORTD as input PWM1_Set_Duty(current_duty_1);
TRISC = 0x00; // PORTC as output }
PWM1_Init(5000); // Initialize PWM1 if (!RD2_bit) // if button on RD2 pressed
PWM2_Init(5000); // Initialize PWM2 {
PWM1_Start(); // start PWM1 Delay_ms(40);
current_duty_2++; // increment current_duty_2
PWM2_Start(); // start PWM2 PWM2_Set_Duty(current_duty_2);
PWM1_Set_Duty(current_duty_1); // Set current duty for PWM1 }
PWM2_Set_Duty(current_duty_2); // Set current duty for PWM2
if (!RD3_bit) // if button on RD3 pressed
while (1) // endless loop {
{ Delay_ms(40);
if (!RD0_bit) // if button on RD0 pressed current_duty_2--; // decrement current_duty_2
PWM2_Set_Duty(current_duty_2);
{
}
Delay_ms(40);
current_duty_1++; // increment current_duty_1 Delay_ms(10); // slow down change pace a little
}
PWM1_Set_Duty(current_duty_1); //Change the duty cycle
}
}
ECCP MODULE
Enhanced CCP modules in some PIC controllers.
Working is same as CCP module
Only register , flag and pin name and position changes
ECCP1CON
ECCP2CON
ECCPR1L , ECCPR1H etc