You are on page 1of 2

Real Time Embedded System Lab Department of Electrical & Computer Engineering

Lab 08- Using CCP1 or CCP2 in pulse-width modulation (PWM)


mode
Task 01: Here we will use the compare mode to generate a 50Hz output with 50% duty cycle.
Crystal oscillator of frequency 20MHz is used with PIC18F452. Pin RC2 is used as output pin. And
Oscilloscope is used to monitor the output wave.

C Code:
/*
* File: lab8pwm.c
* Author: Osman
*
* Created on November 3, 2022, 10:41 PM
*/
#include <pic18f4520.h>
#pragma config OSC = HS
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF
#include <xc.h>

int main(void)
{
// Set up PWM (see from PIC18F4620 datasheet)
CCP1CON = 0b00001100; // Enable PWM on CCP1
TRISCbits.RC2=0; // Make pin 17 (RC1/CCP2) an output
T2CON = 0b00000100; // Enable TMR2 with prescaler = 1
PR2 = 249;
CCPR1L = 25;

while(1)
{
// 50% duty cycle for 500ms
CCPR1L = 125;
_delay(100);

// 10% duty cycle for 500ms


CCPR1L = 25;
_delay(100);

// 0% duty cycle for 500ms


CCPR1L = 0;
_delay(100);
}
}

Registration No: FA19-BSEE-011


Real Time Embedded System Lab Department of Electrical & Computer Engineering

Output:

Registration No: FA19-BSEE-011

You might also like