You are on page 1of 52

19ELC212

MICROCONTROLLERS AND
APPLICATIONS

LAB MANUAL

Department of Electrical and Electronics Engineering,


Amrita school of Engineering, Coimbatore
Amrita Vishwa Vidyapeetham, India.
Department of Electrical & Electronics Engineering

Vision

Mould generations of Electrical and Electronics Engineers on global standards with


multi disciplinary perspective to meet evolving societal needs.

Mission

• Empower students with knowledge in Electrical, Electronics and allied


Engineering facilitated in innovative classrooms and state-of-the art
laboratories.
• Inculcate technical competence and promote research through industry
interactions, field exposures and global collaborations.
• Promote professional ethics and selfless service.
LIST OF EXPERIMENTS
S.No. Name of the experiment
1 Introduction to MPLAB IDE
2 Assembly code for mathematical computations
3 Embedded C code for ON/OFF control of LEDs
4 Seven segment display interfacing
5 Matrix keypad interfacing

6 Waveform generation using Timers


7 Generation of PWM signal
8 Analog to digital conversion
9 USART interfacing
10 Demonstration of an application using 8051 microcontrollers
11 Mini project implementation

LAB CONDUCTION PLAN


WEEK TASK
1 Experiment – 1
2 Experiment – 2, and information about the mini project and batch
formation.
3 Experiment – 3 and introduction to MPLAB IDE using embedded C
compiler.
4 Experiment – 4
5 Experiment - 5, and mini project topic submission.
6 Experiment – 6
7 Experiment – 7
8 Experiment – 8
9 Experiment – 9 and discussion on mini-project topics and implementation
10 Mini-project implementation.
11 Mini-project implementation
12 Final evaluation of the mini project.
19ELC212 MICROCONTROLLERS AND APPLICATIONS L-T-P: 3-0-3-4

Syllabus:
Unit1: Introduction to micro controllers-Architecture and programming, register files,
Memory Organisation, Tristate-logic, Buses-Memory Address register-Memory
addressing-Read and write operations, ROM-RAM-PROM-EPROM-E2PROM.
Unit2: PIC16FXXX architecture, operation, data and program memory organization,
special function registers, addressing modes, instruction set. Assembler, assembler
directives, simple programs, conditional branching. Subroutines, nested subroutines,
interrupt, ISR, priority.
Unit3: Peripherals: Port configuration, Parallel Slave Port, LED and Keyboard
interface, Timers/Counters, WatchDog Timer, ADC, USART, CCP module.
Introduction to 8051 microcontrollers: Architecture, Instruction Set, Interrupts, Ports,
Timers.

Text Books:
Myke Predko, “Programming and customizing the PIC microcontroller”, Tata McGraw
Hill Publishing Company Limited, Third Edition, 2008.
References:
T. R. Padmanabhan, “Introduction to microcontrollers and applications”, First Edition,
Narosa publishing house private limited, 2007. PIC Micro mid-Range MCU Family
Reference Manual -Micro Chip Technology Inc.

COURSE OUTCOMES:
CO Course Outcomes
CO01 Understanding of concepts of processors and microcontrollers.
CO02 Ability to program PIC16F877A microcontroller.
CO03 Design a microcontroller-based system for various applications.
CO04 Ability to develop PIC16F877A applications through simulation and
hardware.
EVALUATION PATTERN:

COMPONENTS & RUBRICS FOR LAB EVALUATION:


Components Marks Instrument Guidelines

S.No

Program flow/ logic 15 Report 0-3 - Logic

1 & & 0-7 – Program

Simulation PIC 0-5 – Simulation/Hardware


development
board /
Simulation
software

Results and Report


2 5 0-3 – Result
inferences
0-2 - Understanding

Total 20
19ELC212 – Microcontrollers and Applications

Experiment No #1

INTRODUCTION TO MPLAB IDE


The PIC microcontroller programming is performed using ‘MPLAB’ IDE (integrated
development environment). Simulation software can interact with the embedded
project with screen indicators, for example, LED, LCDs, switches, and buttons. The
simulations can be carried out to test the system’s behaviour by feeding appropriate
inputs through the sensors interfaced, and output can be observed through the various
actuators connected.

