Microprocessor and Microcontroller Complete Book
Microprocessor and Microcontroller Complete Book
VSEMESTER
Electronics & Communication Engineering
Mr. GANESHA H S
Assistant Professor & Assistant Registrar
Dept. of Electronics & Communication Engineering
JSS University, NOIDA, UP.
Breach of this condition is liable for legal action. For binding mistakes,
misprints or for missing pages, etc., the publisher’s liability is limited to
replacement within one month of purchase by a similar edition. All
expenses in this connection are to be borne by the purchaser.
Edition : First
Pages : x + 245
Price : Rs. 200/-
ISBN : 978-93-5996-719-6
Dedicated to
our
Beloved
Family, Friends
&
Teachers
PREFACE
This book is written as a textbook on Microprocessor & Microcontroller for the fifth-
semester engineering students, following the syllabus of Dr. A.P.J. Abdul Kalam
Technical University, Uttar Pradesh. The aim is to help students grasp the
fundamental concepts of Communication Engineering at a basic level. The book
strives to introduce students and engineers to Communication Engineering using
easily understandable explanations, incorporating recent information, and drawing
comparisons between theories and real-world phenomena. The text employs plain
and lucid language to elucidate the subject's core concepts.
The book covers all major topics of Microprocessor & Microcontroller, accompanied
by numerous illustrations, sketches, and diagrams designed to provide essential
technical information in a simple manner, facilitating easy comprehension by
students. The book also includes exercises at the end of chapters to assess
understanding.
All five units are thoroughly addressed both theoretically and through problem-
solving. A significant number of examination problems are solved to reinforce
theoretical concepts. The book's orientation towards examinations is evident in the
inclusion of solutions to examination question papers. This book will be valuable not
only to students in engineering and polytechnic colleges but also to teachers. We
welcome and highly appreciate suggestions for improving this book.
SYLLABUS
UNIT 1: Introduction to Microprocessor:
Introduction to Microprocessor: Microprocessor architecture and its operations,
Memory, Input & output devices, The 8085 MPU- architecture, Pins and signals,
Timing Diagrams, Logic devices for interfacing, Memory interfacing, Interfacing
output displays, Interfacing input devices, Memory mapped I/O.
TEXT BOOK
3. Mazidi Ali Muhammad, Mazidi Gillispie Janice, and McKinlay Rolin D., “The 8051
Microcontroller and Embedded Systems using Assembly and C”, Pearson, 2nd Edition,2006.
CONTENTS
i
3.2.3 DMA Signals 89
3.2.4 Interfacing 8237a DMA Controller with the 8085 90
3.2.5 Programming The 8237 91
3.2.6 DMA Operation 91
3.3 8255 PROGRAMMABLE PERIPHERAL INTERFACE 92
3.3.1 8255 modes of operation 93
3.3.2 PIN DIAGRAM OF 8255 94
3.3.3 8253/8254 PROGRAMMABLE INTERVAL TIMER (PIT) 96
3.3.4 OPERATION OF 8253 98
3.4 PIN DIAGRAM OF 8253/54 103
3.4.1 Applications of 8253/8254 104
3.4.2 3.4.2 DIFFERENCE BETWEEN 8253 AND 8254 105
3.5 8259 PROGRAMMABLE INTERRUPT CONTROLLER 106
3.5.1 Pin Diagram of 8259 108
3.6 8251 USART & RS 232C 109
3.7 3.7 RS232C 111
3.7.1 Electrical Specifications 111
3.7.2 Working of Rs 232c 112
UNIT-4: 8051 MICROCONTROLLER BASICS
4.1 INTRODUCTION TO MICROPROCESSOR & MICROCONTROLLERS:
114
MICROPROCESSOR
4.1.1 Microcontroller 114
4.1.2 Difference between Microprocessor & Microcontroller 115
4.1.3 RISC and CISC processors 116
4.1.4 Difference between RISC and CISC processors 116
4.1.5 Selection Of Microcontrollers 116
4.1.6 Embedded Microcontrollers 117
4.2 8051 ARCHITECTURES 117
4.2.1 STACK (8-bit) 119
4.2.2 Applications of Microcontrollers 120
4.2.3 Special Function Register (Sfr) 122
4.2.4 Features Of 8051 Microcontroller 122
4.3 MEMORY ORGANIZATION 123
4.3.1 Register banks or General-purpose RAM 124
4.3.2 Bit addressable RAM 124
4.3.3 External RAM 125
4.4 I/O PORTS FUNCTIONS 127
4.4.1 PORT 0 (Pins 32-39) 127
4.4.2 PORT 1 (Pins 1-8) 129
4.4.3 PORT 2 (Pins 21-28) 129
4.4.4 PORT 3 (Pins 10-17) 129
ii
4.5 8051 PIN DIAGRAM 130
4.6 EXTERNAL MEMORY (ROM & RAM) INTERFACING 131
4.6.1 Interfacing External Data 131
4.6.2 Interfacing External Rom 20
4.7 8051 ADDRESSING MODES 141
4.8 SPECIAL FUNCTION REGISTERS (SFR’s) 143
UNIT-5: Assembly programming and instruction of 8051
(Annexure-2: 8051 Instruction Set)
5.1 PROGRAMMING 8051 TIMERS 149
5.2 Timer Resistors 149
5.2.1 TIMER 0 Register 149
5.2.2 TIMER 1 Register 150
5.3 TMOD (Timer Mode) register 150
5.3.1 FOR TIMER 1 150
5.3.2 FOR TIMER 0 151
5.4 TCON Register (Timer Control Register) 153
5.5 TIMER MODES 154
5.5.1 TIMER IN MODE 1 154
5.5.2 TIMER IN MODE 2 156
5.6 COUNTER MODE 157
5.6.1 COUNTER 0 IN MODE 1 157
5.6.2 COUNTER 1 IN MODE 1 158
5.6.3 COUNTER 0 IN MODE 2 158
5.6.4 COUNTER 1 IN MODE 2 159
5.7 MAXIMUM COUNT VALUE 160
5.8 SERIAL COMMUNICATION 163
5.8.1 Serial communication 163
5.8.2 Parallel communication 163
5.8.3 DATA TRANSFER RATES 164
5.8.4 BAUD RATE IN THE 8051 164
5.8.5 SCON (serial control) register 165
5.8.6 SBUF register 166
5.8.7 PCON Register 166
5.9 Programming the 8051 to transfer data serially 167
5.10 PROGRAMMING THE 8051 TO RECEIVE DATA SERIALLY 168
5.10.1 SCON register configuration 169
5.10.2 TMOD REGISTER CONFIGURATION 169
5.11 INTERRUPTS 171
5.11.1 INTERRUPT & POLLING METHODS 172
5.11.2 STEPS IN EXECUTING AN INTERRUPT 172
5.11.3 DIFFERENT TYPES OF INTERRUPT 173
iii
5.11.4 IE AND IP REGISTERS 173
5.11.5 Interrupt Priority 174
5.11.6 ENABLING OR DISABLING OF INTERRUPTS 177
5.12 STACK 178
5.13 SUBROUTINE 181
5.14 ADC0804 (ANALOG TO DIGITAL CONVERTER) 182
5.15 LCD INTERFACING 185
5.15.1 LCD Commands 186
5.15.2 LCD Timing for READ 187
5.15.3 LCD Timing for WRITE 187
5.16 Stepper motor 191
5.16.1 Configuration 192
5.16.2 Stepper motor controller circuit advantages 193
5.16.3 Stepper Motor Applications 193
5.16.4 Stepper motor controller 194
Annexure-1: 8085 Programming 204
Annexure-2: 8051 Instruction Set 216
iv
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
UNIT
Introduction to Microprocessor
SYLLABUS
Introduction to Microprocessor: Microprocessor architecture and its operations, Memory, Input
& output devices, the 8085 MPU- architecture, Pins and signals, Timing Diagrams, Logic devices for
interfacing, Memory interfacing, Interfacing output displays, Interfacing input devices, Memory
mapped I/O.
The 8085 microprocessor is typically programmed using assembly language, which provides low-
level control over its operations. Programmers write code in assembly language and then
assemble it into machine code for execution. It has historical significance in the world of
computing and served as a stepping stone for the development of more advanced
microprocessors and microcontrollers.
1.1.1 Microcomputer:
Page | 1
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
3. Input and
4. Output
The microcomputer is a programmable digital device, designed with registers, flip-flops and
timing elements. It is designed for general-purpose computing tasks and can be used for various
applications.
1.1.2. Microprocessor:
A microprocessor is a specific component within a microcomputer, and it serves as the central
processing unit (CPU) of the system.
Page | 2
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Store data temporarily during the execution in the defined R/W memory locations called
the stack.
Reset IN: When the reset pin is activated by an external key, all the internal operations are
suspended and the program counter is cleared i.e. PC = 0000 H. Now, the program execution
will begin from the address 0000 H.
Interrupt: The microprocessor can be interrupted from the execution of instructions and
asked to execute some other instructions called an Interrupt Service Routine (ISR). The
microprocessor resumes it operation after completing the Interrupt service routine.
Ready: It is used by the microprocessor to sense whether a peripheral is ready to transfer data
or not.
If Ready = 1, the peripheral is ready.
If Ready = 0, the microprocessor waits till it goes high.
Hold: When the HOLD pin is activated by an external signal, the microprocessor relinquishes
control of buses and allows the external peripheral to use them. For example: The HOLD signal
is used in Direct Memory Access (DMA) data transfer.
The 8085 microprocessor performs operation using three sets of communication lines called
buses:
1 The Address : The 8085 microprocessor uses a 16-bit address bus to access
bus memory locations. This provides a maximum addressable memory
space of 64 KB (kilobytes). This address bus is used for memory
addressing, allowing the microprocessor to fetch. The 8085 MPU
with its 16-bit address lines is capable of addressing 216 = 65.536
memory location (64 Kb) instructions and data from memory and
to write data to memory.
Page | 3
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
2 The data bus : The 8085 microprocessor has an 8-bit bi-directional data bus. Data
bus can transfer data in 8-bit chunks or one byte between the
microprocessor and memory, input/output (I/O) devices, and
other components.
3 The control bus : The control bus is comprised of various single lines that carry
synchronization signals. The control bus is not a group of lines
like Address bus or data bus, but individual lines that provide a
pulse to indicate an MPU operation.
The control bus in the Intel 8085 microprocessor is a set of
signals that are used to control various operations within the
microprocessor. These control signals coordinate actions such
as memory read and write operations, input/output (I/O)
operations, and the sequencing of instructions.
Page | 4
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Accumulator
Accumulator is an 8-bit register which can hold 8-bit data.
Accumulator is a special register used for temporary data storage and manipulation
during arithmetic and logical operations.
It also stores the result of the operation carried out by the Arithmetic and Logic unit.
Page | 5
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
STACK:
The stack is a region of memory used for storing data temporarily during program execution.
Page | 6
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Temporary register:
Temporary register is an 8-bit register. This register acts as a temporary memory during the
arithmetic and logical operations. This temporary register can only be accessed by the
microprocessor and it is completely inaccessible to programmers.
D7 D6 D5 D4 D3 D2 D1 D0
𝐒 𝐙 𝐀𝐂 𝐏 𝐂𝐘
The five bits indicate the five-status flag and three bits are undefined. The combinations of these
8-bits are called Program Status Word (PSW). The 5 flags bits are Carry (CY), Zero (Z), Sign (S),
Parity (P) and Auxiliary Carry (AC)
CARRY (CY):
After performing arithmetic & logic operation if there is a carryout from the MSB (D 7 i.e.
7th bit) then CY = 1, otherwise CY = 0.
If there is a borrow from subtraction or comparison, the carry flag CY=1; otherwise CY=0.
ZERO FLAG:
After performing arithmetic & logic operation if the result in the accumulator is zero, then Z=1
and if the result in the accumulator is non-zero then Z=0.
SIGN FLAG:
After performing arithmetic & logic operation if the most significant bit (MSB) of the result in
accumulator is 1 i.e. MSB=1, then S=1; otherwise S=0. (if MSB=1, then S=1 & if MSB=0, then S=0).
Page | 7
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
REGISTER ARRAY
Two additional registers, called temporary registers 𝐖 and 𝐙, are included in the register array
and they are used internally by 8085 microprocessor. These registers are not available to the
programmer. These registers are used to hold 8-bit data during the execution of some
instructions.
ADDRESS BUS:
The address bus is a set of physical lines that carry the memory address information. It
specifies the location in memory or a peripheral device where data needs to be read from
or written to.
In 8085 microprocessor address bus is 16-bit A0 to A15.
The lower address lines are multiplexed address and data lines i.e. AD0 to AD7 and
higher address line are A8 to A15.
The address bus is unidirectional. The width of the address bus determines the range of
memory addresses that can be accessed, allowing access to 64 KB of memory locations
(i.e. 216 = 64 KB).
DATA BUS:
The data bus is set of physical lines that carry the actual data being transferred between
the microprocessor and memory or peripheral devices.
In 8085 microprocessor data bus is 8-bit bidirectional allowing it to transfer 8-bits of
data at a time.
NUMERICAL ON FLAGs
1. Determine the flag bits in the 8085 microprocessor after adding 80H and 60H.
SOLUTION:
CY D7 D6 D5 D4 D3 D2 D1 D0
80 0 1 0 0 0 0 0 0 0
+ 60 + 0 1 1 0 0 0 0 0
E0 1 1 1 0 0 0 0 0
Page | 8
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
So, after adding 80H and 60H, the flag values are:
D7 D6 D5 D4 D3 D2 D1 D0
𝐒 𝐙 𝐀𝐂 𝐏 𝐂𝐘
1 0 0 0 0
2. Determine the flag bits in the 8085 microprocessor after adding CB H and E9 H.
SOLUTION:
CY D7 D 6 D5 D4 D3 D2 D1 D0
1 1 1 1 1
CB 1 1 0 0 1 0 1 1
+ E9 + 1 1 1 0 1 0 0 1
1B4 1 1 0 1 1 0 1 0 0
Page | 9
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Figure 1.8 (a): Pin diagram of 8085 Figure 1.8 (b): Functional Pin diagram
Page | 10
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
RST 7.5:
The RST 7.5 interrupt is a maskable interrupt that is
generated by a software instruction. RST 7.5 can be enabled
or disabled by the microprocessor.
When RST 7.5 is triggered, the microprocessor jumps to a
specific memory location address 002C to execute a
predefined routine.
It has the second highest priority.
(INTERRUPTS)
RST 6.5:
TRAP The RST 6.5 interrupt is a maskable interrupt that is
RST 7.5 generated by a software instruction. RST 6.5 can be enabled
6-11 or disabled by the microprocessor.
RST 6.5
RST 5.5 When RST 6.5 is triggered, the microprocessor jumps to a
INTR specific memory location address 0034 to execute a
̅̅̅̅̅̅̅
𝐈𝐍𝐓𝐀 predefined routine.
It has the third highest priority.
RST 5.5:
The RST 5.5 interrupt is a maskable interrupt that is
generated by a software instruction. RST 5.5 can be enabled
or disabled by the microprocessor.
When RST 5.5 is triggered, the microprocessor jumps to a
specific memory location address 003C to execute a
predefined routine.
It has the fourth highest priority.
Page | 11
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
SUMMARY OF INTERRUPTS
Address
Maskable or Non-
Interrupt Priority order or
Maskable
Location
1 (Highest Non-maskable
TRAP 0024
priority) (NMI)
RST 7.5 2 Maskable 003C
RST 6.5 3 Maskable 0034
RST 5.5 4 Maskable 002C
5 (Lowest No
INTR Maskable
priority) specific
AD0 to AD7:
AD0 to AD7 are multiplexed address and data lines that form
the lower byte of the 16-bit Address/Data bus. AD0 – AD7 are
8-bit bi-directional and serve as both A0 – A7 and D0 – D7
simultaneously.
12- They are used to send both address and data between the
AD0-AD7
19 microprocessor and memory or peripheral devices
During the execution of the instruction, these lines carry the
address bits during the early part, then during the late parts
of the execution, they carry the 8 data bits.
A8 to A15:
A8-A15 are a set of eight unidirectional lines that form the
21-
A8-A15 upper part of the 16-bit Address bus.
28
They are used to transmit the most significant bits of the
memory address during memory read and write operations.
Page | 12
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
When ̅̅̅̅̅̅̅̅̅
𝐑𝐄𝐒𝐄𝐓 ̅̅̅𝐈𝐍 = 𝟎, the microprocessor enters a reset state.
36 ̅̅̅̅̅̅̅̅̅ 𝐈𝐍
𝐑𝐄𝐒𝐄𝐓 ̅̅̅ During this state, all registers and flags are cleared, and the
program counter (PC=00) is typically set to its initial value.
CLK OUT in the 8085 microprocessor is an output signal that
37 CLK (OUT) carries the microprocessor's clock frequency. This signal can be
used as the system clock for other devices.
HLDA (Hold Acknowledge):
HLDA is a signal used to acknowledge a HOLD request.
38 HLDA It indicates that the HOLD request has been received.
The microprocessor takes over the buses after the removal of
the HOLD request when HLDA goes low.
HOLD (Hold Request):
It indicates that another device is requesting the use of the
address and data bus.
After receiving a HOLD request, the microprocessor
39 HOLD
relinquishes (hands over) the use of the buses as soon as the
current machine cycle is completed.
The microprocessor regains the buses after the removal of the
HOLD signal.
40 VCC +5V DC supply
Note:
In the 8085 microprocessor, "HOLD" and "HLDA" (Hold Acknowledge) are control
signals used to manage external requests for control of the system bus.
0 0 0 Halt
0 0 1 Memory WRITE
0 1 0 Memory READ
Page | 13
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
1 0 1 IO WRITE
1 1 0 IO READ
0 1 1 Op code fetch
1 1 1 Interrupt acknowledge
Each instruction of the 8085 processor consists of one to five machine cycles, i.e., when the 8085
processor executes an instruction, it will execute some of the machine cycles in a specific order.
Instruction Cycle:
The time required to execute an instruction is called instruction cycle.
Status of Various Signals for different Operations of 8085 is shown in table below.
Page | 14
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Page | 15
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Page | 16
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Page | 17
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Page | 18
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Op-
Instruction Operand Bytes MC T Detail
code
Add immediate to
ACI 24 ACI 8-bit data 2 2 7
Accumulator with Carry
Add immediate to
ADI 24 ADI 8-bit, data 2 2 7
accumulator
16-bit Unconditional
CALL CALL 3 5 18
address Subroutine call
Complement
CMA CMA None 1 1 4
Accumulator
Page | 19
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Compare Immediate
CPI CPI 8-bit 2 2 7
with accumulator
Decimal Adjust
DAA DAA None 1 1 4
Accumulator
Reg., 4,1
DCR DCR 1,1 1,3 Decrement source by 1
Mem. 0
5
2 or
or
HLT HLT None 1 mor Halt and enter wait state
mo
e
re
Input data to
8-bit port
IN IN 2 3 10 accumulator from a port
address
with 8-bit address
16-bit
LDA LDA 3 4 13 Load accumulator direct
address
Reg. Pair,
Load Register Pair
LXI LXI 16-bit 3 3 10
Immediate
data
Page | 20
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
MOV Rd, Rs
1 4 Move-copy from source
MOV MOV M, Rs 1
2 7 to destination
MOV Rd, M
Reg., Data
2 2 7
MVI MVI Mem., Move immediate 8 bit
2 3 10
Data
Rotate Accumulator
RRC RRC None 1 1 4
Right
Page | 21
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
6
(in
808
5), Copy H and L registers to
SPHL SPHL None 1 1
5(i the Stack pointer (SP)
n
808
0)
Subtract register or
Reg.,
SUB SUB 1,1 1,2 4,7 memory from
Mem.
Accumulator
Exclusive OR immediate
XRI XRI 8-bit data 2 2 7
with accumulator
The memory is made up of semiconductor material used to store the programs and data. Three
types of memory are,
Process memory
Primary or main memory
Secondary memory
Page | 22
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Having two power supply pins (one for connecting required supply voltage (V and the
other for connecting ground).
The control signals needed for static RAM are chip select (chip enable), read control
(output enable) and write control (write enable).
The control signals needed for read operation in EPROM are chip select (chip enable) and
read control (output enable).
1.4.2. Decoder
It is used to select the memory chip of processor during the execution of a program. No of IC's
used for decoder is,
2-4 decoder (74LS139)
3-8 decoder (74LS138)
Page | 23
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Page | 24
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Memory Interfacing
The following are the steps involved in interfacing memory with 8085 processor.
1. First decide the size of memory requires to be interfaced. Depending on this we can say how
many address lines are required for it. For example, if you want to interface 4KB (212) memory
it requires 12 address lines. Remaining address lines can be used in address decoding.
2. Depending on the size of memory required and given address range, construct address
decoding circuitry. This address decoding circuitry can be implemented with NAND gates and/or
decoders or using PAL (when board size is a constraint).
3. Connect data bus of memory to processor data bus.
4. Generate the control signals required for memory using IO/M’, WR’, RD’ signals of 8085
processor.
The result of ‘address decoding’ is the identification of a register for a given address.
A large part of the address bus is usually connected directly to the address inputs of the
memory chip.
This portion is decoded internally within the chip.
What concerns us is the other part that must be decoded externally to select the chip.
This can be done either using logic gates or a decoder.
Example
A0-A11 address lines are directly connected to address bus of memory chip. A12-A15 are used
for generating chip select signal for memory chip.
Address decoding circuit using 3X8 decoder
Page | 25
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
A15 line is use for enabling 74x138 decoder chip. A12, A13, A14 lines are connected to 74X138
chip as inputs. When these lines are 010 output should be ‘0’. This is provided at O2 pin of 74X138
chip.
A15, A14, A13, A12 inputs should be 1010, for enabling the chip. So, the circuit for this is as shown
above.
There are two types of address decoding mechanism, based on address lines used for generating
chip select signal.
1. Absolute decoding
2. Partial decoding
Absolute decoding
All the higher order lines of microprocessor, left after using the required signals for memory are
completely used for generating chip select signal as shown in above example. This type of
decoding is called absolute decoding.
Partial decoding
Only some of the address lines of microprocessor left after using the required signals for memory
are used for generating chip select signal. Because of this multiple address ranges will be formed.
If total memory space is not required for the system then, this type of address decoding can be
used. The advantage of this technique is fewer components are required for memory interfacing
because of this board size reduces and in turn cost reduces.
Example 1
Connect 512 bytes of memory to 8085
1. For interfacing 512 bytes 9 address lines are required. So A0-A8 can be used to directly connect
to address bus of memory.
2. In the remaining A9-A15 for example only A15-A12 are used for generating chip select signal.
A11-A9 are don’t care signals.
Page | 26
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Example 2
Consider a system in which 32kb memory space is implemented using four numbers of 8kb
memory. Interface the EPROM and RAM with 8085 processor.
The total memory capacity is 32Kb. So, let two number of 8kb n memory be EPROM and the
remaining two numbers be RAM. Each 8kb memory requires 13 address lines and so the address
lines A0- A12 of the processor are connected to 13 address pins of all the memory. The address
lines and A13 - A14 can be decoded using a 2-to-4 decoder to generate four chips select signals.
These four chips select signals can be used to select one of the four memory IC at any one time.
The address line A15 is used as enable for decoder. The simplified schematic memory
organization is shown.
Page | 27
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Figure 1.23: Interfacing 16KB EPROM and 16KB RAM with 8085
Page | 28
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Example 3
Interface EPROM of 16 K using 8K X 8 chips and a RAM of 8K using 4K X 8 chips to the system
lines of 8085 using a 3X8 decoder.
Required EPROM size: 16Kb
Available EPROM size: 8Kb
Number of EPROM Chips: 16Kb/8Kb = 2
Address lines in 8Kb Chip: 8Kb= 8×1kb = 23×210 = 213
Number of address lines in 8K×8 = 13 (A0 to A12)
A13, A14 and A15 are used for chip select logic using decoder
Number of data Lines = 8 (D0 to D7)
Required RAM size: 8Kb
Available RAM size: 4Kb
Number of RAM Chips: 8Kb/4Kb =2
Address lines in 4Kb Chip: 4Kb= 4×1kb = 22×210 = 212
Number of address lines in 4K×8 = 12 (A0 to A11)
Number of data Lines = 8 (D0 to D7)
Page | 29
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Example 4
Design a system for 8085 such that it contains 4KB of EPROM and 2KB of RAM using two 2KB of
EPROM and two 1KB of RAM. Draw the complete interfacing diagram.
Required EPROM size: 4Kb
Available EPROM size: 2Kb
Number of EPROM Chips: 2
Address lines in 2Kb chip: 2Kb= 2×1kb = 2×210 = 211
Number of address lines in 2K×8 = 11 (A0 to A10)
A11, A12 and A13 are used for chip select logic using decoder
A14, A15 are connected to the two active low enable pins of decoder
Number of data Lines = 8 (D0 to D7)
Required RAM size: 2Kb
Available RAM size: 1Kb
Number of RAM Chips: 2Kb/1Kb =2
Address lines in 1Kb chip: 1Kb= 1×1kb = 210
Number of address lines in 1K×8 = 10 (A0 to A9)
Number of data Lines = 8 (D0 to D7)
Page | 30
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
Using I/O devices data can be transferred between the microprocessor and the outside
world.
This can be done in groups of 8 bits using the entire data bus. This is called parallel I/O.
The other method is serial I/O where one bit is transferred at a time using the SID and
SOD pins on the Microprocessor.
There are two ways to interface 8085 with I/O devices in parallel data transfer mode:
Memory Mapped I/O
I/O Mapped I/O
Page | 31
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
I/O devices are interfaced using address from memory space. That means I/O device address are
part of addresses given to memory locations.8085 uses 16-bit address to memory interfacing. So,
any address between 0000H-FFFFH can be given to each peripheral. But the addresses given to
peripheral can’t be used for memory. Memory control signals are used as read and write control
signals for peripherals. And all the operations that can be performed on memory can also be
performed on peripherals. No need of using I/O instructions such as IN, OUT.
In this method separate address space is given to I/O devices. Each I/O device is given an 8-bit
address. Hence maximum 256 devices can be interfaced to the processor. The address range for
the I/O devices is 00H-FFH. I/O control signals are used to perform read, write operations. For
reading data from I/O device or writing data to I/O device IN, OUT instructions need to be used.
Arithmetic and logical operations can’t be performed directly on I/O devices as in memory
mapped I/O. I/O devices can be interfaced, by using buffers for simple I/O i.e., by using address
decoding circuit to enable buffer. For handshake I/O or to interface more peripherals ICs like
8255 peripheral programmable interface (PPI) can be used.
Sl.
Memory Mapped I/O IO mapped I/O
No.
1 I/O is treated as memory. I/O is treated I/O.
2 16-bit addressing. 8- bit addressing.
3 More Decoder Hardware. Less Decoder Hardware.
4 Can address 216=64k locations. Can address 28=256 locations.
5 Less memory is available. Whole memory address space is available.
6 Memory Instructions are used. Special Instructions are used like IN, OUT.
7 Memory control signals are used. Special control signals are used.
Arithmetic and logic operations can be Arithmetic and logic operations cannot be
8
performed on data. performed on data.
9 Data transfer b/w register and I/O. Data transfer b/w accumulator and I/O.
Page | 32
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
As mentioned earlier, the 8085 gives 8 bit I/O address. This means it can select one of the 256 I/O
ports. To select an appropriate, I/O device, it is necessary to do following things.
1. Decode the address to generate unique signal corresponding to the device address
on the bus.
2. When device address signal and control signal (IOR or IOW) both are low,
generate device select signal.
3. Use device select signal to activate the Input Output Interfacing Techniques.
Page | 33
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
When the switch is in the released position, the status of line is high otherwise status is low. With
this information microprocessor can check a particular key is pressed or not.
Page | 34
Microprocessor & Microcontroller Unit-1: Introduction to Microprocessor
The code (data) DAH must be sent on the latch to glow LEDs L1, L3 and L6.
Program:
MVI A, DAH ; Loads the data in the accumulator.
OUT 81H ; sends the data on the latch.
Page | 35
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
UNIT
Basic Programming concepts
SYLLABUS
Basic Programming concepts: Flow chart symbols, Data Transfer operations, Arithmetic
operations, Logic Operations, Branch operation, Writing assembly language programs,
Programming techniques: looping, counting and indexing. Additional data transfer and 16-bit
arithmetic instruction, Logic operation: rotate, compare, counter and time delays, 8085 Interrupts.
Page | 36
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
2.2. ADDRESSING MODES:
The CPU can access data in various ways. The data could be in a memory or in register or it may
be an immediate value (CONSTANT). The various ways of accessing these data are called
addressing mode.
Page | 37
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
STA 2500 H ; Store accumulator directly to memory location. In this instruction,
the content of the accumulator is stored at memory location 2500 H.
IN 02 H ; Read data from the PORT B. (Here 02 H is the address of PORT C).
Example 1:
Instruction Comment
LXI H, 3500 H ; Load the H-L pair with 3500 H
MOV A, M ; Move the content of the memory location, whose address is in H-L
pair i.e. 3500 H, to the accumulator
HLT ; Halt
Example 2:
Instruction Comment
LXI H, 3500 H ; Load the H-L pair with 3500 H
ADD A, M ; Add the content of the memory location, whose address is in H-L pair
i.e. 3500 H, to the content of the accumulator
HLT ; Halt
Example 3:
Instruction Comment
LDAX B ; The BC register pair is used as an address and the content of the
memory location specified by the BC pair is copied to the
accumulator.
Examples:
Instruction Comment
CMA ; Complement Accumulator. The CMA instruction complements (flips)
all the bits in the accumulator. It changes 0s to 1s and 1s to 0s.
RAL ; Rotate Accumulator Left through Carry. The RAL instruction rotates
the bits in the accumulator to the left through the carry flag. The carry
flag is shifted into the least significant bit (LSB), and the LSB is shifted
into the carry flag.
NOP : This instruction does nothing and is used for creating delays in a
program.
Page | 38
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
INSTRUCTION FORMAT:
OPCODE OPERAND1, OPERAND 2
Each instruction has two parts: one is task to be performed, called the operation code
(opcode), and the second is the data to be operated on, called the operand. It may
have more than operand.
The operand (or data) can be specified in various ways. It may include 8-bit or 16-bit
data, an internal register, a memory location, or 8-bit or 16-bit address. In some
instructions, the operand is implicit.
CLASSIFICATION OF INSTRUCTIONs:
These instructions can be classified into five different groups:
1. Data Transfer Group
2. Arithmetic Group
3. Logical Group
4. Branch Control Group
5. I/O and Machine Control Group
Page | 39
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Example:
The "AND" instruction performs a bitwise AND operation on two operands, setting the
result to 1 in each bit position where both operands have a 1.
Page | 40
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
^ or . (dot) AND operation
ᵛ OR Operation
⊕ EXCLUSIVE OR Operation
H at the end of an instruction, it indicates that the number is in
H
hexadecimal (base-16) notation.
affected
Bytes
Flags
Syntax Operation Description Example
MOV A, B
Move or copy the content of source
Move or copy
register B to destination register A.
the content of
source register
None
MOV Rd, Rs (Rd) (Rs) Before Execution: 1
Rs to
Let A = XX H & B = 04 H
destination
After Execution:
register Rd.
A = 04 H & B = 04 H
MOV M, B
Move or copy Move or copy the content of source
the content of register B to destination memory
source register location pointed by register M
Rs to (H-L).
None
MOV M, Rs (M) (Rs) destination Before Execution: 1
memory Let M (H-L) = 3000 H &
location pointed B = 04 H
by register M 3000 H = XX
(H-L). After Execution:
3000 H = 04 H & B = 04 H
MOV B, M
Move or copy the content of the
Move or copy memory location pointed by
the content of register M (H-L) to destination
the memory register B.
location pointed Before Execution:
None
MOV Rd, M (Rd) (M) by register pair Let M (H-L) = 3000 H & 1
M 3000 H = 55 H
(H-L) to B = XX
destination
register Rd. After Execution:
B= 55H & 3000 H = 55 H
MVI A, 55 H
Move 8-bit immediate data 55 H to
Move 8-bit
the destination register A
immediate data
None
Page | 41
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
MVI M, 55 H
Move 8-bit immediate data 55 H to
Move 8-bit the memory location pointed by
immediate data register M (H-L)
to the memory
location pointed Before Execution:
None
MVI M, data M 8-bit data 1
by register M Let M (H-L) = 3000 H
(H-L) 3000 H = XX
55 H is 8-bit data
After Execution:
3000 H = 55 H
affected
Bytes
Flags
Syntax Operation Description Example
The contents of
a memory
LDA 3000 H
location,
The contents of a memory location,
specified by a
3000 H, is copied to the accumulator
16-bit address
LDA 16-bit in the operand,
Before Execution:
address is copied to the
Let M (H-L) = 3000 H
A (16-bit accumulator.
None
3000 H = 55 H 3
(Load address) or
A = XX
Accumulator Load the
Directly) content (8-bit
After Execution:
data) of the
A = 55
specified
3000 H = 55 H
memory
location into
accumulator.
LDAX B
Load the accumulator (A) with the
contents of the memory location
Load the pointed to by the register pair B-C
accumulator (A)
with the Before Execution:
contents of the Let B (B-C) = 3000 H None
LDAX rp A (rp) 1
memory B=30 H & C=00 H
location pointed 3000 H = 55 H
to by the A = XX
register pair
After Execution:
A = 55
3000 H = 55 H
LXI H, 3000 H
Loads the HL register pair with the
16-bit hexadecimal value 3000H.
LXI rp, 16-bit
data Loads 16-bit Before Execution:
data in the Let H (H-L) = XXXX
(rp) 16-bit
None
3
Load HL pair location into the content of the next memory
to the content (H) (16-bit register L and location 3001 H is loaded into
of the address address + 1 i.e. the content of register H
Page | 42
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
next memory the next
location) memory Before Execution:
location is Let 3000 H = 11 H
loaded into 3001 H = 22 H
register H L = XX & H = XX i.e. HL = XXXX
After Execution:
3000 H = 11 H
3001 H = 22 H
L = 11 H & H = 22 H
i.e. HL = 2211 H
affected
Bytes
Flags
Syntax Operation Description Example
SHLD 3000 H
Stores the contents of the HL
register pair into the memory
Stores the locations
contents of the i.e. content of L register in 3000 H
HL register pair and the content of H register in
into the 3001 H.
memory
SHLD 16-bit locations Before Execution:
None
(address) (L)
3
address (address+ 1) (H) i.e. content of L Let 3000 H = XX
register in 3001 H = XX
address and the L = 11 & H = 22 i.e. HL = 2211 H
content of H
register in After Execution:
address +1. 3000 H = 11 H
3001 H = 22 H
HL = 2211 H
STA 3000 H
The content of the accumulator is
STA 16-bit stored in the memory location
The content of
address 3000 H.
the accumulator
Before Execution:
is stored in the
Let 3000 H = XX
None
(address) (A) memory 3
Store A = 55 H
location
accumulator
specified in the
content to After Execution:
instruction.
direct address 3000 H = 55 H
A = 55 H
Page | 43
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
and L with D Let H = 11 H & L = 22 H
and E pair i.e. H-L = 1122 H
Let D = 33 H & E = 44 H
i.e. D-E = 3344 H &
After Execution:
H = 33 H & L = 44 H
i.e. H-L = 3344 H
D = 11 H & E = 22 H
i.e. D-E = 1122 H
affected
Bytes
Flags
Syntax Operation Description Example
ADD B
Before Execution:
Let A = 9A H
B = 89 H
Flag: S=0, Z=0, AC=0, P=0, and CY=0
Execution:
The contents of
9A H= 1001 1010
register are
89 H= 1000 1001
added to the
All Flags
23 H= 0010 0011
A (A) + contents of
ADD R 1
(Reg) accumulator.
After Execution:
The result is
A = 23 H
stored in
B = 89 H
accumulator.
Flag: S=0, Z=0, AC=1, P=0, and CY=1
ADD M
Before Execution:
Let A = 20 H
HL = 2500 H
Flag: S=0, Z=0, AC=0, P=0, and CY=0
Memory
24FF H 75 H
The contents of
2500 H 08 H
memory are
2501 H 21 H
added to the
All Flags
2502 H 13 H
A (A)+ contents of
ADD M 1
(Memory) accumulator.
The result is Execution:
stored in 20 H= 0010 0000
accumulator. 08 H= 0000 1000
28 H= 0010 1000
After Execution:
A = 28 H
HL = 2500 H
Page | 44
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Memory
24FF H 75 H
2500 H 08 H
2501 H 21 H
2502 H 13 H
ADI B2 H
Before Execution:
Let A = C4 H
Immediate Data = B2 H
Flag: S=0, Z=0, AC=0, P=0, and CY=0
All Flags
A (A)+ 8-bit contents of 76 H= 0111 0110
ADI 8-bit 2
Data accumulator.
The result is
stored in After Execution:
accumulator. A = 76 H
ADC B
Before Execution:
Let A = 9A H
B = 89 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
(Reg)+(CY)
accumulator.
The result is
stored in After Execution:
accumulator. A = 24 H
B = 89 H
Flag: S=0, Z=0, AC=1, P=1, and CY=1
ADC M
The contents of Before Execution:
memory and Let A = 20 H
Carry Flag (CY) HL = 2500 H
All Flags
A (A)+ are added to the Flag: S=0, Z=0, AC=0, P=0, and CY=1
ADC M (Memory)+(CY contents of
1
) accumulator. Memory
The result is 24FF H 75 H
stored in 2500 H 08 H
accumulator. 2501 H 21 H
2502 H 13 H
Page | 45
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Execution:
20 H= 0010 0000
08 H= 0000 1000
CY 1
29 H= 0010 1001
After Execution:
A = 29 H
HL = 2500 H
Memory
24FF H 75 H
2500 H 08 H
2501 H 21 H
2502 H 13 H
ACI B2 H
Before Execution:
Let A = C4 H
Immediate Data = B2 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
All Flags
A (A)+ 8- added to the 77 H= 0111 0111
ACI 8-bit
2
bits Data+(CY) contents of
accumulator.
The result is After Execution:
stored in A = 77 H
accumulator.
Flag: S=0, Z=0, AC=0, P=1, and CY=1
DAD B
Before Execution:
The 16-bit
Let HL = 2233 H
contents of the
BC = 1122 H
register pair are
Flag: CY=1
Carry Flag
added to the
HL (Reg.
DAD Rp contents of H-L
1
Page | 46
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Flag: CY=0
SUB B
Before Execution:
Let A = 9A H
B = 89 H
Flag: S=0, Z=0, AC=0, P=0, and CY=0
The contents of
Execution:
the register are
9A H= 1001 1010
subtracted from
All Flags
89 H= 1000 1001
the contents of
SUB R A (A)- (R) 11 H= 0001 0001
1
the accumulator.
The result is
After Execution:
stored in
A = 11 H
accumulator.
B = 89 H
Flag: S=0, Z=0, AC=0, P=1, and CY=0
SUB M
Before Execution:
Let A = 20 H
HL = 2500 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
Memory
24FF H 75 H
2500 H 08 H
2501 H 21 H
2502 H 13 H
the accumulator.
The result is After Execution:
stored in A = 18 H
accumulator. HL = 2500 H
Memory
24FF H 75 H
2500 H 08 H
2501 H 21 H
2502 H 13 H
SUI 13 H
All Flags
The 8-bit
A (A)- 8 bits Before Execution:
SUI 8-bit immediate data is
2
Data Let A = 05 H
subtracted from
Immediate data= 13 H
Page | 47
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
the contents of Flag: S=0, Z=0, AC=0, P=0, and CY=0
the accumulator.
The result is Execution:
stored in 05 H= 0000 0101
accumulator. 13 H= 0001 0011
F2 H= 1111 0010
After Execution:
A = F2 H
SBB B
Before Execution:
Let A = 25 H
B = 13 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
The contents of
the register and Execution:
Borrow Flag (i.e. 25 H= 0010 0101
CY) are 13 H= 0001 0011
All Flags
A (A)- (R)- subtracted from CY 1
SBB R
1
(CY) the contents of 11 H= 0001 0001
the accumulator.
The result is After Execution:
stored in A = 11 H
accumulator.
Flag: S=0, Z=0, AC=0, P=1, and CY=0
SBB M
Before Execution:
Let A = 20 H
HL = 2500 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
Memory
24FF H 75 H
2500 H 08 H
The contents of 2501 H 21 H
the memory 2502 H 13 H
location and
Borrow Flag (i.e. Execution:
All Flags
(CY)
the contents of CY 1
the accumulator. 17 H= 0001 0111
The result is
stored in
accumulator. After Execution:
A = 17 H
HL = 2500 H
Memory
24FF H 75 H
2500 H 08 H
2501 H 21 H
Page | 48
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
2502 H 13 H
SBI 13 H
Before Execution:
Let A = 18 H
Immediate data= 13 H
Flag: S=0, Z=0, AC=0, P=0, and CY=1
The 8-bit
immediate data Execution:
and the Borrow 18 H= 0001 1000
Flag (i.e. CY) is 13 H= 0001 0011
All Flags
A (A)- 8 bits subtracted from CY 1
SBI 8-bits
2
data -(CY) the contents of F2 H= 0000 0100
the accumulator.
The result is After Execution:
stored in A = 04 H
accumulator.
Flag: S=0, Z=0, AC=0, P=0, and CY=0
INR E
Before Execution:
Let E = 1C H
1
The result is
stored in the After Execution:
same place. E= 1D H
After Execution:
HL= 2500 H
Page | 49
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Memory
24FF H XX H
2500 H 1D H
2501 H XX H
2502 H XX H
Execution:
The contents of 2050 H= 0010 0000 0101 0000
the designated + 1
register pair are 2051 H= 0010 0000 0101 0001
None
INX Rp Rp (Rp)+ 1 incremented by 1
1
and their result is After Execution:
stored at the BC= 2051 H
same place.
Flag: S=0, Z=0, AC=0, P=1, and CY=NO
CHANGE
24FF H XX H
and their result is
2500 H 1C H
stored at the
2501 H XX H
same place.
2502 H XX H
Execution:
1C H= 0001 1100
Page | 50
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
- 1
1B H= 0001 1011
After Execution:
HL= 2500 H
Memory
24FF H XX H
2500 H 1B H
2501 H XX H
2502 H XX H
None
DCX Rp Rp (Rp)- 1 decremented by 1 24FF H= 0010 0100 1111 1111
1
and their result is
stored at the After Execution:
same place. HL= 24FF H
A(Binary
1
Page | 51
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
D7 D6 D5 D4 D3 D2 D1 D0
1 1
A 3 8 = 0 0 1 1 1 0 0 0
+ 1 3 = 0 0 0 1 0 0 1 1
4 B = 0 1 0 0 1 0 1 1
Result in Accumulator is 4B H and not an BCD.
D7
D6
D5
D4
D3
D2
D1
D0
1 1 1
A 4 B = 0 1 0 0 1 0 1 1
+ 0 6 = 0 0 0 0 0 1 1 0
A 5 1 = 0 1 0 1 0 0 0 1
Result in Accumulator is BCD 51.
affected
Bytes
Flags
Syntax Operation Description Example
Page | 52
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Perform a ANI 3C H
logical AND ALL
ANI 8-bit data operation Before Execution:
between the A = A7 H (Z,
(A) (A) ^
AND accumulator (A) data 3C H S,
(data) 1
immediate and an 8-bit P,
data with data value & the After Execution: CY
accumulator result is stored A = 24 H &
back in the AC)
accumulator.
(A) (A) ORA B
v (r) Performs a logical OR operation between the
contents of A and the B register, and the
ORA r
Note: result is stored back in accumulator A.
performs a
OR symbol
bitwise OR
ᴠ Before Execution:
operation ALL
A = 55 H
between the
ORA r B = 65 H
binary values in (Z,
the accumulator S,
OR register Where "r" After Execution: 1
(A) and the P,
with can be any of A = 75 H
specified CY
accumulator the following B = 65 H
register (r) & &
registers:
the result is AC)
B, C, D, E, H,
stored back in
L or M
the
(Indirect
accumulator.
addressing
using the HL
pair)
affected
Bytes
Flags
Syntax Operation Description Example
Perform a ORA M
logical OR
operation Before Execution:
between the A = 55 H
contents of the M = HL = 3000 H ALL
accumulator (A) 3000 = 65 H
ORA M
and the data (Z,
(A) (A)
stored in the After Execution: S,
OR memory v (M) 1
memory A = 75 H P,
with
location pointed 3000 = 65 H CY
accumulator
to by the HL &
register pair & AC)
the result is
stored back in
the
accumulator.
Perform a ORI 65 H
logical OR ALL
ORI 8-bit data operation Before Execution:
between the A = 55 H (Z,
(A) (A) v
OR accumulator (A) data 65 H S,
(data) 1
immediate and an 8-bit P,
data with data value & the After Execution: CY
accumulator result is stored A = 75 H &
back in the AC)
accumulator.
XRA r (A) (A) ⊕ XRA r XRA B ALL
(r) performs a Performs a logical EX-OR operation between
EX-OR bitwise EX-OR the contents of A and the B register, and the (Z,
1
register Note: operation result is stored back in accumulator A. S,
with EX-OR symbol between the P,
accumulator ⊕ binary values in Before Execution: CY
Page | 53
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
the accumulator A = 55 H &
(A) and the B = 65 H AC)
specified
Where "r" register (r) & After Execution:
can be any of the result is A = 30 H
the following stored back in B = 65 H
registers: the
B, C, D, E, accumulator.
H, L or M
(Indirect
addressing
using the
HL pair)
XRA M XRA M
perform a
bitwise XOR Before Execution:
(exclusive OR) A = 55 H
operation M = HL = 3000 H
(A) (A) ⊕ between the 3000 = 65 H
(M) contents of the ALL
XRA M accumulator (A) After Execution:
Note: and the data A = 30 H (Z,
EX-OR EX-OR symbol stored in the 3000 = 65 H S,
1
memory ⊕ memory P,
with location pointed CY
accumulator to by the HL &
register pair AC)
(indirect
addressing) &
the result is
stored back in
the
accumulator.
XRI 8-bit data XRI 65 H
perform a
logical XOR Before Execution:
(exclusive OR) A = 55 H ALL
(A) (A) ⊕ 8- operation
bit data between the After Execution: (Z,
contents of the A = 30 H S,
XRI 8-bit data 1
Note: accumulator (A) P,
EX-OR symbol and an 8-bit CY
⊕ immediate data &
value & the AC)
result is stored
back in the
accumulator.
CMA CMA
(Complement
Accumulator) Before Execution:
instruction is A = 0F H
used to perform
ALL
a bitwise After Execution:
CMA complement A = F0 H
(Z,
̅) (bitwise NOT)
(A) (𝑨 S,
Complement operation on the 1
P,
the contents of the
CY
accumulator accumulator (A).
&
This means that
AC)
all the bits in the
accumulator are
inverted: 0s
become 1s, and
1s become 0s.
Page | 54
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
CMC is used to CMC
complement Before Execution:
CMC (toggle) the CY = 0 (one bit)
state of the
(CY)
Complement Carry Flag (CY) After Execution:
(̅̅̅̅
𝑪𝒀) CY 1
the carry bit in the status CY = 1 (one bit)
(CY) register.
The contents of
the A & r
remain
unchanged.
CMP M
Before Execution:
CMP M M (HL) = 3000 H
instruction is A = 55 H
used to compare 3000 H = 56 H
the contents of
the accumulator After Execution:
(A) with the A = 55 H ALL
data stored in 3000 = 56 H
CMP M
the memory (Z,
(A) - (M) location pointed S,
Compare 1
to by the HL Zero Flag (Z): Set if A = M P,
memory with
register pair Sign Flag (S): Set if the result is negative (A < CY
accumulator
(indirect M). &
addressing). Carry Flag (CY): Set if a borrow occurred AC)
during subtraction (A < M).
The contents of Parity Flag (P): Set if the number of set bits
the A & M in the result is even.
remain Auxiliary Carry Flag (AC): Set if a borrow
unchanged. occurred from bit 4 to bit 3 during
subtraction.
Page | 55
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
performs a CY = 1 (one Bit)
subtraction
between the Zero Flag (Z): Set if A = 8-bit data value.
accumulator and Sign Flag (S): Set if the result is negative (A <
the immediate data).
data value Carry Flag (CY): Set if a borrow occurred
without altering during subtraction (A < data).
the contents of Parity Flag (P): Set if the number of set bits
the accumulator. in the result is even.
Auxiliary Carry Flag (AC): Set if a borrow
occurred from bit 4 to bit 3 during
subtraction.
Before Execution:
A = 55 H
CY = X ALL
RLC (CY) A7
A7 A6 (Z,
Rotate A0 A7 S, 1
accumulator P,
left CY
&
AC)
After Execution:
A = AA H
CY = 0
Page | 56
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
After Execution:
A = AA H
CY = 1
After Execution:
A = 21 H
CY = 1
Page | 57
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
affected
Bytes
Flags
Syntax Operation Description Example
JMP 2050
Before Execution:
2041 X
2042 JMP 2050
2045 X
. X
Transfers the 204F X
program 2050 X
Jumps to the sequence to 2051 X
JMP 16-Bit
None
address the described 3
Address
memory After Execution:
address. 2041 X
2042 JMP 2050
2045 X
. X
204F X
2050 X
2051 X
JC 2050
Before Execution:
2041 X
2042 JC 2050
2045 X
. X
204F X
If condition is
true address 2050 X
PC (Program 2051 X
JC 16-Bit Jump, if carry
None
Counter) 3
Address flag is set. After Execution:
If condition is
false PC PC+3 2041 X If If
True False
2042 JC 2050
2045 X
. X
204F X
2050 X
2051 X
JNC 2050
If condition is Before Execution:
true address 2041 X
PC (Program Jump, if carry 2042 JNC 2050
JNC 16-Bit
None
Page | 58
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
After Execution:
2041 X If If
True False
2042 JNC 2050
2045 X
. X
204F X
2050 X
2051 X
JZ 2050
Before Execution:
2041 X
2042 JZ 2050
2045 X
. X
If condition is 204F X
true address 2050 X
PC (Program 2051 X
JZ 16-Bit Jump, if zero
None
Counter) 3
Address flag is set. After Execution:
If condition is
false PC PC+3 2041 X If If
True False
2042 JZ 2050
2045 X
. X
204F X
2050 X
2051 X
JNZ 2050
Before Execution:
2041 X
2042 JNZ 2050
2045 X
. X
204F X
2050 X
2051 X
After Execution:
2041 X If If
True False
2042 JNZ 2050
If condition is
2045 X
JNZ 16-Bit true address
Jump, if zero . X
Address PC (Program
None
Page | 59
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
JP 2050
Before Execution:
2041 X
2042 JP 2050
2045 X
. X
If condition is 204F X
true address 2050 X
PC (Program Jump, if 2051 X
JP 16-Bit Counter) positive i.e. 3
None
Address If condition is sign flag is After Execution:
false PC PC+3 reset. 2041 X If If
True False
2042 JP 2050
2045 X
. X
204F X
2050 X
2051 X
JM 2050
Before Execution:
2041 X
2042 JM 2050
2045 X
. X
204F X
If condition is
true address 2050 X
Jump, if 2051 X
PC (Program
JM 16-Bit minus i.e.
None
Counter) 3
Address sign flag is After Execution:
If condition is
set. 2041 X If If
false PC PC+3
True False
2042 JM 2050
2045 X
. X
204F X
2050 X
2051 X
JPE 2050
Before Execution:
2041 X
2042 JPE 2050
2045 X
. X
204F X
If condition is 2050 X
true address 2051 X
Jump, if
PC (Program
JPE 16-Bit parity even
None
Page | 60
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
JPO 2050
Before Execution:
2041 X
2042 JPO 2050
2045 X
. X
204F X
If condition is 2050 X
true address 2051 X
Jump, if
PC (Program
JPO 16-Bit parity odd i.e.
None
Counter) After Execution: 3
Address parity flag is
If condition is 2041 X If If
reset.
false PC PC+3 True False
2042 JPO 2050
2045 X
. X
204F X
2050 X
2051 X
affected
Bytes
Flags
Syntax Operation Description Example
CALL 3050
Before Execution:
2041 X
2042 CALL
3050
2045 X
. X
Program 204F X
sequence Jumps 2050 X
This 2051 X
to the
instruction is SP=2573 H
subroutine
used to PC=2042 H
address
branch to the After Execution:
CALL 16-Bit subroutine
None
SP-1=2572H=45H
SP-2=2571H=20H
SP=2571 H
PC=3050 H
Page | 61
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
CC 3050
Before Execution:
2041 X
2042 CC 3050
2045 X
. X
If CY=1, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call 2042 CC
CC 16-Bit
None
byte) (SP-2) subroutine if 3050 3050 3
Address (SP-2) SP carry status
2045 X (If True
16 Bit CY=1 CY=1)
. X
AddressPC 204F X
If CY=0, 2050 X
program flow
continues in the If SP-1=2572H=45H
main program CY=1 SP-2=2571H=20H
sequentially. SP=2571 H
PC= No Change PC=3050 H
If PC=2045 H
CY=0 SP=2573 H
CNC 3050
Before Execution:
2041 X
2042 CNC 3050
2045 X
. X
204F X
If CY=0,
2050 X
Program
2051 X
sequence Jumps
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call 2042 CNC
CNC 16-Bit 3050 3050
None
Page | 62
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
CZ 3050
Before Execution:
2041 X
2042 CZ 3050
2045 X
. X
If flag Z=1, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call 2042 CZ
CZ 16-Bit
None
byte) (SP-2) subroutine if 3050 3050 3
Address (SP-2) SP zero status 2045 X (If True
16 Bit flag Z=1 . X Z=1)
AddressPC 204F X
If flag Z=0, 2050 X
program flow
continues in the If SP-1=2572H=45H
main program Z=1 SP-2=2571H=20H
sequentially. SP=2571 H
PC= No Change PC=3050 H
If PC=2045 H
Z=0 SP=2573 H
CNZ 3050
Before Execution:
2041 X
2042 CNZ 3050
2045 X
. X
If flag Z=0, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call
2042 CNZ
CNZ 16-Bit
None
Page | 63
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
CP 3050
Before Execution:
2041 X
2042 CP 3050
2045 X
. X
204F X
If flag S=0,
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call 2042 CP
CP 16-Bit
None
byte) (SP-2) subroutine if 3050 3050 3
Address (SP-2) SP sign status 2045 X (If True
16 Bit flag S=0 . X S=0)
AddressPC 204F X
If flag S=1, 2050 X
program flow
continues in the If SP-1=2572H=45H
main program S=0 SP-2=2571H=20H
sequentially. SP=2571 H
PC= No Change PC=3050 H
If PC=2045 H
S=1 SP=2573 H
CM 3050
Before Execution:
2041 X
2042 CM 3050
2045 X
. X
If flag S=1, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call
2042 CM
CM 16-Bit
None
Page | 64
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
CPE 3050
Before Execution:
2041 X
2042 CPE 3050
2045 X
. X
If flag P=1, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call
2042 CPE
CPE 16-Bit
None
byte) (SP-2) subroutine if 3
3050 3050
Address (SP-2) SP parity status
2045 X (If True
16 Bit flag P=1
. X P=1)
AddressPC 204F X
If flag P=0, 2050 X
program flow
continues in the If SP-1=2572H=45H
main program P=1 SP-2=2571H=20H
sequentially. SP=2571 H
PC= No Change PC=3050 H
If PC=2045 H
P=0 SP=2573 H
CP0 3050
Before Execution:
2041 X
2042 CPO 3050
2045 X
. X
If flag P=0, 204F X
Program 2050 X
sequence Jumps 2051 X
to the SP=2573 H
subroutine PC=2042 H
address After Execution:
PC (Higher
byte) (SP-1) Subroutine
PC (Lower Call
2042 CPO
CPO 16-Bit
None
Page | 65
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
affected
Bytes
Flags
Syntax Operation Description Example
The program
sequence is RET
transferred
from the Before Execution:
subroutine to
RET stands for
the calling If SP=2095H
return from the
program. Memory
subroutine.
2095 50
The two bytes 2096 20
PC (Lower
None
RET from the top 1
byte) (SP)
of stack are PC=XXXX H
PC (Higher
copied into After Execution:
byte) (SP+1)
the program
SP SP+2
counter and PC=2050 H
the program SP=2097 H
counter
execution
begins at the
new address.
RC
If condition Before Execution:
(CY=1) is If SP=2095H
true, it Memory
returns to the 2095 50
calling
2096 20
program
None
RC Return on Carry 1
PC=3000 H
If condition
After Execution:
(CY!=1) is not
IF CY=1 IF CY=0
true, it
continues the PC=2050 H PC=3000 H
sequence SP=2097 H SP=2095 H
RNC
If condition Before Execution:
(CY=0) is If SP=2095H
true, it Memory
returns to the 2095 50
calling
2096 20
RNC Return with No program
None
1
Carry
PC=3000 H
If condition
After Execution:
(CY!=0) is not
IF CY=0 IF CY=1
true, it
continues the PC=2050 H PC=3000 H
sequence SP=2097 H SP=2095 H
If condition RP
(S=0) is true,
it returns to Before Execution:
the calling If SP=2095H
Return on
None
RP program Memory 1
positive
2095 50
If condition 2096 20
(S!=0) is not
true, it PC=3000 H
Page | 66
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
continues the After Execution:
sequence IF S=0 IF S=1
PC=2050 H PC=3000 H
SP=2097 H SP=2095 H
RM
None
RM 1
minus
If condition PC=3000 H
(S!=1) is not After Execution:
true, it IF S=1 IF S=0
continues the PC=2050 H PC=3000 H
sequence SP=2097 H SP=2095 H
RPE
If condition
(P=1) is true, Before Execution:
it returns to If SP=2095H
the calling Memory
program 2095 50
Return on Parity
None
RPE 2096 20 1
Even
If condition
(P!=1) is not PC=3000 H
true, it After Execution:
continues the IF P=1 IF P=0
sequence PC=2050 H PC=3000 H
SP=2097 H SP=2095 H
RPO
If condition
(P=0) is true, Before Execution:
it returns to If SP=2095H
the calling Memory
program 2095 50
Return on Parity
None
RPO 2096 20 1
Odd
If condition
(P!=0) is not PC=3000 H
true, it After Execution:
continues the IF P=0 IF P=1
sequence PC=2050 H PC=3000 H
SP=2097 H SP=2095 H
RZ
If condition
(Z=1) is true, Before Execution:
it returns to If SP=2095H
the calling Memory
program 2095 50
None
Page | 67
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
RNZ
None
RNZ 1
Zero
If condition PC=3000 H
(Z!=0) is not After Execution:
true, it IF Z=0 IF Z=1
continues the PC=2050 H PC=3000 H
sequence SP=2097 H SP=2095 H
affected
Bytes
Flags
Syntax Operation Description Example
Example 1:
PUSH B
Before Execution:
Let SP = 3000 H
Push the B = 55 H
content of C = 66 H
register pair rp
((SP-1)) (rh) to stack After Execution:
((SP-2)) (rl) ((SP-1)) (B)
((SP-2)) (C)
(SP) (SP)-2 The stack
PUSH rp pointer register (SP) (SP)-2
is decremented
The "rp" can be and the contents 2FFF = 55 H
one of the of the high order 2FFE = 66 H
Push the following register register (B, D, H, SP = 2FFD H
content of pairs: A) are copied None 1
register into that Example 2:
pair rp to BC location. The PUSH PSW
stack DE stack pointer
HL register is Before Execution:
PSW (Program decremented Let SP = 3000 H
Status Word, again and the A = 77 H
which includes contents of PSW = 88 H
the accumulator the low-order
and flags) register (C, E, L, After Execution:
flags) are copied ((SP-1)) (A)
to that ((SP-2)) (PSW)
location.
(SP) (SP)-2
2FFF = 77 H
2FFE = 88 H
SP = 2FFD H
Page | 68
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Example 1:
POP B
Before Execution:
Let SP = 3000 H
3000 = 55 H
3001 = 66 H
B = XX H
The contents of C = XX H
(rl) ((SP)) the memory
(rh) ((SP+1)) location pointed After Execution:
out by the stack (rl) ((3000))
(SP) (SP)+2 pointer register (rh) ((3000 + 1))
is copied to the
low-order (SP) (3000) +2
POP (retrieve) a register (C, E, L,
POP rp 16-bit value from status flags) of C = 55 H
the stack and load the operand. B = 66 H
it into the The stack BC = 6655 H
POP specified register pointer is SP = 3002 H
(retrieve) a pairs (rp). The incremented
None
1
16-bit value "rp" can be one of by 1 and the
from the the following contents of that
stack and register pairs: memory Example 2:
load it into location are POP PSW
rp BC copied to
DE the high-order Before Execution:
HL register (B, D, H, Let SP = 3000 H
PSW (Program A) of the 3000 = 55 H
Status Word, operand. The 3001 = 66 H
which includes stack
the accumulator pointer register PSW = XX H
and flags) is again A = XX H
incremented by
1. After Execution:
(PSW) ((3000))
(A) ((3000 + 1))
(SP) (3000) +2
PSW = 55 H
A = 66 H
SP = 3002 H
SPHL
Copy H and L SPHL
Copy H and
None
(SP) (HL) registers to the The value stored in the HL register pair 1
L registers
stack pointer. is copied into the stack pointer (SP).
to the stack
pointer
XTHL
The contents of the L register are
exchanged with the stack
Exchange the
(L) (SP) location pointed out by the contents of
values of the HL
the stack pointer
None
2
Input ) the specified
IN 10 H
accumulato input port and
Page | 69
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
r from I/O Port address is loads it into the IN instruction reads data from the input
Port the 8-bit accumulator (A). port with an address of 10H and stores
immediate value the data in the accumulator (A)
that represents
the address of
the input port
from which data
is to be read.
(Port address)
(A)
OUT instruction
OUT Port
sends the data Example:
address Port address" is
stored in the OUT 20 H
the 8-bit
accumulator to
None
Output from immediate value 2
accumulator the output port OUT instruction sends the data stored in
that represents
to I/O Port address the accumulator to the output port with
the address of
specified in the an address of 20H.
the output port
instruction
to which data is
to be sent
EI
Enable Interrupt used to enable interrupts in the 8085 microprocessor except
None
the TRAP interrupt. When interrupts are enabled using the "EI" instruction, the 1
Enable
microprocessor will respond to interrupt requests from external devices.
Interrupts
DI Disable Interrupt is used to disable interrupts in the 8085 microprocessor
temporarily except the TRAP interrupt. When interrupts are disabled using the
None
1
Disable "DI" instruction, the microprocessor will not respond to interrupt requests
Interrupts from external devices.
This is a
multipurpose
instruction used
to read the status
of
interrupts 7.5,
RIM
6.5, 5.5 and read
serial data input
None
Read 1
bit. The
Interrupt
instruction loads
Mask
eight bits in the
accumulator with
the
following Figure 1
interpretations as
shown in figure 1
This is a
multipurpose
instruction and
used to
implement the
SIM
8085 interrupts
7.5, 6.5, 5.5, and
None
Set 1
serial data
Interrupt
output. The
Mask
instruction
interprets the
accumulator
contents as
shown in figure 2 Figure 2
NOP
No operation is performed when this instruction is executed. The registers and
None
flags and memory remain unaffected. The purpose of the "NOP" instruction is to 1
No
provide a delay.
operation
"Halt" is used to halt or stops the execution of the microprocessor and put it into
HLT a halt state. When the microprocessor is in a halt state, it stops fetching and
None
1
executing instructions until it is reset or interrupted.
NOTE:
Page | 70
Microprocessor & Microcontroller Unit-2: Basic Programming Concepts
Halt and The "HLT" instruction is often used to save power when the microprocessor is
enter wait not actively performing tasks and needs to wait for external events or interrupts
state to resume processing.
Page | 71
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
UNIT
16-bit Microprocessor and
Peripheral devices
SYLLABUS
16-bit Microprocessors (8086): Architecture, Pin Description, Physical address, Segmentation,
Addressing modes.
Peripheral Devices: 8237 DMA Controller, 8255 Programmable Peripheral Interface,
8253/8254 Programmable Timer/Counter, 8259 Programmable Interrupt Controller, 8251
USART and RS 232C.
16-BIT MICROPROCESSOR
3.1 Introduction to 16-bit Microprocessor 8086
Page | 72
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Figure 3.1 shows the 8086 Microprocessor internal architecture.
The 8086 microprocessor has two main execution units:
i. The Bus Interface Unit (BIU) and
ii. The Execution Unit (EU).
The BIU is responsible for fetching instructions from memory and decoding them, while the
EU executes the instructions. The BIU also manages data transfer between the
microprocessor and memory or I/O devices.
Segment Registers:
The 8086 has four primary segment registers: CS (Code Segment), DS (Data Segment), SS (Stack
Segment), and ES (Extra Segment). Each of these registers holds a 16-bit value that represents
the base address of a specific segment in memory.
Code Segment (CS) register : CS is a 16 Bit register, holds the base address for the Code
(64 KB) Segment. All programs are stored in the Code Segment and
accessed via the IP.
Data Segment (DS) register : DS is a 16 Bit register, holds the base address for the Data
(64 KB) Segment.
Stack Segment register : SS is a 16 Bit register, holds the base address for the Stack
(64 KB) Segment.
Extra Segment register : ES is a 16 Bit register, holds the base address for the Extra
(64 KB) Segment.
Page | 73
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
The code segment (CS) is multiplied by 10H to give the 20-bit physical address of the Code
Segment.
The address of the next instruction is calculated by using the formula CS x 10H + IP.
Address Bus:
The 8086 microprocessor has a 20-bit address bus, which can address up to 1 MB of memory (220
= 1,048,576), and a 16-bit data bus, which can transfer 16-bit data between the microprocessor
and memory or I/O devices.
Data Bus:
The data bus is used to transfer data between the microprocessor and memory. The data bus is
16 bits wide, allowing the 8086 to transfer 16-bit data words at a time.
Control Bus: The control bus is used to transfer control signals between the microprocessor and
other components in the computer system. The control bus is used to send signals such as read,
write, and interrupt requests, and to transfer status information between the microprocessor and
other components.
General-Purpose Registers:
8086 has four 16-bit general purpose registers AX, BX, CX, and DX which store intermediate
values during execution. Each of these has two 8-bit parts (higher and lower) i.e. AH, AL, BH, BL,
CH, CL, DH and DL to store 8-bit data.
Page | 74
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
CX register: (Combination of CL and CH Registers)
It holds the count for instructions like a loop, rotates, shifts and string operations.
DX register: (Combination of DL and DH Registers)
It is used with AX to hold 32-bit values during multiplication and division
Control Unit:
The Control Unit in the 8086 microprocessor is a component that manages the overall operation
of the microprocessor. It interprets the opcode of an instruction, determines the operation to be
performed, and generates control signals to execute that operation.
Instruction Decode:
The EU decodes the instruction fetched from memory to understand what operation needs to
be performed and which operands to use.
It works in parallel with the Prefetch Unit, which fetches instructions from memory and stores
them in a queue.
Flag Register:
The 8086 flag register contents indicate the results of computation in the ALU. It also contains
some flag bits to control the CPU operations.
It consists of 9 active flags out of 16. The remaining 7 flags marked ‘X’ are undefined flags.
These 9 flags are of two types:
Conditional flags (6) and
Control flags (3)
Page | 75
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
The conditional flags are:
Carry Flag (CF) : CF=1; Whenever there is a carry or borrow out of the MSB (most
significant bit) of a result
CF=0; Otherwise.
Parity Flag (PF) : PF=1; If the number of 1’s in the result are even.
PF=0; If the number of 1’s in the result are odd.
Auxiliary Carry : AF=1; If a carry is generated out of the lower nibble (out of D3 bit)
Flag (AF) AF=0; Otherwise.
Sign Flag (SF) : SF=1; if the MSB of the result is 1. For signed operations such a
number is treated as negative.
SF=0; Otherwise.
Trap Flag (TF) : TF=1; the execution will be done step by step where it executes one
instruction at a time i.e. start single stepping mode. This is useful for
debugging.
TF=0; Otherwise (the free-running operation will be done).
Interrupt Flag (IF) : IF=1; interrupts are enabled and can be serviced by the CPU.
IF=0; interrupts are disabled.
Page | 76
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
The figure below represents the pin diagram of 8086 microprocessor:
From figure 3.3, it is clear that from pin number 24 to 32, we have shown the different
configuration for minimum and maximum mode. Excluding these 8 pins, the rest 32 pins are the
same for both minimum as well as maximum mode.
Page | 77
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
specific tasks or data. When a device sends an interrupt request to the
INTR pin, the CPU acknowledges it, pauses its current operation, and
processes the interrupt. This enables efficient communication
between the CPU and external devices.
It provides timing to the microprocessor for operations. Its frequency
19 CLK is different for different versions, i.e. 5MHz, 8MHz and 10MHz.
̅̅̅̅̅̅̅̅̅ pin is used to reset the microprocessor, returning it to its
The 𝐑𝐄𝐒𝐄𝐓
̅̅̅̅̅̅̅̅̅ initial or default state. When a low-level signal (logic 0) is applied to
21 𝐑𝐄𝐒𝐄𝐓
the ̅̅̅̅̅̅̅̅̅
𝐑𝐄𝐒𝐄𝐓 pin, it forces the 8086 CPU to restart its operation from the
beginning, clearing all registers.
READY signal is used by the peripherals and memory devices in order
to show the readiness for the next operation.
22 READY It is an active high signal. When it is high, it indicates that the device is
ready to transfer data. When it is low, it indicates wait state.
These are basically 3 status pins and are active low. This means that if
the status at all the 3 pins is 0 then it shows that multiple interrupts
are to be handled in maximum mode.
The table below is representing the status of the processor in
different combinations
̅̅̅̅
𝐒𝐨 ̅̅̅̅
𝐒𝟏 ̅̅̅̅
𝐒𝟐 Status
26 ̅̅̅̅ 0 0 0 Interrupt acknowledgement (INTA)
𝐒𝐨 , ̅̅̅̅
𝐒𝟏 & ̅̅̅̅
𝐒𝟐
to
0 0 1 I/O Read
28
0 1 0 I/O Write
0 1 1 Halt
1 0 0 Opcode fetch
1 0 1 Memory Read
1 1 0 Memory Write
1 1 1 None
Page | 78
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
The MN/𝐌𝐗 ̅̅̅̅̅ pin shows whether the 8086 microprocessor is
operating in the minimum mode or maximum mode.
̅̅̅̅̅̅
𝐁𝐇𝐄 is an acronym for Bus High Enable. The combination of the ̅̅̅̅̅̅
𝐁𝐇𝐄
signal and S7 status informs about the existence of the data on the bus.
Also, different combinations show whether the bus is containing
overall 16-bit, upper byte or lower byte of the data.
34 ̅̅̅̅̅̅/ S7
𝐁𝐇𝐄 ̅̅̅̅̅̅
𝐁𝐇𝐄 S7 Status
0 0 All 16-bit data will be accessed
0 1 Upper byte of the data will be accessed
1 0 Lower byte of the data will be accessed
1 1 None or Idle
35 A16/S3, Out of 20 address bits, 4 are present in the multiplexed form with the
to A17/S4, status signals. In the case of memory operations, these pins act as an
38 A18/S5 and address bus and contain the memory address of any particular
A19/S6 instruction or data.
However, from I/O operations these pins are low that shows the status
of the processor.
Basically, the signal at S3 and S4 show that which segment is currently
accessed by the microprocessor among the four segments present in
it.
S3 S4 STATUS
0 0 Extra Segment access
0 1 Stack segment access
1 0 Code segment access
1 1 Data segment
S5: when enabled, shows the presence of an interrupts in the
microprocessor. So, basically, it serves as an interrupt flag.
S6: Shows the status of the bus master for the current operation i.e.
Whether the 8086 is the bus master or any other proficient device is
acting as the bus master.
When S6= 0; it indicates the 8086 is holding the access of the bus
otherwise it is high i.e., 1.
40 VCC The external power supply of +5V is connected to the microprocessor
Page | 79
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
̅̅̅̅̅̅
𝐃𝐄𝐍 is used for data enable and this is an active low pin.
26 ̅̅̅̅̅̅
𝐃𝐄𝐍 ̅̅̅̅̅̅
𝐃𝐄𝐍0; then the transceiver gets enabled and it separates the data from
the multiplexed address and data bus.
The 8086 microprocessor has 20 lines address bus. With 20 address lines, the memory, allowing
it to address memory of up to 220 bytes i.e. 220= 1,048,576 bytes (1 MB). 8086 can access memory
with address ranging from 00000 H to FFFFF H.
Page | 80
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Explain the physical address, offset address and segment address in context to 8086.
AKTU Question Paper 2021-22, 2 MARKS
The starting (base) address and end address (top) of each segment are as below:
Segment Register:
The 8086 has four primary segment registers: CS (Code Segment), DS (Data Segment), SS (Stack
Segment), and ES (Extra Segment). Each of these registers holds a 16-bit value that represents
the base address of a specific segment in memory.
Offset Address:
Within each segment, memory is addressed using an offset value. The offset is a 16-bit value that
specifies the location of data or instructions within the segment. It's essentially a displacement
from the base address of the segment.
Physical Address:
The 20-bit address of a data or instructions is called its Physical Address i.e. a physical address
represents a specific location in the memory where data or instructions are stored. To calculate
the physical address of a memory location, the 8086 combines the value in the segment register
with the offset. This 20-bit physical address allows access to up to 1 MB of memory.
Page | 81
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Rules of Segmentation
The starting address of a segment should be such that it can be evenly divided by 16.
Minimum size of a segment can be 16 bytes and the maximum can be 64 kB.
Solution:
The offset of the CS Register is the IP register. Therefore, the effective address of the memory
location pointed by the CS register is calculated as follows:
Physical address = (Base address of CS register X 10H) + Address of IP
Physical address = 4042 X 10H + 0580
Page | 82
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
=40420+0580
Physical address =409A0H
Example 3: Calculate the effective address for the following register: SS: 3640H, SP: 1735H, BP:
4826H
Solution:
Both SP and BP are the offsets for Stack Register (SS). The address calculated when BP is taken as
the offset gives the starting address of the stack. The address when SP is taken as the offset
denotes the memory location where the top of the stack lies.
Therefore, the effective address for both these cases is:
(SS X 10H) + SP = 3640H X 10H + 1735H
= 36400H + 1735H
= 37B35H
(SS X 10H) + BP = 3640H X 10H + 4826H
= 36400H + 4826H
= 3AC26H
Example 4: The value of the DS register is 3032H. And the BX register contains a 16-bit value
which is equal to 3032H. 0008H is added to BX. ADD BX, 0008H, the register AX contains some
value which needs to be stored at a location as follows: MOV [BX], AX
Calculate the address at which the value of the AX will be stored
Solution:
After executing the first instruction, the value of BX Register is as follows:
BX = 303AH
The BX register is an offset of the Data Segment (DS) register. So, the location at which the value
of the AX register will be stored is calculated as follows:
(DS X 10H) + BX = 3032H X 10H +303AH
= 30320H + 303AH
= 3335AH
Example 5: You are provided the following values: DS: 3056H, IP: 1023H, BP: 2322H and SP:
3029H. Can you calculate the effective address of the memory location as per the DS register?
Solution:
No, the effective address of the DS register cannot be calculated from the given values because
none of the given offset is an offset of the DS Register.
Page | 83
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
3.1.5. Addressing Modes of 8086
Discuss the various addressing modes in 8085 along-with examples.
AKTU Question Paper 2020-21, 10 MARKS
Discuss the various addressing modes available in 8086 along-with examples.
AKTU Question Paper 2021-22, 10 MARKS
The Intel 8086 microprocessor supports various addressing modes, which determine how
operands (data) are accessed or addressed in instructions. These addressing modes provide
flexibility in how data is fetched or manipulated during program execution. The commonly used
addressing modes of the 8086 are as follows:
1 Immediate In this addressing mode, the operand is a constant value specified in the
Addressing: instruction itself.
Example: MOV AX, 1234; loads the value 1234 H into the AX register.
2 Register In this addressing mode, the operand is stored in one of the CPU registers.
Addressing: Example: ADD AX, BX; adds the value in the BX register to the AX
register.
3 Direct In this addressing mode, the operand's address is directly specified in the
Addressing: instruction.
Example: MOV AX, [1592H]
MOV AL, [0A00H]; moves the value at memory address 0A00H into the
AL register.
4 Register In this addressing mode addressing mode allows data to be addressed at
Indirect any memory location through an offset address held in any of the
Addressing: following registers: BP, BX, DI & SI.
Example: MOV AX, [BX]; Suppose the register BX contains 4895H, then
the contents 4895H are moved to AX
5 Based In this addressing mode, the offset address of the operand is given by the
Addressing: sum of contents of the BX/BP registers and 8-bit/16-bit displacement.
Example: MOV DX, [BX+04]
6 Based-index In this addressing mode, the operands offset address is found by adding
addressing the contents of SI or DI register and 8-bit/16-bit displacements.
mode: Example: MOV BX, [SI+16]
7 Based In this addressing mode, the operands offset is computed by adding the
indexed with base register contents and Index registers contents and 8 or 16-bit
displacement displacement.
mode: Example: MOV AX, [BX+DI+08]
8 Scaled Index In this addressing mode, an index register is multiplied by a scaling factor
Addressing and added to a base register to form an address.
Mode Example: MOV AL, [BX+SI*2] moves the value at the memory location
pointed to by (BX+SI*2) into the AL register.
Page | 84
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
3.1.6. EVOLUTION OF MICROPROCESSORS:
Explain the various generations of microprocessor.
AKTU Question Paper 2022-23, 10 MARKS
Page | 85
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
As technology continues to evolve, microprocessors are expected to become more powerful,
energy-efficient, and versatile, enabling a wide range of applications in both consumer and
industrial domains.
8086 ARCHITECTURE
Draw the architecture of 8086 Microprocessors and explain its all blocks.
AKTU Question Paper 2022-23, 10 MARKS
MEMORY SEGMENTATION
Discuss the memory segmentation in 8086 and the various segments of the memory.
AKTU Question Paper 2020-21, 2 MARKS
PHYSICAL ADDRESS
Explain the physical address, offset address and segment address in context to 8086.
AKTU Question Paper 2021-22, 2 MARKS
EVOLUTION OF MICROPROCESSORS
Explain the various generations of microprocessor.
AKTU Question Paper 2022-23, 10 MARKS
Page | 86
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
PERIPHERAL DEVICES
Peripheral Devices: 8237 DMA Controller, 8255 Programmable Peripheral Interface,
8253/8254 Programmable Timer/Counter, 8259 Programmable Interrupt Controller, 8251
USART and RS 232C.
With the help of a functional block diagram and working of 8257 DMA controller.
AKTU Question Paper 2022-23, 10 MARKS
Explain Direct Memory Access (DMA).
AKTU Question Paper 2022-23, 2 MARKS
Explain Direct Memory Access (DMA).
AKTU Question Paper 2020-21, 2 MARKS
Illustrate the process of DMA with the help of 8237 DMA controller.
AKTU Question Paper 2020-21, 10 MARKS
Direct Memory Access is a process where data is transferred between two peripherals
directly without the involvement of the microprocessor.
This process employs the 2 signals HOLD and HLDA pin on the microprocessor.
HOLD: This is an active high input signal to the 8085 microprocessor from another master
requesting the use of the address and data buses. After receiving the Hold request, the
microprocessor relinquishes the buses. The Hold Acknowledge (HLDA) signal is sent out. The
microprocessor regains the control of the buses after HOLD goes low.
HLDA (Hold Acknowledge): This is an active high output signal indicating that the
microprocessor is relinquishing control of the buses.
A DMA controller uses these signals as if it were a peripheral requesting the MPU for the control
of the buses. The MPU communicates with the controller by using the Chip Select line, buses, and
control signals. Once the controller has gained control, it plays the role of a processor for data
transfer.
To perform data transfer operation, the DMA should have the following
1 a data bus,
2 an address bus,
3 Read/Write control signals, and
4 control signals to disable its role as a peripheral and to enable its role as a processor.
This process is called switching from the slave mode to the master mode.
Page | 87
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
DMA CHANNEL AND INTERFACING:
Figure 3.7 shows a logical pin out and internal registers of the 8237A and interfacing with the
8085 using a 3-to-8 decoder.
The 8237 has four independent channels, CHO to CH3. Internally, two 16-bit registers are
associated with each channel: One is used to load a starting address of the byte to be copied
and the second is used to load a count of the number of bytes to be copied.
Figure 3.7 shows eight such registers that can be accessed by the MPU. The addresses of these
registers are determined by four address lines, A3 to A0, and the Chip Select (CS) signal.
Page | 88
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Address lines Hex
Selected Channels/Register etc.
A3 A2 A 1 A0 Address
0000 00 H CHO Memory Address Register (MAR)
0001 01 H CH0 Count Register
0010 02 H CH1 Memory Address Register (MAR)
0011 03 H CH1 Count Register
.
.
.
The address 0000 on lines A3 to A0 selects CHO Memory Address Register (MAR) and address
0001 selects the next register, CH0 Count. Similarly, all the remaining registers are selected in
sequential order. The last eight registers are used to write commands or read status as
shown in figure 3.7, the MPU accesses the DMA controller by asserting the signal Y_0 of the
decoder. Therefore, the addresses of these internal registers range from 00 to 0FH as follows:
The signals that are necessary to understand the DMA operations are explained as follows:
CLK: Clock input to 8237. The maximum clock frequency is 5 MHz. In the 8085 system, the
processor clock is inverted and applied to CLK 8237.
CS: Logic low chip select signal. It is an input signal to select 8237 during programming mode.
RESET : Reset input to 8237. Connected to a system reset, when the RESET signal
goes high the command, status, request, and temporary registers are
cleared. It also clears the first-last flip-flop and sets the mask register.
READY : Ready input signal and it is tied to VCC for normal timings. When READY
input is tied low, the 8237 enters a wait state. This is used to get extra time
in DMA machine cycles to transfer data between slow memory and IO
devices.
HRQ : Hold request output signal. It is the hold request signal sent by 8237 to the
processor HOLD pin, to make a request for the bus to perform a DMA
transfer.
HLDA : Hold acknowledge input signal. It is the hold acknowledge signal to be sent
by the processor to inform the acceptance of the hold request.
DREQ3 – : These are the four independent, asynchronous input signals to the DMA
DREQ0 channels from peripherals such as floppy disks and the hard disk. Used by
IO devices to request for DMA to transfer.
DACK3 – : These are output lines to inform the individual peripherals that a DMA is
DACK0 granted. DREQ and DACK are equivalent to handshake signals in I/O
devices.
DB7 – DB0 : Data bus lines. These pins are used for data transfer between the processor
and DMA.
Page | 89
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
̅̅̅̅̅
𝐈𝐎𝐑 : Bidirectional IO read control signal. It is an input control signal for reading
the DMA controller during programming mode and an output control signal
for reading the IO device during DMA (memory) write cycle.
̅̅̅̅̅̅
𝐈𝐎𝐖 : Bidirectional IO writes control signal. It is an input control signal for writing
the DMA controller during programming mode and an output control signal
for writing the IO device during DMA (memory) read cycle.
EOP : End of process. It is a bidirectional low active signal. It is used either as an
input to terminate a DMA process or as an output to inform the end of the
DMA transfer to the processor. This output can be used as an interrupt to
terminate DMA.
A3 – A0 : Four bidirectional address lines. Used as input address during
programming mode to select internal registers. During DMA mode the low-
order four bits of memory address are output by 8237 on these lines.
A7 – A4 : Four unidirectional address lines. Used to output the memory address bits
A7 to A4 during DMA mode.
AEN : Address enable output signal. It is used to enable the address latch
connected to DB7 – DB0 pins of 8237. It is also used to disable any buffers in
the system connected to the processor.
ADSTB : Address strobe output signal. It is used to latch the high-byte memory
address issued through DB7 to DB0 lines by 8237 during DMA mode into an
external latch.
̅̅̅̅̅̅̅̅
𝐌𝐄𝐌𝐑 : Memory read control signal. It is an output control signal issued during a
DMA read operation.
̅̅̅̅̅̅̅̅̅
𝐌𝐄𝐌𝐖 : Memory write control signal. It is an output control signal issued during
DMA write operation.
Page | 90
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
3.2.4 INTERFACING 8237A DMA CONTROLLER WITH THE 8085:
To implement the DMA transfer, the 8237 should be initialized by writing into various control
registers discussed earlier in the DMA channels and interfacing section.
Page | 91
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
To initialize the 8237, the following steps are necessary.
1 Write a control word in the Mode register that selects the channel and specifies the type
of transfer (Read, Write, or Verify) and the DMA mode (block, single-byte, etc.).
2 Write a control word in the Command register that specifies parameters such as priority
among four channels, DREQ and DACK active levels, and timing, and enables the 8237.
3 Write the starting address of the data block to be transferred in the channel Memory
Address Register (MAR).
4 Write the count (the number of the bytes in the data block) in the channel Count register.
5 The starting address of the data block is 4075H and subsequent data bytes have memory
addresses in increasing order.
6 The Command parameters should be: normal timing. fixed priority, late write. DREQ and
DACK are both active low.
7 Set up the demand mode whereby the DMA can complete the data transfer without any
interruption.
Page | 92
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
3.3 8255 PROGRAMMABLE PERIPHERAL INTERFACE:
Explain the pin diagram of 8255 along-with the block diagram.
AKTU Question Paper 2020-21, 10 MARKS
Explain the CWR of 8255 Programmable Peripheral Interface and also discuss the BSR
mode. AKTU Question Paper 2022-23, 2 MARKS
With the help of a functional block diagram explain the organization and working of 8255
microprocessor. AKTU Question Paper 2022-23, 2 MARKS
PPI 8255 is a general purpose programmable I/O device designed to interface the
microprocessor with its outside world such as ADC, DAC, keyboard etc.
Page | 93
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
1. DATA BUS BUFFER:
The 8-bit bidirectional data bus buffer is used to interface the 8255 internal data bus with
the system data bus (Microprocessor).
The direction of the data buffer is decided by read and write control signals.
When the read is activated, it transmits data to the system data bus.
When a write is activated, it receives data from the system data bus.
4. PORT C
Port C consists of an 8-bit bidirectional data output latch/buffer and an 8-bit data input
buffer.
It is divided into 2 sections, Port C upper PCU and Port C lower PCL. These two sections
can be programmed and used separately as a 4-bit I/O port.
It can be used as
i. Simple I/O
ii. Handshake signals
iii. Status signal inputs.
Port C is used in combination with port A & Port B for both the status and handshaking
signals.
Page | 94
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
BSR (BIT SET RESET MODE):
The content of the control word register will be as follows, when used in the BSR mode
Page | 95
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
If MSB of Control Word Register (D7) is 1, PPI works in input-output mode. This is further divided
into three modes:
1. Mode 0: Input / Output mode
2. Mode 1: Input / Output with handshaking
3. Mode 2: Bidirectional I/O port with handshaking
Mode 0:
This is a basic input/output mode, whose features are:
All the three ports (Port A, Port B & Port C) can be programmed in either input or output
mode.
Ports don’t have handshake or interrupt capability
Mode 1:
In this mode, input or outputting of data is carried out by taking the help of handshaking
signals, also known as strobe signals. The basic features of this mode are:
Ports A and B can function as 8-bit I/O ports, taking the help of pins of Port C.
I/Ps and O/Ps are latched.
Interrupt logic is supported.
Handshake signals are exchanged between CPU and peripheral prior to data transfer.
In this mode, Port C is called status port.
There are two groups in this mode Group A and Group B. They can be configured
separately. Each group consists of an 8-bit port and a 4-bit port. This 4-bit port is used for
handshaking in each group.
Mode 2:
In this mode, the ports can be utilized for the bidirectional flow of information by
handshaking signals.
The pins of group A can be programmed to acts as bidirectional data bus and the Port C
upper (PC7 – PC4) are used by the handshaking signal. The rest 4 lower Port C bits are
utilized for I/O operations.
Port B can be programmed in mode 0 & 1 and in mode 1 the lower bits of Port C of group
B are used for handshaking signals.
It also has interrupt handling capacity.
Page | 96
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
These are 8-bit bi-directional data bus lines, connected to the system
D0-D7 Data Bus
data bus for data transfer between CPU and 8255.
It is the signal used for read operation. A low signal at this pin shows
̅̅̅̅
𝐑𝐃 Read that CPU is performing read operation at the ports or status word
through data buffer.
It is the signal used for write operation. A low signal at this pin allows
̅̅̅̅̅
𝐖𝐑 Write the CPU to perform write operation over the ports or control register
of 8255 using the data bus buffer.
Address These are basically used to select the desired port among all the
A0-A1
lines ports of the 8255. i.e. Port A, Port B, Port C, and Control register.
This is an active HIGH input signal used to reset 8255. When 8255 is
RESET Reset reset, it clears the control word register and all ports are set to input
mode.
These are 8-bit bidirectional I/O pins used to send data to the
PA0- Port A pins
peripheral or to read data from the peripheral. The contents are
PA7 0 to 7
transferred to/from Port A.
Port B pins
PB0-PB7 These are 8-bit bidirectional I/O pins used the same as PA0-PA7
0 to 7
These are 8-bit bidirectional I/O pins. These lines are divided into 2
Port C pins sections i.e. PC0-PC3 and PC4-PC7. These two sections can be
PC0-PC7
0 to 7 individually used to transfer 4 bits of data from two separate port C
sections.
Demonstrate the architecture of 8253/54 Programmable Timer and discuss the control
word register.
AKTU Question Paper 2021-22, 10 MARKS
Name the 06 operating modes of 8254.
AKTU Question Paper 2022-23, 2 MARKS
The Intel 8253 Programmable Interval Timer (PIT) is timing device commonly used in
microprocessor-based systems to generate precise time delays and control events. It provides
a reliable and accurate timing reference for various applications.
The Intel 8253 and 8254 are Programmable Interval Timers (PTIs) designed for microprocessors
to perform timing and counting functions using three 16-bit counters each capable of handling
clock inputs up to 10 MHz. Each counter has 2 input pins, i.e. Clock & Gate, and 1 pin for “OUT”
output. To operate a counter, a 16-bit count is loaded in its register. On command, it begins to
decrement the count until it reaches 0, then it generates a pulse that can be used to interrupt
the CPU.
Page | 97
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
In the above figure, there are three counters, a data bus buffer, Read/Write control logic, and a
control register. Each counter has two input signals - CLOCK & GATE, and one output signal - OUT.
Read/Write Logic:
It includes 5 signals, i.e. RD, WR, CS, and the address lines A0 & A1. In the peripheral I/O mode,
the RD and WR signals are connected to IOR and IOW, respectively. In the memory mapped I/O
mode, these are connected to MEMR and MEMW. Address lines A0 & A1 of the CPU are connected
to lines A0 and A1 of the 8253/54, and CS is tied to a decoded address. The control word register
and counters are selected according to the signals on lines A0 & A1.
̅̅̅̅ A1 A0 OPERATION
𝐂𝐒
0 0 0 Counter 0
0 0 1 Counter 1
0 1 0 Counter 2
0 1 1 Control Word Register
COUNTERS:
Each counter consists of a single, 16 bit-down counters, which can be operated in either binary
or BCD. Its input and output are configured by the selection of modes stored in the control word
register. The programmer can read the contents of any of the three counters.
Page | 98
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Control Word Register:
This register is accessed when lines A0 & A1 are at logic 1. It is used to write a command word,
which specifies the counter to be used, its mode, and either a read or write operation. Following
table shows the result for various control inputs.
WRITE Operation:
Write a control word into control register.
Load the low-order byte of a count in the counter register.
Load the high-order byte of count in the counter register.
READ OPERATION:
1. Simple Read:
It involves reading a count after inhibiting the counter by controlling the gate input or the
clock input of the selected counter, and two I/O read operations are performed by the
CPU. The first I/O operation reads the low-order byte, and the second I/O operation reads
the high order byte.
Page | 99
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
5. Mode4(Software Triggered Strobe)
6. Mode 5 (Hardware Triggered Strobe)
The Description and operation of various modes of timer are depicted in the figures below.
This is used for event counting. After writing the control word, OUT is low at first. It will remain
low until the counter reaches 0, it is decremented by 1 after each clock cycle. Then the OUT
goes high, and remains high until a new count is there or a new Mode 0 control word is written
into the counter. The GATE=1 indicates enable counting, and 0 indicates disable counting.
Page | 100
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
OUT will be high at first, it will go low on the clock pulse following a trigger to begin the one-
shot pulse. It will remain 0 until the counter reaches 0.
Page | 101
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Initially OUT is low. When the counting is enabled, it goes HIGH. This process repeats
periodically. This mode is used as frequency divided.
If the GATE is 1, then the counting is enabled, otherwise it is disabled. This mode is used to
generate the square wave. The time period is equal to count. If the count is even, the on-time of
wave is count/2. Otherwise on-time is (count+1)/2 and off-time is (count-1)/2.
Page | 102
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
If the GATE is 1, then the counting is enabled, otherwise it is disabled. Initially OUT value is
high and go low when count is at the last stage. The count is reloaded again for subsequent
clock pulse.
Page | 103
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Initially OUT value is high. The counting is triggered by the rising edge of the Gate (Clock pulse).
When initial count is expired the OUT becomes low for one clock pulse, then high again. After
writing the control word and the initial count, the counter will not be loaded until clock pulse
after one trigger.
Intel 8253 is a 24 Pin programmable IC. It has three counters which work independently and
whose width is of 16-bits.
VCC and GND These are the Power supply and ground pins which 8253 uses +5V as power
supply
D7-D0 These are 8-bit bidirectional data bus lines, connected to the system data bus
for data transfer between 8085 and 8254.
̅̅̅̅
RD ̅̅̅̅=0, the microprocessor reads the data from the
It is active low pin. When 𝐑𝐃
Selected counter
Page | 104
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
̅̅̅̅̅
WR It is active low pin. When ̅̅̅̅̅ 𝐖𝐑=0, the microprocessor writes into
counter/control register i.e. writes control information/loading of counters.
A1 & A0 A1 & A0 pins are connected to the address bus. These pins are used for the
selection of counters and control word register.
A1 A0 OPERATION
0 0 Counter 0
0 1 Counter 1
1 0 Counter 2
1 1 Control Word Register
̅̅̅
CS ̅̅̅= 0 then 8254
This is an active low input signal, used to select the 8254 IC. If CS
will be active and take part in data transfer from/to 8085 otherwise 8254 will
be in the de-active state.
Gate0 Controls function of counter 0 (i.e. gate terminals for triggering purpose)
Gate1 Controls function of counter 1 (i.e. gate terminals for triggering purpose)
Gate2 Controls function of counter 2 (i.e. gate terminals for triggering purpose)
FEATURES OF 8253/54:
The most prominent features of 8253/54 are as follows:
It has three independent 16-bit down counters.
It can operate from DC up to 10MHz.
The three counters can be programmed for either binary or BCD count.
It is compatible with almost all microprocessors.
8254 has a powerful command called READ BACK command, which allows the user to
check the count value, the programmed mode, the current mode, and the status of the
counter.
Counters can be programmed in six different modes.
Page | 105
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
3.4.2 DIFFERENCE BETWEEN 8253 AND 8254:
Sl.
8253 8254
No.
1 Its operating frequency is 0 - 2.6 MHz Its operating frequency is 0 - 10 MHz
2 It uses N-MOS technology It uses H-MOS technology
3 Read-Back command is not available Read-Back command is available
Reads and writes of the same counter Reads and writes of the same counter can
cannot be interleaved. be interleaved.
(cannot perform read and write (interleaving reads and writes of the
4
operations on the same counter same counter is a synchronization
simultaneously or in an alternating technique that allows multiple tasks to
fashion) access and manipulate a counter)
Page | 106
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
INTERRUPT REQUEST REGISTER (IRR): The interrupts at IRQ input lines are handled by
Interrupt Request Register internally. IRR stores all the interrupt requests in it in order to serve
them one by one on the priority basis.
IN-SERVICE REGISTER (ISR): This register stores all the interrupt requests those are being
served, i.e. ISR keeps a track of the requests being served.
PRIORITY RESOLVER: This unit determines the priorities of the interrupt requests appearing
simultaneously. The highest priority is selected and stored into the corresponding bit of ISR
during INTA pulse. The IR0 has the highest priority while the IR7 has the lowest one, normally in
fixed priority mode. The priorities however may be altered by programming the 8259 in rotating
priority mode.
INTERRUPT MASK REGISTER (IMR): This register stores the bits required to mask the interrupt
puts. IMR operates on IRR at the direction of the Priority Resolver.
INTERRUPT CONTROL LOGIC: This block manages the interrupt and interrupt acknowledge
signals to be sent to the CPU for serving one of the eight interrupt requests. This also accepts
interrupt acknowledge (INTA) signal from CPU that causes the 8259 to release vector address on
to the data bus.
DATA BUS BUFFER: This tristate bidirectional buffer interfaces internal 8259 bus to the
microprocessor system data bus. Control words, status and vector information pass through
buffer during read or write operations.
READ WRITE CONTROL LOGIC: This circuit accepts and decodes commands from the CPU. This
also allows the status of the 8259 to be transferred on to the data bus.
CASCADE BUFFER/COMPARATOR: This block stores and compares the ID's of all the 8259 used
in the system. The three I/O pins CAS0-2 are outputs, when the 8259 is used as a master. The
same pins act as inputs when the 8259 is in slave mode. The 8259 in master mode sends the ID
of the interrupting slave device on these lines. The slave thus selected, will send its pre-
programmed vector address on the data bus during the next INTA pulse.
INTERRUPT SEQUENCE:
The powerful features of the 8259 in a microcomputer system are its programmability and the
interrupt routine addressing capability. The latter allows direct or indirect jumping to the specific
interrupt routine requested without any polling of the interrupting devices. The normal sequence
of events during an interrupt depends on the type of CPU being used. The events occur as follows
in an 8085 system:
1. One or more of the INTERRUPT REQUEST lines (IR7-0) are raised high, setting the
corresponding IRR bit(s).
Page | 107
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
2. The 8259 evaluates these requests, and sends an INT to the CPU, if appropriate.
3. The CPU acknowledges the INT and responds with an INTA pulse.
4. Upon receiving an INTA from the CPU group, the highest priority ISR bit is set, and the
corresponding IRR bit is reset. The 8259 will also release a CALL instruction code
(11001101) onto the 8-bit Data Bus through its D7-0 pins.
5. This CALL instruction will initiate two more INTA pulses to be sent to the 8259A from the
CPU group.
6. These two INTA pulses allow the 8259 to release its preprogrammed subroutine address
onto the Data Bus. The lower 8-bit address is released at the first INTA pulse and the
higher 8-bit address is released at the second INTA pulse. 7. This completes the 3-byte
CALL instruction released by the 8259. In the AEOI mode the ISR bit is reset at the end of
the third INTA pulse.
7. Otherwise, the ISR bit remains set until an appropriate EOI command is issued at the end
of the interrupt sequence.
When the 8259 PIC receives an interrupt, INT becomes active and an interrupt acknowledge cycle
is started. If a higher priority interrupt occurs between the two INTA pulses, the INT line goes
inactive immediately after the second INTA pulse. After an unspecified amount of time the INT
line is activated again to signify the higher priority interrupt waiting for service. This inactive
time is not specified and can vary between parts.
Page | 108
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Bi-directional, tristate, buffered data lines. Connected to data bus directly or
D0-D7
through buffers
̅̅̅̅
RD Active low read control
̅̅̅̅̅
WR Active low write control
A0 Address input line, used to select control register
̅̅̅
CS Active low chip select
CAS0-2 Bi-directional,3-bit cascade lines. In master mode, PIC places slave ID no. on
these lines. In slave mode, the PIC reads slave ID no. from master on these lines.
It may be regarded as slave- select.
̅̅̅/EN
SP ̅̅̅̅ Slave program / enable. In non-buffered mode, it is SP-bar input, used to
distinguish master/slave PIC. In buffered mode, it is output line used to enable
buffers.
INT Interrupt line, connected to INTR of microprocessor
̅̅̅̅̅̅̅
INTA Interrupt ack, received active low from microprocessor
IR 0-7 Asynchronous IRQ input lines, generated by peripherals.
1.Data Bus Buffer: It basically interfaces the 8251 with the internal system buses of the
processor.
The data bus buffer has 8-bit bidirectional data bus that allows the transfer of data bytes, status
or command word between the processor and external devices.
2.Read/Write Control Logic: This functional unit generates a control signal for the operation of
8251 according to the signal present in the control bus of the processor. Basically, it performs
decoding operation of the control signal produced by the processor, so that respective operation
can be performed by the USART.
The control formats for system operation is stored in control and command word registers
present in the read/write logic unit.
Page | 109
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
The signals handled by the read/write control logic unit are discussed below:
CS: It is chip select. A low signal at this pin shows that processor has selected 8251 in order
to communicate with the peripheral devices.
C/D: As the system has control, status and data register. So, when a high signal is present at
this pin then control or status register is addressed. While in case of low signal data register
is addressed.
RD and WR: Both read and write are active low signal pins. A low signal at RD shows that the
processor is reading the control, status or data bytes from the 8251. While at WR indicates
the write operation over the data bus of 8251.
CLK and RESET: CLK stands for clock and it produces the internal timing for the device. While
an active high signal at the RESET pin puts the 8251 in the idle mode.
3. Transmit Buffer: This unit is used to change the parallel data received from the CPU into serial
data by inserting the necessary framing information. Once the data is transformed into serial
form, then in order to transmit it to the external devices, it is provided to the TxD pin of the 8251.
This unit consists of 2 registers. These are as follows:
Buffer register: Basically, the data provided by the processor is stored in the buffer register.
As we know that initially, the CPU provides parallel data to 8251. So, the processor loads the
parallel data to the buffer register. Further, this data is fed to the output register.
Output register: The parallel data from the buffer register is fed to the empty output register.
This register changes the 8-bit parallel data into a stream of serial bits. Then further the serial
data is provided at the TxD pin so as to have its transfer to the peripheral device.
It performs both synchronous and asynchronous transmission and reception. Thus, in case of
asynchronous transmission, start and stop bit is added by the transmitter in order to notify the
external devices about the data transmission.
But in case of synchronous transmission, the clock signal is used thus there exists no need of
adding additional bits expect the parity bit (if required).
4. Transmit Control: As the name of the unit is itself indicating that it is controlling the
transmission action. And it does so by accepting and sending signals both externally and
internally.
The various control signal generated by this unit are as given below:
TxRDY: It implies transmit ready. This signal is used to notify the processor that the buffer
register of the 8251 is empty and ready to accept the data.
The status read operation is utilized by the processor in order to check the presence of the
signal.
TxE: This stands for transmitter empty. It is an active high signal that indicates that the output
buffer is empty and thus data received from the processor can be loaded to it for conversion.
TxC: It stands for transmitter clock and is an active low pin. It controls the rate of character
transmission by the USART.
However, 8251 offers programmable clock rate. As by writing appropriate mode word in the
mode set register the clock division can be programmed.
5. Receive Buffer:
This unit takes the serial data from the external devices, changes the serial data into the
parallel form so that it can be accepted by the processor. It consists of 2 registers: receiver
input register and buffer register.
When the external device is ready to send the data to the 8251 then it sends a low signal
to the RxD line of the 8251. In asynchronous mode, once 8251 receives a low signal it
considers that signal as start bit of the data.
So, once the start bit is successfully accepted by 8251, then it also receives the whole data
bits in serial form along with parity and stop bits.
Once the data is received by the receiver input register then it converts the data bits in
parallel form and sends it to the receiver buffer register.
In case of the synchronous mode of operation, according to the clock input, the external
device loads the serial data bits in the receiver input register. And on converting the serial
data to parallel format the receiver input register sends the data to the buffer register.
Page | 110
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
6. Receiver Control
This unit controls the operation of the receiver buffer. It manages the data reception, along with
that it also detects the presence of false start bit, error in parity bit, framing errors etc.
RxRDY: It stands for receiver ready. When this signal goes high then it indicates that the
receiver buffer register is holding the data and is ready to transfer it to the processor. Once
the CPU reads the data sent by the 8251 then this pin is reset.
RxC: It stands for receiver clock. This clock signaling controls the rate at which the 8251
receives the data in the synchronous mode of operation. It is provided by the modem and is
equal to the baud rate. While asynchronous mode offers the clock rate as 1, 16 or 64 times of
the baud rate as it is programmable.
7. Modem Control: This unit of 8251 holds input and output control signals that simplify the
operation of the whole system. The control circuitry for handing various signals is provided by
the modem control unit. It includes DTS, RTS, DTR and CTS.
These are all active low signals.
DSR: Stands for data set ready and the signal is used to check whether the data set is ready or
not when the processor is in the urge of communication.
DTR: Implies data terminal ready. An active-low signal at this pin shows that the 8251 is now
ready to accept the data from the processor.
RTS: It stands for the request to send. A low signal shows an assertion for data transmission.
CTS: Clear to send. When 8251 receives a low signal at this pin then it clears all the data
present in the modem in order to allow further communication.
3.7 RS232C
Pin Description
1 Data carrier detect (DCD)
2 Received data (RxD)
3 Transmitted data (TxD)
4 Data terminal ready (DTR)
5 Signal ground (GND)
6 Data set ready (DSR)
7 Request to send (RTS)
8 Clear to send (CTS)
9 Ring indicator (Rl)
RS232 is an Interface and the protocol between DTE (data terminal equipment) and DCE (data
communication equipment) using serial binary data exchange. Here C is used for the current
version. Universal Asynchronous Data Receiver & Transmitter (UART), attached in a
motherboard, used in connection with RS232 for transmitting data to any serial device like
modem or printer from its DTE interface.
Page | 111
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
Figure: RS 232C
3.7.1 ELECTRICAL SPECIFICATIONS:
1. Voltages:
There can be two states in the signal level of RS232C pins.
Mark state – It is the high bit which is represented by binary 1 and have negative
voltages. Its voltage limits for transmitting signal ranges from -5 to -15V. Its voltage
limits for receiving signals ranges from -3 to -25V.
Space state – It is the low bit which is represented by binary 0 and have positive
voltages. Its voltage limits for transmitting signal ranges from +5 to +15V. Its voltage
limits for receiving signals ranges from +3 to +25V.
4. Current:
Maximum current rating is 3Amps at the maximum operating voltage of 250V AC.
Page | 112
Microprocessor & Microcontroller Unit-3: 16-bit Microprocessor and Peripheral devices
RS232C requires 25 pins connector for connecting DTE and DCE. Here is the list of pins and
signals of RS232C and the connection between DTE and DCE using drivers and receivers.
1. Data Carrier Detect: After a data terminal is detected, a signal is sent to the data set that is
going to be transmitted to the terminal.
2. Received Data: The data set receives the initial signal via the receive data line (RxD).
3. Transmitted Data: The data terminal gets a signal from the data set, a confirmation that
there is a connection between the data terminal and the data set.
4. Data Terminal Ready: A positive voltage is applied to the data terminal ready (DTR) line, a
sign that the data terminal is prepared for the transmission of data.
5. Signal Ground: A return for all the signals on a single interface, the signal ground (SG) offers
a return path for serial communications. Without SG, serial data cannot be transmitted
between devices.
6. Data Set Ready: A positive voltage is applied to the data set ready (DSR) line, which ensures
the serial communications between a data terminal and a data set can be completed.
7. Request to Send: A positive voltage indicates the request to send (RTS) can be performed,
which means the data set is able to send information to the data terminal without
interference.
8. Clear to Send: After a connection has been established between a data terminal and a distant
modem, a clear to send (CS) signal ensures the data terminal recognizes that communications
can be performed.
9. Ring Indicator: The ring indicator (RI) signal will be activated if a modem that operates as
a data set detects low frequency. When this occurs, the data terminal is alerted, but the RI
will not stop the flow of serial data between devices.
HANDSHAKING:
Before the actual data transfer, signals are transmitted from DTE to DCE in order to make
connections by a process known as handshaking. Following is the sequence of signal
handshaking:
Initially, the computer activates RTS signal to modem when a data is transferred from
computer to modem.
Modem in turn activates the DCD and then the CTS gets activated.
Computer then sends data on TXD. After the data transmission is completed, the computer
deactivates the RTS which causes the modem to deactivate CTS.
APPLICATIONS:
It is used in establishing communication between the computer and embedded systems.
1. Due to its lower costs, it plays a vital role in CNC machines and servo controllers
2. Some microcontroller boards and PLC machines uses RS232C.
3. RS232C ports are used to communicate in headless systems in the absence of any network
connection.
4. Many Computerized Numerical Control Systems are containing RS232C port.
Page | 113
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
UNIT
8051 Microcontroller Basics
SYLLABUS
8051 Microcontroller Basics: Inside the Computer, Microcontrollers and Embedded Processors,
Block Diagram of 8051, PSW and Flag Bits, 8051 Register Banks and Stack, Internal Memory
Organization of 8051, IO Port Usage in 8051, Types of Special Function Registers and their uses in
8051, Pins Of 8051. Memory Address Decoding, 8031/51 Interfacing with External ROM And RAM.
8051 Addressing Modes.
The microprocessor mainly contains CPU and general-purpose registers. It does not have
built-in RAM, ROM, I/O ports etc. on the chip.
The microprocessors are commonly referred to as general-purpose microprocessor.
Examples:
Intel: 8086, 80286, 80386, 80486, Pentium etc.
Motorola: 68000, 68010, 68020, 68030 etc.
4.1 MICROCONTROLLER:
Page | 114
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
A Microcontroller is a single integrated circuit (IC) that is capable of performing
specific task (e.g. washing machine, toy, Telephone, Printer, Video game etc.
Microcontroller has a CPU (a microprocessor) and in addition it has built-in RAM, ROM,
Input/output devices, Timers/Counters on a single chip.
Examples: 8051, 8052, ARM processor etc.
Note:
7 Access time for memory & I/o devices are Less access time for built – in memory & I/o
more. devices.
8 Large number of instructions set. Limited number of instructions set.
9 Few pins are multifunctional. More number of pins are multifunctional.
10 Very few bit handling instructions Many bit handling instructions
11 Design is very flexible Design is less flexible
12 Versatile. Not versatile.
Page | 115
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
13 High cost Low cost
14 General-purpose applications. Single-purpose applications in which cost,
space & power are critical.
15 Examples: Intel: 8086, 80286, 80386, Examples:8051, 8052, ARM processor, PIC
80486, Pentium etc. Motorola: 68000, controllers etc.
68010, 68020, 68030 etc.
Page | 116
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Amount of RAM & ROM on chip
Power consumption
The number of input pins & the timer on the chip
Cost per unit
Easy to upgrade
Packaging (The number of pins & the packaging format. This determines the required
space & assembly layout.)
Page | 117
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
The ALU can perform arithmetic functions on 8-bit data i.e. addition, subtraction,
multiplication & division.
Similarly, the logic unit perform logical operations such as AND, OR, NOT etc.
Register:
Register are used to store information temporarily, while the information could be
a byte of data to be processed, or
an address pointing to the data to be fetched
The majority of 8051 register are 8-bit registers. The most widely used registers are
Accumulator (A), for all arithmetic and logic instructions.
B, R0, R1, R2, R3, R4, R5, R6, R7.
DPTR (data pointer), and PC (Program Counter).
A register (Accumulator):
Accumulator is a 8 bit register & is widely used for arithmetic and data transfer
operations.
The arithmetic operations are addition, subtraction, multiplication, division & Boolean bit
manipulating etc.
The data transfer operation between the 8051 microcontroller and any external memory.
Note: It can be accessed through its SFR address of 0E0H.
B-register:
The B-register is always used with the A-register to store 8-bit result of multiplication &
division operations
It is used as temporary register where data may be stored.
The 8051 has two 16-bit timers/counters, they can be used either as
Timers to generate a time delay or as
Event counters to count events happening outside the microcontroller.
The two timers are
Page | 118
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
i) Timer/Counter T0 and
ii) Timer/Counter T1
Each register can be used either for Timer or counter and can be divided into Two 8-bit
registers called Timer Low (TL) and Timer High (TH).
4.2.1 STACK (8-bit)
Explain the organization of stack in 8051.
AKTU Question Paper 2021-22, 2 MARKS
The stack is a section of RAM used by the CPU to store information temporarily. This
information could be data or an address.
The register used to access the stack is called the SP (stack pointer) register. The stack
pointer in the 8051 is only 8 bits wide
The storing of a CPU register in the stack is called a PUSH, and loading the contents of the
stack back into a CPU register is called a POP.
Memory Organization:
The 8051 microcontroller's memory is divided into
1) Internal RAM – 128 bytes: Used for temporarily storing and keeping intermediate
results and variables.
2) ROM – 4Kbytes: Used for permanent saving program being executed
Special Function Registers (SFR):
The operations of 8051 are done by a group of specific internal registers, each called a special
function Register (SFR).
Interrupts:
The 8051 Microcontroller has 6 interrupts:
RESET, INT0, INT1, Timer0 (TF0), Timer1 (TF1), Serial Port (TI/RI)
CY AC F0 RS1 RS0 OV - P
D7 D6 D5 D4 D3 D2 D1 D0
Page | 119
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
The program status word (PSW) register is an 8-bit register. It is also referred to as the
flag register. The only 6 bits are used by the 8051. The two unused bits are user-definable
flags.
The Carry (CY), Auxiliary carry (AC), Overflow (OV) and Parity (P) are called Conditional
flags because these flags indicate some conditions that resulted after an instruction was
executed.
Auxiliary carry Flag (AC): After performing arithmetic & logic operation if a carry from D3 to D4
bit then AC = 1, otherwise AC = 0.
RS 1 & RS 0: These two bits are used to change Register Bank and are shown below in table.
Table: Register Bank Selector
RS1 RSO Register Bank Address
0 0 Bank 0 00H-07H
0 1 Bank 1 08H-0FH
1 0 Bank 2 10H-17H
1 1 Bank 3 18H-1FH
Example 4.1
Show the status of the CY, AC and P flag after the addition of 38H and 2FH in the following
instructions.
MOV A, #38H
ADD A, #2FH ; after the addition A=67H, CY=0
Solution:
1 1 1 1
38 0 0 1 1 1 0 0 0
Page | 120
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
+ 2F + 0 0 1 0 1 1 1 1
67 1 0 1 1 0 0 1 1 1
Example 4.2
Show the status of the CY, AC and P flag after the addition of 9CH and 64H in the following
instructions.
MOV A, #9CH
ADD A, #64H ; after the addition A=00H, CY=1
Solution:
1 1 1 1 1 1
9C 1 0 0 1 1 1 0 0
+ 64 + 0 1 1 0 0 1 0 0
1 00 1 0 0 0 0 0 0 0 0
Example 4.3
Show the status of the CY, AC and P flag after the addition of 88H and 93H in the following
instructions.
MOV A, #88H
ADD A, #93H ; after the addition A=1BH, CY=1
Solution:
1
66 1 0 0 0 1 0 0 0
+ 93 + 1 0 0 1 0 0 1 1
1 1B 1 0 0 0 1 1 0 1 1
Page | 121
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.2.3 SPECIAL FUNCTION REGISTER (SFR):
The functions of any 5 SFR can be explained i.e. Accumulator, PSW, B register, Ports, Timers, DPTR
etc.
Page | 122
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.3 MEMORY ORGANIZATION:
Explain the internal memory organization of the microcontroller 8051.
AKTU Question Paper 2020-21, 10 MARKS
1. Internal RAM
The internal data memory consists of 256 bytes; these are divided into two parts:
i) Internal data RAM- 00H-1FH (128 bytes)
ii) Special function registers- 80H-FFH (128 bytes)
Page | 123
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
3) Scratch pad area
Page | 124
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
The area of bit addressable RAM is usually used to store bit variables.
The address ranges from 20h to 2Fh (16 bytes) is bit-addressable RAM. Each bit can be
accessed from 00H to 7FH.
The total bit addressable location are 16 bytes x 8 bits = 128 bits.
Each bit can be accessed from 00H to 7FH.
The programming using bit addressable area saves wastage of memory.
Note:
For example, Bit 0 of byte 20h has the bit address 0, and bit 7 of byte 2Fh has the bit address 7Fh).
Page | 125
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
External data memory is 64 K Bytes read/write memory.
The external data memory is indirectly accessed through a Data Pointer Register, it is
slower than access to internal data memory.
PROGRAM MEMORY:
The Program Memory is used for permanent saving program being executed.
The 8051 microcontroller has an on chip internal program ROM of 4K size and if needed
can add an external memory of size 60K maximum by interfacing i.e. total 64K size
memory.
The Program memory accessed through EA pin. The EA is an active low input.
When EA is connected to VCC i.e. EA =1, the 8051 can access 4 K bytes of internal ROM i.e.
0000H to 0FFFH and external ROM of 60 K bytes i.e. 1000H to FFFFH.
When EA is connected to GND i.e. EA =0, then all program fetches are directed to external
ROM i.e. 0000H to FFFFH.
Page | 126
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.4. I/O PORTS FUNCTIONS:
Discuss the significance of I/O ports along-with their dual roles in 8051.
AKTU Question Paper 2021-22, 10 MARKS
iii) Port 2: P2.0 to P2.3 configured as input & P2.4 to P2.7 configured as output port.
MOV A, #0F0H ; A=F0H
MOV P2, A ; Make P2.0 to P2.3 as output & P2.4 to P2.7 as input pins.
Page | 127
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Solution:
The P0 is 1st loaded with 55H = 01010101 and its complement i.e. 10101010 = AAH is
given to P0 continuously.
Page | 128
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.4.2 PORT 1 (Pins 1-8)
Port 1 occupies a total of 8 pins.
Port 1 does not need any pull-up resistors since it already has pull-up resistors
internally.
Upon reset, Port 1 is configured as input port.
MOV A, #55H
BACK: MOV P1, A
ACALL DELAY
CPL A ; complement register A
SJMP BACK
Page | 129
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
External data memory write & memory read. These are
̅̅̅̅̅ active low pins.
P3.6 16 𝐖𝐑
When RD =0, microcontroller reads the data from
external RAM.
̅̅̅̅ When WR =0, microcontroller writes the data into
P3.7 17 𝐑𝐃
external RAM.
Note:
Table 2 RESET Values 0f 8051 ports
RESET Values
PORTS
Binary Hex
P0 11111111 FF
P1 11111111 FF
P2 11111111 FF
P3 11111111 FF
Name the pins of 8051 used for external memory interfacing and list their functions.
Ans. The pins which are used for external memory interfacing are:
.
Refer pin details of 8051 to explain the functions of each pin.
Page | 130
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
The 8051 microcontroller is a dual in-line pin packages has 40 pins, out of which 32 pins are
assigned for Ports P0, P1, P2 and P4, where each port takes 8 pins.
The rest of the pins are VCC, GND, XTAL1, XTAL2, RST, EA , ALE/ PROG and PSEN .
Pin
Pin Name Description
No.
Page | 131
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
20 VSS It is a ground pin i.e. VSS=0V
Page | 132
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.6 EXTERNAL MEMORY (ROM & RAM) INTERFACING:
4.6.1 Interfacing External Data
Page | 133
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
To address up to 64 K Bytes of external Program memory then the hardware should be
configured as shown in figure 1.9.2.
The Port 0 outputs the low address (A0 to A7) while Port 2 outputs the high address
(A8 to A15).
The Port 0 is a multiplexed address/data bus. The LATCH is used to demultiplex address
and data bus. The LATCH will be enabled when ALE=1, so output of LATCH has lower
order address A0-A7 as shown in Fig 1.9.2.
The MOVC instruction is used to get data from code space.
The EA is an active low input pin. When EA is connected to GND i.e. EA =0, then all
program fetches are directed to external ROM i.e. 0000 H to FFFF H.
The PSEN is an active low pin used to access the external program memory (ROM),
PSEN pin is connected to the OE pin of the ROM chip.
To access the program code, EA must be grounded then PSEN will go low to enable the
external ROM to place a byte of program code on the data bus.
Example 4.8:
Interface 4K RAM to 8051 Microcontroller
Page | 134
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.9:
Interface 4K ROM to 8051 Microcontroller
Page | 135
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.10:
Interface 4K ROM & 4K RAM to 8051 Microcontroller
Page | 136
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.11:
Interface 8K RAM to 8051 Microcontroller
Example 4.12:
Interface 8K ROM to 8051 Microcontroller
Describe the method of interfacing 8K PROM to 8051 microcontroller
10-Mark
Page | 137
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Interface 8K DATA ROM to 8051 Microcontroller
Fig. 4.26 c: Interface 8K of single external ROM for both CODE and DATA
Page | 138
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.13:
Interface 8K EPROM & 4K RAM to 8051 Microcontroller
Page | 139
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.14:
Interface 16K EPROM & 8K RAM to 8051 Microcontroller.
Example 4.15: Interfacing 8 Kbyte RAM and 8 Kbyte ROM to 8051 microcontroller.
Page | 140
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.16: Interfacing 8 Kbyte RAM and 8 Kbyte ROM to 8051 microcontroller.
Fig. 4.31: Interfacing 16 Kbyte DATA RAM, DATA ROM and PROM to 8051
microcontrollers.
Page | 141
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.7 8051 ADDRESSING MODES:
Explain the various addressing modes of 8051 microcontroller.
AKTU Question Paper 2022-23, 10 MARKS
Illustrate the addressing modes of 8051 microcontroller. Support your answer with
suitable examples.
AKTU Question Paper 2021-22, 10 MARKS
Compare the immediate and direct addressing mode in context to 8051 instruction set.
AKTU Question Paper 2020-21, 2 MARKS
The CPU can access data in various ways. The data could be in a memory or in register or it may
be an immediate value (CONSTANT). The various ways of accessing these data are called
addressing mode.
There are 5 addressing modes in 8051
1. Immediate addressing mode
2. Resister addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode.
MOV DPTR, A ; will give an error because A=8 bit and DPTR= 16-bit
MOV R4, R7 ; is invalid
Page | 142
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
The register bank locations are accessed by its address or by its register names.
In this instruction address is given as a part of the instructions.
Examples:
MOV R1, 30H ; Save content of RAM location 30H in R1
MOV 50H, A ; Save content of A in RAM location 50H
MOV A, 4 ; is same as copying R4 into A
MOV A, R4 ; copy R4 into A
NOTE:
The “#” sign distinguishes between the immediate and direct addressing mode. The
absence of the “#” sign is the direct addressing mode.
Advantages
The advantage is that it makes accessing data dynamic rather than static as in direct
addressing mode. Looping is not possible in direct addressing mode.
Limitations
R0 and R1 are the only registers that can be used for pointers in register indirect
addressing mode Since R0 and R1 are 8 bits wide, their use is limited to access any
information in the internal RAM.
The accessing of externally connected RAM or on-chip ROM need 16-bit pointer. In such
case, the DPTR register is used.
NOTE:
Indexed addressing mode is widely used in accessing data elements of look-up table
entries located in the program ROM.
Page | 143
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.8 SPECIAL FUNCTION REGISTERS (SFR’s):
Discuss the significance of following SFR’s of 8051- PSW, TCON.
AKTU Question Paper 2022-23, 10 MARKS
Accumulator (A register):
It is an 8-bit register.
It holds a data and receives the result of the arithmetic instructions.
ACC is usually accessed by direct addressing and its physical address is E0H. Accumulator
is both byte and bit addressable. if you want to access the second bit (i.e. bit 1), you may
use E1H and for third bit E2H and so on.
B Register:
Page | 144
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
It is 8-bit register.
It is bit and byte-addressable register.
You can access 1-bit or all 8-bits by a physical address F0 H. Suppose to access a bit 1, we
have to use F1 H.
The B register is only used for multiplication and division arithmetic operations.
Page | 145
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
PSW Register Bits
Timer/Counter Register:
The 8051 has two 16-bit Programmable timers / counters (Timer 0 – Timer 1).
Which can be used either as timer to generate a time delay or as counter to count events
happening outside the microcontroller.
The Counters and Timers in 8051 microcontrollers contain two special function
registers: TMOD (Timer Mode Register) and TCON (Timer Control Register).
TMOD Register:
TMODE register is an 8-bit register.
Page | 146
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
TCON Register:
Timer Control or TCON is an 8-bit Register and is used to start or stop the Timers of 8051
Microcontroller. It also contains bits to indicate if the Timers has overflowed. The TCON SFR also
consists of Interrupt related bits.
SP (Stack Pointer):
The stack is a portion of a RAM Used by the CPU to store data or memory address on
temporary basis.
The stack pointer register is used to access the stack is known as SP register. The stack
pointer register is 8-bits wide. It can take a value of 00 to FFH. The RAM memory location
08H is the first location used for the stack. When the 8051 microcontroller is initialized,
the SP register contains the value 07H.
If we want to store CPU register data into the stack this is known as PUSH operation and
if we getting the data from stack back into a CPU register this is known as a POP operation.
PC (program Counter):
It is a 16-bit register. It is use to hold the address of the memory location from where the
next instruction to be fetched.
When the 8051 initializes PC starts at 0000h and it is automatically is incremented every
time after an instruction is executed. (So, in this way PC maintain the sequence of program
execution).
Due to this the width of the PC decides the max program length in bytes.
IE (Interrupt Enable):
The IE or Interrupt Enable Register is used to enable or disable individual interrupts. If a bit is
SET, the corresponding interrupt is enabled and if the bit is cleared, the interrupt is disabled. The
Bit7 of the IE register i.e. EA bit is used to enable or disable all the interrupts.
IP (Interrupt Priority):
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low. If
a bit is CLEARED, the corresponding interrupt is assigned low priority and if the bit is SET, the
interrupt is assigned high priority
Page | 147
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
SCON (Serial Control):
The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial Port. It is
located as an address of 98H. Using SCON, we can control the Operation Modes of the Serial Port,
Baud Rate of the Serial Port and Send or Receive Data using Serial Port.
Port Registers:
8051 microcontrollers have 4 bidirectional I/O ports. Port 0, Port 1, Port 2 and Port 3 (P0, P1, P2
and P3). Which can be work as input or output port. Each port having 8-bits. Hence, total 32
input/output pins allow the microcontroller to communicate with outside world means this port
allow to be connected with the peripheral devices.
Page | 148
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
UNIT
Assembly programming and
instruction of 8051
SYLLABUS
Each register can be used either for Timer or counter and can be divided into Two 8-bit
registers called Timer Low (TL) and Timer High (TH) as shown in Fig.
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various
timer operation modes.
TCON is a bit-addressable 8-bit register used for timer control.
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto-reload
1 1 3 Split mode
Page | 150
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
5.3.2: FOR TIMER 0:
Bit 3: Gate (Gating control)
When GATE=1, the Timer 0 can be started/stopped by the external sources i.e.
̅̅̅̅̅̅̅=1.
hardware control. The Timer 0 will start only when GATE=1, TR0 =1 & 𝐈𝐍𝐓𝟎
When GATE=0, the Timer 0 can be started/stopped by the software control
(instructions). The Timer 0 will start only when GATE=0 & TR0 =1 (regardless of the
̅̅̅̅̅̅̅ pin).
State of 𝐈𝐍𝐓𝟎
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto-reload
1 1 3 Split mode
Note: TMOD register configuration for Timer 0/1 in Mode 1 & Mode 2.
TMOD
Timer & Mode TMOD Register
value
Page | 151
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Problem.5.1:
Find the values of TMOD to operate as timers in the following modes.
(a) Mode 1 Timer 1
(b) Mode 2 Timer 0, Mode 2 Timer 1
(c) Mode 0 Timer 1
Solution:
a) TMOD is 00010000 = 10H The gate control bit and C/T bit are made 0, and the unused
timer (Timer 0 bit is also 0)
b) TMOD is 01010010 = 52H
c) TMOD is 00000000H = 00H
Problem.5.2:
Indicate which mode and which timer are selected for each of the following
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
Solution:
We convert the value from hex to binary
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
Problem.5.3:
Find the timer’s clock frequency and its period for various 8051-based system, with the
crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
We know that in 8051, XTAL oscillator frequency is divide by 12 circuit as shown in figure
1
Frequency f = 12 × 11.0592MHz = 𝟗𝟐𝟏. 𝟔 𝐊𝐇𝐳
1 1
Time T = f = 921.6KHz = 𝟏. 𝟎𝟖𝟓 𝝁𝐬
Page | 152
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Machine Cycle
C × 12 d
T=
f
Where, T is the time for instruction to be executed
f is the crystal frequency and
C is the number of machine cycles.
Problem 5.4:
For 8051 microcontroller, find the time taken for an instructions which takes
i) 1 Machine cycle
ii) 2 Machine cycle
iii) 4 Machine cycles
Solution:
𝐂×𝟏𝟐 𝐝 1×12
i) 𝐓 = = 6 = 𝟏. 𝟎𝟖𝟓 𝝁𝐒𝐞𝐜
𝐟 11.0592×10
𝐂×𝟏𝟐 𝐝 2×12
ii) 𝐓 = = = 𝟐. 𝟏𝟕𝟎 𝝁𝐒𝐞𝐜
𝐟 11.0592×106
𝐂×𝟏𝟐 𝐝 4×12
iii) 𝐓 = 𝐟 = 11.0592×106 = 𝟒. 𝟑𝟒𝟎 𝝁𝐒𝐞𝐜
The lower four bits (bit 0 to bit 3) are set aside for controlling the interrupt bits.
Bit Bit Function
Bit
Name
7 TF1 Timer 1 Overflow flag.
TF1=1, when timer 1 register overflows.
TF1=0, when processor vectors to execute interrupt service routine
located at program address 001Bh.
Page | 153
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
TF0=0, when processor vectors to execute interrupt service routine
located at program address 000Bh.
Page | 154
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
5.5: TIMER MODES:
In 8051, timer can operate in any one of four modes: Mode 0, Mode 1, Mode 2 & Mode 3.
The M1 & M0 bits in TMOD register determines the type of mode.
The C/𝐓
̅ =0, Timer mode selected.
TIMER 0 IN MODE 1:
TIMER 1 IN MODE 1:
Page | 155
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
4. Timer 1 started and it counts until it reaches its maximum value i.e. FFFF H and it rolls
over to 0000H. Now it will set the TF1 bit in TCON register.
Keep monitoring TF1 with the “JNB TF1, here” instruction until TF1 is set.
5. Stop the Timer 1 (Set TR1=0)
6. Clear TF1 flag for the next round.
7. Go back to Step 2 to load TH1 and TL1 again.
TIMER 0 IN MODE 2:
TIMER 1 IN MODE 2:
Page | 156
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
̅ =0, TMOD=20H & TCON=40H
Note: C/𝐓
For Counter 0 clock pulse is fed through T0 (P 3.4) and Counter 1 clock pulse is fed through T1
(P 3.5).
Page | 157
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Keep monitoring TF0 with the “JNB TF0, here” instruction until TF0 is set.
5. Stop the Counter 0 (Set TR0=0)
6. Clear TF0 flag for the next round.
7. Go back to Step 2 to load TH0 and TL0 again.
5.6.2: COUNTER 1 IN MODE 1:
1 ̅=𝟎
C/𝐓 ̅ =1
C/𝐓
For timer operation clock pulses are For counter operation externals clock pulses
3
provided by crystal oscillator circuit. are applied to pin P3.4 (T0) and P3.5 (T1).
Maximum count rate is 1/12 of oscillator Maximum count rate is 1/24 of oscillator
6
frequency. frequency.
Page | 159
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
5.7: MAXIMUM COUNT VALUE:
The maximum count value of the timers in each mode is given in the table
Table: Maximum count value of the timer in each mode
Maximum count value
Timer Mode Timer size Initial value
Hexadecimal (H) Decimal (d)
FORMULAE
𝟏𝟐
Time delay = [Maximum Count Value −( Initial count +1)] × ( Crystal Frequency )
Crystal Frequency
Initial Count = [Maximum Count Value − ( Time delay × 12
)] + 1
12
Maximum delay =[Maximum Count Value × ( )]
Crystal Frequency
Problem5. 4
Write a 8051 assembly program to generate a delay of 12 𝝁𝐬 using Timer0 in Mode1 with
XTAL frequency of 22 MHz.
Solution:
Crystal Frequency
[Initial value − 1] = Maximum value − Delay ×
12
12 𝜇s × 22 MH𝑧
= FFFF H −
12
= 65535 − 12
= 65513 (in decimals)
Initial value = 65513 + 1 = 65514 = FFEA H
The initial value (16-bit) should be loaded into the 16 -bit timer register THTL as TH1 =
FF (MSB) and TL1 - EA (LSB).
For timer 0 in mode 𝟏 TMOD = 𝟎𝟏 𝐇
The initial value is loaded into Timer0 register i.e. TLO = EA H & THO = FF H.
Assembly Language Program (ALP)
ORG 00H
MOV TMOD, #01 ; Timer 0, Mode 1 (16-bit mode)
HERE: MOV TLO, #OEAH ; TL 0 = 𝐹2H, the low byte
MOV TH0, #0FFH ; TH0=FEH, the high byte
SETB TR0 ; Start timer 0
WAIT: JNB TFO, WAIT ; Wait till TF0=1
CLR TR0 ; Stop timer 0
CLR TF0 ; Clear timer 0 flag
SJMP HERE
END
Page | 160
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Problem 5.5
Write an ALP program to generate 50% duty cycle on P1.1. Use timer 1 in mode 1 to
generate the delay. Use an initial value of FFAA H for the timer and a crystal frequency of
11.0592 MHz. Also calculate the frequency of square wave.
Solution:
In this example, the same delay is used for both ON time and OFF time of the square wave i.e.
T1 = T2
The time period of the square wave is
We know that T1 = T2 T = T1 + T2
T = T1 + T1 = 2T1 OR 2T2
The time delay is given by
𝟏𝟐
Time Delay 𝐓𝟏 = × (Final value − Initial value + 𝟏)
Crystal Frequency
12
= × (FFFF − FFAA + 1) = 1.085 𝜇s × 56 H = 1.085 𝜇s × 86 D
11.0592 MHz
Time Delay 𝐓𝟏 = 𝟗𝟑. 𝟑𝟏𝝁𝐬
1 1
Hence frequency of square wave 'f' = = = 𝟓. 𝟑𝟓𝟖𝟒𝐊𝐇𝐳
2 T1 2 × 93.31𝜇sec
Problem 5.6
Generate a square wave with an ON time of 4 ms and an OFF time of 3 ms on pin P1.1.
Assume crystal frequency of 22 MHz. Use timer 1 in mode 1.
Solution:
In this example, the square wave has different ON time (4ms) and OFF time (3ms), hence the intial
values for ON time & OFF time are different and need to compute separtely.
Note: Timer 1 in mode 1 is an 16-bit mode. Therefore the maximim count value is FFFFH.
Page | 161
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Hence
Crystal frequency
(Initial value − 𝟏) = Maximum value of mode 𝟎 − Required delay ×
𝟏𝟐
4 × 10−3 × 22 × 106
= FFFF −
12
3 × 10−3 × 22MHz
= FFFF H −
12
= 65535 − 7333
= 58202 + 1
Initial value = E35BH
ORG 00H
MOV TMOD,#10H ;Timer 1, Mode 1(16-bit mode)
SJMP HERE
𝐓 = 𝐓𝟏 + 𝐓𝟐 = 𝟓𝟎 × 𝟏𝟎−𝟔 𝐬
Crystal frequency
(Initial value − 𝟏) = ( Maximum value of mode 𝟐) − Required delay × 𝟏𝟐
22×106
= (FF) − 25 × 10−6 s × 12
= 255 − 45.88 = 209 + 1 = D2 H
Initial value = 𝐃𝟐 𝐇
Page | 163
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Note:
1. To use a higher crystal frequency.
2. To change a SMOD bit in the PCON register i.e. SMOD=1 will double the baud rate
as shown in above table.
Page | 164
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
SCON is an 8-bit register used to program the start bit, stop bit, and data bits of data framing,
among other things.
0 0 Serial Mode 0
7 & SM0 &
6 SM1 0 1 Serial Mode 1, 8-bit data, 1 stop bit, 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
Receive Enable
It is a bit-addressable register
4 REN
When REN=1, it allows 8051 to receive data on RxD pin.
Transfer bit 8
3 TB8 Set/Cleared by hardware to determine state of the 9th bit data transmitted in 9-
bit UART (In mode 2 & 3). We make TB8=0 since it is not used in our application.
Receive bit 8
2 RB8 Set/Cleared by hardware to indicate state of the 9th bit data received -bit UART
(In mode 2 & 3). We make RB8=0 since it is not used in our application.
1 TI Transmit Interrupt
Page | 165
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
When 8051 finishes the transfer of 8-bit character
When 8051 receives data serially via RxD, it gets rid of the start and stop bits and
0 RI places the byte in SBUF register
It raises the RI flag bit to indicate that a byte has been received and should be
picked up before it is lost.
RI is raised halfway through the stop bit.
Page | 166
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Baud Rate
SMOD = 0 SMOD = 1
Baud Rates Baud Rates
9,600 19,200
4,800 9,600
2,400 4,800
1,200 2,400
4,5 - Not used by 8051 microcontroller.
&6
GF1 & General purpose flags not implemented
2&3
GF0
PD IDL Status
Page | 167
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
6 After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by the "CLR TI"
instruction in order for this new byte to be transferred.
FORMULAE
Crystal frequency 𝟏
Baud rate = ×
𝟏𝟐 × 𝟑𝟐 (𝟐𝟓𝟔 − 𝐓𝐇𝟏)
Crystal frequency
TH1 = 𝟐𝟓𝟔 −
𝟏𝟐 × 𝟑𝟐 × Baud rate
Problem:5.8
With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates.
(i) 9600 (ii) 4800 (iii) 2400 (iv) 1200
Solution:
i) 9600 Baud Rate
Crystal frequency
𝐓𝐇𝟏 = 𝟐𝟓𝟔 −
𝟏𝟐 × 𝟑𝟐 × Baud rate
11.0592 × 106
TH1 = 256 − = 256 − 3 = 253
12 × 32 × 9600
𝐓𝐇𝟏 = 𝐅𝐃 𝐇
Page | 168
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Crystal frequency
TH1 = 256 −
12 × 32 × Baud rate
11.0592 × 106
TH1 = 256 − = 256 − 24 = 232
12 × 32 × 4800
𝐓𝐇𝟏 = 𝐅𝐀 𝐇
FD -3 9600
FA -6 4800
F4 -12 2400
E8 -24 1200
Note: In 8051, serial communication uses a standard crystal frequency i.e. 11.0592MHz.
Problem.5.8:
Write a program for the 8051 to transfer letter “A” serially at 9600 baud, continuously.
Solution:
Page | 169
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
ORH 00H
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-3 ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV SBUF,#”A” ;letter “A” to transfer
HERE: JNB TI, HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending A
END
Problem.5.9:
Write an ALP and C program to serially transmit the message “ECE” continuously at a baud
rate of 9600, 8-bit data and 1 stop bit.
Solution:
I METHOD II METHOD
ACALL SEND JZ GO
RET END
END
Problem.5.10:
Write an ALP and C program to serially transmit the message “HELLO” continuously at a
baud rate of 9600, 8-bit data and 1 stop bit.
Page | 170
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Solution:
ORG 00H
MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
MOV A, # “H”
ACALL SEND
MOV A, # “E”
ACALL SEND
MOV A, # “L”
ACALL SEND
MOV A, # “L”
ACALL SEND
MOV A, # “0”
ACALL SEND
SJMP AGAIN
SEND: MOV SBUF, A
WAIT: JNB TI, WAIT
CLR TI
RET
END
Problem.5.11:
Write an ALP and C program to serially transmit the message “JSSATE NOIDA” continuously
at a baud rate of 9600, 8-bit data and 1 stop bit.
Solution:
ORG 00H
MOV TMOD, #20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
repeat: MOV DPTR,#msg
up: CLR A
MOVC A,@A+DPTR
JZ repeat
ACALL send
INC DPTR
SJMP up
send: MOV SBUF,A
5.11: INTERRUPTS
An interrupt is the occurrence of a condition (an event) that causes a temporary suspension of a
program while the event is serviced by another program (Interrupt Service Routine ‘ISR’.
OR
An interrupt is an external or internal event that interrupts the microcontroller to inform it that
a device needs its service.
Page | 171
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
INTERRUPTS:
Whenever any device needs its service, the device notifies the microcontroller by sending it an
interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is
doing and serves the device. The program which is associated with the interrupt is called the
interrupt service routine (ISR) or interrupt handler.
POLLING:
Microcontroller continuously monitors the status of a given device. When the conditions met, it
performs the service. After that, it moves on to monitor the next device until each one is serviced.
Sl.
INTERRUPTS POLLING
No.
1 Whenever any device needs its service, Microcontroller can monitor the status of
the device notifies the microcontroller several devices and serve each of them as
by sending it an interrupt signal. Upon certain conditions are met. After that, it
receiving an interrupt signal, the moves on to monitor the next device until
microcontroller interrupts whatever it each one is serviced.
is doing and serves the device.
2 Each device can get the attention of the The polling method cannot assign priority
microcontroller based on the priority since it checks all devices in a round-robin
assigned to it. fashion.
Page | 172
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
5.11.3: DIFFERENT TYPES OF INTERRUPT:
The 8051 has six sources of interrupts and only five interrupts are available to the user. The
RESET interrupt is not available to the user.
RESET: When the RESET pin is activated, the 8051 jumps to address location 0000H.
External:
Hardware Interrupts
There are two external hardware interrupts and also referred to as EX1 and EX2.
1. ̅̅̅̅̅̅̅
INT0 (P3.2) and its interrupt vector address is 0003H.
2. ̅̅̅̅̅̅̅
INT1 (P3.3) and its interrupt vector address is 0013H
Page | 173
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
ET2 IE.5 Enables or disables Timer 2 overflow or capture interrupt (8052 only).
ES IE.4 Enables or disables the serial port interrupt.
ET1 IE.3 Enables or disables Timer 1 overflow interrupt.
ET1 IE.2 Enables or disables external interrupt 1.
ET0 IE.1 Enables or disables Timer 0 overflow interrupt.
EX0 IE.0 Enables or disables external interrupt 0.
For example, that if external hardware interrupts 0 and 1 are activated at the same time, external
interrupt 0 is responded to first. Only after ̅̅̅̅̅̅̅
INT0 has been serviced, ̅̅̅̅̅̅̅
INT1 serviced, since has the
lowest priority.
PRIORITY SETTING
Interrupt priority is done by programming a register called Interrupt Priority (IP) Register. Upon
power-up reset, the IP register contains all 0s, making the priority sequence based. To give a
higher priority to any of the interrupts, we make the corresponding bit in the IP register high.
Priority bit = 1 assigns high priority. Priority bit = 0 assigns low priority.
- IP.7 Reserved
- IP.6 Reserved
Page | 174
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Each interrupt source can be programmed to have one of the two priority levels by setting (high
priority) or clearing (low priority) a bit in the IP (Interrupt Priority) Register. A low priority
interrupt can itself be interrupted by a high priority interrupt, but not by another low priority
interrupt. If two interrupts of different priority levels are received simultaneously, the request of
higher priority level is served. If the requests of the same priority level are received
simultaneously, an internal polling sequence determines which request is to be serviced.
Problem:5.12
Discuss what happens if interrupts INT0, TF0, and INT1 are activated at the same time.
Assume priority levels were set by the power-up reset and the external hardware
interrupts are edge-triggered.
Solution:
If these three interrupts are activated at the same time, they are latched and kept internally. Then
the 8051 checks all five interrupts according to the sequence listed in Table. If any is activated, it
services it in sequence. Therefore, when the above three interrupts are activated, IE0 (external
interrupt 0) is serviced first, then timer 0 (TF0), and finally IE1 (external interrupt 1).
Problem:5.13
(a) Program the IP register to assign the highest priority to INT1 (external interrupt 1),
(b) Discuss what happens if INT0, INT1, and TF0 are activated at the same time. Assume the
interrupts are both edge-triggered.
Solution:
(a) MOV IP, #00000100B ; IP.2=1 assign INT1 higher priority. The instruction SETB IP.2 also will
do the same thing as the above line since IP is bit-addressable.
(b) The instruction in Step (a) assigned a higher priority to INT1 than the others; therefore, when
INT0, INT1, and TF0 interrupts are activated at the same time, the 8051 services INT1 first, then
it services INT0, then TF0. This is due to the fact that INT1 has a higher priority than the other
two because of the instruction in Step (a). The instruction in Step (a) makes both the INT0 and
TF0 bits in the IP register 0. As a result, the sequence in Table a gives a higher priority to INT0
over TF0
Problem:5.14
Assume that after reset, the interrupt priority is set the instruction MOV IP,#00001100B.
Discuss the sequence in which the interrupts are serviced.
Solution:
The instruction “MOV IP #00001100B” (B is for binary) and timer 1 (TF1) to a higher priority
level compared with the reset of the interrupts. However, since they are polled according to Table
a, they will have the following priority.
Page | 175
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Timer Interrupt 0 (TF0)
Lowest Priority Serial Communication (RI+TI)
Problem:5.15
Show the instructions to
(a) Enable the serial interrupt, timer 0 interrupt, and external hardware interrupt 1 (EX1),
and
(b) Disable (mask) the timer 0 interrupt, then
(c) Show how to disable all the interrupts with a single instruction.
Solution:
(a)
MOV IE, #10010110B ; enable serial, timer 0, EX1
Another way to perform the same manipulation is
SETB IE.7 ; EA=1, global enable
SETB IE.4 ; enable serial interrupt
SETB IE.1 ; enable Timer 0 interrupt
SETB IE.2 ; enable EX1
(b)
CLR IE.1 ; mask (disable) timer 0 interrupt only
(c)
CLR IE.7 ; disable all interrupts
Problem:5.16
Discuss the interrupt priority order achieved by the execution of MOV IP,#11H instruction
Page | 176
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Differentiate between RET and RETI
RET
The RET instruction returns the program from a subroutine.
RET pops the return address from the stack and continue execution there and making the
8051 return to where it left. The high byte and low byte address of PC from the stack and
decrements the SP by 2.
The execution of the instruction will result in the program to resume from the location
just after the CALL instruction.
No flags are affected.
Example:
Suppose SP=0BH originally and an interrupt is detected during the instruction ending at
location 0213H
RAM Internal locations 0AH and 0BH contain the values 14H and 02H respectively.
The RETI instruction leaves SP=09H and returns program execution to location 0214H.
RETI
The RETI instruction returns the program from an interrupt subroutine.
RETI pops the high byte and low byte address of a PC from the stack and restores the
interrupt logic to accept additional interrupts.
SP decrements by 2 and no other registers are affected. However, the PSW is not
automatically restored to its pre-interrupt status.
After the RETI, program execution will resume immediately after the point at which the
interrupt is detected.
No flags are affected.
Example:
Suppose SP=0BH originally and an interrupt is detected during the instruction ending at
location 0213H
RAM Internal locations 0AH and 0BH contain the values 14H and 02H respectively.
The RETI instruction leaves SP=09H and returns program execution to location 0214H.
Explain why we cannot use RET instead of RETI as the last instruction of an ISR.
Solution:
RET and RETI perform the same actions of popping off the top two bytes of the stack into
the program counter, and making the 8051 return to where it left.
However, RETI also performs an additional task of clearing the interrupt-in-service flag,
indicating that the servicing of the interrupt is over and the 8051 now can accept a new
interrupt on that pin.
If we use RET instead of RETI as the last instruction of the interrupt service routine, then
it will simply block any new interrupt on that pin after the first interrupt, since the pin
status would indicate that the interrupt is still being serviced.
In the case of TF0, TF1, TCON.1 and TCON.3, they are cleared due to the execution of RETI.
Page | 177
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
5.12: STACK
Stack is a section of internal RAM used by the CPU to store information temporarily. This
information could be data or an address.
The register used to access the stack is called the stack pointer (SP) register.
The stack pointer is a 8-bit register used by the 8051 to hold an internal RAM address that is
called the top of the stack.
When data is to be placed on the stack, the SP increments before storing data on the stack i.e.
SP = SP+1, so that the stack grows up as data is stored.
When 8051 is RESET, the SP is set to 07H
As the data is retrieved from the stack, the byte is read from stack & then SP decrements i.e.
SP = SP-1 to point to the next available byte of stored data.
Storing the data onto stack is called a PUSH.
Retrieving the contents of the stack is called a POP.
RAM location 08H is the 1st location used by the stack to store the data
SP is 1st incremented by one i.e. SP = SP+1, then the contents of R2 is stored in top of stack i.e. 08H
address.
Example 2:
ORG 00H
MOV R2, #30H
MOV R3, #40H
MOV R4, #41H
PUSH 2
PUSH 3
PUSH 4
END
Page | 178
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example 3
POP 4
POP 3
POP 2
Page | 179
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Jump and Call Instructions:
Jump and call instructions replaces the contents of program counter (PC) with new address and
program execution to start from that new address. The difference of this new address from
address in program where jump or call instruction is called range of jump or call.
i) Relative range:
The Jump can be within -128 bytes. (for backward Jump) or +127 bytes (for forward
Jump) of memory relative to the address of current program counter (PC).
Jump or call instruction with relative range will be of 2-byte instructions. The 1st byte is
opcode and second byte is relative address of target location.
ii) Absolute range:
In 8051, program memory is divided into logical divisions called pages each of 2k byte.
Maximum size program memory is 64 K bytes. Size of each page is 2 K bytes. Maximum
𝟔𝟒 𝐊𝐛
number of pages = 𝟐 𝐊𝐛 = 𝟑𝟐 pages
Page | 180
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
In absolute range, Jump can be within a single page.
The upper 5-bits of PC holds the page number and lower 11-bits holds the address
within that page.
i.e., 25 → 32 page
211 → 2 Kb range
This range allows the Jump to anywhere in the memory location from 0000 h to FFFFh.
The Jump or call instructions with this range will be of 3-byte instructions in which 1st
byte is opcode and 2nd and 3rd bytes represent the 16-bit address of target location.
Table: 5.5 COMPARE RELATIVE RANGE, ABSOLUTE RANGE AND LONG RANGE:
Type of Jump or
Ranges No. of bytes Example
CALL
Anywhere within
Long range program 3-byte instructions LCALL
(0000 H to FFFF H)
5.13: SUBROUTINE
A subroutine is a program that may be used many times in the execution of a larger program. The
subroutine could be written into the body of the main program everywhere it is needed, resulting
in the fastest possible code execution
Call and the stack
Page | 181
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
A call instruction causes a jump to the address where the called subroutine is located. At the end
of the subroutine the program resumes operation at the opcode address immediately following
the call.
The stack area of internal RAM is used to automatically store the address, called the return
address, of the instruction found immediately after the call. The stack pointer register holds the
address of the last space used on the stack.
Page | 182
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
ADCs are mainly classified into
i. Serial ADCs and
ii. Parallel ADCs
Pin diagram of ADC0804
̅̅̅
CS
1 Chip select is an active low input used to activate the ADC0804 chip.
Chip Select
It is an input signal and active low. The ADC converts the analog input
to its binary equivalent and holds it in an internal register.
2 ̅̅̅̅
RD
When CS̅̅̅ = 0,, if a high-to-low pulse is applied to the RD
̅̅̅̅ pin, the 8-
bit digital output shows up at the D0-D7.
5 ̅̅̅̅̅̅̅
INTR This is an active low output pin.
When ̅̅̅̅̅̅̅
INTR = 0, indicate end of conversion (EOC) to 8051.
VIN(+) pin is connected with analog input to be converted to digital
6&7 VIN(+) & VIN(-)
VIN(-) pin is connected to ground
These are the differential analog input given by VIN = VIN(+) - VIN(-)
8 A GND Analog ground for analog signal
Page | 183
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
It is an input voltage used for the reference voltage.
9 Vref/2 If this pin is open, the analog input voltage for ADC is in the range of
0V to 5V.
Write the schematic, algorithm and a program to interface a ADC 0804 to 8051
Page | 184
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Fig. 5.29: 8051 connections to ADC0804 with self-clocking
ALGORITHM
1 Set P1 as input port and P2.7 as input pin
̅̅̅̅̅ = low to high signal.
2 Send a start of conversion to ADC. WR
3 Wait for end of conversion by reading in the INTR pin (INTR = EOC = 0 )
4 Enable ̅̅̅̅
RD = 0, so that converted data is put on D0 − D7 lines
5 Read in the digital data on D0 − D7 using P1
6 Call hexadecimal to ASCII conversion.
7 Call data display to display the ASCII values on LCD.
Assembly language interfacing programming
RD BIT P2. 5 ; RD
WR BIT P2. 6∘ ;WR (start conversion)
INTR BIT P2. 7 ;end-of-conversion
MYDATA EQU P1 ; P1.0 − P1.7 = D0 − D7 of the ADC804
MOV P1, #0FFH ; make P1 = input
SETB INTR
BACK: CLR WR ; WR = 0
SETB WR ; WR = 1 L-to-H to start conversion
HERE: JB INTR, HERE ; wait for end of conversion
CLR RD ; conversion finished, enable RD
MOV A, MYDATA ; read the data
ACALL CONVERSION ; hex-to-ASCII conversion
ACALL DATA DISPLAY ;display the data
SETB ; make RD = 1 for next round
SJMP BACK
Page | 185
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Table.5.6: LCD Commands Code
Page | 186
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example:
Page | 187
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Write the schematic, algorithm and a program to interface a alphanumeric LCD to
8051 and to display ‘INDIA’.
ORG 00H
MOV A,#38H ;Initialize LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1, pos. 4
Page | 188
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#’I’ ;display letter I
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#’N’ ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#’D’ ;display letter D
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#’I’ ;display letter I
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#’A’ ;display letter A
ACALL DATAWRT ;call display subroutine
Page | 189
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
ALGORITHM
1. Initialize the LCD using the lcdcmd subroutine.
2. Send the ASCII value ‘H’, ‘E’, ‘L’, ‘L’, ‘O’ to PORT P1 and call lcddata subroutine with 250
ms delay.
3. Send the commands 38H, OEH, 01H, 06H and 86H to PORT P1
4. Make RS = 0 & R/W = 0
5. Make enable Pin E=1 for a 1 ms.
6. Make enable Pin E=0
7. Return to calling program
8. Latch ASCII data on PORT P1 into data register to display on LCD
9. Send the commands 38H, OEH, 01H, 06H and 86H to PORT P1
10. Make RS = 1 & R/W = 0
11. Make enable Pin E=1 for a 1 ms.
12. Make enable Pin E=0
13. Return to calling program
14. Call delay subroutine.
ORG 00H
MOV A,#38H ;initialize LCD 2 lines ,5x7 matrix
ACALL COMMAND ;issue command
MOV A,#0EH ;LCD on, cursor on
ACALL COMMAND ;issue command
MOV A,#01H ;clear LCD command
ACALL COMMAND ;issue command
MOV A,#06H ;shift cursor right
ACALL COMMAND ;issue command
MOV A,#86H ;cursor: line 1, pos. 6
ACALL COMMAND ;command subroutine
COMMAND:
ACALL READY ;is LCD ready?
MOV P1,A ;issue command code
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
Page | 190
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
CLR P2.2 ;E=0,latch in
RET
DATA_DISPLAY:
ACALL READY ;is LCD ready?
MOV P1,A ;issue data
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W =0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0,latch in
RET
READY:
SETB P1.7 ;make P1.7 input port
CLR P2.0 ;RS=0 access command reg
SETB P2.1 ;R/W=1 read command reg
;read command reg and check busy flag
BACK:SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 H-to-L pulse
JB P1.7,BACK ;stay until busy flag=0
RET
END
The most common stepper motor has four stator windings that are paired with center-tapped
common as shown in figure. This type of stepper motor is commonly referred to as a four-phase
or unipolar stepper motor. The center tap allows a change of current direction in each of two coils
when a winding is grounded, thereby resulting in a polarity change of the stator. Notice that while
a conventional motor shaft runs freely, the stepper motor shaft moves in fixed repeatable
increment, which aloe one to move it to a precise position.
The direction of the rotation is dictated by the stator poles. The stator poles are determined by
the current sent through the wire coils. As the direction of the current is changed, the polarity is
also changed causing the reverse motion of the rotor. The stepper motor hers has a total of 6
leads: 4 leads representing the four-stator winding and 2 common for the center-tapped leads.
Page | 191
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
A stepper motor is brushless and synchronous motor which divides the complete rotation into
number of steps. Each stepper motor will have some fixed step angle and motor rotates at this
angle. The main principle of this circuit is to rotate the stepper motor step wise at a particular
step angle. The ULN2003A IC is used to drive stepper motor as the controller cannot provide
current required by the motor.
The circuit mainly consists of:
8051 microcontrollers
ULN2003A
Stepper Motor
The circuit consists of 8051 microcontroller, ULN2003A and Stepper Motor. The Motor is
connected to the Port 2 of the microcontroller through a driver IC. The ULN2003A is a current
driver IC. It is used to drive the current of the stepper motor as it requires more than 60mA
current. It is an array of Darlington pairs. It consists of seven pairs of Darlington arrays with
common emitter. The IC consists of 16 pins in which 7 are input pins, 7 are output pins and
remaining are VCC and Ground. The first four input pins are connected to the microcontroller. In
the same way, four output pins are connected to the stepper motor.
Stepper motor has 6 pins. In these six pins, 2 pins are connected to the supply of 12V and the
remaining are connected to the output of the stepper motor. Stepper motor rotates at a given
step angle. Each step in rotation is a fraction of full cycle.
The stepper motors will have stator and rotor. Rotor has permanent magnet and stator has coil.
The basic stepper motor has 4 coils with 90 degrees rotation step. These four coils are activated
in the cyclic order. The figure shown above gives the direction of rotation of the shaft of the
stepper motor. There are different methods to drive a stepper motor. Some of these are
explained below.
Full Step Drive (1.80/Step): In this method two coils are energized at a time. Thus, here
two opposite coils are excited at a time.
Half Step Drive (0.90/Step): in this method coils are energized alternatively. Thus, it
rotates with half step angle. In this method, two coils can be energized at a time or single
coil can be energized. Thus, it increases the number of rotations per cycle.
Page | 192
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Table5.8.1: Full-Step: 4-Step Sequence
Winding Winding Winding Winding Counter-
Step Clockwise
Clockwise A B C D
1 1 0 0 1
2 1 1 0 0
3 0 1 1 0
4 0 0 1 1
Page | 193
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
rotation of a DC stepper motor in clockwise and anti-clockwise directions is shown in the figure
below. The controller is simple and easy to construct, and can be used in many applications
including machine control and robotics for controlling the axial rotation.
Example:
Sketch the schematic for interfacing a stepper motor to 8051
Solution:
Example:
Write the schematic, algorithm and a program to interface a stepper motor to 8051 and
to rotate the motor in clock wise direction using normal 4 step sequence.
Solution:
Page | 194
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
To generate the full-step 4-step sequence, the initial values to be used for clockwise are
66H, 33H, 99H and CCH.
To generate the full-step 4-step sequence, the initial values to be used for counter-
clockwise are CCH, 99H, 33H and 66H.
Algorithm
DJNZ R0,UP1
DJNZ R0,UP1
RET
END
Page | 195
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example:
Write the schematic, algorithm and a program to interface a stepper motor to 8051 and
to rotate the motor in anti-clock wise direction using wave drive sequence
Solution:
To generate the wave-drive 4-step sequence, the initial values to be used for clockwise
are 8H, 4H, 2H and 1H.
To generate the wave-drive 4-step sequence, the initial values to be used for counter-
clockwise are 1H, 2H, 4H and 8H.
Algorithm
1. For counter-clockwise direction load the sequence 01H into P1
2. Call 100 ms delay
3. Load the sequence 02H into P1
4. Call 100 ms delay
5. Load the sequence 04H into P1
6. Call 100 ms delay
7. Load the sequence 08H into P1
8. Call 100 ms delay
9. Repeat from step 1
Page | 196
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Method 1 Method 2
DJNZ R0,UP1
DJNZ R0,UP1
RET
END
Example:
A switch is connected to pin P2.7. Write a ALP program to monitor the status of SW and
perform the following:
i. If SW = 0, the stepper motor moves clockwise.
ii. If SW = 1, the stepper motor moves counterclockwise.
Page | 197
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
To generate the full-step 4-step sequence, the initial values to be used for clockwise are
66H, 33H, 99H and CCH.
To generate the full-step 4-step sequence, the initial values to be used for counter-
clockwise are CCH, 99H, 33H and 66H.
Algorithm
1. If SW = 0, rotate the motor in clockwise direction
2. Load the sequence 66H into P1
3. Call 100 ms delay
4. Load the sequence 33H into P1
5. Call 100 ms delay
6. Load the sequence 99H into P1
7. Call 100 ms delay
8. Load the sequence CCH into P1
9. Call 100 ms delay
10. Else (i.e. if SW =1)
11. Rotate motor in counter clockwise direction i.e. load the sequence CCH into P1
12. Call 100 ms delay
13. Load the sequence 99H into P1
14. Call 100 ms delay
15. Load the sequence 33H into P1
16. Call 100 ms delay
17. Load the sequence 66H into P1
18. Call 100 ms delay
19. Repeat from step 1
MOV A, #66H
Page | 198
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
NEXT: JNB P2.7, CLOCKWISE
SJMP NEXT
SJMP BACK
ACALL DELAY
ACALL DELAY
SJMP BACK
DJNZ R0,UP1
RET
END
Page | 199
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example:
Write a program to rotate a motor 800 in the clockwise direction. The motor has a step
angle of 20. Use the 4-step sequence.
Algorithm
1. Initialize a counter with 40 steps
2. Load the sequence 66H into P1
3. Call delay
4. For clockwise direction rotate the phase sequence right
5. Decrement counter (R0) and repeat from step 1 until counter is zero
6. If counter is zero i.e. R0=0, Stop.
Page | 200
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example:
Write a program to rotate a motor 800 in the clockwise direction. The motor has a step
angle of 20. Use the 4-step sequence.
Algorithm
1. Initialize a counter with 40 steps
2. Load the sequence 66H into P1
3. Call delay
4. For clockwise direction rotate the phase sequence right
5. Decrement counter (R0) and repeat from step 1 until counter is zero
6. If counter is zero i.e. R0=0, Stop.
Page | 201
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Example:
Write a program to rotate a motor in the clockwise direction.
ORG 000H
START: MOV R0, #04 ; Initialize R0 with 4
RPT: MOV DPTR, #0100H ; Load DPTR with the address 0100H
CLR A ; Clear accumulator A
MOVC A, @A+DPTR ; Move the value from the code memory to A
MOV P1, A ; Output the value to Port 1
ACALL DELAY ; Call the delay subroutine
INC DPTR ; Increment DPTR to point to the next value
DJNZ R0, RPT ; Decrement R0 and jump to RPT if not zero
SJMP START ; Jump back to START
DELAY: ; Delay subroutine
MOV R2, #10 ; Initialize outer loop count for delay
OUTER_LOOP: MOV R3, #50 ; Initialize inner loop count for delay
INNER_LOOP: NOP ; No operation (adjust as needed for timing)
NOP
DJNZ R3, INNER_LOOP ; Decrement R3 and jump if not zero
DJNZ R2, OUTER_LOOP ; Decrement R2 and jump if not zero
RET
Example:
Write a program to rotate a motor in the counter clockwise direction.
ORG 000H
START: MOV R0, #04 ; Initialize R0 with 4
RPT: MOV DPTR, #0100H ; Load DPTR with the address 0100H
CLR A ; Clear accumulator A
MOVC A, @A+DPTR ; Move the value from the code memory to A
MOV P1, A ; Output the value to Port 1
ACALL DELAY ; Call the delay subroutine
INC DPTR ; Increment DPTR to point to the next value
DJNZ R0, RPT ; Decrement R0 and jump to RPT if not zero
Page | 202
Microprocessor & Microcontroller Unit-5: Assembly Programming & Instruction of 8051
Page | 203
Microprocessor & Microcontroller Annexure-1: 8085 Programming
LEVEL-1
1. Addition of Two 8-Bit Numbers
ADD B ;A=A+B
SUB B ;A=A-B
Expected Result: The difference of 0AH and 5H is stored in memory location 4001H.
LOOP: ADD C ; A = A + C
DCR B ; Decrement B by 1
Page | 204
Microprocessor & Microcontroller Annexure-1: 8085 Programming
JNZ LOOP ; If B is not zero, repeat
Objective: Compare two 8-bit numbers and store the larger number in a specific
memory location.
JMP END
EQUAL: STA 4003H ; Store A in memory 4003H (if they are equal)
Expected Result: The larger of the two numbers (0AH and 07H) is stored in memory
location 4003H.
Expected Result: The sum of the two 16-bit numbers stored in 4000H-4001H and
4002H-4003H is saved in 4004H-4005H.
Page | 206
Microprocessor & Microcontroller Annexure-1: 8085 Programming
DCR C ; Decrement counter
Expected Result: The sum of the five numbers is stored in memory location 4800H.
ANI 01H ; AND with 01H to check the least significant bit
JMP END
Expected Result: 00H is stored at 4900H if the number is even, 01H if it is odd.
LEVEL-2
9. 8085 programs to arrange given numbers in ascending order
HEX
Address Labels Mnemonics Comments
Codes
LXI H,
8000 21, 40, 80 START Pointer to the IN-BUFFER
8040H
Page | 207
Microprocessor & Microcontroller Annexure-1: 8085 Programming
HEX
Address Labels Mnemonics Comments
Codes
DA, 00,
801B JC START If flag is 1, exchange occurred
80
Page | 208
Microprocessor & Microcontroller Annexure-1: 8085 Programming
2006 INX H
2008 CMP B
Page | 209
Microprocessor & Microcontroller Annexure-1: 8085 Programming
11. Flashing LED connected to port 1 of the 8051 Microcontroller
NOP
NOP
RET
END
Page | 210
Microprocessor & Microcontroller Annexure-1: 8085 Programming
END
The program above creates a square wave on P1.0 with a high-time of 50 μs and a low-
time of 50 μs. Since the interval is less than 256 μs, timer mode 2 can be used. An overflow
every 50 μs requires a TH0 reload value of 50 counts less than 00H, or -50. The program
uses a complement bit instruction (CPL) rather than SETB and CLR. Between each
complement operation, a delay of 1/2 the desired period (50 μs) is programmed using
Timer 0 in 8-bit auto-reload mode. The reload value is specified using decimal notation
as - 50, rather than using hexadecimal notation. The assembler performs the necessary
conversion. Note that the timer overflow flag (TF0) is explicitly cleared in software after
each overflow.
13. Write a program to show the use of INT0 and INT1 of 8051.
ORG 0000H
ORG 0013H
ORG 30H
END
Page | 211
Microprocessor & Microcontroller Annexure-1: 8085 Programming
Practice Problems
1. Write a Program to Convert Hexadecimal to ASCII Code and ASCII Code to
Hexadecimal
Hint:
ALGORITHM:
TEST CASE:
Output –
ALGORITHM:
Step 4. If content of accumulator is less than 0A then goto step 6 else goto step 5.
Page | 212
Microprocessor & Microcontroller Annexure-1: 8085 Programming
Step 6. Store content of accumulator to memory location 3050.
INTERFACING
1. INTERFACING OF 8085 WITH 8253
The Intel 8253 and 8254 are Programmable Interval Timers designed for
microprocessors to perform timing and counting functions using three 16-bit registers.
Each counter has 2 input pins, i.e. Clock & Gate, and 1 pin for “OUT” output. To operate a
counter, a 16-bit count is loaded in its register. On command, it begins to decrement the
count until it reaches 0, then it generates a pulse that can be used to interrupt the CPU.
Step 1- Connect the card to the kit through 50 pin cable. Ensure that the pin-1 of the card
is connected to the pin-1 of the Kit Bus connector.
Step 2- Connect +5V and GND to the kit through power supply. Switch ON the supply and
press RESET.
PROGRAM:
2002 D3 4B OUT 4B
2006 D3 49 OUT 49
Page | 213
Microprocessor & Microcontroller Annexure-1: 8085 Programming
200A D3 49 OUT 49
To interface DAC with 8085 to demonstrate the generation of square, saw tooth and
triangular wave.
ALGORITHM:
The D/A converter interface is provided on the kit in mapped mode. The DAC 0800 used
here is an 8-bit DAC and has A0 as the I/O address. The chip provides output of 0 to 8
volts. The DAC output is available at Pin No 2 of 26 pin FRC connector J7.
The staircase, square wave or triangular outputs can be generated using the chip.
STEP 2-Put the contents on output port having address A0 (I/O address on which DAC
is connected)
STEP 5-Put the contents on output port having address A0( I/O address on which DAC
is connected)
In the similar way students can write the programs for SAWTOOTH and TRIANGULAR
waveform simply by initializing the accumulator, adjusting the contents of it and
transferring the contents on port address A0.
STEP 2- INR A
Page | 214
Microprocessor & Microcontroller Annexure-1: 8085 Programming
STEP 3-Put the contents on output port having address A0 (I/O address on which DAC
is connected)
STEP 6- DCR A
STEP 7- Put the contents on output port having address A0 (I/O address on which DAC
is connected)
Page | 215
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Assembler Directives
The assembler directives are the statements that directs the assembler what to do during
assembling.
They reserve memory space for data, define constants, and tell assembler where to
assemble program in a memory.
They are also referred as pseudo instructions statements as they are effective only during
the assembly of the program but they do not generate any machine code.
ORG (origin)
The ORG directive is used to indicate the beginning of the address.
The number that comes after ORG can be either in hex or in decimal.
If the number is not followed by H, it is decimal and the assembler will convert it to
hexadecimal.
Some assemblers use “. ORG” (dot ORG) instead of “ORG” for the origin directive.
Example: ORG 1000H
DB (Define data)
It is used to define the 8-bit data and most widely used data directive in the assembler.
When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII
formats. For decimal, the “D” after the decimal number is optional, “B” for binary and “H”
for hexadecimal.
To indicate ASCII, simply place the characters in single quotes or double quotes. The
assembler will assign the ASCII code for the numbers or characters automatically.
The DB directive is the only directive that can be used to define ASCII strings larger than
two characters; therefore, it should be used for all ASCII data definitions.
Examples:
ORG 1000H
DATA1: DB 40 ; DECIMAL NUMBER
DATA2: DB 00100111B ; BINARY NUMBER
DATA3: DB 1Fh ; HEXADECIMAL NUMBER
DATA4: DB "DIPLOMA" ; ASCII CHARACTER
DATA5: DB "2016" ; ASCII NUMBER
END
EQU
EQU is used to define a constant without occupying a memory location.
The EQU directive does not set aside storage for a data item but associates a constant
value with a data label.
When the label appears in the program, its constant value will be substituted for the label.
Page | 216
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Assume that there is a constant used in many different places in the program, and the
programmer wants to change its value throughout. By the use of EQU, one can change it
once and the assembler will change all of its occurrences.
Example:
ORG 1000H
COUNT EQU 40H
MOV A, #COUNT ; COPY 40 INTO A
END
EQU indicates to the assembler the end of the source (asm) file.
The END directive is the last line of an 8051 program, meaning that in the source code
anything after the END directive is ignored by the assembler.
Some assemblers use “. END” (“dot END”) instead of “END”.
Example:
ORG 1000H
COUNT EQU 40H
MOV A, #COUNT ; COPY 40 INTO A
END
NOTE:
Brackets indicate that a field is optional, and not all lines have them. Brackets should not be
typed in the instructions.
INSTRUCTION SET
Based on the operations performed, the instruction set of 8051 are classified as
1. Arithmetic instructions
2. Logical instructions
3. Data transfer instructions
4. Boolean instructions
5. Program branching instructions.
Each instruction has two parts: operation code and operands. The operands may be one
or more i.e. operation code operand 1, operand 2
Example: MOV A, B
The following nomenclatures for register, data, address and variables are used while
write instructions.
Table 2.1.1: Nomenclatures for register, data, address and variables are used while write
instructions.
A Accumulator
B "B" register
C Carry bit
Rn Register R0 - R7 of the currently selected register bank
Direct 8-bit internal direct address for data. The data could be in lower 128bytes of
RAM (00 - 7FH) or it could be in the special function register (80 - FFH).
@Ri 8-bit external or internal RAM address available in register R0 or R1. This is
used for indirect addressing mode.
#data8 Immediate 8-bit data available in the instruction.
#data16 Immediate 16-bit data available in the instruction.
Page | 217
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Addr11 11-bit destination address for short absolute jump. Used by instructions AJMP &
ACALL. Jump range is 2 kbyte (one page).
Addr16 16-bit destination address for long call or long jump.
Rel 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all
conditional jumps.
bit Directly addressed bit in internal RAM or SFR
Arithmetic Instructions
Syntax Flags affected Bytes Cycles
ADD A, #data CY, OV & AC 2 1
Operation (A) (A) + 8-bit data
Description Adds the 8-bit immediate data with accumulator contents and result is
stored in accumulator.
Example ADD A, #04H
Before Execution After Execution
A = 05H A = 09H
Page | 218
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Syntax Flags affected Bytes Cycles
ADDC A, Rn CY, OV & AC 1 1
Operation (A) (A) + (C) + (Rn) where n = 0,1,2,3,4,5,6,7 i.e. R0 to R7
Description Adds the contents of register Rn, the carry flag and the accumulator
contents, result is stored in accumulator.
Example ADDC A, Rn where n = 0,1,2,3,4,5,6,7 i.e. R0 to R7
Before Execution After Execution
A = 05H, CY=0 & Rn = 04H A = 09H
A = 05H, CY=1 & Rn = 04H A = 0AH
Page | 219
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Before Execution After Execution
A = 05H, 50H = 04H A = 01H
Page | 220
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Operation (Rn) (Rn) - 1
Description Decrement the content of register Rn by 1
Example DEC R0
Before Execution After Execution
R0 = 05H R0 = 04H
Page | 221
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Syntax Flags affected Bytes Cycles
DAA CY & AC 1 1
Operation Decimal Adjustment Accumulator.
If (A)3-0 > 9 or (AC) = 1
Then
Add +6 to (A)3-0
Result: Result:
4 5 H 5 0 H
+ 1 5 H
+ 5 5 H
5 A H INVALID BCD
+ 6 After DAA A 5 H INVALID BCD
6 0 H Valid BCD + 6 After DAA
1 0 5 H Valid BCD
Page | 222
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
ARITHMETIC INSTRUCTIONS
Cycle
Flags
Byte
Mnemonics Operation
Affected
ADD A, #data 2 1 (A) (A) + 8-bit data CY, OV & AC
ADD A, Rn 1 1 (A) (A) + (Rn) CY, OV & AC
ADD A, direct 2 1 (A) (A)+ (direct address) CY, OV & AC
ADD A, @Ri 1 1 (A) (A) + ((Ri)) CY, OV & AC
ADDC A, #data 2 1 (B) (A) + (C) + 8-bit data CY, OV & AC
ADDC A, Rn 1 1 (B) (A) + (C) + (Rn) CY, OV & AC
ADDC A, direct 2 1 (A) (A) + (C) + (direct address) CY, OV & AC
ADDC A, @Ri 1 1 (A) (A) + (C) + ((Ri)) CY, OV & AC
SUBB A,#data 2 1 (A) (A) - #-bit data CY, OV & AC
SUBB A, Rn 1 1 (A) (A) - (Rn) CY, OV & AC
SUBB A, direct 2 1 (A) (A) - (direct address) CY, OV & AC
SUBB A, @Ri 1 1 (A) (A) - ((Ri)) NONE
INC A 1 1 (A) (A) + 1 NONE
INC Rn 1 1 (Rn) (Rn) + 1 NONE
INC direct 2 1 (direct) (direct) + 1 NONE
INC @Ri 1 1 ((Ri)) ((Ri)) + 1 NONE
DEC A 1 1 (A) (A) - 1 NONE
DEC Rn 1 1 (Rn) (Rn) - 1 NONE
DEC direct 2 1 (direct) (direct) - 1 NONE
DEC @Ri 1 1 ((Ri)) ((R1)) - 1 NONE
INC DPTR 1 2 (DPTR) (DPTR) + 1 NONE
MUL AB 1 4 (B)15- 8 (A)7-0 AxB CY & OV
Quotient in A & CY
DIV AB 1 4
Remainder in B A/B
Decimal Adjustment Accumulator. CY & AC
If (A)3-0 > 9 or (AC) = 1
Then
Add +6 to (A)3-0
DA A 1 1
If (A)7-4 > 9 or (CY) = 1
Then
Add +6 to (A)7-4
DATA TRANSFER INSTRUCTIONS
Syntax Flags affected Bytes Cycles
MOV A, #data NONE 2 1
Operation (A) 8-bit data
Description Moves the 8-bit immediate data to the Accumulator.
Example MOV A, #04H
Before Execution After Execution
A = XX A = 04H
Page | 224
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
MOV direct, Rn NONE 2 2
Operation (direct) (Rn) where n = 0,1,2,3,4,5,6,7 i.e. R0 to R7
Description Moves the contents of register Rn to Accumulator.
Example MOV 50H, R1
Before Execution After Execution
R1 = 04H & 50H = XX 50H = 04H
Page | 225
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Description The MOVC instruction moves a byte from the code or program memory to the
accumulator.
The Code Memory address from which the byte will be moved is calculated by
summing the value of the Accumulator with either DPTR.
Example MOVC A,@A+DPTR
Page | 226
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Page | 227
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Operation (A)3-0 ((Ri))3-0 where i = 0 & 1 i.e. R0 & R1
Description Exchanges the low-order nibble indirect RAM with the accumulator contents.
Example XCHD A,@R0
Before Execution After Execution
A = FFH, R0 = 50H & 50H = BBH A = BFH, R0 = 50H & 50H = FBH
Cycle
Flags
Byte
Mnemonics Operation
Affected
MOV A, #data 2 1 (A) 8-bit data NONE
MOV A, Rn 1 1 (A) (Rn) NONE
MOV A, direct 2 1 (A) (direct) NONE
MOV A, @Ri 1 1 (A) ((Ri)) NONE
MOV Rn, A 1 1 (Rn) (A) NONE
MOV Rn, #data 2 1 (Rn) 8-bit data NONE
MOV Rn, direct 2 2 (Rn) (direct) NONE
MOV direct, A 2 1 (direct) (A) NONE
MOV direct, Rn 2 2 (direct) (Rn) NONE
MOV direct, direct 3 2 (direct) (direct) NONE
MOV @Ri, A 1 1 ((Ri)) (A) NONE
MOV @Ri, #data 2 1 ((Ri)) 8-bit data NONE
MOV @Ri, direct 2 2 ((Ri)) (direct) NONE
MOV DPTR, #data 3 2 (DPTR) 16-bit data NONE
MOVC A,@A+DPTR 1 2 (A) (A + DPTR) NONE
1 2 (PC) (PC + 1) NONE
MOVC A,@A+PC
(B) (A+PC)
MOVX A,@Ri 1 2 (A) ((Ri)) NONE
MOVX A,@DPTR 1 2 (A) ((DPTR)) NONE
MOVX @Ri,A 1 2 ((Ri)) (A) NONE
MOVX @DPTR,A 1 2 ((DPTR)) (A) NONE
2 2 (SP) (SP) + 1 NONE
PUSH direct
(SP) (direct)
2 2 (direct) (SP) NONE
POP direct
(SP) (SP) - 1
XCH A, Rn 1 1 (B) (Rn) NONE
XCH A, direct 2 1 (B) (direct) NONE
XCH A, @Ri 1 1 (A) ((Ri)) NONE
XCHD A,@Ri 1 1 (A)3-0 ((Ri))3-0 NONE
LOGICAL INSTRUCTIONS
Syntax Flags affected Bytes Cycles
ANL A,#data NONE 2 1
Operation (A) (A) AND 8-bit data
Description AND the content of 8-bit immediate data with the content of accumulator
and result is stored in accumulator.
Example ANL A,#0FH
Before Execution After Execution
A = FFH A = 0FH
Page | 228
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Operation (A) (A) AND (Rn) where n = 0,1,2,3,4,5,6,7 i.e. R0 to R7
Description AND the content of register Rn with the content of accumulator and result is
stored in accumulator.
Example ANL A,R1
Before Execution After Execution
A = FFH & R1 = 0FH A = OFH
Page | 229
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Page | 230
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Example XRL A,#09H
Before Execution After Execution
A = 39H & 8-bit data = 09H A = 30H
Page | 231
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Operation Clear Accumulator
(A) 0
Description The content of Accumulator is cleared (A=00H). All the bits of the accumulator
are set to 0.
Example CLR A
Before Execution After Execution
A = FFH A = 00H
Example RL A
Before Execution After Execution
A = C2 H A = 85H
Page | 232
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Description The 8-bits in the accumulator & the carry flag are together rotated 1-bit to the
left. The bit-7 moves into the Carry flag. The original state of the carry flag
moves into the bit-0 position.
Example RLC A
Before Execution After Execution
A = C2 H & CY = 0 A = 84H & CY =1
Example RR A
Before Execution After Execution
A = C2 H A = 61H
Example RRC A
Before Execution After Execution
A = C2 H & CY = 0 A = 61H & CY = 0
Page | 233
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
LOGICAL INSTRUCTIONS
Cycle
Flags
Byte
Mnemonics Operation
Affected
ANL A,#data 2 1 (A) (A) AND 8-bit data NONE
ANL A,Rn 1 1 (A) (A) AND (Rn) NONE
ANL A,direct 2 1 (A) (A) AND (direct address) NONE
ANL A,@Ri 1 1 (A) (A) AND (Ri)) NONE
ANL direct,A 2 1 (direct) (direct) AND (A) NONE
ANL direct,#data 3 2 (direct) (direct) AND 8-bit data NONE
ORL A,#data 2 1 (A) (A) OR 8-bit data NONE
ORL A,Rn 1 1 (A) (A) OR (Rn) NONE
ORL A,direct 2 1 (A) (A) OR (direct) NONE
ORL A,@Ri 1 1 (A) (A) OR ((Ri)) NONE
ORL direct,A 2 1 (A) (direct) OR (A) NONE
ORL direct,#data 3 2 (direct) (direct) OR 8 - bit data NONE
XRL A,#data 2 1 (A) (A) EX-OR 8-bit data NONE
XRL A,Rn 1 1 (B) (A) EX-OR (Rn) NONE
XRL A,direct 2 1 (A) (A) EX-OR (direct address) NONE
XRL A,@Ri 1 1 (A) (A) EX-OR ((Ri)) NONE
XRL direct,A 2 1 (A) (direct) EX-OR (A) NONE
CLR A 1 1 (A) 0 NONE
CPL A 1 1 NONE
Boolean Instructions
Syntax Flags affected Bytes Cycles
CLR C CY 1 1
Operation (CY) 0
Description Clear the carry flag bit.
Example CLR C
Before Execution After Execution
CY = 1 CY = 0
Page | 234
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Page | 235
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Page | 236
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Boolean Instructions
Cycle
Flags
Byte
Mnemonics Operation
Affected
CLR C 1 1 (CY) 0 CY
CLR bit 1 1 (bit) 0 NONE
SETB C 1 1 (CY) 1 CY
SETB bit 2 1 (bit) 1 NONE
CPL C 1 1 (CY) NOT(CY) CY
CPL bit 2 1 (bit) NOT(bit) NONE
ANL C,bit 2 2 (C) (C) AND (bit) CY
ANL C,/bit 2 2 (C) (C) AND [ NOT(bit) ] CY
ORL C,bit 2 2 (C) (C) OR (bit) CY
ORL C,/bit 2 2 (C) (C) OR [ NOT(bit) ] CY
MOV C,bit 2 1 (C) (bit) CY
MOV bit,C 2 2 (bit) (C) NONE
Page | 237
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Page | 238
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Its range is -32768 bytes to +32767 bytes.
The destination may be anywhere within the 64 Kbytes of program memory.
Example LJMP LABEL
Page | 239
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Operation (PC) (PC) + 3
If (bit) = 1
Then,
(PC) (PC) + rel
Description Jump if bit set.
If the indicated bit is SET (1), jump to the address indicated otherwise
proceeds (Execute) with the next instruction.
Example 0090 SETB ACC.0
0091 JB ACC.0, NEXT
0092 MOV P1,A
0093 NEXT: INC R1
Before Execution After Execution
ACC.0 = 1 & PC = 0091 ACC.0 = 1 & PC = 0093
Page | 240
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
If ACC ≠ 0, then processor jump to the specified address (i.e. LABEL),
If ACC = 0, then processor proceeds (Execute) with the next instruction.
Example 0090 MOV A,#05H
0091 ADD A,#03H
0092 JNZ, NEXT
0093 MOV P1,A
0094 NEXT: INC R1
Before Execution After Execution
A = 08H & PC = 0092 PC = 0094
Page | 241
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
else
(C) 0
Description Compare and Jump if not equal.
The magnitudes of the source byte and destination byte are compared. If they
are not equal, it jumps to the target.
Page | 242
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
0094 NEXT: INC R1
Before Execution After Execution
Rn = 05H & PC = 0092 PC = 0094
Page | 243
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
Program Branching Instructions
Cycle
Flags
Byte
Mnemonics Operation
Affected
(PC) (PC) + 2
(SP) (SP) + 1
(SP) (PC)7-0
ACALL addr11 2 2 NONE
(SP) (SP) + 1
(SP) (PC)15-8
(PC)10-0 Page address
(PC) (PC) + 3
(SP) (SP) + 1
(SP) (PC)7-0
LCALL addr16 3 2 NONE
(SP) (SP) + 1
(SP) (PC)15-8
(PC) Page address
(PC)15-8 (SP)
(SP) (SP) - 1
RET 1 2 NONE
(PC)7-0 (SP)
(SP) (SP) - 1
(PC)15-8 (SP)
(SP) (SP) - 1
RETI 1 2 NONE
(PC)7-0 (SP)
(SP) (SP) - 1
(PC) (PC) + 2
AJMP addr11 2 2 NONE
(PC)10-0 (A)10-0
(PC) (PC) + 2
LJMP addr16 3 2 NONE
(PC) (PC) + rel
(PC) (PC) + 2
SJMP rel 2 2 NONE
(PC) (PC) + rel
(PC) (PC) + 2
If CY = 1
JC rel 2 2 NONE
Then,
(PC) (PC) + rel
(PC) (PC) + 2
If CY = 0
JNC rel 2 2 NONE
Then,
(PC) (PC) + rel
(PC) (PC) + 3
If (bit) = 1
JB bit,rel 3 2 NONE
Then,
(PC) (PC) + rel
(PC) (PC) + 3
If (bit) = 1
JBC bit,rel 3 2 NONE
Then,
(PC) (PC) + rel
JMP @A+DPTR 1 2 (PC) (A) + (DPTR) NONE
(PC) (PC) + 2
If A ≠ 0
JNZ rel 2 2 NONE
Then,
(PC) (PC) + rel
(PC) (PC) + 2
If A = 0
JZ rel 2 2 NONE
Then,
(PC) (PC) + rel
Page | 244
Microprocessor & Microcontroller Annexure-2: 8051 Instruction Set
(PC) (PC) + 3
If (A) ≠ (direct)
then
(PC) (PC) + relative address
CJNE A,direct,rel 3 2 CY
If (A) < (direct)
then
(C) 1
else
(C) 0
(PC) (PC) + 3
If (A) ≠ data
then
(PC) (PC) + relative address
CJNE A,#data,rel 3 2 CY
If (A) < (data)
then
(C) 1
else
(C) 0
(PC) (PC) + 3
If (Rn) ≠ data
then
(PC) (PC) + relative address
CJNE Rn,#data,rel 3 2 CY
If (Rn) < (data)
then
(C) 1
else
(C) 0
(PC) (PC) + 3
If ((Ri)) ≠ data
then
(PC) (PC) + relative address
CJNE @Ri,#data,rel 3 2 CY
If ((Ri)) < (data)
then
(C) 1
else
(C) 0
(PC) (PC) + 2
(Rn) (Rn) - 1
DJNZ Rn,rel 2 2 If (Rn) ≠ 0 i.e. (Rn) > 0 & (Rn) < 0 NONE
Then,
(PC) (PC) + rel
(PC) (PC) + 2
(direct) (direct) - 1
If (direct) ≠ 0 i.e. (direct) > 0 &
DJNZ direct,rel 3 2 NONE
(direct) < 0
Then,
(PC) (PC) + rel
NOP 1 1 (PC) (PC) + 1 NONE
Page | 245