Login

Lost your password?
Don't have an account? Sign Up

Capture/Compare/PWM (CCP) Module in PIC18F4550

Would you like to join our comprehensive practical video course on the PIC18F4550 Microcontroller?

Introduction: CCP module in PIC (PIC18F4550)

The Capture/Compare/PWM (CCP) module is one of the most versatile peripherals available in the PIC18F4550 microcontroller. It allows developers to measure time intervals, generate precise timing signals, and produce Pulse Width Modulated (PWM) outputs for controlling devices like motors and LEDs. PIC18F4550 includes two CCP modules (CCP1 and CCP2), which can operate in any of the three modes — Capture, Compare, or PWM — depending on the application requirements.

Overview of CCP Module

The CCP module works by using one of the internal timers (Timer1, Timer2, or Timer3) as a time base. Depending on the selected mode, it can either capture a timestamp on an input event, compare a timer value with a register, or generate a PWM output.

CCP Operating Modes
  • Capture Mode: Records the timer value when an event occurs on the CCP pin (used for frequency or pulse width measurement).
  • Compare Mode: Generates an output when the timer value matches the CCP register (used for time-based events).
  • PWM Mode: Produces a continuous pulse waveform with controllable duty cycle and frequency (used for motor control or LED dimming).

Capture Mode

In Capture mode, the CCP module captures the value of a timer when a specific event occurs on the input pin (CCP1 or CCP2). These events can be a rising edge, falling edge, or both. The captured value is stored in the CCPRx register, which can be used to calculate the time between events.

Steps to Configure Capture Mode:
  1. Select the timer source (usually Timer1 or Timer3).
  2. Set the capture mode in the CCPxCON register (e.g., capture on every rising edge).
  3. Enable the CCP interrupt if you want to handle captures through ISR.
  4. Read the captured value from CCPRxH:CCPRxL registers.
Example Applications:
  • Measuring the period or frequency of an input signal
  • Determining pulse width of an external waveform
  • Event timing in automation systems

Compare Mode

In Compare mode, the CCP module compares the value of the timer with the value loaded into the CCPRx register. When both values match, a pre-defined output action occurs (toggle, set, clear, or generate an interrupt).

Steps to Configure Compare Mode:
  1. Select the timer (Timer1 or Timer3) as the time base.
  2. Set the desired compare mode in CCPxCON.
  3. Load the compare value into CCPRx registers.
  4. Configure output pin and enable CCP interrupt (if needed).
Example Applications:
  • Generating time delays
  • Producing periodic events
  • Triggering ADC conversions at precise intervals

PWM Mode

The PWM mode of the CCP module is widely used in applications like motor control, LED dimming, and signal generation. In this mode, the CCP pin outputs a square wave with variable duty cycle.
The PWM period is determined by the PR2 register, while the duty cycle is set using CCPRxL and DCxB bits in CCPxCON.

Steps to Configure PWM Mode:
  1. Select Timer2 as the time base for PWM generation.
  2. Configure the PR2 register for the desired frequency.
  3. Set the CCPxCON for PWM mode and duty cycle bits.
  4. Load the duty cycle value into CCPRxL and DCxB bits.
  5. Enable the output pin (e.g., RC2 for CCP1).
Example Applications:
  • DC motor speed control
  • Servo motor position control
  • LED brightness control

Example Code (PWM Mode using XC8)

void PWM_Init() {
    TRISCbits.TRISC2 = 0; // Set CCP1 pin as output
    PR2 = 249; // Set PWM period
    CCP1CON = 0b00001100; // Configure CCP1 in PWM mode
    CCPR1L = 125; // 50% duty cycle
    T2CON = 0b00000100; // Enable Timer2 with prescaler 1:1
}
C

This example configures CCP1 (RC2 pin) to output a PWM waveform at approximately 5 kHz with 50% duty cycle.

CCP Registers

RegisterDescription
CCP1CON / CCP2CONControl register to select mode and configure output bits
CCPR1L / CCPR2LHolds the low byte of captured or compare value
CCPR1H / CCPR2HHolds the high byte of captured or compare value
T2CONControls Timer2 for PWM mode
PR2Determines PWM frequency
PIR1 / PIR2CCP interrupt flags

Key Differences Between Modes

ModeFunctionTimer UsedExample Use
CaptureRecords timer value on eventTimer1/3Frequency measurement
CompareGenerates output on matchTimer1/3Periodic event generation
PWMGenerates variable duty waveTimer2Motor control

Applications

  • Measuring pulse widths or signal periods
  • Controlling DC or servo motors
  • Generating precisely timed events
  • PWM-based dimming of LEDs

Summary

The Capture/Compare/PWM (CCP) module of PIC18F4550 adds powerful timing and control features for embedded designers. Whether you need to measure a signal, trigger an action at a specific time, or generate PWM output, the CCP module offers a flexible and efficient hardware solution. Understanding how to configure and utilize it helps you build advanced embedded systems with precise timing and control.

Example Code – PWM Generation (CCP1 with Timer2)

#include <xc.h>
#pragma config FOSC = HS
#define _XTAL_FREQ 20000000

void PWM_Init() {
    TRISC2 = 0;       // CCP1 output
    PR2 = 0xFF;       // PWM period
    CCP1CON = 0b00001100; // PWM mode
    CCPR1L = 0x7F;    // 50% duty cycle
    T2CON = 0b00000100;   // Enable Timer2 with prescaler 1:1
}

void main() {
    PWM_Init();
    while(1) {
        // Adjust duty cycle
        for (unsigned char dc = 0; dc < 255; dc++) {
            CCPR1L = dc;
            __delay_ms(10);
        }
    }
}
C

Want to go beyond just configuration and build complete projects?
🎓 Join our Complete Practical Video Course on PIC18F4550 Microcontroller
Get step-by-step guidance on USB, I²C, SPI, UART, ADC, and more — with real circuits and projects!

Q1. How many CCP modules are available in PIC18F4550?

A1. There are two CCP modules — CCP1 and CCP2.

Q2. Which timer is used for PWM generation?

A2. PWM mode uses Timer2 as its time base.

Q3. Can I use both CCP modules simultaneously?

A3. Yes, both CCP1 and CCP2 can operate in independent modes simultaneously.

Q4. What is the resolution of PWM in PIC18F4550?

A4. The PWM resolution is typically 10 bits (depending on the PR2 value).

Q5. Can the CCP module generate interrupts?

A5. Yes, both capture and compare modes can generate interrupts when an event or match occurs.

Home » Recent posts » Learn › Configuring CCP Module in PIC Microcontroller

https://bitziga.com

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*