What is an IDE?

An integrated development environment (IDE) is a software application that helps


programmers develop software code efficiently. It increases developer productivity by
combining software editing, building, testing, and packaging capabilities in an easy-to-
use application. Like writers use text editors, and accountants use spreadsheets,
software developers use IDEs to make their job easier.

What is the purpose of simulation?

Before implementing the practical circuit, we must check whether the embedded
program will work perfectly for our project. Such software provides an environment to
test our microcontroller program.

MPLAB IDE

The PROTEUS software can run the PIC programs we write, and we can check the
program executions similar to that in the hardware by interfacing LEDs, LCDs, motors,
etc. To the microcontroller board/simulator we need to burn the .hex file. To generate
the .hex file, we use MPLAB IDE.

PROCEDURE:

1. First, install the MPLAB software by clicking the below link:


https://www.microchip.com/en-us/tools-resources/archives/mplab-ecosystem
and select MPLAB X v5.10 Then select and install the required compiler:

1
19ELC212 – Microcontrollers and Applications

a. asm (for assembly language programming), installed automatically with


MPALB X v5.10 or
b. CCS C (for C programming) compiler for building the program by clicking
the link below:

https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-
compilers#tabs

choose MPLAB XC8 supports all 8-bit PIC®

c. Go to Downloads, Documentation, and other Resources and click on the


compiler downloads.
d. Choose MPLAB® XC8 Compiler v2.36 and download and install

CONFIGURING THE MPLAB IDE:

i. The MPLAB X software looks like the below figure.

ii. Click the file menu and click the new project
iii. Under the categories click microchip embedded and project as
standalone project.

iv. Press next


v. Select device → family → all families and device → PIC16F877A → next

2
19ELC212 – Microcontrollers and Applications

vi. Select tool (optional) → click → simulator

vii. Select compiler →mpasm

viii. Next
ix. Select project name and folder:
x. Give desired project name and browse to give desired folder location to
store the programs
xi. Click FINISH
xii. After the project is created, click file menu and new file.
xiii. Type the program and save it with an extension .asm/.c
xiv. Add this file to the source file of the project
xv. Then click build project

3
19ELC212 – Microcontrollers and Applications

xvi. Click debug project

xvii. To observe the result, click the window menu → target memory views
program memory → you can see the program

xviii. Click window → target memory views → file register → you can observe
the register content and check your results

CONCLUSION:

The MPLAB IDE installation and configuration steps were completed, and a sample
program was run to understand the execution of a program and how to observe the
output in the IDE.

4
19ELC212 – Microcontrollers and Applications

Experiment No #2

ASSEMBLY CODE FOR MATHEMATICAL COMPUTATIONS

OBJECTIVE:

The experiment aims to perform the arithmetic operations in PIC 16F877A using
assembly language. [CO1]

PRELAB:

Complete the following study before starting the lab


• PIC16F877A Architecture
• PIC Instruction set
• Arithmetic operations
• MPLAB

T
THEORY:

• Peripheral Interface Controllers (PIC) is one of the advanced


microcontrollers widely used in all modernelectronics applications.
• All PIC microcontrollers are RISC microcontrollers, thus having a relatively
reduced number of instructions.
• All PIC microcontroller family uses Harvard architecture which is
characterized by having differentmemories for program and for data.
• The program memory is organized in words of 12, 14, or 16 bits; the data
memory is based on registers of8 bits.
• The access to the diverse I/O devices is carried out through some registers
in the data memory calledspecial function registers (SFRs).
• PIC microcontrollers have a working (W) register and multiple data memory
registers.
• The arithmetic and logic unit (ALU) is one of the fundamental components
in a microcontroller. The ALUexecutes the arithmetic and logic operations
available in the instruction set.
• When carrying out arithmetic or logic operations, one of the operands
must be in the W register. Theresulting value will be placed either in the W
register or in any other register in the data memory.
• Data transfer occurs between the W register and any other register in the
data memory, although some high-end PICs allow data transfer directly
between two data memory registers.

5
19ELC212 – Microcontrollers and Applications

USEFUL PIC INSTRUCTIONS:

MOVLW MOVWF ADDWF ADDLW SUBWF SUBLW INCF DECF


