Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Real Time Embedded System Lab Department of Electrical & Computer Engineering

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:

Registration No: FA19-BSEE-023


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-023


Real Time Embedded System Lab Department of Electrical & Computer Engineering

Output:

Registration No: FA19-BSEE-023


Real Time Embedded System Lab Department of Electrical & Computer Engineering

Lab 09- INTERFACING AND APPLICATION OF SEVEN


SEGMENT DISPLAY AS COUNTER
Task 01: In lab a single 7-segment display has been interfaced and act as counter from 0 to 9.
To display a counter of 0-9, one 7-segment is interfaced with PIC18F4520 in this lab. The seven pins of
the segment are connected to the seven pins of PORTB, via a resistor of 220R. Each pin represents an
alphabet which switches the corresponding LED bar on and off with respect to the code. As per the
connections shown in the circuit diagram, alphabet ‘a’ of the segment is connected to RB0 pin of the
MCU, ‘b’ is connected to RB1 pin and so on.

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:

Registration No: FA19-BSEE-023

You might also like