Professional Documents
Culture Documents
PIC18F4550 Timer Capture (CCP) Mode - PIC Controllers
PIC18F4550 Timer Capture (CCP) Mode - PIC Controllers
Introduction
Input Capture is widely used in many applications such as:
The time of an event occurrence can be recorded by latching the count when an edge is detected (rising or falling). This can be done
using input capture mode in the microcontrollers.
PIC18F4550 has an in-built CCP (Capture / Compare / PWM) module which has input capture mode. The input capture of the
signal has applications like finding frequency, calculating the duty cycle of the input signal, etc.
1. CCP1
2. CCP2
In capture mode, the input pulse should be connected to CCP1 which is multiplexed with PORTC.2 (RC2) as shown below, and
configure it as the input pin.
1. Timer1
2. Timer3
1. CCPR1 Register is a 16-bit event capture register in which the event captured by timer1 or timer3 i.e. TMR1 or TMR3 register is
copied. This is done by the hardware itself. CCPR1L register and CCPR1H register are the two 8-bit registers that are used for
lower and higher byte storage, respectively.
CCPR1 Register
This is an 8-bit register used to select the capture mode. Let’s see the bit configuration of this register.
CCP1CON Register
0100 = Capture mode is selected for every falling edge detected.
0101 = Capture mode is selected for every rising edge detected.
0110 = Capture mode is selected for every 4th rising edge detected.
0111 = Capture mode is selected for every 16th rising edge detected.
And the other combinations of these bits are used for Compare and PWM mode.
PIR1 Register
CCP1IF :
1 = TMR1 or TMR3 register capture occurred (at falling or rising edge of pulse)
We need to enable this (CCP1IF) flag by enabling the CCP1IE bit (CCP Interrupt enable bit) in the PIE1 register.
PIE1 Register
T3CON register is used for selecting a timer for the capture mode. This is shown below
T3CON Register
These two bits in combination are used for Timer and Capture mode selection.
00 = Timer1 is the capture/compare clock source for both the CCP modules.
01 = Timer3 is the capture / compare clock source for the CCP2,
Timer1 is the capture / compare clock source for the CCP1
1x = Timer3 is the capture / compare clock source for both the CCP modules.
RD16 :
This bit is used to specify that the Timer3 register is 16-bit (TMR3) or two 8-bit (TMR3H and TMR3L).
RD16 = 1, Enable one 16-bit register.
The setting of this bit is required only when we select Timer3 for the capture mode.
1. Initialize the CCPCON1 register for capturing the rising or falling edge.
2. Configure the CCP1 pin as an input pin.
3. If you are using Timer3, then configure the T3CON register to select Timer3 for input capture; otherwise, Timer1 is a default.
4. Also, set the value of the T1CON or T3CON register for deciding whether the timer register should be two 8-bit (TMRxL and
TMRxH) or 16-bit (TMRx).
5. Set CCPR1 and TMR3 / TMR1 register to 0.
Capture Operation
Application
Let’s design an application using PIC18F4550 in which we will measure the Frequency of the input signal and display it on 16x2
LCD. The input signal is connected to the CCP1 (RC2) pin for frequency measurement.
/*
Frequency Measurement using Input Capture Mode in PIC18F4550
http://www.electronicwings.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <p18f4550.h>
#include "osc_config.h"
#include "LCD_8bit_file.h"
#include <string.h>
void main()
float Frequency;
TRISCbits.TRISC2=1;
LCD_Init();
memset(frequency_Hz,0,20);
LCD_String_xy(0,1,"Pulse");
PIE1bits.CCP1IE=1;
PIR1bits.CCP1IF=0;
TMR1IF=0;
while(1)
while(!(PIR1bits.CCP1IF)); /*Wait for Interrupt flag which is generated when edge is detected*/
PIR1bits.CCP1IF=0;
while(!(PIR1bits.CCP1IF)); /*Wait for Interrupt flag which is generated when edge is detected*/
PIR1bits.CCP1IF=0;
LCD_String_xy(2,0,frequency_Hz);
TMR1=0;
memset(frequency_Hz,0,20);