15EE305J Microcontroller Lab
15EE305J Microcontroller Lab
15EE305J Microcontroller Lab
COURSE MANUAL
5 Code Conversion
HEX TO ASCII
6 Square root of a given data
CYCLE 2
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION & ANSWERS
1. What is microprocessor?
Aim
To study the microcontroller 80
8051
controller
Architecture of 8051 Microcontroller
Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of most
registers. In other words, the positive voltage on this pin resets the microcontroller. By
applying logic zero to this pin, the program starts execution from the beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or output.
Besides, all of them have alternative functions:
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are
configured as general inputs/outputs. In case external memory is used, the higher address
byte, i.e. addresses A8-A15 will appear on this port. Even though memory with capacity of
64Kb is not used, which means that not all eight port bits are used for its addressing, the rest
of them are not available as inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on it
every time the microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the lower
address byte (A0-A7) on P0 and activates the ALE output. After receiving signal from the
ALE pin, the external register (usually 74HCT373 or 74HCT375 add-on chip) memorizes the
state of P0 and uses it as a memory chip address. Immediately after that, the ALU pin is
returned its previous logic state and P0 is now used as a Data Bus. As seen, port data
multiplexing is performed by means of only one additional (and cheap) integrated circuit. In
other words, this port is used for both data and address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It means that even
there is a program written to the microcontroller, it will not be executed. Instead, the program
written to external ROM will be executed. By applying logic one to the EA pin, the
microcontroller will use both memories, first internal then external (if exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used as
general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE
pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven low (0).
All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured as
inputs or outputs. Accordingly, in total of 32 input/output pins enabling the microcontroller to
be connected to peripheral devices are available for use.
Pin configuration, i.e. whether it is to be configured as an input (1) or an output (0), depends
on its logic state. In order to configure a microcontroller pin as an input, it is necessary to
apply a logic zero (0) to appropriate I/O port bit. In this case, voltage level on appropriate pin
will be 0.
Memory Organization
The 8051 has two types of memory and these are Program Memory and Data Memory.
Program Memory (ROM) is used to permanently save the program being executed, while
Data Memory (RAM) is used for temporarily storing data and intermediate results created
and used during the operation of the microcontroller. Depending on the model in use (we are
still talking about the 8051 microcontroller family in general) at most a few Kb of ROM and
128 or 256 bytes of RAM is used. However…
All 8051 microcontrollers have a 16-bit addressing bus and are capable of addressing 64 kb
memory. It is neither a mistake nor a big ambition of engineers who were working on basic
core development. It is a matter of smart memory organization which makes these
microcontrollers a real “programmers’ goody“.
Special Function Registers (SFRs) are a sort of control table used for running and monitoring
the operation of the microcontroller. Each of these registers as well as each bit they include,
has its name, address in the scope of RAM and precisely defined purpose such as timer
control, interrupt control, serial communication control etc. Even though there are 128
memory locations intended to be occupied by them, the basic core, shared by all types of
8051 microcontrollers, has only 21 such registers. Rest of locations is intentionally left
unoccupied in order to enable the manufacturers to further develop microcontrollers keeping
them compatible with the previous versions. It also enables programs written a long time ago
for microcontrollers which are out of production now to be used today.
PSW register is one of the most important SFRs. It contains several status bits that reflect the
current state of the CPU. Besides, this register contains Carry bit, Auxiliary Carry, two
register bank select bits, Overflow flag, parity bit and user-definable status flag.
P - Parity bit. If a number stored in the accumulator is even then this bit will be
automatically set (1), otherwise it will be cleared (0). It is mainly used during data transmit
and receive via serial communication.
OV Overflow occurs when the result of an arithmetical operation is larger than 255 and
cannot be stored in one register. Overflow condition causes the OV bit to be set (1).
Otherwise, it will be cleared (0).
RS0, RS1 - Register bank select bits. These two bits are used to select one of four register
banks of RAM. By setting and clearing these bits, registers R0-R7
R0 R7 are stored in one of four
banks of RAM.
RS1 RS2 Space in RAM
0 0 Bank0 00h-07h
0 1 Bank1 08h-0Fh
1 0 Bank2 10h-17h
1 1 Bank3 18h-1Fh
CY - Carry Flag is the (ninth) auxiliary bit used for all arithmetical operations and shift
instructions.
DPTR register is not a true one because it doesn't physically exist. It consists of two separate
registers: DPH (Data Pointer High) and (Data Pointer Low). For this reason it may be treated
as a 16-bit
bit register or as two independent 8-bit
8 bit registers. Their 16 bits are primarly used for
external memory addressing. Besides, the DPTR Register is usually used for storing data and
intermediate results.
Stack Pointer (SP) Register
A value stored in the Stack Pointer points to the first free stack address and permits stack
availability. Stack pushes increment the value in the Stack Pointer by 1. Likewise, stack pops
decrement its value by 1. Upon on any reset and power-on,
power on, the value 7 is stored in the Stack
Pointer, which means that the space of RAM reserved for the stack starts at this location. If
another value is written to this register, the entire Stack is moved to the new memory
location.
If neither external memory nor serial communication system are used then 4 ports with in
total of 32 input/output pins are available for connection to peripheral environment. Each bit
within these ports affects the state
te and performance of appropriate pin of the microcontroller.
Thus, bit logic state is reflected on appropriate pin as a voltage (0 or 5 V) and vice versa,
voltage on a pin reflects the state of appropriate port bit.
As you already know, the microcontroller oscillator uses quartz crystal for its operation. As
the frequency of this oscillator is precisely defined and very stable, pulses it generates are
always of the same width, which makes them ideal for time measurement. Such crystals are
also used in quartz watches. In order to measure time between two events it is sufficient to
count up pulses coming from this oscillator. That is exactly what the timer does. If the timer
is properly programmed, the value stored in its register will be incremented (or decremented)
with each coming pulse, i.e. once per each machine cycle. A single machine-cycle
machine cycle instruction
i
lasts for 12 quartz oscillator periods, which means that by embedding quartz with oscillator
frequency of 12MHz, a number stored in the timer register will be changed million times per
second, i.e. each microsecond.
The 8051 microcontroller has 2 timers/counters called T0 and T1. As their names suggest,
their main purpose is to measure time and count external events. Besides, they can be used
for generating clock pulses to be used in serial communication, so called Baud Rate.
Timer T0
As seen in figure below, the timer T0 consists of two registers – TH0 and TL0 representing a
low and a high byte of one 16-digit
digit binary number.
Accordingly, if the content of the timer T0 is equal to 0 (T0=0) then both registers it consists
of will contain 0. If the timer contains for example number 1000 (decimal), then the TH0
register (high byte) will contain the number 3, while the TL0 register (low byte) will contain
decimal number 232.
The TMOD register selects the operational mode of the timers T0 and T1. As seen in figure
below, the low 4 bits (bit0 - bit3) refer to the timer 0, while the high 4 bits (bit4 - bit7) refer
to the timer 1. There are 4 operational modes and each of them is described herein.
• GATE1 enables and disables Timer 1 by means of a signal brought to the INT1 pin
(P3.3):
o 1 - Timer 1 operates only if the INT1 bit is set.
o 0 - Timer 1 operates regardless of the logic state of the INT1 bit.
• C/T1 selects pulses to be counted up by the timer/counter 1:
o 1 - Timer counts pulses brought to the T1 pin (P3.5).
o 0 - Timer counts pulses from internal oscillator.
• T1M1,T1M0 These two bits select the operational mode of the Timer 1.
• GATE0 enables and disables Timer 1 using a signal brought to the INT0 pin (P3.2):
o 1 - Timer 0 operates only if the INT0 bit is set.
o 0 - Timer 0 operates regardless of the logic state of the INT0 bit.
• C/T0 selects pulses to be counted up by the timer/counter 0:
o 1 - Timer counts pulses brought to the T0 pin (P3.4).
o 0 - Timer counts pulses from internal oscillator.
• T0M1,T0M0 These two bits select the oprtaional mode of the Timer 0.
Timer 1
Result:
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE-LAB
1. Specify the number of registers in a 2K memory chip?
2. What is an assembler?
Aim:
To do the arithmetic operations using 8051 microprocessor
Apparatus required:
8085 microprocessor kit
DAC interface kit
Keyboard
Algorithm:
Addition / Subtraction
Step 1 : Move 1H data to memory
Step 2 : Add or subtract 1H data with 2nd data
Step 3 : Initialize data pointer.
Step 4 : Move result to memory pointed by DPTR.
START
Initialize DPTR
Stop
Multiplication / Division
Step 1 : Get 1H data and 2nd data to memory
Step 2 : Multiply or divide 1H data with 2nd data
Step 3 : Initialize data pointer.
Step 4 : Move result to memory pointed by DPTR (first port)
Step 5 : Increment DPTR
Step 6 : Move 2nd part of result to register A
Step 7 : Move result to 2nd memory location pointer by DPTR
START
Increment data
Increment DPTR
Stop
Execution:
Addition:
ML Input ML Output
4101 4500
4103
Program: 8-bit Subtraction:
Execution:
Subtraction:
ML Input ML Output
4101 4500
4103
Execution:
Multiplication:
ML Input Output Address Value
4101 4500
4103
Program: 8-bit Division:
Memory Label Opcode Mnemonics Comments
Location
4100 Start 74 04 MOV A,#04 Move immediate data
to accumulator
4102 75 F0 02 MOV B,#02 Move immediate to B
reg.
4105 84 DIV AB Divide content of A &
B
4106 90 45 00 MOV DPTR, # 4500 Load data pointer with
4500 location
4109 F0 MOVX @DPTR,A Move A to ext RAM
410A A3 INC DPTR Increment data pointer
410B E5 F0 MOV A,B Move remainder to A
410D F0 MOVX @DPTR,A Move A to ext RAM
410E 80 FE SJMP 410E Remain idle in infinite
loop
Execution:
Division:
ML Input Output Address Value
4101 4500
4103
Result:
Thus 8-bit addition, subtraction, multiplication and division is performed using 8051.
POST-LAB
6. Define OPCODE and Operand, and specify the opcode and the operand
in the instruction MOV H, L.
7. Find the machine codes and the number of bytes of for the following
instructions. Identify the opcode and the operands.
a) MVI H,47H
b) ADI F5H
c) SUB C
8. Find the HEX codes for the following instructions, identify the opcodes
and operands, and show the order of entering the codes in memory
STA 2050H
JNZ 2070H
10. Find the hex machine code for the following instruction from the
instruction set and identify the number of bytes of each instruction and
assume that the starting address is 2000H.
MVI B,45H
MVI C, 78H
MOV A,C
ADD B
OUT 07H
HLT.
DEPT. OF ELECTRICAL & ELECTRONICS ENGINEERING
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, Kattankulathur – 603203.
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
3.FINDING MAXIMUM VALUE IN AN ARRAY
PRE-LAB
1. What is a bus?
4. Why are the program counter and Data Pointer 16 bit registers?
Aim:
Write an assembly language program to find the biggest number in an array of 8-bit
unsigned numbers of predetermined length.
Apparatus required:
8051 microcontroller kit
(0-5V) DC battery
Algorithm:
1. Initialize pointer and counter.
2. Load internal memory location 40H as zero.
3. Move the first element of an array to r5 register.
4. Compare the data stored in memory location 40H is equal to or less than the
value of first element of an array.
5. If it is lesser, then move the data of first element to 40H memory location
ELSE increment pointer and decrement counter.
nd
6. Check the counter. If counter is not equal to zero, repeat from the 2
step else Move the R5 register to 40H memory location.
7. Stop the program.
Program:
Memory Label Opcode Mnemonics Comments
Location
4100 90 42 00 MOV DPTR,#4200H
MOV 40H,#00H
4103
75 40 00
Give the number of inputs for
4106
7D 0A MOV R5,#05H finding the largest number.
Moves into accumulator a byte
4108
LOOP2: E0 MOVX A,@DPTR from external memory
Compare and jump not equal to
4109
B5 40 08 CJNE A,40H,LOOP1 ‘A’
410C
LOOP 3 A3 INC DPTR
Decrement and Jump not equal to
410D
DD F9 DJNZ R5,LOOP2 Zero
410F
E5 40 MOV A,40H
4111
F0 MOVX @DPTR,A
4112 HLT 80 FE SJMP HLT
INPUT:
OUTPUT:
RESULT:
Thus the assembly language program was written to find the largest element in an
array and executed using 8051 microcontroller.
POST-LAB
4. Define DPTR.
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
4. SORTING OF DATA-ASCENDING ORDER-DESCEDING
PRE-LAB
5. What is RS 232C?
4. SORTING OF DATA-ASCENDING ORDER-DESCEDING
AIM:
To arrange an array of 8-bit unsigned numbers of known length in an ascending order.
Apparatus required:
8051 microcontroller kit
(0-5V) DC battery
Algorithm:
1. Initialize the register and data pointer.
2. Get first two elements in registers A &B.
3. Compare the two elements of data. If value of B register is high then exchange A
& B data else increment pointer and decrement register R3.
4. Check R3 is zero, and then move the register R5 & R6.
5. Again increment pointer and decrement R4,
6. Check R4 is zero. If no repeat the process from step 2.
7. Otherwise stop the program.
Program:
Memory Label Opcode Mnemonics Comments
Location
MOV R3,#4
4100 7B 04
MOV R4,#4
4102 7C 04
4111 C3 CLR C
412B 1C DEC R4
412E OC INC R 4
4111 C3 CLR C
4114 40 13 JC CHKNXT
412B 1C DEC R4
412E OC INC R 4
INPUT:
OUTPUT:
Memory address Data
SAMPLE INPUT AND OUTPUT DESCENDING
INPUT:
OUTPUT:
Memory address Data
RESULT:
Thus the assembly language program was written to sort the data in an ascending order and
executed using 8051 microcontroller.
POST-LAB
3. Which register bank is used if we alter RS0 and RS1 of the PSW by the
following two instructions?
SetB PSW.3
SetB PSW.4
4. What RAM locations are used for registers R0-R7 for question no.3?
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
5. HEXTO ASCII CONVERSION
PRE-LAB
1. What is T -state?
Apparatus required:
8051 microcontroller kit
(0-5V) DC battery
Algorithm:
INPUT:
OUTPUT:
Result:
Thus the assembly language program was written to convert HEX to ASCII and executed
using 8051 microcontroller.
POST LAB
2. Show the lowest and highest values (in hex) that the 8051 program
counter can take.
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
6. FIND THE SQUARE ROOT OF A GIVEN DATA
PRE-LAB
2. How many ports are there in 8051 and by default the ports are set as
output/input port?
Result:
Thus an assembly language program is written to find the square root of a given data and
executed successfully
POST-LAB
2. How many ports are there in 8051 and by default the ports are set as
output/input port?
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
7. Transfer data serially between two kits
PRE-LAB
2 11 11
3 22 22
4 33 33
Result:
Thus an assembly language program displaying characters on seven segment display has been
executed.
POST-LAB
4. Which register has the SMOD bit, and What is its status when the 8051 is
powered up?
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
8. Seven segment display
PRE-LAB
1. There are different modes that can be used for each timer what are they?
Result:
Thus an assembly language program displaying characters on seven segment display has been
executed.
POST-LAB
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION AND ANSWERS
Algorithm:
1. Move the Port Address of DAC 2 FFC8 to the DPTR.
2. Move the Value of Register A to DPTR and then Call the delay.
3. Move the Value of Register A (FFh) to DPTR and the call the dalay.
4. Repeat the steps 2 and 3.
Result:
Thus an assembly language program for Digital to Analog has been executed.
POST LAB QUESTION AND ANSWERS
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION AND ANSWERS
1. What is NV-RAM?
Algorithm:
1. Make ALE low/high by moving the respective data from A register to
DPTR.
2. Move the SOC( Start Of Conversion) data to DPTR from FFD0
3. Check for the End Of Conversion and read data from Buffer at address
FFC0
4. End the Program.
PROGRAM:
Port Address for 74LS174 Latch: FFC8
Port Address for SOC: FFD0
Port Address for EOC 1: FFD8
Port Address for 74LS 244 Buffer: FFC0
Memory Label Opcode Mnemonics Comments
Location
4100 90 FF C8 MOV DPTR, #FFC8
4103 74 10 MOV A, #10 Select Channel 0 and
4105 F0 MOVX @DPTR, A Make ALE Low
4106 74 18 MOV A, #18 Make ALE High
4108 F0 MOVX @DPTR, A
4109 90 FF D0 MOV DPTR, #FFD0
410C 74 01 MOV A, #01 SOC Signal High
410E F0 MOVX @DPTR, A
410F 74 00 MOV A, #00 SOC Signal Low
4111 F0 MOVX @DPTR, A
4112 90 FF D8 MOV DPTR, #FFD8
4115 WAIT: E0 MOVX A, @DPTR
4116 30 E0 FC JNB E0, WAIT Check For EOC
4119 90 FF C0 MOV DPTR, #FFC0 Read ADC Data
411C E0 MOVX A, @DPTR
4110 90 41 50 MOV DPTR, #4150 Store the Data
4120 F0 MOVX @DPTR, A
4121 HERE: 90 FE SJMP HERE
Result:
Thus an assembly language program is executed for analog to digital conversion.
POST LAB QUESTION AND ANSWERS
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION AND ANSWERS
1. What is an interrupt?
2. Define Polling.
Algorithm:
1. Move the value 081H to the Interrupt Enable pin to enable it.
2. Press INT0 interrupt is enabled. LED’s are on.
3. End the Program.
PROGRAM:
Result:
Thus an assembly language program for the internal interrupt has been done.
POST LAB QUESTION AND ANSWERS
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION AND ANSWERS
Aim:
To write an assembly program to make the stepper motor run in forward and reverse
direction.
Apparatus required:
Stepper motor
8051 microprocessor kit
(0-5V) power supply
Algorithm:
1. Fix the DPTR with the Latch Chip address FFC0
2. Move the values of register A one by one with some delay based on the 2-
Phase switching Scheme and repeat the loop.
3. For Anti Clockwise direction repeat the step 3 by reversing the value
sequence.
4. End the Program
4508 22 RET
Result:
Thus an assembly language program to control of stepper motor was executed
successfully using 8051 Microcontroller kit.
POST LAB QUESTION AND ANSWERS
Title of Experiment :
Register Number :
Date of Experiment :
Date of submission :
Staff Signature
PRE LAB QUESTION AND ANSWERS
Algorithm:
1. Fix the control the control and move the control word to control register.
2. Move the Traffic Light LED Position values to Port A, Port B and Port C
respectively based on the logic.
3. Fix the delay based on the requirement.
3. Execute the program.
PROGRAM:
4100 ORG 4100
CONTRL EQU 0FF0FH
PORT A EQU 0FF0CH
PORT B EQU 0FF0DH
PORT C EQU 0FF0EH
Result:
Thus an assembly language program for the Traffic Light Control has been executed.
POST LAB QUESTION AND ANSWERS
1. What is 8254?
2. What is 8259A?
3. What is 8237?