BTFSS BTFSC COMP COMF

TASK #1

SOURCE CODE:

Verified by: Marks:

6
19ELC212 – Microcontrollers and Applications

TASK #2

SOURCE CODE:

RESULT AND INFERENCE:

Verified by: Marks:

7
19ELC212 – Microcontrollers and Applications

Testing and Verification:

(i) Check the output using MPLAB IDE

(ii) Implementation using PIC16F877A development board.

Submitted by:

Name: Reg. No.:

Experiment Date:

Report Submission date:

Student Task Max.Marks Graded


Marks

Program flow / logic and


15
Simulation
Signature

Inference and Results 5

8
19ELC212 – Microcontrollers and Applications

Experiment No: #3

EMBEDDED C CODE FOR ON/OFF CONTROL OF LEDS

OBJECTIVE:

The objective of the experiment is to configure the port as input/output. [CO4]


(i) To turn-on the LED connected to port with software delay
(ii) To control the LED with pushbutton switch.

PRELAB:

Complete the following study before starting the lab.


• PIC16F877A Architecture
• PIC Instruction set.
• PIC16F877A Pin configuration
• MPLAB

THEORY:

• PIC microcontrollers organize the digital I/Os into “ports”.


• Each port on the PIC16 is a collection of up to 8 individual I/Os,
and they are named PORTA, PORTB, PORTC, etc. These I/Os
are easily configurable and can be accessed individually or as a
group.
• All port pins have registers directly associated with their operation as digital
I/Os.
• The Data Direction register (TRISA, TRISB, TRISC…) determines whether
the pin is an input or an output.
• If the data direction bit is a ‘1’, the pin is an input. If the data direction bit
is a ‘0’ the pin is anoutput.
CIRCUIT DIAGRAM:

a) LED connected to PIC port


b) Switch and LED connected to PIC ports

9
19ELC212 – Microcontrollers and Applications

Circuit Design
Current Limiting Resistor

Let I = LED forward current in Amps (from LED datasheet), Vf = LED


forward voltage drop inVolts (from LED datasheet) and V = microcontroller
output pin voltage.

R = (V – Vf) / I
=

TASK #1

SOURCE CODE:

RESULT AND INFERENCE:

Verified by: Marks:

TASK #2

10
19ELC212 – Microcontrollers and Applications

SOURCE CODE:

RESULT AND INTEREFENCE:

11
19ELC212 – Microcontrollers and Applications

Verified by: Marks:


Testing and Verification:

(iii) Check the output using Logic Analyzer in MPLAB IDE

(iv) Implementation using PIC16F877A development board.

Submitted by:

Name: Reg. No.:

Experiment Date:

Report Submission Date:


Student Task Max. Marks Graded
Marks

Program flow / logic


15
andSimulation

Signature
Inference and 5
Results

12
19ELC212 – Microcontrollers and Applications

Experiment No #4

SEVEN-SEGMENT DISPLAY INTERFACE

OBJECTIVE:

The aim of the experiment is to interface a seven-segment LED display with PIC
microcontroller. [CO4]

PRELAB:

Complete the following study before starting the lab.


• PIC16F877A Architecture
• PIC16F877A Pin configuration
• 7 Segment LED display
• MPLAB

THEORY:

Seven segment display is the most basic and simplest electronics display
device. Simplest, because its working is easy to understand and its interface with
the microcontroller is quite straight-forward. As the name suggests, seven
segments consist of seven LEDs organized in a specific pattern. Each LED is
known as a segment, and it is a and name from ‘a’ to ‘g’. Another additional 8th
LED named ‘dp’ is also present sometimes in a 7-segment display which is used
to illuminate a dot or a decimal point.

Fig. 1 Common anode seven segment display

13
19ELC212 – Microcontrollers and Applications

Table I: Look up table for common anode seven segment display

Decimal G F E D C B A Hex
Number

0 1 0 0 0 0 0 0 0x40

1 1 1 1 1 0 0 1 0x79

2 0 1 0 0 1 0 0 0x24

3 0 1 1 0 0 0 0 0x30

4 0 0 1 1 0 0 1 0x19

