Microcontroller Lab Manual
Microcontroller Lab Manual
Name : ……………………………………………………………………
Roll No : ……………………………………………………………………
1
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
(An Institution of National Importance, Ministry of Education)
THIRUVETTAKUDY, KARAIKAL – 609 609.
BONAFIDE CERTIFICATE
Faculty In-charge
2
INDEX
Page Date of Date of
Date Name of the Experiment Remarks
No. Experiment Submission
3
4
INTRODUCTION TO 8051 MICROCONTROLLER
The 8051 memory is organized in a Harvard Architecture. Both the code memory space
and data memory space begin at location 0x00 for internal or external memory which is different
from the Princeton Architecture where code and data share same memory space. The advantage of
the Harvard Architecture is not only doubling the memory capacity of the microcontroller with
same number of address lines but also increases the reliability of the microcontroller, since there
are no instructions to write to the code memory which is read only.
5
2. Architecture Of 8051 Microcontroller
program but in microcontroller two hardware pins are available for delay generation. These
hardware pins can be also used for counting some external events. How much times a number is
repeated in the given table is calculated by the counter.
In MC8051, two timer pins are available T0 and T1, by these timers we can give the delay
of particular time if we use these in timer mode. We can count external pulses at these pins if we
use these pins in counter mode. 16 bits timers are available. Means we can generate delay between
0000H to FFFFH. Two special function registers are available. If we want to load T0 with 16 bit
data then we can load separate lower 8 bit in TL0 and higher 8 bit in TH0. Inthe same way for T1.
TMOD, TCON registers are used for controlling timer operation.
2.5 Oscillator
It is used for providing the clock to MC8051 which decides the speed or baud rate of MC.
We use crystal which frequency vary from 4MHz to 30 MHz, normally we use 11.0592 MHz
frequency.
2.6 Interrupts
Interrupts are defined as requests because they can be refused (masked) if they are not
used, that is when an interrupt is acknowledged. A special set of events or routines are followed to
handle the interrupts. These special routines are known as interrupt handler or interrupt service
routines (ISR). These are located at a special location in memory. • INT0 and INT1 are the pins
for external interrupts.
8
3.1 Pin Description
The EA' (External Access) pin is used to control the internal or external memory access.
The signal 0 is for external memory access and signal 1 for internal memory access. The PSEN'
(Program Store Enable) is for reading external code memory when it is low (0) and EA is also
0. The ALE (Address Latch Enable) activates the port 0 joined with port 2 to provide 16 bit
external address bus to access the external memory. The ALE multiplexes the P0: 1 for latching
address on P0 as A0-A7 in the 16 bit address buss, 0 for latching P0 as data I/O. P0.x is named
ADx because P0 is multiplexed for Address bus and Data bus at different clock time. WR'
provides the signal to write external data memory RD' provides the signal to read external data
and code memory.
• PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general I/O purpose. The
other ports P0, P2 and P3 have dual roles in addition to their basic I/O function.
• PORT P0 (pins 32 to 39): When the external memory access is required then Port P0 is
multiplexed for address bus and data bus that can be used to access external memory in
conjunction with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port data. It can be
used for general purpose I/O if no external memory presents.
• PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a role (A8-A15) in the
address bus in conjunction with PORT P0 to access external memory.
• PORT P3 (Pins 10 to 17): In addition to acting as a normal I/O port,
➢ P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial
➢ P3.2 and P3.3 can be used as external interrupt pins(INT0’ and INT1’),
➢ P3.4 and P3.5 are used for external counter input pins(T0 and T1),
➢ P3.6 and P3.7 can be used as external data memory write and read control signal
9
OUTPUT - ADDITION (Without Carry):
DATA (Without Carry)
03
INPUTS
02
OUTPUT
10
EX. NO:
8 BIT ADDITION, SUBTRACTION, MULTICATION AND
DATE: DIVISION
AIM:
To perform Addition, Subtraction, Multiplication and Division of 8 bit numbers using 8051
Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller
2. Keyboard
11
OUTPUT - SUBTRACTION (With Carry):
DATA (With Carry)
A0
INPUTS
A2
OUTPUT
BORROW
OUTPUT – MULTIPLICATION:
DATA
INPUTS 03
02
OUTPUT
OUTPUT - DIVISION
DATA
INPUTS 03
02
QUOTIENT
OUTPUT
REMAINDER
12
SUBTRACTION (With Carry):
PROGRAM:
8000: MOV A,#A0
8002: MOV B,#A2
8005: SUBB A,B
8007: MOV DPTR,#4000
800A: MOVX @DPTR,A
800B: SJMP 800B
MULTIPLICATION
PROGRAM:
8000: MOV A,#03
8002: MOV B,#02
8005: MUL AB
8006: SJMP 8006
DIVISION
PROGRAM:
8000: MOV A,#03
8002: MOV B,#02
8005: DIV AB
8006: SJMP 8006
RESULT:
Thus the 8 bit Addition, Subtraction, Multiplication and Division are performed and verified
using 8051 Microcontroller.
13
TABLE - ASCENDING ORDER:
INPUT OUTPUT
LOCATION DATA LOCATION DATA
51H 05 51H 01
52H 03 52H 02
53H 02 53H 03
54H 01 54H 04
55H 04 55H 05
14
EX. NO:
SORTING OF NUMBERS
DATE:
AIM:
To perform sorting of given numbers in ascending and descending order using 8051
Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller
2. Keyboard
ASCENDING ORDER:
PROGRAM:
8000: MOV A,#05
8002: MOV R2,A
8003: DEC R2
L3 8004: MOV A,R2
8005: MOV R3,A
8006: MOV R0,#51
L2 8008: MOV A,@R0
8009: MOV R4,A
800A: INC R0
800B: MOV A,@R0
800C: MOV R5,A
800D: SUBB A,R4
800E: JNC 8016
8010: MOV A,R4
8011: MOV @R0,A
8012: DEC R0
8013: MOV A,R5
8014: MOV @R0,A
8015: INC R0
L1 8016: DJNZ R3,8008
8018: DJNZ R2,8004
801A: SJMP 801A
15
TABLE - ASCENDING ORDER:
INPUT OUTPUT
LOCATION DATA LOCATION DATA
51H 05 51H 05
52H 03 52H 04
53H 02 53H 03
54H 01 54H 02
55H 04 55H 01
16
DESCENDING ORDER:
PROGRAM:
8000: MOV A,#05
8002: MOV R2,A
8003: DEC R2
L3 8004: MOV A,R2
8005: MOV R3,A
8006: MOV R0,#51
L2 8008: MOV A,@R0
8009: MOV R4,A
800A: INC R0
800B: MOV A,@R0
800C: MOV R5,A
800D: SUBB A,R4
800E: JC 8016
8010: MOV A,R4
8011: MOV @R0,A
8012: DEC R0
8013: MOV A,R5
8014: MOV @R0,A
8015: INC R0
L1 8016: DJNZ R3,8008
8018: DJNZ R2,8004
801A: SJMP 801A
RESULT:
Thus, the sorting of given numbers in ascending and descending order was done using 8051
Microcontroller.
17
SQUARE WAVE:
FLOWCHART:
Start
Delay Time
Delay Time
GRAPH:
TRIANGLE WAVE:
GRAPH:
18
EX. NO:
DIGITAL TO ANALOG CONVERTER – WAVE GENERATION
DATE:
AIM:
To write and execute an assembly language program to generate square wave, triangle wave
and sawtooth wave using 8051 Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller trainer kit
2. Digital to Analog interface kit
3. Keyboard
PROGRAM:
SQUARE WAVE:
TRIANGLE WAVE:
Start
Send to DAC
INC Acc by 1
No
Is status = 00H?
Yes
Send to DAC
DEC Acc by 1
No
Is Acc = 00H?
Yes
SAWTOOTH WAVE:
GRAPH:
20
SAWTOOTH WAVE:
FLOWCHART:
Start
Send to DAC
INC Acc by 1
SAWTOOTH WAVE:
RESULT:
Thus the assembly language program to generate square wave, triangle wave and sawtooth
wave was written and executed using 8051 Microcontroller.
21
DISPLAY THE ROOM TEMPERATURE
FLOWCHART:
Start
Set Run/Hold to 1
Read status
Is status = 1
Set Run/Hold to 0
22
EX. NO:
ANALOG TO DIGITAL CONVERTER
DATE: DISPLAY THE ROOM TEMPERATURE
AIM:
To perform analog to digital conversion to display the room temperature using 8051
Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller trainer kit
2. Analog to Digital interface kit
3. Keyboard
DISPLAY THE ROOM TEMPERATURE:
PROGRAM:
8050: 12 10 00 LCALL 1000 ; Clear LCD display
8053: 90 03 08 MOV DPTR,#0308 ; Select Line 2, position 8
8056: 12 10 30 LCALL 1030 ; Locate cursor
8059: 74 00 MOV A,@00 ; Select ADC channel 0
805B: 12 81 00 LCALL 8100 ; Read room temperature
805E: 12 81 50 LCALL 8150 ; Convert ADC result into decimal
8061: 12 80 76 LCALL 8076 ; Left shift decimal result 4 bits
8064: E8 MOV A,R0 ; Get temperature MSB in Acc
8065: 12 10 50 LCALL 1050 ; Display MSB of decimal output
8068: 74 2E MOV A,#2E ; ASCII code for ‘.’
806A: 12 10 40 LCALL 1040 ; Display ‘.’
806D: E9 MOV A,R1 ; Get LSB of decimal output
806E: 12 10 50 LCALL 1050 ; Display LSB
8071: 12 80 A0 LCALL 80A0 ; Delay between samples
8074: 80 DD SJMP 8053 ; Repeat temperature display process
23
807E: E5 83 MOV A,DPH
8080: C4 SWAP A ; Get lower nibble of DPH in
8081: 54 F0 ANL A,#F0 ; Acc upper nibble
8083: 48 ORL A,R0 ; Combine R0
8084: F8 MOV R0,A ; Store MSB in R0
8085: D0 E0 POP ACC
8087: 54 F0 ANL A,#F0
8089: F9 MOV R1,A ; Store LSB in R1
808A: 22 RET
DELAY:
80A0: 78 0A MOV R0,#0A ; Load outer loop count in R0
80A2: 79 FF MOV R1,#FF ; Load inner loop count in R1
80A4: D9 FE DJNZ R1,80A4 ; Decrement register R1 until 0
80A6: D8 FA DJNZ R0,80A2 ; Repeat DL1 loop R0 times
80A8: 22 RET
OUTPUT:
Room Temperature =
RESULT:
Thus, the room temperature was displayed using ADC interface and 8051 Microcontroller.
25
INTERFACING SEVEN SEGMENT DISPLAY:
FLOWCHART:
Start
Restore Acc.
Mask higher nibble
End
26
EX. NO:
INTERFACING SEVEN SEGMENT DISPLAY
DATE:
AIM:
To write and perform an assembly language program to display numbers on seven segment
display using 8051 Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller trainer kit
2. Seven segment display interface
3. Keyboard
THEORY:
Seven segment display is a form of electronic display for displaying decimal numbers that is
an alternate to the more complex dot matrix displays.
PROGRAM:
8000: 75 81 40 MOV SP,#40 ; Initialize stack pointer
8003: 74 12 MOV A,#12 ; Move data to be displayed
8005: 12 80 0A LCALL 800A ; Display the data
8008: 80 FE SJMP 8008 ; Halt
DISP_2DIGIT:
27
OUTPUT:
28
SEVEN SEGMENT DISPLAY CODES TABLE:
RESULT:
Thus the assembly language program to display numbers on seven segment display was
written and verified using 8051 Microcontroller.
29
CLOCK WISE DIRECTION:
FLOWCHART:
Start
Small Delay
Start
Small Delay
30
EX. NO:
INTERFACING STEPPER MOTOR
DATE:
AIM:
To interface stepper motor with 8051 Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller trainer kit
2. Stepper motor interface kit
3. Keyboard
THEORY:
Basically, Stepper motors are electromechanical converters. They differ from the constant speed
motors in the spindle movement. Here the movement of the spindle will be in steps. They respond in a
well-defined way to certain digital signals fed to their control electronics. Stepper motors may be used
as an open system without feedback for control purposes. A stepping motor may therefore replace a
A stepper motor may be classified as Unipolar or Bipolar according to the rotating field
generated. Another classification depends on the material from which the rotor is constructed, either
The operating voltage of the stepper motor is 5V. The motor works with a step angle of 11.25
degrees. The stepper motor has built in gears and it works with a gear ratio of 1:64. This implies the
fact that for every step of shaft movement, you need to give 64 step movement to the motor.
31
ROTATE 90 DEGREES IN COUNTER CLOCK WISE DIRECTION:
FLOWCHART:
Start
Small delay
Decrement counter
Is status = 1
End
32
PROGRAM:
CLOCK WISE DIRECTION:
8000: 90 FF CO MOV DPTR,#FFC0 ; Port address = FFC0H
8003: 74 66 MOV A,#66 ; Store data to energize phase 2 & Phase 3
8005: F0 MOVX @DPTR,A ; Send data to port lines
8006: 11 0B ACALL 800B ; Delay between two steps
8008: 03 RR A ; Rotate right Acc, to get next data
8009: 80 FA SJMP 8005 ; Repeat the above process for ever
33
DELAY:
800B: 78 10 MOV R0,#0A ; Load outer loop count in R0
800D: 79 FF MOV R1,#FF ; Load inner loop count in R1
800F: D9 FE DJNZ R1,#800F ; Decrement register R1 until it becomes 0
8011: D8 FA DJNZ R0,800D ; Repeat DL1 loop R0 times
8013: 22 RET
RESULT:
Thus, the stepper motor was interfaced with 8051 microcontroller.
34
EX. NO:
INTERFACING TRAFFIC LIGHT
DATE:
AIM:
To interface traffic light with 8051 Microcontroller.
APPARATUS REQUIRED:
1. NG Platinum 8051 Microcontroller trainer kit
2. Traffic light interface kit
3. Keyboard
THEORY:
The Traffic Light Interface card is designed to control the traffic where four roads cross each
other. The card is designed in such a manner, that the user can understand the control operations very
easily. Interfacing of the card to the Microprocessor/Microcontroller is done through the bus lines
present in the trainer itself. The card is connected to the trainer through 50 pin FRC connector.
The Traffic Light Interface Card consists of Red, Yellow and Green LEDs for the STOP,
READY and GO signals. For indicating pedestrian signs such as DON'T WALK and WALK, LEDS
Red and Green are used respectively. IC ULN2003 is used to drive the LEDs.
FLOWCHART:
35
PROGRAM:
TRAFFIC LIGHT CONTROL:
8000: 79 09 MOV R1,#09 ; Move loop count to register R1
8002: 78 37 MOV R0,#37 ; Starting address for display table
8004: 75 A0 80 MOV P2,#80
8007: E2 MOVX A,@R0 ; Read the display data for road 1
8008: 90 FF 80 MOV DPTR,#FF80
800B: F0 MOVX @DPTR,A ; Send the display data for road 1
800C: 08 INC R0
800D: E2 MOVX A,@R0 ; Read the display data for road 1
800E: 90 FF 84 MOV DPTR,#FF84
8011: F0 MOVX @DPTR,A ; Send the display data for road 1
8012: 08 INC R0
8013: E2 MOVX A,@R0 ; Read the display data for road 1
8014: 90 FF 88 MOV DPTR,#FF88
8017: F0 MOVX @DPTR,A ; Send the display data for road 1
8018: 08 INC R0
8019: E2 MOVX A,@R0 ; Read the display data for road 1
801A: 90 FF 8C MOV DPTR,#FF8C
801D: F0 MOVX @DPTR,A ; Send the display data for road 1
801E: 08 INC R0
801F: E2 MOVX A,@R0 ; Read the delay data
8020: 08 INC R0
8021: 11 27 ACALL 8027 ; Call the delay routine
8023: D9 E2 DJNZ R1,8007
8025: 80 D9 SJMP 8000 ; Repeat the process again
DELAY ROUTINE:
8027: 7A 05 MOV R2,#05
8029: 7B FF MOV R3,#FF ; Move 0FFH to R3
802B: 7C FF MOV R4,#FF ; Move 0FFH to internal R4
802D: DC FE DJNZ R4,#802D ; Repeat until it becomes 0
802F: DB FA DJNZ R3,#802B ; Decrement R3 & repeat until 0
36
8031: DA F6 DJNZ R3,#8029 ; Decrement R2 & repeat until 0
8033: D5 E0 F1 DJNZ ACC,8027
8036: 22 RET
RESULT:
Thus, the traffic light kit was interfaced with 8051 Microcontroller.
37