You are on page 1of 51

Lab Manual

Real Time Embedded


Systems
EEE446
Lab Manual Real Time Embedded Systems

COMSATS Universiy Islamabad


Department of Electrical & Computer
Engineering

Real Time Embedded Systems


Lab Manual
EEE446

Name: __________________________

Registration No: __________________________


Lab Manual Real Time Embedded Systems

Real Time Embedded Systems


Laboratory Manual
EEE446

Prepared by:

Engr. Amna Bashir


Lab Engineer
CIIT, Sahiwal

Signature:

Supervised by:

Engr. M.Yaseen
Lecturer
CIIT, Sahiwal
Signature:

Approved by:

Engr. Dr. Saeeda


Incharge/ Assistant Professor
CIIT, Sahiwal
Signature:

DEPARTMENT OF ELECTRICAL & COMPUTER


ENGINEERING
COMSATS UNIVERSITY ISLAMABAD
SAHIWAL CAMPUS
Lab Manual Real Time Embedded Systems

PREFACE

Scope and Purpose of the Course

To introduce sufficient knowledge required to understand the design of complete


embedded systems, including their hardware and software.To introduce Microcontrollers
especially PIC18F4520 and its applications in different fields using different modules of
it,hardware/software interface, interface computers to their environment, gain more
experience and confidence in C language programming.

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.

Major Topics in the Course

Microcontrollers, Introduction to PIC18F4520,I/O Ports,LED Blinking,Interrupts and


their applications,Introduce Delay and its applications,Timer0 Module , Timer1 Module,
Timer2 Module, Timer3 Module,Capture/Compare/PWM (CCP) modules, Analogue to
Digital Converter (A/D) module, USART module and speed control of DC Motor /Servo
Motor/Stepper Motorby PIC18 microcontrollerand their different applications.

BOOKS
Text Book

“Real time embedded systems”


Reference Books

“Real time embedded systems”


Lab Manual Real Time Embedded Systems

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

Introduction to Software ToolsMPLAB, PROTEUS, and xc8 Compiler.

Experiment #2

How to Blink an LED using PIC18F4520.

Experiment #3

Interrupts occurring, handling and applications.

Experiment #4

Peripheral Interrupts.

Experiment #5

Timer0 ApplicationUsing TMR0.

Experiment #6

Timer1, Timer2 and Timer3 Modules

Experiment #7

Analyze CCP Module for Capture, Compare and PWM.

Experiment #8

Using CCP1 or CCP2 in Pulse-width modulation (PWM) Mode.

Experiment #9

Analog to Digital Conversion (A/D)

Experiment #10

LCD interfacing

Experiment #11

USART Module

Experiment #12

DC Motor /Servo Motor/Stepper Motor


Lab Manual Real Time Embedded Systems

Lab Experiment:1
Introduction to Software Tools MPLAB, PROTEUS and PIC18F4520
Objectives:

1. To be familiar with software tools like MPLAB, PROTEUS


2. Sufficient Knowledge about PIC18F4520 structure.
3. To know how to make a project using MPLAB and then get the hex file of
thesoftwareprogram of the project.
4. How to simulate the hex file using PROTEUS.

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?

MPLAB is a software program that runs on your PC to provide a development


environment for your embedded system design. In other words it is a Windows program
package that makes writing, coding and developing a program easier (code your project).
It could best be described as developing environment for a standard program language
that is intended for programming microcontrollers.

o Get started to MPLAB

1. Open the program by double clicking on its icon on the desktop.

2. From the project menu choose project wizard and choose PIC18F4520.

4. Brows and name the project, select xc8 compiler and PIC18 family.

5. From view menu choose project; to view the project.

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.

o Get started to PROTEUS

1. From start menu chose the PROTEUS,create new project.

2. In the project to pick any device or element click on Devices (P).

3. Write the name of the PIC18F4520.

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. PIC18F4520 (40 pin Device)

3.1. PIC18F Microcontrollers

 Microcontroller Unit (MCU)


 Microprocessor unit (MPU)
 Harvard Architecture
 Program memory for instructions
 Data memory for data
 I/O ports
 Support devices such as timers
Lab Manual Real Time Embedded Systems

Fig.MCU

Fig.MPU and Memory


Lab Manual Real Time Embedded Systems

3.2. PIC18F I/O PORTS


Pic18F have Five I/O ports

 PORT A through PORT E


 Most I/O pins are multiplexed
 Generally have eight I/O pins
 Addresses already assigned to these ports
 Each port is identified by its assigned SFR

Fig. I/O Ports A and B


