0% found this document useful (0 votes)
349 views19 pages

7 Capture Compare PWM

The document discusses the capture/compare/PWM modules on microcontrollers. It describes the different modes of the CCP modules - capture mode, compare mode, and PWM mode. Capture mode allows timing of events by reading the timer value. Compare mode triggers an output when the timer matches a preset value. PWM mode generates variable duty cycle output signals. Examples are given to demonstrate capturing a pulse width, comparing values to trigger an output, and generating PWM output while varying the duty cycle.

Uploaded by

crazyllmonkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views19 pages

7 Capture Compare PWM

The document discusses the capture/compare/PWM modules on microcontrollers. It describes the different modes of the CCP modules - capture mode, compare mode, and PWM mode. Capture mode allows timing of events by reading the timer value. Compare mode triggers an output when the timer matches a preset value. PWM mode generates variable duty cycle output signals. Examples are given to demonstrate capturing a pulse width, comparing values to trigger an output, and generating PWM output while varying the duty cycle.

Uploaded by

crazyllmonkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Microcontrollers

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

CAPTURE MODE OPERATION BLOCK


DIAGRAM

Microcontrollers

Capture a positive pulse

Capture a positive pulse

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

Capture a positive pulse


#int_ccp1
void ccp1_isr(void) {
static int16 t1_rising_edge;
// If current interrupt is for rising edge.
if(capture_rising_edge) {
setup_ccp1(CCP_CAPTURE_FE); capture_rising_edge = FALSE;
t1_rising_edge = CCP_1;
}
else {
setup_ccp1(CCP_CAPTURE_RE); capture_rising_edge = TRUE;
ccp_delta = CCP_1 - t1_rising_edge; got_pulse_width = TRUE;
}
}
7

Microcontrollers

Capture a positive pulse


main() {
int16 pulse_width_ms; int16 local_ccp_delta;
got_pulse_width = FALSE; capture_rising_edge = TRUE;
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8 );
enable_interrupts(INT_CCP1); enable_interrupts(GLOBAL);
while(1) {
if(got_pulse_width)
{
disable_interrupts(GLOBAL);
local_ccp_delta = ccp_delta;
enable_interrupts(GLOBAL);
pulse_width_ms = local_ccp_delta / 125;
putc(254);
putc(1); delay_ms(10);
printf("%lu ms \n\r", pulse_width_ms);
got_pulse_width = FALSE;
}
}
}

Compare Mode

Microcontrollers

In Compare mode, the 16-bit CCPR1 register value is


constantly compared against the TMR1 register pair
value. When a match occurs, the RC2/CCP1 pin is:
Driven high
Driven low
Remains unchanged

Microcontrollers

COMPARE MODE OPERATION BLOCK


DIAGRAM

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

Pulse Width Modulation


PWM Mode

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

Demo PWM output


#include "16F877A.h"
#fuses XT,NOLVP,NOWDT,PUT
#use delay(clock=4000000)
unsigned int8 current_duty, old_duty;
void main() {
current_duty = 128;
old_duty = 0;
setup_ccp1(ccp_pwm); setup_timer_2(T2_DIV_BY_16,250,1);
set_pwm1_duty(current_duty);
while (1) {
if (!input(PIN_A0))
current_duty +=5;
if (!input(PIN_A1))
current_duty-=5 ;
if (old_duty != current_duty) {
set_pwm1_duty(current_duty);
old_duty = current_duty; OUTPUT_B(old_duty);
}
Delay_ms(200);
}
}

18

Microcontrollers

Demo PWM output

19

You might also like