Real Time Embedded System Lab Department of Electrical & Computer Engineering
Lab 04- TIMER0 MODULE OF PIC18F4520µC AND ITS
APPLICATIONS
Task 01: A simple example to demonstrate the use of PIC Timers. When an overflow occurs the
PIC jumps to ISR where we Increment counter. And when counter becomes 76, we toggle RB1 pin. This
pin is connected to LED. Therefore, we have a LED which is ON for 1 sec and off for 1sec.
C Code:
/*
* File: timer.c
* Author: Syed Haider Abbas
*
* Created on October 8, 2022, 5:32 AM
*/
#include <xc.h>
#define _XTAL_FREQ 2000000
///coding file///
unsigned char counter=0; //Overflow counter
void main(void)
{
//Setup Timer0
T0CONbits.T0PS0=1; //Prescaler is divide by 256
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
T0CONbits.PSA =0; //Timer Clock Source is from Prescaler
T0CONbits.T0CS = 0; //Prescaler gets clock from FCPU (5MHz)
T0CONbits.T08BIT = 1; //8 BIT MODE
INTCONbits.TMR0IE = 1; //Enable TIMER0 Interrupt
INTCONbits.TMR0IF=1;
INTCONbits.PEIE = 1; //Enable Peripheral Interrupt
INTCONbits.GIE = 1; //Enable INTs globally
T0CONbits.TMR0ON = 1; //Now start the timer!
//Set RB1 as output because we have LED on it
TRISB = 0B11111101;
while(1); //Sit Idle Timer will do every thing!
}
//***Main Interrupt Service Routine (ISR)***//
void __interrupt() ISR(void)
{
//Check if it is TMR0 Overflow ISR
if(INTCONbits.TMR0IE && INTCONbits.TMR0IF)
{
//TMR0 Overflow ISR
counter++; //Increment Over Flow Counter
if(counter==76)
{
if(PORTBbits.RB1==0)
PORTBbits.RB1=1; //Toggle RB1 (LED)
else
if(PORTBbits.RB1==1)
PORTBbits.RB1=0;
counter=0; //Reset Counter
Registration No: FA19-BSEE-040
Real Time Embedded System Lab Department of Electrical & Computer Engineering
}
INTCONbits.TMR0IF=0; //Clear Flag
}
}
Output:
Registration No: FA19-BSEE-040
Real Time Embedded System Lab Department of Electrical & Computer Engineering
Lab 05- INTERFACING LCD WITH PICµC AND ITS
APPLICATION
Task 01: Let’s do interface an LCD to microcontroller and display some message or any data
on it by sending command see how to program it.
C Code:
/*
* File: lcd.c
* Author: Syed Haider Abbas
* Created on October 31, 2022, 10:51 AM
*/
#include<pic18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#include <xc.h>
#define LCD PORTD
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char p);
void main()
{
unsigned char P[ ]="Hello World", i;
TRISD=0x00;
TRISC = 0x00;
cmd(0x61);
cmd(0x80);
cmd(0x0C);
cmd(0x06);
cmd(0x38);
while(1)
{
while(P[i]!='\0')
{
dat(P[i]);
i++;
Delay_ms(100);
}
cmd(0x01);
i=0;
}
}
Registration No: FA19-BSEE-040
Real Time Embedded System Lab Department of Electrical & Computer Engineering
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void dat(unsigned char k)
{
LCD=k;
RS=1;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void Delay_ms(unsigned char p)
{
unsigned char i,j;
for(i=0;i<p;i++)
{
for(j=0;j<200;j++);
}
}
Output:
Registration No: FA19-BSEE-040
Real Time Embedded System Lab Department of Electrical & Computer Engineering
Lab 06- APPLICATION AND UNDERSTANDING OF ANALOG
TO DIGITAL CONVERTER
Task 01: Here an input signal of voltage from the potentiometer is given to analog input channel
AN0 then the 10-bit result after conversion has been displayed on 8 bits at PORTC and remaining 2 bits
PORTD.
Matlab Code:
\* File: atdc.c
* Author: Syed Haider Abbas
* Created on October 24, 2022, 8:58 PM
*/
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRTD = OFF
#pragma config CP1 = OFF
#include <xc.h>
void delay(int n);
void main(void)
{
TRISC=0x00;
TRISD=0x00;
TRISAbits.TRISA0=1;
ADCON0=0x81;
ADCON2=0xCE;
while(1) {
delay(1);
ADCON0bits.GO=1;
while(ADCON0bits.DONE==1)
PORTC=ADRESL;
PORTD=ADRESH;
delay(1);
} }
void delay(int n)
{ for(int i=0;i<n;i++)
{ for(int j=0;j<250;j++) { }
} }
Output:
Registration No: FA19-BSEE-040