Similarly have three remaining ports C, D & E consists of up to 8 pins.Depending on the
device selected and features enabled, there are up to five ports available. Some pins of the
I/O ports are multiplexed with an alternate function from the peripheral features on the
device. In general, when a peripheral is enabled, that pin may not be used as a general
purpose I/O pin. Each port has three registers for its operation. These registers are:

• TRIS register (Data Direction register)

• PORT register (reads the levels on the pins of the device)


• LAT register (Data Latch register)
The Data Latch (LAT register) is useful for read-modify write operations on the value
that the I/O pins are driving.
Lab Manual Real Time Embedded Systems

3.3. MCU Support Devices


Microcontroller unit of PIC18 supports the following devices as shown in the
figure given below:

 Oscillator

 Timersfrom Timer0-Timer3

 Capture, Compare and PWM (CCP Modules)

 Serial Communications

 A/D converter

 Parallel Slave Port (PSP)

 Data EEPROM
Lab Manual Real Time Embedded Systems

Fig. MCU Support Devices


3.4. PIC18F4520 Features

3.5. PIC18F Special Features

 Sleep mode

 Watchdog timer (WDT)

 Code protection

 In-circuit serial programming

 In-circuit debugger
Lab Manual Real Time Embedded Systems

3.6. PIC18F4X2 Architecture Block Diagram

Fig. Architecture Block Diagram


Lab Manual Real Time Embedded Systems

Post Lab

 Installing MPLAB and Xc8 compiler

 Installing Proteus

Analysis and Conclusion about the Softwares.

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

 Input Outputs pins of a PIC Microcontroller is divided into different PORTS


containing a group of GPIO (General Purpose Input Output) pins.
 Since PIC 18F4520 is an 8-bit microcontroller, each PORT contains up to 8 Input
Output pins.
 In 18F Microcontrollers, each port is associated with three registers TRIS, LAT
and PORT.
 TRIS stands for Tri-State, it determines the direction of each GPIO pins. Logic 1
at a particular bit of TRIS register makes the corresponding pin Input while Logic
0 at a particular bit makes the corresponding pin Output.
 All Input pins will be in High Impedance state.
 PORT Register is used to Read Data from or Write Data to Input Output pins.
 For an Output pin (TRIS bit is 0), Logic 1 at PORT register makes the
corresponding pin Logic High (VDD) and Logic 0 at PORT register makes the
corresponding pin Logic Low (VSS).
 Since PIC 18F4520 is a 5V device, VDD = 5Vand VSS = 0V.
Lab Manual Real Time Embedded Systems

 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:

 Open MPLAB X IDE.

Fig. MPLAB X IDE

 Click on File >> New Project


Lab Manual Real Time Embedded Systems

Fig. New Project – Step 1

 Select Microchip Embedded >> Standalone Project


 Click Next

New Project – Step 2

 Select Family and Device.


 Click Next
Lab Manual Real Time Embedded Systems

New Project – Step 3

 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

New Project – Step 4

 Select the Compiler XC8.


 Click Next
Lab Manual Real Time Embedded Systems

New Project – Step 5

 Give Project Name, Project Location etc.


 Click Finish

New Project Wizard Completed

 Right Click on the Source Files in the Project Tree.


Lab Manual Real Time Embedded Systems

Adding a Source File

 Select New >> C Main File


 Provide Name and Location of the file

Name and Location – New C Main File

 New Source file is created, you can add code here.


Lab Manual Real Time Embedded Systems

New Source File Created

MPLAB XC8 Programing


Now let’s start to write our first program to blink an LED. Before that you must
include the configuration file in which you have wrote configuration bit settings
or enabled bits according to your requirements. There are two ways to write
configuration bit settings one is to write bit by bit enable or disable pins like
givenbelow: Make this file config.c

#define _XTAL_FREQ 8000000


#include <xc.h>

// Begin Configuration

#pragma config FOSC = HS

// Oscillator Selection bits (HS oscillator)


#pragma config WDTE = ON// Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
Lab Manual Real Time Embedded Systems

#pragma config LVP = OFF // Low-Voltage (Single-Supply)


#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF// Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
//END 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:

Go to Window >> PIC Memory Views >> Configuration Bits


Lab Manual Real Time Embedded Systems

Configuration Bits

You can select the configuration at the bottom of the IDE as shown below:

Configuration Bits

Click Generate Source Code to Output will yield following


Lab Manual Real Time Embedded Systems

Generated Configuration Bits Source

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.

Building the Project

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.

Writing Bit by Bit:

TRISC0 = 1; //Makes 0th bit of PORTC Input


TRISC5 = 0; //Makes 5th bit of PORTC Output
RB3 = 1; //Makes 3ed bit of PORTB at Logic High
RB7 = 0;//Makes 7th bit of PORTB at Logic Low

Writing Entire Register

