Professional Documents
Culture Documents
7 Capture Compare PWM PDF
7 Capture Compare PWM PDF
CAPTURE/COMPARE/PWM
MODULES
Microcontrollers
CCP Modules
CCP : Capture/Compare/PWM.
Allows the user to time and control different events.
Capture Mode, allows timing for the duration of an event
(TMR1 register).
Compare Mode compares values contained in two
registers at some point. One of them is the timer TMR1
register. Allows the user to trigger an external event
when a predetermined amount of time has expired.
PWM - Pulse Width Modulation can generate signals of
varying frequency and duty cycle.
Two modules - CCP1 and CCP2.
The Enhanced PWM features available on CCP1 only
Microcontrollers
Capture Mode
In Capture mode, CCPR1H:CCPR1L captures the
16-bit value of the TMR1 register when an event occurs
on pin RC2/CCP1. An event is defined as one of the
following:
Every falling edge
Every rising edge
Every 4th rising edge
Every 16th rising edge
Microcontrollers
Microcontrollers
Microcontrollers
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
int8 capture_rising_edge;
int8 got_pulse_width;
int16 ccp_delta;
Microcontrollers
Microcontrollers
Compare Mode
Microcontrollers
Microcontrollers
10
Microcontrollers
Compare Mode
11
Microcontrollers
Compare Mode
#include<16f877a.h>
#fuses XT,NOLVP,NOWDT,PUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#define high_start 2
//25
unsigned int8 seconds, high_count;
#INT_CCP1
//Interrupt procedure
clock_isr() {
high_count -= 1;
if(high_count==0) {
++seconds;
if (seconds==60) seconds=0;
high_count=high_start;
putc(254); putc(1);
delay_ms(10);
printf("%u seconds \n\r", seconds);
}
}
12
Microcontrollers
Compare Mode
void main() {
delay_ms(500); putc(254); putc(1); delay_ms(10);
seconds=0;
printf("%u seconds \n\r", seconds);
high_count = high_start;
setup_ccp1 (CCP_COMPARE_INT);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8 );
set_timer1(0);
CCP_1=5000;
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(TRUE) {} ;
}
13
Microcontrollers
In Pulse Width
Modulation mode, the
CCPx pin produces up
to a 10-bit resolution
PWM output
14
Microcontrollers
PWM OUTPUT
15
PWM output
Microcontrollers
#include "16F877A.h"
void main()
{
setup_ccp1(ccp_pwm);
setup_timer_2(T2_DIV_BY_16,250,1);
set_pwm1_duty(500);
while(1){}
}
duty cycle = value / [ 4 * (PR2 +1 ) ]
16
17
Microcontrollers
Microcontrollers
18
Microcontrollers
19