5 0 0 1 0 0 1 0 0x12

6 0 0 0 0 0 1 0 0x02

7 1 1 1 1 0 0 0 0x78

8 0 0 0 0 0 0 0 0x00

9 0 0 1 0 0 0 0 0x10

Seven Segment Display Types


A common pin is also associated with the 7-segment, which is used to identify
the type of 7-segment display, whether it is the common anode or a common
cathode. In a common anode display, the positive pins of all the LEDs are tied
together to form the common pin, which needs to be provided a ‘HIGH’ signal
which means 5 volts signal. Connect that pin to the ground or logic low level to
turn on a specific segment. In a common cathode display, all the cathode
connections of the LEDs are tied together, forming the common pin that needs to
be grounded. To turn on a specific segment in common cathode mode, we
connect that pin to the voltage or logic high level with a microcontroller.

14
19ELC212 – Microcontrollers and Applications

Fig. 2 Seven Segment LED interfacing circuit

SOURCE CODE:

15
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

16
19ELC212 – Microcontrollers and Applications

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Submitted by:

Name: Reg. No.: _

Experiment Date:

Report Submission Date:

Student Task Max.Marks Graded


Marks

Signature Program flow / logic and


15
Simulation

Inference and Results 5

17
19ELC212 – Microcontrollers and Applications

Experiment No #5

MATRIX KEYPAD INTERFACING

OBJECTIVE:

The aim of the experiment is to interface a 3x4 matrix keypad with PIC
microcontroller and display the key values in a seven-segment LED display of the
PIC microcontroller board.
PRELAB:

Complete the following study before starting the lab.


• PIC16F877A Architecture
• PIC16F877A Pin configuration
• Matrix keyboard interfacing
• MPLAB

THEORY:
A keypad is simply an array of pushbuttons connected in rows and columns to
test each for closure with minimum connections. There are 12 keys arranged in
a 3×4 matrix. Assume the columns are labelled 1, 2, 3, 4 and the rows A, B, C. If
we assume that all the rows and columns are initially high, a keystroke can be
detected by setting each row low in turn and checking each column for a zero.

Fig. 1 Keypad interfacing circuit

18
19ELC212 – Microcontrollers and Applications

While coding, pins RB5 to RB7 should be initialized as outputs and pins RB1 to
RB4 as inputs. These input pins are pulled high (logic 1). The output rows are
also initially set to 1. If a 0 is now output on row A, the inputs have no effect when
row A is pressed. If these are checked in turn for a 0, a button in this row that is
pressed can be identified as a specific combination of output and input bits.

SOURCE CODE:

RESULT AND INFERENCE:

Verified by: Marks:

19
19ELC212 – Microcontrollers and Applications

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Submitted by:

Name: Reg. No.:

Experiment Date:

Report Submission Date:

Student Task Max.Marks Graded


Marks

Program flow / logic and


15
Simulation

Signature
Inference and Results 5

20
19ELC212 – Microcontrollers and Applications

Experiment No #6

WAVEFORM GENERATION USING TIMERS

OBJECTIVE:

The experiment aims to configure the Timer peripheral for various applications.
(i) To turn-on the LED connected to port with Timer module. [CO4]

PRELAB:

Complete the following study before starting the lab.


• PIC16F877A Timer0 & Timer1 Module
• PIC16F877A Timer2, CCP
• MPLAB IDE

THEORY:
PIC16F877A consist of three Timer Modules i.e, Timer-0, Timer-1 and Timer-2. Every
Timer needs a clock pulse to tick. The Clock Source can be internal or external. If the
internal clock source is used, then 1/4th of the frequency of the Crystal Oscillator on
the OSC1 and OSC2 pins (FOSC/4) is fed into the Timer.

TIMER 1 MODULE

Timer 1 module is a 16-bit timer/counter unit. It consists of two 8-bit (8+8) registers
(TMR1H, TMR1L), which read and write easily. TMR1 register is a pair of TMR1H and
TMR1L, and its value increment from 0000h to FFFFh and rolls over to 0000h.

Timer 1 module operates in two different modes. They are,

1) Timer mode

2) Counter mode

