You are on page 1of 35

SCHOOL OF ELECTRONICS ENGINEERING

(SENSE)
B. Tech (Electronics and Communication
Engineering)

WIN 2022-23

ECE 3003: MICROCONTROLLER AND ITS


APPLICATIONS

LAB MANUAL

Prepared by: Dr. Padmini T.N


Department of Embedded technology
SENSE, VIT

1
S. No Sections Page No
Introduction to 8051
1 Microcontroller 6

Instructions to use KEIL


2 SIMULATOR 9

Experiments (Conducted in Lab)


3 19

2
S. No List of Experiments (Conducted in Lab)
1 Arithmetic operation using 8-bit data

2 Data transfer between ROM and RAM

3 Stack operations

4 BCD Addition

5 Parallel Ports

6 Timer and counter Programming

7 Serial Communications

8 Timer Interrupt

9 Serial Interrupt

10 LCD Programming

3
S. No List of Challenging Experiments

Write an ALP using 8051 to initiate the stack pointer at 40H location and
1 load 5 data (8-bit data) into Bank 2. Then push all the 5 data into the stack
from bank 2 and retrieve the data from stack to bank 0. Finally add the
last two data retrieved from stack and store the result in accumulator.

Implement the logic circuits using 8051 assembly code in KEIL and
2 simulate the following equations
1. F=(AB+C)’(C’D)’
2. C=AB’+BB’; D=AB

3 Implement BCD to Excess-3 converter using 8051 microcontroller.

Write an ALP using 8051 to check the status of two switches connected to
4 port P2.1 and P2.2. If any one of the switches is ON (logic 1) make the
LED connected to port 2.7 to toggle. Repeat this continuously.
Assume the last digit of your register no as time taken in millisecond to
introduce delay between the ON and OFF of the LED. (For example, if
your register no is 20BEC0867 then consider 7 ms as time delay. For
students bearing register no ending with zero can assume the time delay
as 1 ms)

Write an ALP using 8051 to implement the following: Use counter 1 in


5 mode 2 and after 10 counts on TL1, generate a square waveform on P1.2
by using Timer 0, and show the counts in TL1 on port 2. Assume the last
digit of your register no as the frequency of the square wave in KHz. (For
example, if your register no is 15BEC0867 then f =7 kHz. For students
bearing register no ending with zero can assume the frequency as 1 kHz).

Write a program to send the message “India is our country” to serial port.
6 Assume a switch (SW) is connected to pin P1.2. Monitor its status and set
the baud rate as follows:
SW=0, 4800 baud rate
SW=1, 9600 baud rate
Assume XTAL=11.0592 MHz, 8-bit data, and 1 stop bit.

Write an 8051 assembly program using timer 0 to generate a 500 Hz


7 waveform and timer 1 to generate a 7 kHz square wave frequency on P1.0
and P1.1 respectively using timer interrupts. Then examine the frequency
using the KEIL IDE inbuilt Logic Analyzer.

4
Develop an 8051 ALP to implement the serial communication as per the
8 following conditions, If the Microcontroller receives a number “1” then
generate a square wave for 1 KHz or if the Microcontroller receives a
number “2” then generate a square wave for 2 KHz. If any number other
than 1 and 2, monitor the status of the received number.

Write an ALP using 8051 to implement the following: Assume a switch is


9 connected to P0.7. If the switch is at logic 0, transfer the last digit of your
register number from parallel port P1 to serial port. If the switch is at
logic 1, transfer the last digit of your register number from serial port to
parallel port P1. Repeat it continuously.

To write an ALP to solve the given equation without using lookup table
10

.
1 1 2 2 3 3 i i

Assume X =1 and Y =1, X =2 and Y =2, X =3 and Y =3. X and Y values

are stored in ROM memory starting from 100H. Store the final result in

BCD and send it to parallel port P0 and serial port at 2400 baudrate.

Introduction to 8051 Microcontroller

5
8051 Microcontroller Architecture
The 8051 microcontroller was invented in 1980's by Intel. Its foundation is based on Harvard
architecture and this microcontroller was developed principally for bringing it to be used in
Embedded Systems.

