Skip to content

PWM and CCP module

The CCP module (Capture / Compare / PWM) has three modes; the course focuses on PWMmotor speed, LED dimming, and servo control. The time base is Timer2 + PR2, not Timer0.

This page completes Topic 9 CCP (PWM): PDF formulas, registers, and 5 kHz at 50% example with 20 MHz crystal. Labs: Practice 4 and Practice 6 — Stepper motor.

PWM signal diagram showing period and duty cycle
Duty cycle is the high time within period T_PWM. — Topic 9 CCP Module — UNEXPO · Mechatronics · Microcontrollers
CCP module modes table: capture, compare and PWM
CCP1/CCP2: capture and compare use Timer1; PWM uses Timer2 with PR2 and CCPRxL. — Class Topic 9 — CCP Module (PWM) · UNEXPO
CCP module structure with CCPRxH, CCPRxL and CCPxCON registers
CCPRxH:CCPRxL and CCPxCON. PWM mode uses Timer2. — Class Topic 9 — CCP Module (PWM) · UNEXPO

PWM (Pulse Width Modulation) repeats a digital waveform: the pin toggles high and low with fixed period T_PWM. You change duty cycle — high time as a percentage of T_PWM — to simulate analog power to a motor or LED.

ParameterSymbolWhat it controls
PeriodT_PWMFrequency F_PWM = 1/T_PWM
Duty cycleX %High time / T_PWM
Resolutioneffective bitsHow many duty steps you can program
ModeTimer baseUse
CaptureTimer1Measure pulse width / frequency
CompareTimer1Trigger at precise instant
PWMTimer2 + PR2Motor, LED, servo — course focus

Capture/compare are covered in Timer 1; PWM uses Timer2 separately.

RegisterFunction
PR2Period — TMR2 counts 0 → PR2 → reset
T2CONTimer2 prescaler, TMR2ON
CCPR1L + CCP1CON<5:4>Duty cycle (10 bits on PIC18)
CCP1CONCCP mode — PWM = CCP1M3:0 = 11000x0C

With CCP1CON = 0x0C and Timer2 active, RC2 toggles automatically — no BSF LATC, 2 loop needed. Set BCF TRISC, 2 (output) once at init.

PWM period:

T_PWM = 4 × (PR2 + 1) × Tosc × Timer2_Prescaler

Duty cycle (value to load):

CCPR1L : CCP1CON<5:4> = (T_PWM × X%) / (Tosc × Prescaler)

Resolution (effective bits):

log2( Fosc / (F_PWM × Prescaler) )

With Fosc = 20 MHz, Timer2 prescaler = 1:4, F_PWM = 5 kHz:

PR2 = 1 / (5 kHz × 4 × Tosc × 4) − 1 = 249
50% duty → CCPR1L = 124
  1. BCF TRISC, 2 — RC2/CCP1 as output
  2. Load PR2 for target frequency
  3. Configure T2CON (prescaler, TMR2ON = 1)
  4. Load CCPR1L and DC1B1:DC1B0 in CCP1CON if needed
  5. CCP1CON = 0x0C — PWM mode
config_pwm_5khz_50:
BCF TRISC, 2 ; RC2 output (CCP1)
MOVLW D'249'
MOVWF PR2 ; period ~5 kHz, PS=1:4
MOVLW D'124'
MOVWF CCPR1L ; duty ~50%
MOVLW B'00001101' ; TMR2ON, prescaler 1:4
MOVWF T2CON
MOVLW 0x0C ; CCP1 PWM mode
MOVWF CCP1CON
RETURN

To change motor speed: modify CCPR1L (duty) without recalculating PR2 if PWM frequency stays the same.

ModulePinPractice
CCP1RC2Practice 4 PWM, Practice 6 motor
CCP2RC1Second PWM output if design requires

Both in PWM mode share Timer2 and PR2 — same frequency, different duty in CCPR1L / CCPR2L.

MistakeConsequence
PWM without TMR2ONRC2 idle
Skip TRISCPin does not drive
CCP1CON in capture modeNo PWM waveform
Wrong PR2Unexpected frequency
Mix Timer1 with PWMCapture uses T1; PWM uses T2

Practica · Laboratory

Practice 4 — PWM

Calculate PR2 and CCPR1L, configure Timer2 and CCP1, connect load on RC2. Tutorial: Practice 4.

Practica · Laboratory

Practice 6 — Stepper motor

Use PWM or coil sequence to drive the motor. Requires correct CCP/Timer2 setup. Tutorial: Practice 6.

Which timer does CCP PWM use?

Timer2 with period register PR2.

What value typically sets CCP1 to PWM mode?

CCP1CON = 0x0C (mode bits 1100).

Which pin outputs CCP1?

RC2.

For 50% duty with PR2 = 249, CCPR1L ≈ ?

124 (half the period in duty counts).

Ejercicio · Parcial

Calculate PR2 for F_PWM=5kHz, Fosc=20MHz, prescaler 1:4

PR2 = 1/(5kHz) / (4 × 1/20MHz × 4) − 1 = 249 per the UNEXPO Microcontrollers course example.

Interactive exam

PWM and CCP module

Based on: Clase Tema 9. Modulo CCP (PWM).pdf

0 of 0 answered

PIC18F4550 PWM mainly uses...
The typical PWM1 output pin on PIC18F4550 is...
PR2 in PWM mode mainly defines...
CCP1CON = 0x0C configures CCP1 in...
To generate PWM you must have active...
CCP capture uses as time reference...
With PR2=249 and 50% duty, typical CCPR1L is...
CCP2 on PIC18F4550 typically outputs on...