Sunteți pe pagina 1din 8

CAPTURE/COMPARE/PWM (CCP) Modules in PIC 16F877

Capture-Compare-Pulse-Width-Module (CCP) is a special module designs for modulation and waveform generation applications. This module basically works on three different modes (capture/compare and PWM odes). The PIC 16F877 chip contains two CCP ports (CCP1 and CCP2). Each of this CCP module contains 16 bit registers which works as 16-bit Capture register 16-bit Compare register PWM Master/Slave Duty Cycle registers The CCP1 and CCP2 modules are identical in its operation except in its special event trigger operation. In each CCP modules, the capture, compare and PWM modes using different timer resources. The table below shows the different CCP modes and its timer resources. The detailed explanations and functions of CCP module is given below.

CCP Timer Source-Interaction

CCP1 Module

Capture/Compare/PWM Register 1 (CCPR1) is a 16 bit register comprised of two 8-bit registers: CCPR1L (low byte) and CCPR1H (high byte). The CCP1CON register controls the operation of CCP1. The special event trigger is generated by a compare match and will reset Timer1.

CCP2 Module
Capture/Compare/PWM Register 2 (CCPR2) is comprised of two 8-bit registers: CCPR2L (low byte) and CCPR2H (high byte). The CCP2CON register controls the operation of CCP2. The special event trigger is generated by a compare match and will reset Timer1 and start an A/D conversion (if the A/D module is enabled).

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 The type of event is configured by control bits, CCP1M3:CCP1M0 (CCPxCON<3:0>). When a capture is made, the interrupt request flag bit, CCP1IF (PIR1<2>), is set. The interrupt flag must be cleared in software. If another capture occurs before the value in register CCPR1 is read, the old captured value is overwritten by the new value. The block diagram of capture mode is shown below.

Capture Mode Block Diagram

Compare Mode
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 The action on the pin is based on the value of control bits, CCP1M3:CCP1M0 (CCP1CON<3:0>). At the same time, interrupt flag bit CCP1IF is set. The compare mode block diagram is shown below.

Compare Mode Block Diagram

PWM Mode (PWM)


In Pulse Width Modulation mode, the CCPx pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1 pin an output. Figure shows a simplified block diagram of the CCP module in PWM mode.

PWM Mode Block Diagram

Setup for PWM Operation


The following steps should be taken when configuring the CCP module for PWM operation: 1. Set the PWM period by writing to the PR2 register. 2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits. 3. Make the CCP1 pin an output by clearing the TRISC<2> bit. 4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON. 5. Configure the CCP1 module for PWM operation.

The table below shows PWM FREQUENCIES and RESOLUTIONS AT 20 MHz and registers associated with CCP timer1/2 modules.

Register Organization-1

PIC16f877A, PWM, to increase the speed of motor according to temperature increasing


unsigned char speed=50; CCP1CON = 0b00001100; //PWM Mode PR2 = 0xFF; //PWM Period Setting//PWM frequecy set as 4.88KHz T2CON = 0b00000101; //Timer2 On, prescale 4 CCPR1L=0; else if (temperature>27) {

CCPR1L=speed; }

PIC16f877A, PWM, to increase the speed of motor according to temperature increasi

{ // if(speed<255)speed+=1; CCPR1L=speed; // INCREMENT THE SPEED OF MOTOR delay(5000);

} else if (temperature>35) { //if(speed>0)speed-=1; CCPR1L=speed; // DECREMENT THE SPEED OF MOTOR delay(5000); } 3333333333333333333333333333333333333333333333
// initialise PWM using Capture Compare PWM Module 2 void PWMinitialise(void) { // assume Fosc uses oscillator 8MHz - call systemInitialise() first TRISD2=0; // set RD2/CCP2 is output TRISD7=0; // set RD7 is output PR2=0x3f; // set PR2 to 31.2 kHz PWM (PWM value will be 8 bits) // Configure Capture Compare PWM Module 2 CCPR2L=0; // set PWM duty cycle to 0 CCP2CON=0xC; // set PWM mode TMR2ON=1; // Turn on Timer 2 PWM } // set PWM duty cycle - value between 0 and 100 void PWMcontrol(int dutyCycle) { // get 8 bit value for PWM control at 31.2 kHz PWM dutyCycle = (int) (dutyCycle * 255L / 100L); // get 8 bit value RD7=1; // switch on motor // load most significant 6 bits in CCPR2l and least significant two bits in CCP2CON CCPR2L=dutyCycle >> 2; CCP2CON = (CCP2CON & 0xF) + ((dutyCycle << 4) & 0x30); // set bits 4 and 5 }

S-ar putea să vă placă și