At first it was created by using NMOS technology but the use of NMOS consumed more power
to work therefore Intel re-launch the microcontroller 8051 using CMOS technology and new
edition came up with edition of letter 'C' in the title name, therefore the new modified version of
microcontroller is called by name 80C51.

Features of 8051 Microcontroller:


Let's see the common feature of 8051 microcontroller:
 It having four register banks
 64K bytes on-chip programmable memory (ROM)
 128 bytes on-chip data memory (RAM)
 Address bus is 16-bit unidirectional
 Data bus is 8-bit bidirectional
 128 user defined flags
 16 bit timers
 32 general purpose registers each of 8-bit
 8051 microcontroller offers a number of special features such as ADC, UARTs, Op-
amp, etc.

Let's see the internal architecture of 8051 Microcontroller represented in form of block diagram
as shown below:
Basic components present internally inside 8051 Microcontroller architecture are:

CPU (Central Processing Unit): CPU act as a mind of any processing machine. It
synchronizes and manages all processes that are carried out in microcontroller. User has no
power to control the functioning of CPU. It interprets the program stored in ROM and carries
out from storage and then performs it projected duty. CPU manage the different types of
registers available in 8051 microcontroller.

Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other
program with high priority is request for acquiring the system buses the n interrupts occur in
current running program.

Interrupts provide a method to postpone or delay the current process, performs a sub-routine
task and then restart the standard program again.

6
Types of interrupt in 8051 Microcontroller:

Let's see the five sources of interrupts in 8051 Microcontroller:

 Timer 0 overflow interrupt - TF0


 Timer 1 overflow interrupt - TF1
 External hardware interrupt - INT0
 External hardware interrupt - INT1
 Serial communication interrupt - RI/TI

Memory: For operation Micro-controller required a program. This program guides the
microcontroller to perform the specific tasks. This program installed in microcontroller required
some on chip memory for the storage of the program.

Microcontroller also required memory for storage of data and operands for the short duration. In
microcontroller 8051 there is code or program memory of 4 KB that is it has 4 KB ROM and it
also comprise of data memory (RAM) of 128 bytes.

Bus : Bus is a group of wires which uses as a communication canal or acts as means of data
transfer. The different bus configuration includes 8, 16 or more cables. Therefore, a bus can
bear 8 bits, 16 bits all together.

Types of buses in 8051 Microcontroller:

Let's see the two types of bus used in 8051 microcontroller:

 Address Bus: 8051 microcontrollers is consisting of 16 bit address bus. It is generally


be used for transferring the data from Central Processing Unit to Memory.
7
 Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is generally be used
for transferring the data from one peripherals position to other peripherals.

Oscillator: As the microcontroller is digital circuit therefore it needs timer for their operation.
To perform timer operation inside microcontroller it required externally connected or on-chip
oscillator. Microcontroller is used inside an embedded system for managing the function of
devices. Therefore, 8051 uses the two 16 bit counters and timers. For the operation of this
timers and counters the oscillator is used inside microcontroller.

Application of 8051 Microcontroller

The 8051 microcontroller applications include a large amount of machines because it is used for
incorporating inside a project or to assemble a machine using it.

Let's see the major applications of 8051 Microcontroller:

1. Energy Management : In energy management system the measuring device is used for
calculating the energy consumption in industrialized and domestic applications. These
systems are manufactured by integrating the microcontrollers inside their architecture
configuration.
2. Automobiles : Microcontroller 8051 is to be used for providing automobile solutions.
They are largely be used in hybrid motor vehicles to control engine variations.
3. Touch screens: The advanced degree of microcontroller integrate the touch sensing
ability within their design .Transportable devices such as cell phones, media players and
gaming devices are some example of microcontroller integrated with touch screens.
4. Medical Devices: Microcontroller is used in various medical devices such as glucose
and blood pressure measurement machine for monitoring and measuring the exact result
in real-time computational environment.

8
Instructions to use KEIL SIMULATOR
STEP 1: Open Keil uVision5
(Start >Programs>Keil uVision 5)