The operating mode of timer 1 module is selected by using the clock select bit
(TMR1CS), in timer mode. The Timer mode can be easily selected by clearing the
TMR1CS bit. In this mode, the input clock to the timer is FOSC/4. The synchronize
control bit, T1SYNC, has no effect since the internal clock is always in sync. Timer1
Operation in Synchronized.

Counter Mode

The synchronized Counter mode is selected by setting timer 1 synchronized counter


select bit (TMR1CS). In this mode, the timer increments on every rising edge of clock

21
19ELC212 – Microcontrollers and Applications

input on pin RC1/T1OSI/CCP2 when bit T1OSCEN is set or on pin RC0/T1OSO/T1CKI


when bit T1OSCEN is cleared.

CIRCUIT DIAGRAM:

22
19ELC212 – Microcontrollers and Applications

23
19ELC212 – Microcontrollers and Applications

DELAY CALCULATION:

Desired delay = (Count+1) * Instruction cycle * Prescalar

Instruction cycle = 4/ Fosc

TASK #1:

SOURCE CODE:

24
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

Task #2

SOURCE CODE:

25
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Prepared by:

Name: Reg. No.:

Experiment Date:

Report Submission Date:

Student Task Max.Marks Graded


Marks

Program flow / logic and


15
Simulation

Signature
Inference and Results 5

26
19ELC212 – Microcontrollers and Applications

Experiment. No #7

GENERATION OF PWM SIGNAL

OBJECTIVE:

The objective of the experiment is to configure CCP module in PIC microcontroller to


generate a PWM signal of given frequency and duty cycle. [CO4]

PRELAB:

Complete the following study before starting the lab


• PIC16F877A Timer2, CCP
• MPLAB IDE
• PIC16Fxx reference manual

THEORY:

Timer2 is an 8-bit timer with a prescaler and a postscaler. It can be used as the PWM
time base for thePWM mode of the CCP module(s). The input clock (FOSC/4) has a
prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0
(T2CON<1:0>). The Timer2 module has an 8-bit period register, PR2. Timer2
increments from 00h until it matches PR2 and then resets to 00h on the next increment
cycle. PR2 is a readable and writable register. The PR2 register is initialized to FFh
upon Reset.

CCP module:

Each CCP (Capture/Compare/PWM) module contains a 16-bit register which can


operate as a 16-bit capture register, as a 16-bit compare register or as a 10-bit PWM
master/slave Duty Cycle register.

PWM MODE:

In Pulse Width Modulation (PWM) mode, the CCPx pin produces up to a 10-bit
resolution PWM output.Since the CCPx pin is multiplexed with the PORT data latch,
the corresponding TRIS bit must be cleared to make the CCPx pin an output. A PWM
output has a time-base (period) and a time that the output stays high (duty cycle). The
frequency of the PWM is the inverse of the period (1/period).

27
19ELC212 – Microcontrollers and Applications

28
19ELC212 – Microcontrollers and Applications

29
19ELC212 – Microcontrollers and Applications

PWM Period

The PWM period is specified by writing to the PR2 register. The PWM period can be
calculated usingthe following formula:

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value), specified in units of


time

PWM frequency (FPWM) is defined as 1 / [PWM period].

When TMR2 is equal to PR2, the following three events occur on the next increment
cycle:

• TMR2 is cleared
• The CCPx pin is set (exception: if PWM duty cycle = 0%, the CCPx pin will not be
set)
• The PWM duty cycle is latched from CCPRxL into CCPRxH

PWM Duty Cycle

The PWM duty cycle is specified by writing to the CCPRxL register and to the
DCxB1:DCxB0 (CCPxCON<5:4>) bits. Up to 10-bit resolution is available: the
CCPRxL contains the eight MSbs and CCPxCON<5:4> contains the two LSbs. This
10-bit value is represented by DCxB9:DCxB0. The following equation is used to
calculate the PWM duty cycle:

PWM duty cycle = (DCxB9:DCxB0 bits value) • Tosc • (TMR2 prescale value), in units
of time

The DCxB9:DCxB0 bits can be written to at any time, but the duty cycle value is not
latched into CCPRxHuntil after a match between PR2 and TMR2 occurs (which is the
end of the current period). In PWM mode, CCPRxH is a read-only register. The
CCPRxH register and a 2-bit internal latch double buffer the PWM duty cycle.

