Real Time Embedded Systems Lab Manual
Real Time Embedded Systems Lab Manual
Name: __________________________
Prepared by:
Signature:
Supervised by:
Engr. M.Yaseen
Lecturer
CIIT, Sahiwal
Signature:
Approved by:
PREFACE
Pre-Requisites
The course will require some knowledge of basic C language programming and of basic
logic design. It is assumed that students will already have taken coursesIntroduction to
computer programing, basic digital logic design and Microprocessor Systems
&Interfacing.
BOOKS
Text Book
LEARNING OUTCOMES
GRADING POLICY
Lab Work & Viva Voce Exam 100Marks
Performance of Experiments, Assignments 25%
Sessional –I Viva/Practical 10%
Sessional –II Viva/Practical 15%
Terminal Examination/Viva/LabProject 50%
Each experiment performance will be included in the grading. Lab project can be
assigned in groups or to individuals
Lab Manual Real Time Embedded Systems
Experiments List
Experiment #1
Experiment #2
Experiment #3
Experiment #4
Peripheral Interrupts.
Experiment #5
Experiment #6
Experiment #7
Experiment #8
Experiment #9
Experiment #10
LCD interfacing
Experiment #11
USART Module
Experiment #12
Lab Experiment:1
Introduction to Software Tools MPLAB, PROTEUS and PIC18F4520
Objectives:
Introduction:
These programs are the backbone of the microcontroller based systems; since using
MPLAB we can build the software of the project using C language, and then we can
simulate the project virtually using PROTEUS, finally we can download the program on
the microcontroller and see the results practically by using the burner on the trainer board
available in embedded laboratory. So this experiment includes all the knowledge you will
need to get started to these programs.
1. MPLAB Program
o What is MPLAB?
2. From the project menu choose project wizard and choose PIC18F4520.
4. Brows and name the project, select xc8 compiler and PIC18 family.
6. Then add a file to the source files by right clicking on source files, and choose add
files and then name the file any
Lab Manual Real Time Embedded Systems
2. PROTEUS Program
a. What is PROTEUS?
Proteus contains everything you need to develop, test and virtually prototype your
embedded system designs based around the Microchip Technologies™ PIC18 series of
microcontrollers. The unique nature of schematic based microcontroller simulation with
Proteus facilitates rapid, flexible and parallel development of both the system hardware
and the system firmware. This design synergy allows engineers to evolve their projects
more quickly, empowering them with the flexibility to make hardware or firmware
changes at will and reducing the time to market. Proteus VSM models will fundamentally
work with the exact same HEX file as you would program the physical device with,
binary files (i.e. Hex files) produced by any assembler or compiler.
4. To get LED write led then chose whatever color of led you require.
5. To get a resistor write 1k and chose the first part and so on.
Finally .c file coded in MPLAB compile and build the file will generate .hex file.
Load this hex file to microcontroller in Proteus by double click on microcontroller in
Proteus and giving that address of hex file.And now run the simulations on Proteus
will show the output practically whatever the procedure you followed or mentioned in
the code.
Lab Manual Real Time Embedded Systems
3. Introduction to PIC18F4520:
Objective:
To get familiar with PIC18F4520 microcontroller.
Fig.MCU
Oscillator
Timersfrom Timer0-Timer3
Serial Communications
A/D converter
Data EEPROM
Lab Manual Real Time Embedded Systems
Sleep mode
Code protection
In-circuit debugger
Lab Manual Real Time Embedded Systems
Post Lab
Installing Proteus
Performance Viva
Total/15
(10 marks) (4 marks)
Pre-Lab /3
In-Lab /5
Post-Lab /3
Lab Manual Real Time Embedded Systems
Comments
Lab Experiment: 2
How to blink an LED with PIC18F4520 microcontroller
Objectives:
To get familiar with interfacing an LED display to PIC18F4520
and make a flasher.
To know how to create a delay of your choice.
Tools:
PIC18F4520 microcontroller, MPLAB software with xc8 compiler, PROTEUS software.
Pre-Lab
PORT Read operation reads the Physical State (actual Voltage Level) of I/O pins.
If an I/O pin is at a potential near to VDD, corresponding PORT bit will be Logic
1 and if it at a potential near to VSS, corresponding PORT bit will be Logic 0.
Let’s start from our very initial program of embedded systems lab. The simplest program
to understand all about the software and hardware interaction using MPLAB and Proteus.
So how to start or open new project in MPLAB is given below:
Select your Hardware Tool. Don’t worry if your programmer is not supported.
You can directly burn the hex file after building the project.
Click Next
// Begin Configuration
Other method is #pragma configuration directives, which is used to tell the compiler to
set Configuration Bits of PIC Microcontroller. You can generate it using the MPLAB
IDE as following:
Configuration Bits
You can select the configuration at the bottom of the IDE as shown below:
Configuration Bits
You can simple copy paste this generated code to code editor.Then enter the remaining
code for the blinking of LED.Build the Project.So this is all about how you can configure
the bits according to your requirements.
Hex File will be generated in the location Your Project Folder >>dist>> default >>
production.
Lab Manual Real Time Embedded Systems
Writing Registers:
You can write to PORT and TRIS Registers entirely or bit by bit.
0 0b00000000 0x00
1 0b00000001 0x01
In-Lab
Now let’s move towards our in lab work.Make start our MPLAB first coding program
how to blink a led with XC8 compiler. And see the output on implementing the circuit on
Proteus.
void delay(int);
int n;
void main(void)
{
TRISDbits.TRISD0=0;
while(1)
{
LATDbits.LATD0=1;
delay(25);
LATDbits.LATD0=0;
delay(25);
}
return;
}
void delay(int n)
{
for (int i=0 ; i<n ; i++)
{
for (int j=0 ; j<250 ; j++);
}
}
First statement #define _XTAL_FREQ 8000000 defines the clock frequency of the
microcontroller which is used to calculate delay() function. Second statement #include
<xc.h> includes the header file xc.h which contains the definition ofdelay() function and
TRIS, PORT registers.
Lab Manual Real Time Embedded Systems
U1
C1 2 15
RA0/AN0/C1IN- RC0/T1OSO/T13CKI
3 16
RA1/AN1/C2IN- RC1/T1OSI/CCP2B
4 17
RA2/AN2/C2IN+/VREF-/CVREF RC2/CCP1/P1A
22pF 5 18
RA3/AN3/C1IN+/VREF+ RC3/SCK/SCL
X1 6
RA4/T0CKI/C1OUT RC4/SDI/SDA
23
CRYSTAL 7 24
C2 RA5/AN4/SS/HLVDIN/C2OUT RC5/SDO
14 25
RA6/OSC2/CLKO RC6/TX/CK
13 26
RA7/OSC1/CLKI RC7/RX/DT D1
22pF 33 19
R2
RB0/AN12/FLT0/INT0 RD0/PSP0
34 20
RB1/AN10/INT1 RD1/PSP1 220
35 21 LED-RED
RB2/AN8/INT2 RD2/PSP2
36 22
RB3/AN9/CCP2A RD3/PSP3
37 27
RB4/KBI0/AN11 RD4/PSP4
38 28
RB5/KBI1/PGM RD5/PSP5/P1B
39 29
RB6/KBI2/PGC RD6/PSP6/P1C
40 30
RB7/KBI3/PGD RD7/PSP7/P1D
8
RE0/RD/AN5
9
RE1/WR/AN6
10
RE2/CS/AN7
1
RE3/MCLR/VPP
PIC18F4520
R4
10k
Post Lab
Question:
Write a program and make Proteus circuit to blink 8,8 LEDs on two different output ports
with unique delay.
Performance Viva
Total/15
(10 marks) (4 marks)
Pre-Lab /3
In-Lab /5
Post-Lab /3
Comments
Lab Manual Real Time Embedded Systems
Lab Experiment: 3
Interrupts Occurring,Handling inPIC18F4520uC and
Applications.
Objectives:
To get familiar with PIC18F4520device interrupts and handling or
calling interrupt service routine.
To know how to control interrupt operations.
Tools:
PIC18F4520 microcontroller, MPLAB software with xc8 compiler, PROTEUS
software.
Pre-Lab
Interrupts
The PIC18F4520 devices have multiple interrupt sources and an interrupt priority feature
that allows most interrupt sources to be assigned a high-priority level or a low-priority
level. The high-priority interrupt vector is at 0008h and the lowpriority interrupt vector is
at 0018h. High-priority interrupt events will interrupt any low-priority interrupts that may
be in progress. Now let’s proceed to the registers and bits used to control interrupt
operations.
INTCON Registers
The INTCON registers are readable and writable registers, which contain various enable,
priority and flag bit as shown in the table below:
Lab Manual Real Time Embedded Systems
PIR Registers
The PIR registers contain the individual flag bits for the peripheral interrupts. Due to the
number of peripheral interrupt sources, there are two Peripheral Interrupt Request Flag
registers (PIR1 and PIR2).
PIE Registers
The PIE registers contain the individual enable bits for the peripheral interrupts. Due to
the number of peripheral interrupt sources, there are two Peripheral Interrupt Enable
registers (PIE1 and PIE2). When IPEN = 0, the PEIE bit must be set to enable any of
these peripheral interrupts.
IPR Registers
The IPR registers contain the individual priority bits for the peripheral interrupts. Due to
the number of peripheral interrupt sources, there are two Peripheral Interrupt Priority
registers (IPR1 and IPR2). Using the priority bits requires that the Interrupt Priority
Enable (IPEN) bit be set.
RCON Register
The RCON register contains flag bits which are used to determine the cause of the last
Reset or wake-up from Idle or Sleep modes. RCON also contains the IPEN bit which
enables interrupt priorities.
In-Lab
Let’s do program how interrupts occur and how can we identify whether the interrupt has
been occurred and how calling the interrupt service routine ISR() to deal with the
interrupts.
main.c file
#include <xc.h>
#include "ConfigBits.c"
#include <stdio.h>
#include <stdlib.h>
void delay(int);
bit flag;
if (INTCONbits.INT0F == 1) {
LATBbits.LATB5 = 1;
delay(300);
LATBbits.LATB5 = 0;
delay(300);
INTCONbits.INT0F = 0;
void FlashingRouting() {
LATBbits.LATB5 = ~LATBbits.LATB5;
delay(300);
Lab Manual Real Time Embedded Systems
LATBbits.LATB5 = 0;
void main(void)
TRISB = 0x0F;
INTCONbits.INT0E = 1;
INTCON2bits.INTEDG0 = 0;
INTCONbits.INT0F = 0;
flag = 0;
ei();
while (1)
LATBbits.LATB4 = 1;
delay(500);
LATBbits.LATB4 = 0;
delay(500);
return;
void delay(int n) {
ConfigBits.c file
Lab Manual Real Time Embedded Systems
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor
disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in
hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed
on the SWDTEN bit))
// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with
RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured
as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin
disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow
will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension
and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-
protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-
protected)
Lab Manual Real Time Embedded Systems
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-
protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-
protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh)
not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-
protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-
protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-
protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-
protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-
protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration
registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-
0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not
write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not
protected from table reads executed in other blocks)
// CONFIG7H
Lab Manual Real Time Embedded Systems
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-
0007FFh) not protected from table reads executed in other blocks)
#include <xc.h>
C1
U1
2 15
RA0/AN0/C1IN- RC0/T1OSO/T13CKI
22pF 3 16
RA1/AN1/C2IN- RC1/T1OSI/CCP2B
X1 4
RA2/AN2/C2IN+/VREF-/CVREF RC2/CCP1/P1A
17
CRYSTAL 5 18
C2 RA3/AN3/C1IN+/VREF+ RC3/SCK/SCL
6 23
RA4/T0CKI/C1OUT RC4/SDI/SDA
7 24
RA5/AN4/SS/HLVDIN/C2OUT RC5/SDO
14 25
RA6/OSC2/CLKO RC6/TX/CK
22pF 13 26
RA7/OSC1/CLKI RC7/RX/DT
33 19
RB0/AN12/FLT0/INT0 RD0/PSP0
34 20
RB1/AN10/INT1 RD1/PSP1
35 21
RB2/AN8/INT2 RD2/PSP2
36 22
RB3/AN9/CCP2A RD3/PSP3
R1 37 27
RB4/KBI0/AN11 RD4/PSP4
38 28
RB5/KBI1/PGM RD5/PSP5/P1B
10k 39 29
RB6/KBI2/PGC RD6/PSP6/P1C
40 30
RB7/KBI3/PGD RD7/PSP7/P1D
D1 8
R2 RE0/RD/AN5
9
RE1/WR/AN6
220 10
RE2/CS/AN7
LED-RED 1
RE3/MCLR/VPP
D2 PIC18F4520
R3
R4
220
LED-RED 10k
So this was all about the program to code and configuring bits and make the Proteus
Circuit as shown above.
Post Lab
Questions:
1. What type of interrupts can occur during normal program or circuit execution of
your project?
2. How you will set interrupts priorities and enable disable by seeing register bits
according to your requirement?
Lab Manual Real Time Embedded Systems
Performance Viva
Total/15
(10 marks) (4 marks)
Pre-Lab /3
In-Lab /5
Post-Lab /3
Comments
Lab Experiment: 4
Timer0 Module of PIC18F4520uC and itsApplications.
Objectives:
To get familiar with Timer0 module of PIC18F4520 device.
To know how and where to use Timers.
Tools:
PIC18F4520uC, MPLAB software with xc8 compiler, PROTEUS software.
Lab Manual Real Time Embedded Systems
Pre-Lab
Timer0 Module
The Timer0 module incorporates the following features:
Software selectable operation as a timer or counter in both 8-bit or 16-bit modes
Readable and writable registers
Dedicated 8-bit, software programmable prescaler
Selectable clock source (internal or external)
Edge select for external clock
Interrupt-on-overflow
Timer0 Operation
Timer0 can operate as either a timer or a counter; the mode is selected with the T0CS bit
(T0CON). In Timer mode (T0CS = 0), the module increments on every clock by default
unless a different prescaler value is selected. If the TMR0 register is written to, the
increment is inhibited for the following two instruction cycles. The user can work around
this by writing an adjusted value to the TMR0 register. The Counter mode is selected by
setting the T0CS bit (= 1). In this mode, Timer0 increments either on every rising or
falling edge of pin RA4/T0CKI. The incrementing edge is determined by the Timer0
Source Edge Select bit, T0SE (T0CON); clearing this bit selects the rising edge.
Restrictions on the external clock input are discussed below. An external clock source
Lab Manual Real Time Embedded Systems
can be used to drive Timer0; however, it must meet certain requirements to ensure that
the external clock can be synchronized with the internal phase clock (TOSC). There is a
delay between synchronization and the onset of incrementing the timer/counter.
Prescaler
An 8-bit counter is available as a prescaler for the Timer0 module. The prescaler is not
directly readable or writable; its value is set by the PSA and T0PS bits (T0CON) which
determine the prescaler assignment and prescale ratio. Clearing the PSA bit assigns the
prescaler to the Timer0 module. When it is assigned, prescale values from 1:2 through
1:256 in power-of-2 increments are selectable. When assigned to the Timer0 module, all
instructions writing to the TMR0 register (e.g., CLRF TMR0, MOVWF TMR0, BSF
TMR0, etc.) clear the prescaler count.
Lab Manual Real Time Embedded Systems
In-Lab
A simple example to demonstrate the use of PIC Timers. In this example the TIMER0 is
configured as follows.
/*
*8 Bit Mode
*Clock Source from Prescaler
*Prescaler = FCPU/256 (Note: FCPU= Fosc/4)
*Over flow INT enabled
*/
As our FCPU=20MHz/4=5MHz// (We are running from 20MHz XTAL)
When an over flow occurs the PIC jumps to ISR where weIncrement counter. And when
counter becomes 76 we toggleRB1 pin. This pin is connected to LED. Therefore wehave a
LED which is ON for 1 sec and Off for 1sec.
///coding file///
void main(void)
{
//Setup Timer0
T0CONbits.T0PS0=1; //Prescaler is divide by 256
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
Lab Manual Real Time Embedded Systems
if(counter==76)
{
if(PORTBbits.RB1==0)
PORTBbits.RB1=1; //Toggle RB1 (LED)
else
if(PORTBbits.RB1==1)
PORTBbits.RB1=0;
C1 2 15
RA0/AN0/C1IN- RC0/T1OSO/T13CKI
3 16
RA1/AN1/C2IN- RC1/T1OSI/CCP2B
4 17
RA2/AN2/C2IN+/VREF-/CVREF RC2/CCP1/P1A
22pF 5 18
RA3/AN3/C1IN+/VREF+ RC3/SCK/SCL
X1 6
RA4/T0CKI/C1OUT RC4/SDI/SDA
23
CRYSTAL 7 24
C2 RA5/AN4/SS/HLVDIN/C2OUT RC5/SDO
14 25
RA6/OSC2/CLKO RC6/TX/CK
13 26
RA7/OSC1/CLKI RC7/RX/DT
22pF D1 33 19
R2 34
RB0/AN12/FLT0/INT0 RD0/PSP0
20
RB1/AN10/INT1 RD1/PSP1
220 35 21
RB2/AN8/INT2 RD2/PSP2
LED-RED 36 22
RB3/AN9/CCP2A RD3/PSP3
37 27
RB4/KBI0/AN11 RD4/PSP4
38 28
RB5/KBI1/PGM RD5/PSP5/P1B
39 29
RB6/KBI2/PGC RD6/PSP6/P1C
40 30
RB7/KBI3/PGD RD7/PSP7/P1D VDD
8
RE0/RD/AN5
9
RE1/WR/AN6
10
RE2/CS/AN7
1
RE3/MCLR/VPP
PIC18F4520
Post Lab
Questions:
3. Write code and show Proteus circuit from any of above mentioned applications?
Lab Manual Real Time Embedded Systems
Performance Viva
Total/15
(10 marks) (4 marks)
Pre-Lab /3
In-Lab /5
Post-Lab /3
Comments
Lab Experiment: 5
Timer1 Module of PIC18F4520uC and itsApplications.
Objectives:
To get familiar with Timer1 module of PIC18F4520 device.
To know how and where to use Timer1.
Tools:
PIC18F4520uC, MPLAB software with xc8 compiler, PROTEUS software.
Lab Manual Real Time Embedded Systems
Pre-Lab
Timer1 Module
The Timer1 timer/counter module incorporates the following features:
• Software selectable operation as a 16-bit timer or counter
• Readable and writable 8-bit registers (TMR1H and TMR1L)
• Selectable clock source with device clock or Timer1 oscillator internal options
• Interrupt-on-overflow
• Reset on CCP Special Event Trigger
• Device clock status flag
Tools:
PIC18F4520 microcontroller, MPLAB software with xc8 compiler, PROTEUS
software.
Pre-Lab