STEP2: Start a new project (File>New>µvision project

STEP3: Type any name without extension, and Save in your folder
9
STEP 4: Select Nxp >P89V51RD2

10
STEP 5 (selectyes)

Blank space will appear

11
STEP 6: To create source file
Open a new file (FileNew File)

STEP 6 (Contd): A new window will open with blank space to type the code

12
STEP 7: Type the code

STEP 8: Click file menu and save

13
STEP 9: Save the file name with extension as  .asm

Step 10: Right click the source group and selectAdd existing file to the source group

14
Step 11: select the saved file from the folder and clickADD and close

Step12: Expand the source group in project work space and ensure your file is under
source group

15
Step13: Select”Translate” on the left corner and check for syntax Errors

Step14: Select”Build” on the left corner

Step15: Select”Rebulid” on the left corner

16
Step16: DebugStart Debug

Step17: Click ”ok”

Registers will appear on the left

17
Step18: Click DebugRun

Check the results

18
Experiments (Conducted in Lab)

Experiment -1
Arithmetic operation using 8-bit data
Program – 1

Write and assemble a program to add the following data and then use the simulator to examine
the CY flag.
Input Data 92H, 23H, 66H, 87H, F5H

ORG 0000h
MOV A, #92H; Load first i/p data in A register.
MOV B, #23H; Load second i/p data in B register.
ADD A, B ; Add the two data
JNC L1 ; check for carry
INC R0; Increment reg R0, If CARRY occurs
L1: MOV B, A
MOV A, #66H; Load third i/p data in A register.
ADD A, B
JNC L2
INC R0
L2: MOV B,A
MOV A,#87H; Load fourth i/p data in A register.
ADD A,B
JNC L3
INC R0
L3: MOV B,A
MOV A,#0F5H; Load fifth i/p data in A register.
ADD A, B
JNC L4
INC R0
L4: SJMP L4
END

Program 2

Write a program to calculate y where y = x2 + 2x + 9.


x is between 0 and 9 and the look-up table for x2 is located at the address (code space) of 200H.
Register R0 has the x, and at the end of the program R2 should have y.
Use the simulator to change the x value and single-step through the program,
examining the registers as you go.

ORG 000H
19
MOV DPTR, #200H
MOV A, #03H; loading x value, eg.x=3
MOV R1, A; copy x value to R1
MOV R0,A; copy x value to R0
ADD A, R1; Add with carry( adding to get 2x)
MOV R1, A; R1 will have 2x value
MOV A, R0; Load A with x value
MOVC A, @A+DPTR; Mov from code space (ROM) to A register(get X2 from lookup table
into register A)
ADD A,#09H; x2+9
ADD A, R1; x2+2x+9
MOV R2, A
HERE: SJMP HERE
ORG 200H
DB 00H,01H,04H,09H,10H,19H,24H,31H,40H,51H
END

ADDRESS(h) Data(h)
0200h 00
0201h 01
0202h 04
0203h 09
0204h 10
: :

20
Experiment -2
Data transfer between ROM and RAM

Program – 1

Write a program to transfer a string of data from code space starting at address 200H to RAM
locations starting at 40H. The data is as shown below:

0200H: DB "VIT UNIVERSITY"


Using the simulator, single-step through the program and examine the data transfer and
registers.
ORG 0000H
MOV A, #00H ; clear accumulator
MOV DPTR,#0200H; Load 200h in DPTR which will act as source pointer
MOV R7,#0EH; The length of the string is loaded in R1 register
MOV R0,#40H; Load the destination pointer in R0 register
LOOP: CLR A
MOVC A, @A+DPTR; Move from ROM to Accumulator
MOV @R0,A; Move from Accumulator to RAM
INC DPTR
INC R0
DJNZ R7, LOOP ; Decrement the count and check for zero
HERE: SJMP HERE
ORG 0200H
DB "VIT UNIVERSITY"
END

Program 2

Add the following subroutine to the program 1, single-step through the subroutine and examine
the RAM locations. After data has been transferred from ROM space into RAM, the subroutine
should copy the data from RAM locations starting at 40H to RAM locations starting at 60H.

ORG 000H
MOV DPTR, #200H
MOV R0, #40H
MOV R1, #0EH
LOOP:CLR A
MOVC A,@A+DPTR
MOV @R0,A

21
INC R0
INC DPTR
DJNZ R1,LOOP
MOV R0,#40H; Initializing R0 as source pointer
MOV R1,#60H; Initializing R1 as destination pointer
MOV R3,#0EH; Length of the string
LOOP2: CLR A
MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R3, LOOP2
HERE:SJMP HERE
ORG 200H
DB "VIT UNIVERSITY"
END

22
Experiment -3
Stack operations

Program 1

Write and assemble a program to load values into each of registers R0 - R4 and then push each
of these registers onto the stack. Single-step the program, and examine the stack and the SP
register after the execution of each instruction.

ORG 0000H
MOV R0,#25H
MOV R1,#35H
MOV R2,#45H
MOV R3,#55H
MOV R4,#65H
PUSH 0
PUSH 1
PUSH 2
PUSH 3
PUSH 4
Hlt:SJMP Hlt
END

Program 2

Write and assemble a program to load values into each of registers R0 - R4 and then push each
of these registers onto the stack and pop them back. Single-step the program, and examine the
stack and the SP register after the execution of each instruction.

ORG 0000H
MOV R0,#25H
MOV R1,#35H
MOV R2,#45H
MOV R3,#55H
MOV R4,#65H
PUSH 0
PUSH 1
PUSH 2
PUSH 3
PUSH 4
POP 0
POP 1
POP 2

23
POP 3
POP 4
H1:SJMP H1
END

Program 3

Write an 8051 assemble language program to:


Set SP = 0D, (b) Put a different value in each of RAM locations 0D, 0C, 0B, 0A, 09, and 08,
(c) POP each stack location into registers R0 - R4. Use the simulator to single-step and examine
the registers, the stack, and the stack pointer.

ORG 0000H
MOV SP,#0DH
MOV 08H, #10H
MOV 09H, #11H
MOV 0AH, #12H
MOV 0BH, #13H
MOV 0CH,#14H
MOV 0DH,#16H
POP 0
POP 1
POP 2
POP 3
POP 4
L1:SJMP L1
END

24
Experiment -4
BCD Addition

Program 1

Write a program to add 10 bytes of data and store the result in registers R2 and R3.
The bytes are stored in the ROM space starting at 200H. The data would look as
follows:
MYDATA: DB 92,34,84,129,... ;pick your own data.
Notice that you must first bring the data from ROM space into the CPU's RAM,
then add them together. Use a simulator to single-step the program and examine the
data.

ORG 000H
MOV DPTR,#200H; Initialize source pointer
MOV R0,#10; No of data
LOOP:CLR A
MOVC A,@A+DPTR; Fetch the data from ROM
ADD A,R2
JNC NEXT; Check for carry after addition
INC R3
NEXT:INC DPTR; Increment the source pointer
MOV R2,A ; Move the result to R2 register
DJNZ R0,LOOP ; check the count, if it is not zero continue with next addition
HERE:SJMP HERE
ORG 200H
DB 22H,43H,23H,34H,31H,77H,91H,33H,43H,7H
END

Program 2

Write a program to add 10 bytes of BCD data and store the result in R2 and R3.
The bytes are stored in ROM space starting at 300H. The data would look as follows:
MYDATA: DB 92H,34H,84H,29H,... ;pick your own data.
Notice that you must first bring the data from ROM space into the CPU's RAM,
then add them together. Use a simulator to single-step the program and examine the
data.

ORG 000H
MOV DPTR,#300H; Initialize source pointer

25
MOV R0,#10H; No of data
LOOP:CLR A
MOVC A,@A+DPTR ; Fetch the data from ROM
ADD A,R2
DA A; Decimal adjust accumulator
JNC NEXT ; Check for carry after addition
INC R3
NEXT:INC DPTR ; Increment the source pointer
MOV R2,A ; Move the result to R2 register
DJNZ R0,LOOP ; check the count, if it is not zero continue with next addition
HERE:SJMP HERE
ORG 300H
DB 22H,43H,23H,34H,31H,77H,91H,33H,43H,7H
END

Program 3

Write a program to add two multi-byte BCD numbers together and store the result in
RAM locations 40H - 44H. The two multi-byte items are stored in the ROM space
starting at 120H and 150H. See the following example data.
ORG 120H
DATA_1: DB 54H,76H,65H,98H ;number 98657654H
DATA_2 DB 93H,56H,77H,38H ;number 38775693H

Pick your own data for your program. Notice that you must first bring the data from
ROM space into the CPU's RAM and then add them together. Use a simulator to single-
step the program and examine the data.

ORG 0000H
MOV R2,#4H; INTIALIZE COUNT TO ADD 4 TIMES
MOV R0,#40H ; INITALIZE DESTINATION POINTER
MOV DPTR,#120H ; INITALIZE SOURCE POINTER
LOOP:CLR A
MOVC A,@A+DPTR ; FETCH A BYTE FROM THE MULTIBYTE DATA
AVAILABLE IN ROM
MOV R1,A
MOV A,#30H;
MOVC A,@A+DPTR; FETCH A BYTE FROM THE MULTIBYTE DATA
AVAILABLE IN ROM
ADDC A,R1; ADD WITH CARRY
DA A; DECIMAL ADJUST ACCUMULATOR
MOV @R0,A; MOVE THE RESULT TO RAM
INC R0
INC DPTR
DJNZ R2,LOOP; CHECK FOR COUNT
HERE: SJMP HERE
ORG 120H
DB 54H,76H,65H,98H

26
ORG 150H
DB 93H,56H,77H,38H
END

Experiment -5
Parallel Ports

Program 1

Write and assemble a program to toggle all the bits of P0, P1, and P2 continuously by
sending 55H and AAH to these ports. Put a time delay between the "on" and "off" states.
Then using the simulator, single-step through the program and examine the ports. Do not
single-step through the time delay call.

ORG 0000H
HERE : MOV P0,#55H
MOV P1,#55H
MOV P2,#55H
ACALL DELAY
MOV P0,#0AAH
MOV P1,#0AAH
MOV P2,#0AAH
ACALL DELAY
SJMP HERE
DELAY : MOV R1,#04H
BACK: MOV R2,#20H
AGAIN:DJNZ R2, AGAIN
DJNZ R1,BACK
RET
END

Program 2

Using a simulator, write a program to get a byte of data from P1 and send it to P0 and
P2. Also, give a copy of it to registers R0, R1, and R2. Single-step the program and
examine the ports and registers.

ORG 0000H
MOV P1,#0FFH
MOV A,P1
MOV P0,A

27
MOV P2,A
MOV R0,A
MOV R1,A
MOV R2,A
END

Experiment -6

Timer and Counter Programming


Program 1

Write a program using timer 0 to generate a 500 Hz square wave frequency on one of the
pins of P1. Then examine the frequency using the KEIL IDE inbuilt Logic Analyzer.

Initial value calculation:

Frequency f=500Hz
Total time period T=2ms
Required delay=TON=TOFF=1ms
[(FFFF-XXXX)+1]*1.085µs = 1ms
(65536-X)* 1.085µs = 1ms
X=(64614)D
X=XXXX = FC66H

ORG 0000H
MOV TMOD,#01H
HERE: MOV TL0,#66H
MOV TH0,#0FCH
CPL P1.0
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
END

Program 2

Write a program using timer 1 to generate a 1 kHz square wave frequency on one of the
pins of P1. Then examine the frequency using the oscilloscope.

Initial value calculation:

28
Frequency f=1 KHz
Total time period T=1ms
Required delay=TON=TOFF=0.5ms
[(FFFF-XXXX)+1]*1.085µs = 0.5ms
XXXX = FE33H

ORG 0000H
MOV TMOD,#10H
HERE: MOV TL1,#33H
MOV TH1,#0FEH
CPL P1.0
ACALL DELAY
SJMP HERE
DELAY: SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
RET
END

Program 3

Assume a room can accommodate 10 people. Write an 8051 program to count the
number of people entering the room. If the count reaches 10 turn on LED.

ORG 0000H
MOV TMOD,#06h; counter 0 in mode2
CLR P0.1;TURN OFF LED
SETB P3.4; intilalize i/p port
MOV TH0,#00
SETB TR0; Start Counter0
AGAIN:MOV A, TL0
MOV P1,A
CJNE A,#10D ,AGAIN
SETB P0.1; turn ON LED.
END

29
Experiment -7

Serial Communication
Program 1

Write a program to transfer “HELLO” through serial port at 9600 baudrate.

ORG 0000H
XX: MOV DPTR,#MYDATA
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
MOV R1,#06H
AGAIN:CLR A
MOVC A,@A+DPTR
MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
INC DPTR
DJNZ R1,AGAIN
SJMP XX
MYDATA: DB ‘HELLO ‘
END

Program 2

Write a program to receive a data from serial port at 9600 baud rate and transmit through
parallel port.

ORG 0000H
MOV TMOD,#20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
HERE:JNB RI,HERE
MOV A,SBUF
MOV P1,A
CLR RI

30
SJMP HERE

Experiment -8

Timer Interrupt
Program 1

• Write an 8051 program to get data from one port and send it to another port
continuously while an interrupt will do the following: One of the timers will toggle the
P2.1 bit every 100 microseconds.

Initial value calculation:

Time delay is 100 microseconds


Timer0 in mode 2
[(FFH-XX)+1] X 1.085µs=100µs
XX=A3H

ORG 0000H
LJMP MAIN

ORG 000BH
CPL P2.1
RETI

ORG 0030H
MAIN: MOV TMOD,#02H
MOV P0,#0FFH
MOV TH0,#A3h
MOV IE,#10000010B
SETB TR0
BACK: MOV A,P0
MOV P1,A
SJMP BACK
END

31
Experiment -9

Serial Interrupt
Program 1

Write an 8051 program to get data from a single bit of P1.2 and send it to P1.7
continuously while an interrupt will do the following: A serial interrupt service routine
will receive data from a PC and display it on one of the ports.

ORG 0000H
LJMP MAIN
ORG 0023H
LJMP SERIAL
ORG 0030H
MAIN:SETB P1.2
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON, #50H
MOV IE,#10010000B
SETB TR1
BACK:MOV C,P1.2
MOV P1.7,C
SJMP BACK
SERIAL:JB TI,TRANS
MOV A,SBUF
MOV P2,A
CLR RI
RETI
TRANS:CLR TI
RETI
END

32
Experiment -10
LCD Programming

Program 1

Connect the LCD to your 8051 Kit. Then write and run a program to display your name on line
1 of the LCD (first name followed by last name with a space in between).
Note: If you are not monitoring the busy flag of the LCD, put a few milliseconds delay in your
program.

;P1.0-P1.7 are connected to LCD data pins D0-D7


; P2.0 is connected to RS pin of LCD
; P2.1 is connected to R/W pin of LCD
; P2.2 is connected to E pin of LCD

ORG 0000H
MOV A,#38H
ACALL CMNWRT
ACALL DELAY
MOV A,#0EH
ACALL CMNWRT
ACALL DELAY
MOV A,#01H
ACALL CMNWRT
ACALL DELAY
MOV A,#06H
ACALL CMNWRT
ACALL DELAY
MOV A,#80H
ACALL CMNWRT
ACALL DELAY
MOV A,#'V'
ACALL DATWRT
ACALL DELAY
MOV A,#'I'
ACALL DATWRT

33
ACALL DELAY
MOV A,#'T'
ACALL DATWRT
ACALL DELAY
AGAIN: SJMP AGAIN
CMNWRT:
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DATWRT:
MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DELAY: MOV R3,#0FFH
MOV R4,#0FFH
MOV R5,#55H
HERE: DJNZ R5,HERE
HERE1: DJNZ R4, HERE1
HERE2: DJNZ R3,HERE2
RET
END

34
35

You might also like