This double buffering is essential for glitchless PWM operation. When CCPRxH
and 2-bit latch match thevalue of TMR2 concatenated with the internal 2-bit Q
clock (or two bits of the TMR2 prescaler), the CCPxpin is cleared.

30
19ELC212 – Microcontrollers and Applications

CIRCUIT DIAGRAM:

Motor connected to PWM pin using MOSFET PWM control using/ switch

TASK #1:

CALCULATIONS:

SOURCE CODE:

31
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

32
19ELC212 – Microcontrollers and Applications

Task #2

CALCULATIONS:

SOURCE CODE:

33
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Submitted by:

Name: Reg. No.:

Experiment Date:

Report Submission Date:

Student Task Max.Marks Graded


Marks

Program flow / logic and


15
Simulation

Signature
Inference and Results 5

34
19ELC212 – Microcontrollers and Applications

Experiment No.# 08

ANALOG TO DIGITAL CONVERSION

OBJECTIVE:

The aim of the experiment is to configure the ADC in PIC microcontroller and to
convert analog signal to digital values using ADC module. [CO4]

PRELAB:

Complete the following study before starting the lab


• PIC16F877A ADC module
• PIC16Fxx reference manual

THEORY:
A/D CONVERTER:
The analog-to-digital (A/D) converter module has up to eight analog inputs for a
device. The converter then generates a digital result of this analog level via successive
approximation. This A/D conversion, of the analog input signal, results in a
corresponding 10-bit digital number. The analog reference voltages (positive and
negative supply) are software selectable to either the device’s supply voltages (AVDD,
AVSS) or the voltage level on the AN3/VREF+ and AN2/VREF pins. The A/D module

35
19ELC212 – Microcontrollers and Applications

has four registers. These registers are: A/D Result High Register (ADRESH), A/D
Result Low Register (ADRESL), A/D Control Register0 (ADCON0), A/D Control
Register1 (ADCON1). The ADCON0 register controls the operation of the A/D module.
The ADCON1 register configures the functions of the port pins.

A/D RESULT REGISTERS:


The ADRESH : ADRESL register pair is the location where the 10-bit A/D result is
loaded at the completion of the A/D conversion. This register pair is 16-bits wide. The
A/D module gives the flexibility to left or right justify the 10-bit result in the 16-bit result
register. The A/D Format Select bit (ADFM) controls this justification.
The following steps should be followed for doing an A/D conversion:
1. Configure the A/D module:
• Configure analog pins / voltage reference/ and digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D conversion clock (ADCON0)
• Turn on A/D module (ADCON0)
2. Configure A/D interrupt (if desired):
• Clear the ADIF bit

36
19ELC212 – Microcontrollers and Applications

• Set the ADIE bit


• Set the GIE bit
3. Wait the required acquisition time.
4. Start conversion:
• Set the GO/DONE bit (ADCON0)
5. Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared or ADIF bit to be set OR
• Waiting for the A/D interrupt
6. Read A/D Result register pair (ADRESH : ADRESL), clear the ADIF bit, if
required.
7. For next conversion, go to step 1 or step 2 as required.

Acquisition time is the time that the A/D module’s holding capacitor is connected to
the external voltage level. Then there is the conversion time of 12 TAD, which is
started when the GO bit is set. The sum of these two times is the sampling time. There
is a minimum acquisition time to ensure that the holding capacitor is charged to a level
that will give the desired accuracy for the A/D conversion.

37
19ELC212 – Microcontrollers and Applications

CIRCUIT DIAGRAM:

TASK #1

CONFIGURING ADC:

38
19ELC212 – Microcontrollers and Applications

SOURCE CODE:

39
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Submitted by:

Name: Reg. No.: _

Experiment Date:

Report Submission Date: Student Task Max.Marks Graded


Marks

Program flow / logic and


15
Simulation

Signature Inference and Results 5

40
19ELC212 – Microcontrollers and Applications

Experiment No #9

USART INTERFACE
OBJECTIVE:

The objective of this experiment is to configure the USART module of PIC


microcontroller to transmit and receive serial data. [CO4]