You should be familiar with following C Programming concepts.

 A number with a prefix ‘0b’ indicates a binary number.


 A number with a prefix ‘0x’ indicates a hexadecimal number.
 A number without prefix is a decimal number.

Let’s see some examples…

Decimal Binary Hexadecimal

0 0b00000000 0x00

1 0b00000001 0x01

128 0b10000000 0x80

255 0b11111111 0xFF

PORTB = 0xFF; //Makes all pins of PORTB Logic High


TRISC = 0x00; //Makes all pins of TRISC Output
Lab Manual Real Time Embedded Systems

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.

Complete Code – LED Blinking


#define _XTAL_FREQ 8000000
#include <xc.h>
#include <p18f4520.h>
// BEGIN CONFIG
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF,LVP=OFF

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

Proteus Circuit Diagram

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

So this was all about your this lab.

Post Lab
Question:
Write a program and make Proteus circuit to blink 8,8 LEDs on two different output ports
with unique delay.

Circuit Analysis/ Conclusion


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

Registers used to Control Interrupt Operation


There are ten registers which are used to control interrupt operation. And these registers
are given below as:
 RCON
 INTCON
 INTCON2
 INTCON3
 PIR1, PIR2
 PIE1, PIE2
 IPR1, IPR2
Lab Manual Real Time Embedded Systems

Interrupt Sources Bits to Control their Operation


In general, interrupt sources have three bits to control their operation. They are given as:
 Flagbit to indicate that an interrupt event occurred
 Enablebit that allows program execution to branch to the interrupt vector address
when the flag bit is set
 Prioritybit to select high priority or low priority

PIC18 Interrupt LOGIC

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

INTCON: Interrupt Control Register

INTCON2: Interrupt Control Register2


Lab Manual Real Time Embedded Systems

INTCON3: Interrupt Control Register 3


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).

PIR1: Peripheral Interrupt Request (Flag) Register1

PIR2: Peripheral Interrupt Request (Flag) Register 2


Lab Manual Real Time Embedded Systems

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.

PIE1: Peripheral Interrupt Enable Register 1


Lab Manual Real Time Embedded Systems

PIE2: Peripheral Interrupt Enable Register 2


Lab Manual Real Time Embedded Systems

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.

IPR2: Peripheral Interrupt Priority Register 1

IPR2: Peripheral Interrupt Priority Register 2


Lab Manual Real Time Embedded Systems

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.

RCON: Reset Control Register


Lab Manual Real Time Embedded Systems

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;

void interrupt CheckSwitchPressed() {

if (INTCONbits.INT0F == 1) {

for (intflashcount = 0 ; flashcount<5 ; flashcount++)

LATBbits.LATB5 = 1;

delay(300);

LATBbits.LATB5 = 0;

delay(300);

INTCONbits.INT0F = 0;

void FlashingRouting() {

for(intflash_count=0 ; flash_count<=10 ; flash_count++)

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) {

for (int i=0 ; i<n ; i++) {

for (int j=0 ; j<250 ; j++);

ConfigBits.c file
Lab Manual Real Time Embedded Systems

#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor
disabled)

#pragma config IESO = OFF // Internal/External Oscillator Switchover bit

// 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))

#pragma config WDTPS = 32768 // Watchdog Timer PostscaleSelect bits (1:32768)

// 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)

// #pragma config statements should precede project file includes.

#define _XTAL_FREQ = 8000000

#include <xc.h>

Proteus Circuit Diagram

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

3. Write code and show results on Proteus circuit of above requirements?

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

T0con: Timer0 Control Register

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.

Timer0 Block Diagram (8-BIT MODE)

Timer0 Block Diagram (16-BIT MODE)

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

Registers Associated With Timer0

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)

Time Period = 0.2uS


Prescaler Period = 0.2 x 256 = 51.2uS
Overflow Period = 51.2 x 256 = 13107.2 uS
= 0.0131072 sec

So we need 1/0.0131072 Over Flows to count for 1 sec


= 76.2939 Overflows

So we keep a counter to keep track of overflows.

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.

#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;
Lab Manual Real Time Embedded Systems

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()
{
//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


}
INTCONbits.TMR0IF=0;//Clear Flag
}
}

Proteus Circuit Diagram


Lab Manual Real Time Embedded Systems

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:

1. How we can use Timer0 as Timer and Counter?

2. Describe different applications of Timer0 as Counter and as Timer?

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

Lab Experiment: don’t know


Peripheral InterruptDue to USART ModuleinPIC18F4520
Microcontroller
Objectives:
 To
 To

Tools:
PIC18F4520 microcontroller, MPLAB software with xc8 compiler, PROTEUS
software.

Pre-Lab

You might also like