Professional Documents
Culture Documents
MICROCONTROLLERS AND
APPLICATIONS
LAB MANUAL
Vision
Mission
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:
S.No
Total 20
19ELC212 – Microcontrollers and Applications
Experiment No #1
What is an IDE?
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
19ELC212 – Microcontrollers and Applications
https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-
compilers#tabs
ii. Click the file menu and click the new project
iii. Under the categories click microchip embedded and project as
standalone project.
2
19ELC212 – Microcontrollers and Applications
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
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
OBJECTIVE:
The experiment aims to perform the arithmetic operations in PIC 16F877A using
assembly language. [CO1]
PRELAB:
T
THEORY:
5
19ELC212 – Microcontrollers and Applications
TASK #1
SOURCE CODE:
6
19ELC212 – Microcontrollers and Applications
TASK #2
SOURCE CODE:
7
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
8
19ELC212 – Microcontrollers and Applications
Experiment No: #3
OBJECTIVE:
PRELAB:
THEORY:
9
19ELC212 – Microcontrollers and Applications
Circuit Design
Current Limiting Resistor
R = (V – Vf) / I
=
TASK #1
SOURCE CODE:
TASK #2
10
19ELC212 – Microcontrollers and Applications
SOURCE CODE:
11
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
Signature
Inference and 5
Results
12
19ELC212 – Microcontrollers and Applications
Experiment No #4
OBJECTIVE:
The aim of the experiment is to interface a seven-segment LED display with PIC
microcontroller. [CO4]
PRELAB:
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.
13
19ELC212 – Microcontrollers and Applications
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
14
19ELC212 – Microcontrollers and Applications
SOURCE CODE:
15
19ELC212 – Microcontrollers and Applications
16
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
17
19ELC212 – Microcontrollers and Applications
Experiment No #5
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:
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.
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:
19
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
Signature
Inference and Results 5
20
19ELC212 – Microcontrollers and Applications
Experiment No #6
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:
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.
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
21
19ELC212 – Microcontrollers and Applications
CIRCUIT DIAGRAM:
22
19ELC212 – Microcontrollers and Applications
23
19ELC212 – Microcontrollers and Applications
DELAY CALCULATION:
TASK #1:
SOURCE CODE:
24
19ELC212 – Microcontrollers and Applications
Task #2
SOURCE CODE:
25
19ELC212 – Microcontrollers and Applications
Prepared by:
Experiment Date:
Signature
Inference and Results 5
26
19ELC212 – Microcontrollers and Applications
Experiment. No #7
OBJECTIVE:
PRELAB:
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:
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:
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
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
32
19ELC212 – Microcontrollers and Applications
Task #2
CALCULATIONS:
SOURCE CODE:
33
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
Signature
Inference and Results 5
34
19ELC212 – Microcontrollers and Applications
Experiment No.# 08
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:
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.
36
19ELC212 – Microcontrollers and Applications
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
Submitted by:
Experiment Date:
40
19ELC212 – Microcontrollers and Applications
Experiment No #9
USART INTERFACE
OBJECTIVE:
PRELAB:
THEORY:
UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER
(USART)
41
19ELC212 – Microcontrollers and Applications
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.
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
43
19ELC212 – Microcontrollers and Applications
CIRCUIT DIAGRAM:
44
19ELC212 – Microcontrollers and Applications
CONFIGURING USART:
SOURCE CODE:
45
19ELC212 – Microcontrollers and Applications
Submitted by:
Experiment Date:
Student Task Max.Marks Graded
Report Submission Date: Marks
46