PRELAB:

Complete the following study before starting the lab.


• PIC16F877A USART module
• PIC16Fxx reference manual

THEORY:
UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER
(USART)

The Universal Synchronous Asynchronous Receiver Transmitter (USART) module is


one of the two serial I/O modules (other is the SSP module). The USART is also known
as a Serial Communications Interface or SCI. The USART can be configured as a full
duplex asynchronous system that can communicate with peripheral devices such as

41
19ELC212 – Microcontrollers and Applications

CRT terminals and personal computers. It can also be configured as a half-duplex


synchronous system that can communicate with peripheral devices such as A/D or
D/A integrated circuits, Serial EEPROMs etc.

The USART can be configured in the following modes:

• Asynchronous (full duplex)


• Synchronous - Master (half duplex)
• Synchronous - Slave (half duplex)

The SPEN bit (RCSTA<7>), and the TRIS bits, have to be set to configure the TX/CK
and RX/DT pins for the USART.

USART Baud Rate Generator (BRG)

The BRG supports both the Asynchronous and Synchronous modes of the USART. It
is a dedicated 8-bit baud rate generator. The SPBRG register controls the period of a

42
19ELC212 – Microcontrollers and Applications

free running 8-bit timer. In asynchronous mode bit BRGH (TXSTA<2>) also controls
the baud rate. In synchronous mode bit BRGH is ignored.

Given the desired baud rate and Fosc, the nearest integer value for the SPBRG
register can be calculated, where X equals the value in the SPBRG register (0 to 255).

It may be advantageous to use the high baud rate (BRGH = 1) even for slower baud
clocks. This is because the FOSC / (16(X + 1)) equation can reduce the baud rate
error in some cases. Writing a new value to the SPBRG register causes the BRG timer
to be reset (or cleared). This ensures the BRG does not wait for a timer overflow before
outputting the new baud rate

USART Asynchronous Mode


Asynchronous mode is selected by clearing the SYNC bit (TXSTA<4>).
The USART Asynchronous module consists of the following important elements:
• Baud Rate Generator
• Sampling Circuit
• Asynchronous Transmitter
• Asynchronous Receiver

43
19ELC212 – Microcontrollers and Applications

USART Asynchronous Transmitter


Steps to follow when setting up a Asynchronous Transmission:
1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud
rate is desired, set the BRGH bit.
2. Enable the asynchronous serial port by clearing the SYNC bit and setting the
SPEN bit.
3. If interrupts are desired, then set the TXIE, GIE and PEIE bits.
4. If 9-bit transmission is desired, then set the TX9 bit.
5. Enable the transmission by setting the TXEN bit, which will also set the TXIF
bit.
6. If 9-bit transmission is selected, the ninth bit should be loaded in the TX9D bit.
7. Load data to the TXREG register (starts transmission).

USART Asynchronous Receiver


Steps to follow when setting up an Asynchronous Reception:
1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud
rate is desired, set bit BRGH.
2. Enable the asynchronous serial port by clearing the SYNC bit, and setting the
SPEN bit.
3. If interrupts are desired, then set the RCIE, GIE and PEIE bits.
4. If 9-bit reception is desired, then set the RX9 bit.
5. Enable the reception by setting the CREN bit.
6. The RCIF flag bit will be set when reception is complete and an interrupt will be
generated if the RCIE bit was set.
7. Read the RCSTA register to get the ninth bit (if enabled) and determine if any
error occurred during reception.
8. Read the 8-bit received data by reading the RCREG register.
9. If any error occurred, clear the error by clearing the CREN bit.

CIRCUIT DIAGRAM:

44
19ELC212 – Microcontrollers and Applications

CONFIGURING USART:

SOURCE CODE:

45
19ELC212 – Microcontrollers and Applications

RESULT AND INFERENCE:

Verified by: Marks:

Testing and Verification:

(i) Program flow / logic

(ii) Implement the circuit in PIC16F877A and verify the task.

Submitted by:

Name: Reg. No.:

Experiment Date:
Student Task Max.Marks Graded
Report Submission Date: Marks

Program flow / logic and


15
Simulation

Inference and Results 5


Signature

46

You might also like