
Capture/Compare/PWM (CCP) Module in PIC18F4550
Microchip Technology: CCP module in PIC (PIC18)
- Complete Video Course on PIC18F4550 Microcontroller
- Introduction: CCP module in PIC (PIC18F4550)
- Overview of CCP Module
- Capture Mode
- Compare Mode
- PWM Mode
- Example Code (PWM Mode using XC8)
- CCP Registers
- Key Differences Between Modes
- Applications
- Summary
- Example Code – PWM Generation (CCP1 with Timer2)
- Learn More
- Complete Video Course on PIC18F4550 Microcontroller
- 💬Frequently Asked Questions (FAQ)
Would you like to join our comprehensive practical video course on the PIC18F4550 Microcontroller?
Complete Video Course on 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.
Microchip Technology: CCP module in PIC (PIC18)
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:
- Select the timer source (usually Timer1 or Timer3).
- Set the capture mode in the CCPxCON register (e.g., capture on every rising edge).
- Enable the CCP interrupt if you want to handle captures through ISR.
- 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:
- Select the timer (Timer1 or Timer3) as the time base.
- Set the desired compare mode in CCPxCON.
- Load the compare value into CCPRx registers.
- 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:
- Select Timer2 as the time base for PWM generation.
- Configure the PR2 register for the desired frequency.
- Set the CCPxCON for PWM mode and duty cycle bits.
- Load the duty cycle value into CCPRxL and DCxB bits.
- 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
}CThis example configures CCP1 (RC2 pin) to output a PWM waveform at approximately 5 kHz with 50% duty cycle.
CCP Registers
| Register | Description |
|---|---|
| CCP1CON / CCP2CON | Control register to select mode and configure output bits |
| CCPR1L / CCPR2L | Holds the low byte of captured or compare value |
| CCPR1H / CCPR2H | Holds the high byte of captured or compare value |
| T2CON | Controls Timer2 for PWM mode |
| PR2 | Determines PWM frequency |
| PIR1 / PIR2 | CCP interrupt flags |
Key Differences Between Modes
| Mode | Function | Timer Used | Example Use |
|---|---|---|---|
| Capture | Records timer value on event | Timer1/3 | Frequency measurement |
| Compare | Generates output on match | Timer1/3 | Periodic event generation |
| PWM | Generates variable duty wave | Timer2 | Motor 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);
}
}
}CMicrochip Technology: CCP module in PIC (PIC18)
Learn More
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!
➡️ Start Learning Now at “Master PIC18F4550 Programming“
Complete Video Course on PIC18F4550 Microcontroller

💬Frequently Asked Questions (FAQ)
A1. There are two CCP modules — CCP1 and CCP2.
A2. PWM mode uses Timer2 as its time base.
A3. Yes, both CCP1 and CCP2 can operate in independent modes simultaneously.
A4. The PWM resolution is typically 10 bits (depending on the PR2 value).
A5. Yes, both capture and compare modes can generate interrupts when an event or match occurs.


