Professional Documents
Culture Documents
Rtes L 7-9
Rtes L 7-9
Lab 07- Analysis of CCP Module for Capture, Compare and PWM
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: inlab1ccppwm.c
* Author: Osman *
* Created on November 3, 2022, 10:24 PM
*/
#include <xc.h>
#pragma OS=HS
#pragma WDT=OFF
#pragma LVP=OFF
#define _XTAL_FREQ 20000000
void main(void) {
CCP1CON=0x05;
T3CON=0x0;
T1CON=0x0;
TRISB=0;
TRISD=0;
TRISCbits.RC2=1;
CCPR1L=0; CCPR1H=0;
while(1) {
TMR1H=0;
TMR1L=0; PIR1bits.CCP1IF=0; while(PIR1bits.CCP1IF==0) {
T1CONbits.TMR1ON=1; }
PIR1bits.CCP1IF=0;
while (PIR1bits.CCP1IF==0)
{
T1CONbits.TMR1ON=0;
}
PORTB=CCPR1L;
PORTD=CCPR1H;
} return;
}
Output:
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);
Output:
C Code:
#include <xc.h>
//#include "config.h" // Configuration bits file stored in a header
file
#define _XTAL_FREQ 20000000 //define crystal frequency to 20MHz
// This array stores binary bit pattern that will be send to PORTD
unsigned char
binary_pattern[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}
;
void main(void)
{
TRISD = 0x00; //define PORTD as a output pin
while(1)
{
//this loop sends all binary patterns to PORTD
for (int i=0;i<10;i++)
{
PORTD = binary_pattern[i];
__delay_ms(1000); //add delay of one second
}
}
return;
}
Output: