Microprocessors and Introduction to Microcontroller _Text
Microprocessors and Introduction to Microcontroller _Text
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
(i)
About the Author
A. P. Godse
· Completed M.S in Software Systems with distinction from Birla Institute of Technology.
· Completed B.E. in Industrial Electronics with distinction from University of Pune in 1990.
· Worked as a Professor at Vishwakarma Institute of Technology, Pune.
· Worked as a Technical Director at Noble Institute of Technology, Pune.
· Worked as selection Committee member for M. S. admission for West Virginia University,
Washington D.C.
· Developed Microprocessor Based Instruments in co-ordination with Anna Hazare for
Environmental Studies Laboratory, at Ralegan Siddhi.
· Developed Microprocessor Lab in-house for Vishwakarma Institute of Technology.
· Worked as Subject Expert for a State Level Technical Paper Presentation Competition, Pune.
· Awarded on 26th Jan 2001 by Pune Municipal Corporation for contributing in education field
and technical writing.
· Awarded as a “Parvati Bhushan Puraskar” for contributing in the education field.
· Since 1996, writing books on various engineering subjects. Over the years, many of books are
recommended as the reference books and text books in various national and international
engineering universities.
D. A. Godse
· Completed M.E and pursuing Ph.D. in Computer Engineering from Bharati Vidyapeeth’s
University Pune.
· Completed B.E. in Industrial Electronics from University of Pune in 1992.
· Working as a Professor and Head of Information Technology Department in B.V.C.O.E.W, Pune.
· Subject Expert for syllabus setting of Computer Engineering and Information Technology branches
at the faculty of Engineering of Pune University.
· Subject Expert and Group Leader for syllabus setting of Electronics, Electronics and
Telecommunication and Industrial Electronics branches at the faculty of Maharashtra State, Board
of Technical Education.
· Subject In-charge for Laboratory Manual Development, Technical Teacher’s Training Institute,
Pune.
· Subject In-charge for Question Bank Development Project, Technical Teacher’s Training Institute,
Pune.
· Subject In-charge for the preparation of Teacher’s Guide, Board of Technical Examination,
Maharashtra state.
· Subject Expert for a State Level Technical Paper Presentation Competition organized by Bharati
Vidyapeeth’s Jawaharlal Nehru Institute of Technology, Pune.
· Local Inquiry Committee (LIC) member of Engineering faculty of Pune University.
· Awarded on 15th August 2006 by Pune Municipal Corporation for contributing in education field
and technical writing.
· Awarded on the occasion of International Women’s Day at Yashawantrao Chavan Pratishthan
Sabhagrih, Mumbai by Bharatiya Shikshan Sanstha.
(ii)
®
Microprocessors and
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
Atul P. Godse
M. S. Software Systems (BITS Pilani)
B.E. Industrial Electronics
Formerly Lecturer in Department of Electronics Engg.
Vishwakarma Institute of Technology
Pune
® TM
TECHNICAL
PUBLICATIONS
An Up-Thrust for Knowledge
(iii)
Microprocessors and
Introduction to Microcontroller
(8085, 8086, 8051 - Architecture, Interfacing and Programming)
All publishing rights (printed and ebook version) reserved with Technical Publications. No part of this book
should be reproduced in any form, Electronic, Mechanical, Photocopy or any information storage and retrieval
system without prior permission in writing, from Technical Publications, Pune.
Published
®
by : TM
Amit Residency, Office No.1, 412, Shaniwar Peth, Pune - 411030, M.S. INDIA.
TECHNICAL P h . : + 9 1 - 0 2 0 - 2 4 4 9 5 4 9 6 / 9 7 , Te l e f a x : + 9 1 - 0 2 0 - 2 4 4 9 5 4 9 7
PUBLICATIONS Email : [email protected] Website : www.vtubooks.com
Printer :
Repro India Ltd.
50/2 TTC, MIDC,
Mahape, Navi Mumbai
Price ` 585/-
(iv)
9789350992609 [1]
The importance of Microprocessors and Introduction to
Microcontroller is well known in various engineering fields.
Overwhelming response to our books on various subjects inspired us to
write this book. The book is structured to cover the key aspects of the
subject Microprocessors and Introduction to Microcontroller.
®
topics. Each chapter is well supported with necessary illustrations,
practical examples and solved problems. All the chapters in the book
are arranged in a proper sequence that permits each topic to build
P
upon earlier studies. All care has been taken to make students
comfortable in understanding the basic concepts of the subject.
R
The book not only covers the entire scope of the subject but
explains the philosophy of the subject. This makes the understanding of
this subject more clear and makes it more interesting. The book will be
E very useful not only to the students but also to the subject teachers.
The students have to omit nothing and possibly have to cover nothing
F
more.
A
making this book a reality. Much needed moral support and
encouragement is provided on numerous occasions by our whole
family. We wish to thanks the Publisher and the entire team of
C Technical Publications who have taken immense pain to get this book
in time with quality printing.
Authors
A. P. Godse
D. A. Godse
Dedicated to God
(v)
TT able of Contents
Chapter - 1 8085 Processor (1 - 1) to (1 - 22)
1.1 Features ............................................................................................................ 1 - 2
1.2 Architecture of 8085......................................................................................... 1 - 3
1.2.1 Register Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 4
(vi)
1.4.3 Reset Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 13
2.2.7 Input/Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 29
2.5.2 Flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 38
(vii)
2.5.4 Assembly Language Program to Machine Language Program . . . . . . . . . . . . . 2 - 40
(viii)
4.2 8085 Interrupt Structure and Operation .......................................................... 4 - 4
4.2.1 Types of Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 - 4
(ix)
6.4.2.1 Control Circuitry, Instruction Decoder, ALU . . . . . . . . . . . . . . . 6 - 8
(x)
7.2 Addressing Memory ..........................................................................................7 - 6
7.3 Addressing I/O ...................................................................................................7 - 8
7.4 Minimum Mode 8086 System and Timings .......................................................7 - 8
7.4.1 Minimum Mode Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 - 8
(xiii)
Review Questions ................................................................................................12 - 20
Two Marks Questions with Answers ..............................................................12 - 21
(xv)
Chapter - 15 Programmable Interval Timer/Counter (15 - 1) to (15 - 18)
8253/8254
15.1 Features .........................................................................................................15 - 2
15.2 Block Diagram................................................................................................15 - 3
15.3 Operational Description ................................................................................15 - 5
15.4 Mode Definition.............................................................................................15 - 7
15.5 Programming Examples ...............................................................................15 - 14
15.6 Interfacing of 8253/54 in I/O Mapped I/O ..................................................15 - 15
Review Questions ...............................................................................................15 - 16
Two Marks Questions with Answers .............................................................15 - 17
(xvi)
Chapter - 17 8051 Microcontroller (17 - 1) to (17 - 36)
17.1 Introduction to 8051 Microcontroller ...........................................................17 - 2
17.2 Features of 8051 and 8051 Family Microcontrollers.....................................17 - 3
17.3 Architecture of 8051......................................................................................17 - 4
17.3.1 Central Processing Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 - 4
18.1.6 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 4
18.5.2 Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 23
18.5.3 Subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 25
18.8.2 Jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 - 36
(xix)
Program 25 : To generate a square wave on the port pin P1.0.......................... 18 - 61
Program 26 : To find the sum of 10 numbers stored in the array. ..................... 18 - 62
Program 27 : Data transfer from memory block B1 to memory block B2. ......... 18 - 64
Program 28 : Data transfer from memory block B1 to memory block B2. ......... 18 - 65
Program 29 : To search a byte in a given numbers............................................. 18 - 66
Program 30 : Multiply two 8-bit numbers using repetitive addition.................. 18 - 68
Program 31 : To find the average of given N numbers....................................... 18 - 69
Program 32 : To find factorial of a number ........................................................ 18 - 71
Program 33 : To find Fibonacci series of N given terms. .................................... 18 - 72
Program 34 : Write an assembly language program to move 5 bytes of data stored
at location 8000H onwards to the location C000H onwards and
vice-versa. ..................................................................................... 18 - 73
Program 35 : An array of 10 numbers is stored at location 4000H onwards ..... 18 - 74
Program 36 : Write an assembly language program to realize following logic circuit
using Boolean instructions of 8051............................................... 18 - 75
Program 37 : Write a program to load accumulator with values 55H and complement
70 times........................................................................................ 18 - 75
Program 38 : Program to count the number of ONE's and ZERO's in two consecutive
data memory locations. ................................................................ 18 - 75
Program 39 : Write a program to save the status of bits P1.3 and P1.4 on RAM bit
location 5 and 6 respectively. ....................................................... 18 - 76
Program 40 : What is the content of R5 after execution of the following program ? ...
.............................................................................................................................18 - 76
Review Questions ................................................................................................18 - 77
Two Marks Questions with Answers ..............................................................18 - 78
(xx)
Chapter - 19 8051 I/O Ports, Timer, Serial Port
& Interrupts (19 - 1) to (19 - 48)
(xxii)
Lab Experiments
Lab Experiment 1 : Store 8-bit data in memory.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 42
Lab Experiment 7 : Check results after execution of INR B, INR C and INX B instructions. . . 2 - 47
Lab Experiment 8 : Check results after execution of DCR C, DCR B and DCX B instructions. 2 - 47
(xxiii)
Lab Experiment 22 : Calculate the sum of series of numbers. . . . . . . . . . . . . . . . . . . . . . . 3 - 4
Lab Experiment 23 : Data transfer from memory block B1 to memory block B2. . . . . . . . . . 3 - 6
Lab Experiment 35 : Add each element of array with the elements of another array.. . . . . . 3 - 21
Lab Experiment 41 : Find the number of negative, zero and positive numbers. . . . . . . . . . 3 - 27
Lab Experiment 42 : Multiply two eight bit numbers with shift and add method. . . . . . . . . . 3 - 29
Lab Experiment 43 : Divide 16-bit number with 8-bit number using shifting technique. . . . 3 - 30
(xxiv)
Lab Experiment 45 : Program to test RAM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 - 35
Lab Experiment 52 : Generate and display BCD up counter with frequency 1 Hz. . . . . . . . 3 - 44
Lab Experiment 53 : Generate and display BCD down counter with frequency 1 Hz . . . . . 3 - 45
Lab Experiment 55 : Identify the error and correct the given delay routine. . . . . . . . . . . . . 3 - 48
(xxv)
Lab Experiment 68 : Transmit message using 8251. . . . . . . . . . . . . . . . . . . . . . . . . . . 13 - 20
Lab Experiment 70 : Hardware and software for 64-key matrix keyboard interface . . . . . . 14 - 7
Lab Experiment 71 : Hardware and software for interfacing 8-digit 7-segment display. . . 14 - 13
Lab Experiment 72 : Hardware and software for 8 ´ 8 keyboard interface using 8279. . . 14 - 36
Lab Experiment 73 : Hardware and software to interface 8 ´ 4 matrix keyboard using 8279 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 41
Lab Experiment 74 : Hardware and software to interface eight 7-segment digits using 8279. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 42
Lab Experiment 76 : Interface 4´ 4 matrix keyboard and 4 digit 7-segment display using 8279 .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 - 47
(xxvi)
1 8085 Processor
Contents
1.1 Features . . . . . . . . . . . . . . . . . . April/May-04
1.2 Architecture of 8085 . . . . . . . . . . . . . . . . . . April/May-04, Nov./Dec.-04,
. . . . . . . . . . . . . . . . . . Dec.-07, 08, 09, 10, June-06,
. . . . . . . . . . . . . . . . . . May-10, 11
1.3 Pin Definitions of 8085 . . . . . . . . . . . . . . . . . . May/June-09, Nov./Dec.-06, 08, 09,
. . . . . . . . . . . . . . . . . . April/May-10
1.4 Bus Organization . . . . . . . . . . . . . . . . . . Nov./Dec.-04
(1 - 1)
TM
TM
12. It provides five hardware interrupts : TRAP, RST 7.5, RST 6.5, RST 5.5 and INTR.
13. It has serial I/O control which allows serial communication.
14. It provides control signals (IO/M, RD, WR) to control the bus cycles and hence
external bus controller is not required.
15. The external hardware (another microprocessor or equivalent master) can detect
which machine cycle microprocessor is executing using status signals
(IO/M, S0, S1). This feature is very useful when more than one processors are
using common system resources (memory and I/O devices).
16. It has a mechanism by which it is possible to increase its interrupt handling
capacity.
17. The 8085 has an ability to share system bus with Direct Memory Access controller.
This feature allows to transfer large amount of data from I/O device to memory or
from memory to I/O device with high speeds.
18. It can be used to implement three chip microcomputer with supporting I/O
devices like IC 8155 and IC 8355.
Fig. 1.1 (See Fig. 1.1 on next page) shows the architecture of 8085.
It consists of various functional blocks as listed below :
· Registers
· Arithmetic and Logic Unit
· Instruction decoder and machine cycle encoder
· Address buffer
· Address/Data buffer
· Incrementer/Decrementer address latch
· Interrupt control
· Serial I/O control
· Timing and control circuitry.
TM
2. Temporary Registers
a) Temporary data register b) W and Z registers.
2. Temporary Registers :
a) Temporary Data Register : The ALU has two inputs. One input is supplied by the
accumulator and other from temporary data register. The programmer cannot access this
temporary data register. However, it is internally used for execution of most of the
arithmetic and logical instructions.
For example : ADD B is the instruction in the arithmetic group of instructions which
adds the contents of register A and register B and stores result in register A. The addition
operation is performed by ALU. The ALU takes inputs from register A and temporary
data register. The contents of register B are transferred to temporary data register for
applying second input to the ALU.
b) W and Z registers : W and Z registers are temporary registers. These registers are
used to hold 8-bit data during execution of some instructions. These registers are not
available for programmer, since 8085 uses them internally.
TM
S Z X AC X P X CY
c) Instruction Register : In a typical processor operation, the processor first fetches the
opcode of instruction from memory (i.e. it places an address on the address bus and
memory responds by placing the data stored at the specified address on the data bus). The
CPU stores this opcode in a register called the instruction register. This opcode is further
sent to the instruction decoder to select one of the 256 alternatives.
TM
The 8085 executes seven different types of machine cycles. It gives the information
about which machine cycle is currently executing in the encoded form on the S0, S1 and
IO/M lines. This task is done by machine cycle encoder.
TM
Fig. 1.4 (a) and (b) show 8085 pin configuration and functional pin diagram of 8085
respectively. The signals of 8085 can be classified into seven groups according to their
functions.
+5 V GND
1 2 40 20
X1 X2 VCC VSS
X1 1 40 VCC Serial SID 5
I/O
SOD 4
X2 2 39 HOLD ports
28
RESET OUT 3 38 HLDA A15
High-order
SOD 4 37 A8 address bus
CLK(OUT)
TRAP 6 21
SID 5 36 RESET IN RST 7.5 7
Externally initiated signals
19
RST 6.5 8
TRAP 6 35 READY AD0
RST 5.5 9 Multiplexed
RST 7.5 7 34 IO / M address / data
INTR 10
AD7 bus
RST 6.5 8 33 S1
12
READY 35
RST 5.5 9 32 RD
HOLD 39
INTR 10 31 WR RESET IN 36
8085A
INTA 11 30 ALE 8085A
AD0 12 29 S0
acknowledgment
INTA 11
External signal
AD7 19 22 A9
VSS 20 21 A8 3 37
TM
LC Tuned Circuit :
It is a LC resonant tank circuit. The
resonant frequency for this circuit is given by
X1
1
L C fr =
2p L ( C ext + C int )
X2
Where Cint is the internal capacitance and
it is normally 15 pF. The output frequency of
Fig. 1.6 LC circuit this circuit has 10 % variations. To minimize
the variations in the output frequency, it is
recommended to have C ext at least twice that of Cint i.e. 30 pF.
TM
+5 V
X1 Pull-up
resistance
Crystal
External
X1
X2 clock
8085
C
Non-connected X2
(NC)
Fig. 1.8 Crystal clock circuit Fig. 1.9 External frequency source
External Clock :
Fig. 1.9 shows how to drive clock input of 8085 with external frequency source. Here
external clock is applied at X 1 input and X 2 input is kept open.
IC 74LS373
AD0 D Q A0
AD1 A1
AD2 A2
CLK
AD3 A3
AD4 A4
AD5 A5
AD6 A6
AD7 A7
G OC
Enable Output control
ALE
D0
D1
D2
D3
D4
D5
D6
D7
+5 V
IN 4148 75 K
To 8085
Reset
100 W
1 mF
No
contact
TM
As we know that, after power up or reset 8085 fetches its first instruction from 0000H
address, and it has to be the first instruction from monitor program. Therefore EPROM
consisting of monitor program must be located from address 0000H in any
8085 microprocessor system.
8085
IO/M
MEMR
RD
WR
MEMW
IOR
IOW
TM
0 0 1 0 1 1 1
0 1 0 1 0 1 1
0 1 1 1 1 1 1
1 0 1 1 1 0 1
1 1 0 1 1 1 0
1 1 1 1 1 1 1
Table 1.1
Same truth table can be implemented using 3:8 decoder as shown in Fig. 1.13.
+5V
G VCC Y0
Y1
MEMR
Y2
3:8 MEMW
WR A Decoder Y3
RD B Y4
IO/M C (74LS138) Y5
IOR
Y6
IOW
Y7
G1 G2
TM
Unidirectional Buffers : 1
20
1A1 VCC 1G 1Y1
As we know, the address bus is 2 18
unidirectional, 8-bit unidirectional buffer,
1A2 1Y2
74LS244 is used to buffer higher address 4 16
Bi-directional Buffer : 17
2A8 2Y4
3
9 A8 B8 11
DIR G
1 19
Direction Enable
control
1 19 10
+5 V +5 V
40 35 20
AD7
1 VCC READY 18 VCC 19
X1 A7
17 16
A15 28 A6
2MHz 14 15
crystal 2 74LS373 A5 Low
X2 A8 13 12
20 pF A4 order
21 8 9 A3 Address
7 6
AD7 19 A2 bus
4 4 5
SOD AD0 A1
AD0 3 2
5 SID G OC GND A0
12
33 S 11 1 10
1 30
NC 29 8085 A ALE
S0 34 +5 V
37 IO/M
CLK OUT 32
38 RD 9 11
100K HLDA 31 D7
11 WR 8 12 D6
INTA 7 13
36 74LS245 D5
6 14
RESET IN Bidirectional D4 Data
1mF 6 5 15
TRAP Bus D3 bus
1K 7 4 16
Driver D2
RST7.5 3 17
8 D1
RST6.5 2 18 D0
RESET DIR G GND
9
RST5.5 OUT
10 1 19 10 20
INTR
From 39
Interrupt HOLD +5 V
source Vss
6 16
20 VCC
G3
O7
O6 9
IO/M IOW Control
3 74LS138 10 bus
A O5 IOR
RD 2 2 3-to-8 13
A decoder O2 MEMW
WR 1 1 14
A O1 MEMR
5 0
G2 O0
G1 GND
4 8
It also shows clock and reset circuits. Interrupt lines which are not in use are
grounded. This is necessary because floating interrupt line may cause false triggering of
interrupt. Similarly, since the DMA controller is not used, HOLD line is also grounded. As
we know READY signal is used to synchronize slow peripherals with the microprocessor.
When it is low, microprocessor enters in the wait state and when it is high, it indicates
that the memory or peripheral is ready to send or receive data. Here, the READY signal is
tied high to prevent the microprocessor from entering the wait state. ALE signal is
connected to the clock input of the latch, to latch the low order address in T1 of the
machine cycle. To control the direction of the bi-directional buffer 74LS245, RD signal from
8085 is connected to DIR input of the bi-directional buffer. Thus, when RD signal is low,
DIR is low and data flows from memory or I/O device to the microprocessor, performing
read operation. When RD signal is high, DIR is high and data flows from microprocessor
to memory or I/O device performing write operation.
Review Questions
Section 1.1
Q.1 Explain architectural features of 8085. May-04, Marks 4
Section 1.2
Q.1 With neat functional block diagram, explain the architecture of 8085 microprocessor.
June-06, Dec.-07, Dec.-04,08,09,10, May-04,10,11,12, Marks 16
Q.2 Explain the architecture, data flow and instruction execution of 8085 microprocessor.
May-11, Marks 8
Q.3 Give the format of flag register in 8085. Explain each flag.
Q.4 Define the function of parity flag and zero flag in 8085. June-12, Marks 2
Section 1.3
Q.1 Write about the pin configuration of 8085 processor and explain them in detail.
June-09, Marks 16
TM
Section 1.4
Q.1 Draw the schematic of latching low-order address bus in 8085 microprocessor.
Dec.-11, Marks 2
TM
Q.10 What is the need for ALE signal in 8085 microprocessor? Dec.-04,09, May-10
Ans. : The ALE signal is used to demultiplex (separate) AD0 - AD7 lines to A0 - A7
(address lines) and D0 - D7 (data lines). The separation of address lines and data lines is
achieved by connecting a external latch to AD0 - AD7 lines and enabling the latch when
ALE signal is active.
Q.13 If a 5 MHz crystal is connected with 8085; what is the value of system clock
frequency and one T-state ? Dec.-07
Crystal frequecny 5 MHz
Ans. : System clock frequency = = = 2.5 MHz,
2 2
1
one T-state = = 0.4 µsec.
2.5 ´ 10 6
Q.14 What are the important control signals in 8085 microprocessor ? Dec.-08
Ans. : The important control signals in 8085 microprocessor are : ALE, IO M, RD and
WR
TM
Contents
2.1 Instruction Classification
2.2 Instruction Set of 8085 . . . . . . . . . . . . . . . . . . May/June-07,09; April/May-04, 10
. . . . . . . . . . . . . . . . . . Nov./Dec.-07, 08, 09
2.3 Addressing Modes
2.4 Instruction Set Summary
2.5 Assembly Language Programming
2.6 Programming Examples
2.7 Instruction Comparisons
2.8 Instruction Formats
(2 - 1)
TM
In the previous chapter we have studied block diagram of microprocessor 8085. The
block diagram shows microprocessor's functions for data processing and data handling. It
also shows how each of these logic functions are connected together. Such microprocessor
performs a particular task by executing proper sequence of instructions. Thus to perform a
task in a particular microprocessor system, programmer has to know the instructions
supported by microprocessor used in the microprocessor system.
This chapter explains the set of instructions supported by the 8085 microprocessor and
explains how to write programs (set of instructions written in a proper sequence to
perform a particular task) using them. This chapter also gives a large number of programs
to perform different tasks.
TM
of B and C registers cannot be added directly. To add two 16-bit numbers the
8085 provides DAD instruction. It adds the data within the register pair to the
contents of the HL register pair and resulted sum is stored in the HL register
pair.
· Subtraction : Any 8-bit number, or the contents of a register, or the contents of a
memory location can be subtracted from the contents of the accumulator and the
result is stored in the accumulator. The resulted borrow bit is stored in the carry
flag. In 8085, no two other registers can be added directly.
· Increment/Decrement : The 8085 has the increment and decrement instructions
to increment and decrement the contents of any register, memory location or
register pair by 1.
TM
In this section, the instructions from all groups are explained with the help of
examples. Before to discuss these instructions, let us get familiar with the notations used in
the explanation of instructions. These are:
Notation Meaning
M Memory location pointed by HL register pair
r 8-bit register
rp 16-bit register pair
rs Source register
rd Destination register
addr 16-bit address / 8-bit address
1. MVI r, data (8) This instruction directly loads a specified register with an 8-bit data
given within the instruction. The register r is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Example :
MVI B, 60H ; This instruction will load 60H directly into the B register.
2. MVI M, data (8) This instruction directly loads an 8-bit data given within the
instruction into a memory location. The memory location is specified
by the contents of HL register pair.
204FH 204FH
2051H 2051H
TM
3. MOV rd, rs This instruction copies data from the source register into destination
register. The rs and rd are general purpose registers such as A, B, C,
D, E, H and L. The contents of the source register remain unchanged
after execution of the instruction.
Operation : rd ¬ rs
Example : A = 20H
MOV B, A ; This instruction will copy the contents of register A (20H) into
register B.
4. MOV M, rs This instruction copies data from the source register into memory
location pointed by the HL register pair. The rs is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Operation : (HL) ¬ rs
5. MOV rd, M This instruction copies data from memory location whose address is
specified by HL register pair into destination register. The contents of
the memory location remain unchanged. The rd is an 8-bit general
purpose register such as A, B, C, D, E, H and L.
Operation : rd ¬ (HL)
6. LXI rp, data (16) This instruction loads immediate 16 bit data specified within the
instruction into register pair or stack pointer. The rp is 16-bit register
pair such as BC, DE, HL or 16-bit stack pointer.
TM
Example :
LXI B,1020H ; This instruction will load 10H into B register and 20H into C
register.
7. STA addr This instruction stores the contents of A register into the memory
location whose address is directly specified within the instruction.
The contents of A register remain unchanged.
Operation : (addr) ¬ A
Example : A = 50H
STA 2000H ; This instruction will store the contents of A register (50H) to
memory location 2000H.
8. LDA addr This instruction copies the contents of the memory location whose
address is given within the instruction into the accumulator. The
contents of the memory location remain unchanged.
Operation : A ¬ (addr)
9. SHLD addr This instruction stores the contents of L register in the memory
location given within the instruction and contents of H register at
address next to it. This instruction is used to store the contents of H
and L registers directly into the memory. The contents of the H and
L registers remain unchanged.
10. LHLD addr This instruction copies the contents of the memory location given
within the instruction into the L register and the contents of the next
memory location into the H register.
11. STAX rp This instruction copies the contents of accumulator into the memory
location whose address is specified by the specified register pair. The
rp is BC or DE register pair. This register pair is used as a memory
pointer. The contents of the accumulator remain unchanged.
Operation : (rp) ¬ A
12. LDAX rp This instruction copies the contents of memory location whose
address is specified by the register pair into the accumulator. The rp
is BC or DE register pair. The register pair is used as a memory
pointer.
Operation : A ¬ (rp)
13. XCHG This instruction exchanges the contents of the register H with that of
D and of L with that of E.
Operation : H « D and L « E
TM
1. ADD r This instruction adds the contents of the specified register to the
contents of accumulator and stores result in the accumulator. The r
is 8-bit general purpose register such as A, B, C, D, E, H and L.
Operation : A ¬A + r
2. ADD M This instruction adds the contents of the memory location pointed by
HL register pair to the contents of accumulator and stores result in
the accumulator. The HL register pair is used as a memory pointer.
This instruction affects all flags.
Operation : A ¬A + M
3. ADI data (8) This instruction adds the 8 bit data given within the instruction to
the contents of accumulator and stores the result in the accumulator.
Example : A = 50H
ADI 70H ; This instruction will add 70H to the contents of the accumulator
(50H) and it will store the result in the accumulator (C0H).
4. ADC r This instruction adds the contents of specified register to the contents
of accumulator with carry. This means, if the carry flag is set by
some previous operation, it adds 1 and the contents of the specified
register to the contents of accumulator, else it adds the contents of
the specified register only. The r is 8-bit general purpose register
such as A, B, C, D, E, H and L.
TM
Operation : A ¬ A + r + CY
Operation : A ¬ A + M + CY
6. ACI data (8) This instruction adds 8 bit data given within the instruction to the
contents of accumulator with carry and stores result in the
accumulator.
7. DAD rp This instruction adds the contents of the specified register pair to the
contents of the HL register pair and stores the result in the HL
register pair. The rp is 16-bit register pair such as BC, DE, HL or
stack pointer. Only higher order register is to be specified for register
pair within the instruction.
Operation : HL ¬ HL + rp
TM
DAD D ; This instruction will add the contents of DE register pair, 1020H to
the contents of HL register pair, 2050H. It will store the result,
3070H in the HL register pair.
8. SUB r This instruction subtracts the contents of the specified register from
the contents of the accumulator and stores the result in the
accumulator. The register r is 8-bit general purpose register such as
A, B, C, D, E, H and L.
Operation : A ¬A – r
Operation : A ¬A – M
10. SUI data (8) This instruction subtracts an 8 bit data given within the instruction
from the contents of the accumulator and stores the result in the
accumulator.
Example : A = 40H,
SUI 20H ; This instruction will subtract 20H from the contents of accumulator
(40H). It will store the result (20H) in the accumulator.
TM
11. SBB r This instruction subtracts the specified register contents and borrow
flag from the accumulator contents. This means, if the carry flag
(borrow for subtraction) is set by some previous operation, it
subtracts 1 and the contents of the specified register from the
contents of accumulator, else it subtracts the contents of the specified
register only. The register r is 8-bit register such as A, B, C, D, E, H
and L.
Operation : A ¬ A – r – CY
SBB C ; This
; instruction will subtract the contents of C register (20H) and
carry flag (1) from the contents of accumulator (40H). It will store
the result (40H – 20H – 1 = 1FH) in the accumulator.
12. SBB M This instruction subtracts the contents of memory location pointed by
HL register pair from the contents of accumulator and borrow flag
and stores the result in the accumulator.
Operation : A ¬ A – M – CY
13. SBI data (8) This instruction subtracts 8 bit data given within the instruction and
borrow flag from the contents of accumulator and stores the result in
the accumulator.
Operation : A ¬ A - data(8) - CY
TM
14. DAA This instruction adjusts accumulator to packed BCD (Binary Coded
Decimal) after adding two BCD numbers.
Instruction works as follows :
1. If the value of the low - order four bits (D3-D0) in the accumulator
is greater than 9 or if auxiliary carry flag is set, the instruction adds
6 (06) to the low-order four bits.
2. If the value of the high-order four bits (D7 - D4) in the
accumulator is greater than 9 or if carry flag is set, the instruction
adds 6 (60) to the high-order four bits.
Example :
If, A = 0011 1001 = 39 BCD
and C = 0001 0010 = 12 BCD then
ADD C ; Gives A = 0100 1011 = 4BH
DAA ; adds 0110 because 1011>9,
; A=0101 0001 = 51 BCD
If A = 1001 0110 = 96 BCD
and D = 0000 0111 = 07 BCD then
ADD D ; Gives A = 1001 1101 = 9DH
DAA ; adds 0110 because 1101 > 9,
A = 1010 0011 = A3H,
1010 > 9 so adds 0110 0000,
A = 0000 0011 = 03 BCD, CF = 1.
Operation : r ¬r + 1
Example : B = 10H
INR B ; This instruction will increment the contents of B register (10H) by
one and stores the result (10+1 = 11H) in the same i.e. B register.
16. INR M This instruction increments the contents of memory location pointed
by HL register pair by 1. The result is stored at the same memory
location. The HL register pair is used as a memory pointer.
Operation : M¬ M+1
TM
17. INX rp This instruction increments the contents of register pair by one. The
result is stored in the same register pair. The rp is register pair such
as BC, DE, HL or stack pointer (SP).
Operation : rp ¬ rp + 1
Example : HL = 10FFH
INX H ; This instruction will increment the contents of HL register pair
(10FFH) by one. It will store the result (10FF + 1 = 1100H) in the
same i.e. HL register pair.
18. DCR r This instruction decrements the contents of the specified register by
one. It stores the result in the same register. The register r is 8-bit
general purpose register such as A, B, C, D, E, H and L.
Operation : r ¬r – 1
Example : E = 20H
DCR E ; This instruction will decrement the contents of E register (20H) by
one. It will store the result (20 – 1 = 1FH) in the same, i.e. E
register.
19. DCR M This instruction decrements the contents of memory location pointed
by HL register pair by 1. The HL register pair is used as a memory
pointer. The result is stored in the same memory location.
Operation : M ¬M – 1
20. DCX rp This instruction decrements the contents of register pair by one. The
result is stored in the same register pair. The rp is register pair such
TM
Operation : rp ¬ rp – 1
Example : DE = 1020H
DCX D ; This instruction will decrement the contents of DE register pair
(1020H) by one and store the result (1020 – 1 = 101FH) in the
same, DE register pair.
Operation : A¬ AÙr
1010 1010
0000 1111
————————
0 0 0 0 1 0 1 0 = 0AH
2. ANA M This instruction logically ANDs the contents of memory location
pointed by HL register pair with the contents of accumulator. The
result is stored in the accumulator. The HL register pair is used as a
memory pointer.
Operation : A¬ AÙM
Example : A = 01010101 = (55H), HL = 2050H(2050H) ® 10110011 = (B3H)
ANA M ; This instruction will logically AND the contents of memory location
pointed by HL register pair (B3H) with the contents of accumulator
0101 0101
(55H). It will store the result (11H) in the accumulator.
1011 0011
–––––––––––––––
0 0 0 1 0 0 0 1 = 11H
TM
3. ANI data This instruction logically ANDs the 8 bit data given in the instruction
with the contents of the accumulator and stores the result in the
accumulator.
Operation : A ¬ A Ù data (8)
1 1 1 1 0 0 0 0 Masking pattern
X X X X 0 0 0 0 Result
Masked bits
TM
Operation : A ¬ A Å data
Example : A = 10110011 = (B3H)
XRI 39H ; This instruction will logically XOR the contents of accumulator
1011 0011 (B3H) with 39H. It will store the result (8AH) in the accumulator.
00 1 1 1 0 0 1
————————
1 0 0 0 1 0 1 0 = 8AH
The XOR instruction is used if some bits of a register or memory location must be
inverted. This instruction allows part of a number to be inverted or complemented. This is
illustrated in Fig. 2.2.
X X X X X X X X Result
Inverted bits
Fig. 2.2 Inversion of part of a number using XOR operation
7. ORA r This instruction logically ORs the contents of specified register with
the contents of accumulator and stores the result in the accumulator.
Each bit in the accumulator is ORed with corresponding bit in
register r. i.e. D0 bit in accumulator is ORed with D 0 bit in register r,
D1 in A with D1 in r and so on upto D7 bit. The register r is 8-bit
general purpose register such as A, B, C, D, E, H and L.
Operation : A¬ AÚ r
+ 1 1 1 1 0 0 0 0 Setting pattern
1 1 1 1 X X X X Result
Set bits
Operation : A– r
TM
11. CMP M This instruction subtracts the contents of the memory location
specified by HL register pair from the contents of the accumulator
and sets the condition flags as a result of subtraction. It sets zero flag
if A = M and sets carry flag if A < M. The HL register pair is used
as a memory pointer.
Operation : A – M
Example : A = 1011 1000 (B8H), HL = 2050H and (2050H) = 1011 1000 (B8H)
CMP M ; This instruction will compare the contents of memory location
(B8H) and the contents of accumulator. Here A = M so zero flag
will set after the execution of the instruction.
12. CPI data This instruction subtracts the 8 bit data given in the instruction from
the contents of the accumulator and sets the condition flags as a
result of subtraction. It sets zero flag if A = data and sets carry flag
if A < data.
Operation : CY ¬ 1
TM
Operation : CY ¬ CY
Operation :
Before Execution
CY B7 B6 B5 B4 B3 B2 B1 B0
After Execution
B7 B6 B5 B4 B3 B2 B1 B0 B7
2. RRC This instruction rotates the contents of the accumulator right by one
position. Bit B0 is placed in B7 as well as in CY.
TM
Operation :
Before execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
After execution
B0 B7 B6 B5 B4 B3 B2 B1 B0
Operation :
Before execution
CY B7 B6 B5 B4 B3 B2 B1 B0
After execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
4. RAR This instruction rotates the contents of the accumulator right by one
position. Bit B0 is placed in CY and CY is placed in B7.
TM
Operation :
Before execution
B7 B6 B5 B4 B3 B2 B1 B0 CY
After execution
CY B7 B6 B5 B4 B3 B2 B1 B0
1. PUSH rp This instruction decrements stack pointer by one and copies the
higher byte of the register pair into the memory location pointed by
stack pointer. It then decrements the stack pointer again by one and
copies the lower byte of the register pair into the memory location
pointed by stack pointer. The rp is 16-bit register pair such as BC,
DE, HL. Only higher order register is to be specified within the
instruction.
TM
SP Lower byte
SP
B C B C 10
1FFFH PUSH D 1FFFH
D 10 E 50 D 10 E 50
2000H 2000H
H L H L
2. PUSH PSW This instruction decrements stack pointer by one and copies the
accumulator contents into the memory location pointed by stack
pointer. It then decrements the stack pointer again by one and copies
the flag register into the memory location pointed by the stack
pointer.
Operation : SP ¬ SP – 1
(SP) ¬ A
SP ¬ SP – 1
(SP) ¬ Flag register
TM
PUSH PSW ; This instruction decrements the stack pointer (SP = 2000H) by one
(SP = 1FFFH) and copies the contents of the accumulator (20H)
into the memory location 1FFFH. It then decrements the stack
pointer again by one (SP = 1FFEH) and copies the contents of the
flag register (80H) into the memory location 1FFEH.
20 80 PUSH PSW 20 80
1FFFH 1FFFH 20
2000H 2000H
SP
(i.e. data 50H) into B register, and increment the stack pointer
again by one.
2002H 2002H
4. POP PSW This instruction copies the contents of memory location pointed by
the stack pointer into the flag register and increments the stack
pointer by one. It then copies the contents of memory location
pointed by stack pointer into the accumulator and increments the
stack pointer again by one.
2002H 2002H
5. SPHL This instruction copies the contents of HL register pair into the stack
pointer. The contents of H register are copied to higher order byte of
stack pointer and contents of L register are copied to the lower byte
of stack pointer.
Operation : SP ¬ HL
TM
Example : HL = 2500H
SPHL ; This instruction will copy 2500H into stack pointer. So after
execution of instruction stack pointer contents will be 2500H.
Operation : L « (SP)
H « (SP + 1)
30 40 60 XTHL 60 50 30
2701H 2701H
2702H 2702H
1. JMP addr This instruction loads the PC with the address given within the
instruction and resumes the program execution from this location.
Operation : PC ¬ addr
Example :
JMP 2000H ; This instruction will load PC with 2000H and processor will fetch
next instruction from this address.
2. Jcond addr This instruction causes a jump to an address given in the instruction
if the desired condition occurs in the program before the execution of
the instruction. The Table 2.1 shows the possible conditions for
jumps.
TM
Note : The stack is a part of read/write memory set aside for storing
intermediate results and addresses.
3000H 3000H
4. C cond addr This instruction calls the subroutine at the given address if a
specified condition is satisfied. Before call it stores the address of
instruction next to the call on the stack and decrements stack pointer
by two. The Table 2.2 shows the possible conditions for calls.
Instruction code Description Condition for CALL
CC Call on carry CY = 1
CNC Call on not carry CY = 0
CP Call on positive S = 0
CM Call on minus S = 1
CPE Call on parity even P = 1
CPO Call on parity odd P = 0
CZ Call on zero Z = 1
CNZ Call on not zero Z = 0
5. RET This instruction pops the return addr (address of the instruction next
to CALL in the main program) from the stack and loads program
counter with this return address. Thus transfers program control to
the instruction next to CALL in the main program.
TM
SP ® 27FD 00
27FE 62
27FF
RET ; This instruction will load PC with 6200H and it will transfer
program control to the address 6200H. It will also increment the
stack pointer by two.
Before Execution After Execution
27FFH 27FFH
6. R condition This instruction returns the control to the main program if the
specified condition is satisfied. Table 2.3 shows the possible
conditions for return.
Instruction code Description Condition for RET
RC Return on carry CY = 1
RNC Return on not carry CY = 0
RP Return on positive S = 0
RM Return on minus S = 1
RPE Return on parity even P = 1
RPO Return on parity odd P = 0
RZ Return on zero Z = 1
RNZ Return on not zero Z = 0
Table 2.3 Conditions for return
7. PCHL This instruction loads the contents of HL register pair into the
program counter. Thus the program control is transferred to the
location whose address is in HL register pair.
Operation : PC ¬ HL
Example : HL = 6000H
PCHL ; This instruction will load 6000H into the program counter.
TM
8. RST n This instruction transfers the program control to the specific memory
address as shown in Table 2.4. This instruction is like a fixed address
CALL instruction. These fixed addresses are also referred to as vector
addresses. The processor multiplies the RST number by 8 to calculate
these vector addresses. Before transferring the program control to the
instruction following the vector address RST instruction saves the
current program counter contents on the stack like CALL instruction
Instruction code Vector Address
RST 0 0 ´ 8 = 0000H
RST 1 1 ´ 8 = 0008H
RST 2 2 ´ 8 = 0010H
RST 3 3 ´ 8 = 0018H
RST 4 4 ´ 8 = 0020H
RST 5 5 ´ 8 = 0028H
RST 6 6 ´ 8 = 0030H
RST 7 7 ´ 8 = 0038H
Table 2.4 Vector addresses for return instructions
Example : SP = 3000H
2000H RST 6 ; This instruction will save the current contents of the program
counter (i.e. address of next instruction 2001H) on the stack and it
will load the program counter with vector address
6 ´ 8= 4810= 30H) 0030H.
1. IN addr(8-bit) This instruction copies the data at the port whose address is specified
in the instruction into the accumulator.
Operation : A ¬ (addr)
Example : Port address = 80H, data stored at port address 80H, (80H) = 10H
IN 80H ; This instruction will copy the data stored at address 80H, i.e. data
10H in the accumulator.
2. OUT addr(8-bit) This instruction sends the contents of accumulator to the output port
whose address is specified within the instruction.
TM
Operation : (addr) ¬ A
Example : A = 40H
OUT 50H ; This instruction will send the contents of accumulator(40H) to the
output port whose address is 50H.
Operation : IE (F/F) ¬ 1
Operation : IE (F/F) ¬ 0
5. SIM This instruction masks the interrupts as desired. It also sends out
serial data through the SOD pin. For this instruction command byte
must be loaded in the accumulator.
D7 D6 D5 D4 D3 D2 D1 D0
TM
Example : i) A = 0EH
D7 D6 D5 D4 D3 D2 D1 D0
Register A
SOD SOE X RST7.5 MSE M7.5 M6.5 M5.5
0 0 0 0 1 1 1 0 = 0EH
SIM ; This instruction will mask RST 7.5 and RST 6.5 interrupts where as
RST 5.5 interrupt will be unmasked. It will also disable serial
output.
6. RIM : This instruction copies the status of the interrupts into the
accumulator. It also reads the serial data through the SID pin.
D7 D6 D5 D4 D3 D2 D1 D0
Example :
RIM ; After execution of RIM instruction if the contents of accumulator
are 4BH then we get following information.
D7 D6 D5 D4 D3 D2 D1 D0
Example :
MVI A, 20H ; Moves 8 bit immediate data (20H) into accumulator
MVI M, 30H ; Moves 8 bit immediate data (30H) into the
; memory location pointed by HL register pair.
LXI SP, 2700H ; Moves 16 bit immediate data (2700H) into SP.
LXI D, 10FFH ; Moves 16 bit immediate data (10FFH) into DE
; register pair ( D = 10H and E = FFH).
Example :
MOV A, B ; Moves the contents of register B into the accumulator.
SPHL ; Moves the contents of HL register pair into stack pointer.
ADD C ; Adds the contents of register C into the contents of accumulator
; and stores result in the accumulator.
Example :
LDA 2000H ; Loads the 8 bit contents of memory location
; 2000H into the accumulator.
SHLD 3000H ; Stores the HL register pair into two consecutive memory
; locations. Lower byte i.e. the contents of L register into memory
; location 3000H and higher byte i.e. the contents of H register
; into memory location 3001H.
TM
Example :
LDAX B ; Loads the accumulator with the contents of
; memory location pointed by BC register pair.
MOV M, A ; Stores the contents of accumulator into the
; memory location pointed by HL register pair.
Example :
CMA ; Complements contents of accumulator.
RAL ; Rotates the contents of accumulator left through carry.
Note : Many of the advanced processors support addressing mode called index
addressing mode. In this mode, the address of the operand within the memory is
generated by adding the offset/displacement to the register specified in the instruction.
The offset/displacement is also a part of the instruction. In 8085 such addressing mode is
not available. However, we can implement such kind of program structure, by using
memory pointer (HL register), any other register pair and a instruction sequence given
below :
LXI H, Base_addr ; Loads the base address
LXI B, Offset/Displacement ; Loads the offset or displacement
DAD B ; Gives the addition of HL and BC in HL register pair.
MOV A, M ; Load the data from memory in the accumulator
By incrementing or decrementing contents of BC register or loading another contents,
we can change the index/offset/displacement.
TM
13 XCHG H « D, L « E No 1 Register
Arithmetic Group
7. DAD rp HL ¬ HL + rp CY 1 Register
TM
Logic Group
Rotate Group
Branch Group
3. PCHL PC ¬ HL No 1 Register
TM
Stack Group
TM
5. SPHL SP ¬ HL No 1 Register
Input/Output Group
1. EI IE(F/F) ¬ 1 No 1 –
2. DI IE(F/F) ¬ 0 No 1 –
3. NOP No operation No 1 –
TM
2.5.2 Flowchart
To develop the programming logic programmer has to
write down various actions which are to be performed in
proper sequence. The flow chart is a graphical tool that
allows programmer to represent various actions which are to
be performed. The graphical representation is very useful for
clear understanding of the programming logic.
The Fig. 2.7 shows the graphic symbols used in the
flowchart.
Oval : It indicates start or stop operation.
Arrow : It indicates flow with direction.
Parallelogram : It indicates input/output operation.
Rectangle : It indicates process operation.
Diamond : It indicates decision making operation.
A Double sided Rectangle : It indicates execution of
Fig. 2.7 Graphic symbols pre-defined process (subroutine).
used in flowchart Circle with alphabet : It indicates continuation.
TM
A : Any alphabet
The Fig. 2.8 shows sample flowchart.
Start
Input process
parameters
Call subroutine
Process
Display results
Stop
Let us define a program statement as 'write an assembly language program to add two
numbers'. The three tasks are involved in this program :
· Load two hex numbers
· Add numbers and
· Store the result in the memory
These tasks can be symbolically presented as flow chart, as shown in the Fig. 2.9.
(See Fig. 2.9 on next page)
Next job is to find the suitable 8085 assembly language instruction/s for each task.
These instructions are as follows :
Task 1 instructions :
MVI A, 20H ; Load 20H as a first number in register A
MVI B, 40H ; Load 40H as a second number in register B
Task 2 instruction :
ADD B ; Add two numbers and save result in register A
TM
Start
Add
numbers
Store the
result
Stop
TM
TM
Flowchart
Sample problem
(2000H) = 14H Start
(2001H) = 89H
Result = 14H + 89H = 9DH
Get the first number
Source program
LXI H, 2000H ; HL points 2000H Get the second number
MOV A, M ; Get first operand
INX H ; HL points 2001H
ADD M ; Add second operand Add two numbers
INX H ; HL points 2002H
MOV M, A ; Store result at 2002H
HLT ; Terminate program execution Store the result
TM
Flowchart
Start
End
TM
Source Program 1
LHLD 2000H ; Get first 16-bit number in HL
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
MOV A, E ; Get lower byte of the first number
ADD L ; Add lower byte of the second number
MOV L, A ; Store result in L register
MOV A, D ; Get higher byte of the first number
ADC H ; Add higher byte of the second number with carry
MOV H, A ; Store result in H register
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution
Source program 2
LHLD 2000H ; Get first 16-bit number
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
DAD D ; Add DE and HL
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution
In program 1 eight bit addition instructions are used (ADD and ADC) and addition is
performed in two steps. First lower byte addition using ADD instruction and then higher
byte addition using ADC instruction. In program 2 16-bit addition instruction (DAD) is
used.
TM
Source program
LHLD 2000H ; Get first 16-bit number in HL
XCHG ; Save first 16-bit number in DE
LHLD 2002H ; Get second 16-bit number in HL
MOV A, E ; Get lower byte of the first number
SUB L ; Subtract lower byte of the second number
MOV L, A ; Store the result in L register
MOV A, D ; Get higher byte of the first number
SBB H ; Subtract higher byte of second number with borrow
MOV H, A ; Store 16-bit result in memory locations 2004H and 2005H.
SHLD 2004H ; Store 16-bit result in memory locations 2004H and 2005H.
HLT ; Terminate program execution.
Flowchart
START
END
TM
Lab Experiment 7 : Check results after execution of INR B, INR C and INX B instructions.
Statement : If the contents of B = FFH and C = FFH then after execution of following
instructions give the contents of register B and register C.
Instructions :
1. INR B
2. INR C
3. INX B
1. INR B
B ® FFH
+ 01H
®B
00H
\ B = 00H and C = FFH
2. INR C
C ® FFH
+ 01H
00H ® C
\ B = FFH and C = 00H
3. INX B
BC ® FF FF H
+ 00 01 H
00 00 H ® B C
\ B = 00H and C = 00H
Lab Experiment 8 : Check results after execution of DCR C, DCR B and DCX B instructions.
Statement : If the contents of B = 00H and C = 00H then after execution of following
instructions give the contents of register B and register C.
Instructions :
1. DCR C
2. DCR B
3. DCX B
1. DCR C
C ® 00H
– 01H
FFH ®C
\ B = 00H and C = FFH
TM
2. DCR B
B ® 00H
– 01H
FFH ® B
\ B = FFH and C = 00H
3. DCX B
BC ® 0 0 0 0 H
– 0 0 0 1 H
F F F F H ® BC
\ B = FFH and C = FFH
Source program
LDA 2200 H ; Get the number Complement the
number
CMA ; Complement the number
ADI, 01H ; Add one in the number
Add one
STA 2300H ; Store the result
HLT ; Terminate program execution
Store the result
End
TM
Flowchart
(See flowchart on next page)
Source program
LDA 2200H ; Get the packed BCD number
ANI F0H ; Mask lower nibble
RRC
RRC
RRC
RRC ; Adjust higher BCD digit as a lower digit
TM
Start
END
TM
Subroutine program :
6100H SUB : PUSH B
6101H PUSH H
6102H LXI B, 4080H
6105H LXI H, 4090H
6108H DAD B
6109H SHLD 2200H
610CH POP H
610DH POP B
610EH RET
Solution : The Table 2.5 shows the instruction sequence and the contents of all registers
and stack after execution of each instruction.
Table 2.5
Sample problem :
(2000H) = 7FH
(2001H) = 89H
Result = 7FH + 89H = 108H
(2002H) = 08H
(2003H) = 01H
Flowchart
Start
End
Source program :
LXI H, 2000H ; HL Points 2000H
MOV A, M ; Get first operand
INX H ; HL Points 2001H
ADD M ; Add second operand
INX H ; HL Points 2002H
MOV M, A ; Store the lower byte of result at 2002H
MVI A, 00 ; Initialize higher byte result with 00H
ADC A ; Add carry in the high byte result
INX H ; HL Points 2003H
MOV M, A ; Store the higher byte of result at 2003H
HLT ; Terminate program execution
TM
Flowchart
Start
Rotate 4 times
right
Store result in
C register
End
TM
Flowchart
Start
Stop
Source program :
MOV A, B
RAR
MOV B, A
MOV A, C
RAR
MOV C, A
HLT
Flowchart
Start
Add HL with
HL
Stop
TM
Sample problem :
HL = 1025 = 0001 0000 0010 0101
\ HL = 0001 0000 0010 0101
+ HL = 0001 0000 0010 0101
-----------------------------------------------
Result = 0010 0000 0100 1010
Source program :
DAD H
TM
6000H LXI SP, 27FFH 6000H LXI SP, 27FFH ; Initialize stack pointer
:
:
TM
Table 2.6
TM
Flowchart :
Start
Result = Result X no
Yes If
number < 2 No = No – 1
?
No Is
No
no = 0
Result = 1 Load counter ?
initialize result
Yes
Store result
End
Subroutine Program :
FACTO : LXI H,0000H
MOV B, C ; Load counter
BACK : DAD D ;
DCR B ;
JNZ BACK ; Multiply by successive addition
XCHG ; Store result in DE
DCR C ; Decrement counter
CNZ FACTO ; Call subroutine FACTO
RET ; Return to main program
TM
Solution :
Flags This instruction affects all flags. This instruction does not affect
flags
T-states required 4 7
Solution :
Flags This instruction affects all flags This instruction affects all flags
TM
T-states required 4 4
Solution :
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states 10 16
4. Memory read
5. Memory read
Solution :
TM
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states 10 6
2. Memory read
3. Memory read
Solution :
Flags This instruction does not affect This instruction does not affect
flags flags
Required T-states T-states required are undefined 4-T states. After this instruction
because this instruction halts processor fetches the next
the processor. The processor instruction after NOP
can be restarted by a valid
interrupt or by applying a
RESET signal
TM
ß Example 2.6 List out differences and similarities between CALL - RET and PUSH-POP
instructions.
Solution : Differences :
1 These instructions are used for the These instructions are used to store register
execution of subroutine data temporarily in memory.
2 CALL instruction store the address of next PUSH instruction stores register contents in
instruction after it in the stack and loads PC the stack.
with address given in the instruction.
3 RET instruction loads the address from POP instruction gets the register contents
stack into PC. from the stack.
Similarities :
1. They use stack memory.
2. CALL and PUSH instructions decrement stack pointer by 2.
3. RET and POP instructions increment stack pointer by 2
4. Instructions do not affect flags except POP PSW instruction.
Review Questions
Section 2.1
Q.1 Explain the classification of the instruction set of 8085 microprocessor with suitable
examples.
Section 2.2
Q.1 Explain the operations carried out when 8085 executes the instructions.
i) MOV A, M ii) XCHG
iii) DAD B iv) DAA Dec.-07, Marks 16
Q.2 With suitable examples, Explain the function of various data transfer and data
manipulation instructions of 8085. May-10,11, Marks 10
Q.3 How are the 8085 instructions classified according to the functional categories ?
Dec.-11, Marks 2
Q.4 Describe with suitable examples the data transfer, loading and storing instructions.
June-12, Marks 8
Q.6 Explain the operations carried out when 8085 executes the instructions.
POP PSW Dec.-07, Marks 16
Q.7 Discuss the organizations of the 8085 stack and the various instructions that will
operate on the stack. Dec-09, June-11, Marks 10
Q.8 Describe with a suitable example the operation of stack. June-12, Marks 8
Q.9 How is PUSH B instruction executed ? Find the status after the execution.
May-11, Marks 2
Q.10 Explain the sequence of events in the execution of CALL and RET instructions.
June-07, Marks 8
Q.11 What is the use of branching instructions ? Give Example. June-12, Marks 2
Q.12 State the function of given 8085 instructions : JP, JPE, JPO, JNZ
May-11, Marks 2
Section 2.3
Q.1 With example explain the different addressing modes of 8085 and the different types
of instruction. Dec.-04, Marks 16
TM
Q.2 Define and explain the addressing modes of 8085 with example.
June-06,07,11, May-08,10,11,12 Dec.-09,10, Marks 16
Section 2.4
Q.1 Explain the instruction set of 8085 with examples. Dec.-08, Marks 16
Section 2.5
Q.1 What is program ?
Q.2 Give the steps involved in programming.
Q.3 What is flowchart ? Explain its use.
Q.4 Explain the process of writing assembly language program with the help of example.
Q.5 What do you mean by hand assembly ? Explain with the help of example.
Q.6 Explain the process of executing the program on the microprocessor training kit.
Section 2.6
Q.1 Write a program with a flowchart to multiply two 8-bit numbers.
Dec.-11, Marks 8
Q.3 Write an assembly language program for arranging an array of 8-bit unsigned number
in ascending order. June-12, Marks 8
Section 2.7
Q.1 Compare the similarities and differences of CALL and RET instructions with PUSH
and POP instructions. Dec.-11, Marks 8
Q.2 Explain the operational difference between the following pairs of instructions.
i) SPHL and XTHL ii) CALL addr and JMP addr
iii) LHLD and SHLD addr iv) XRA A and MVI A, 00H
v) INR A and ADI 01 H vi) DAD RP and DAA.
Section 2.8
Q.1 Describe the instruction format of 8085 microprocessor. May-11, Marks 4
TM
Q.2 How many operations are there in the instruction set of 8085 microprocessor ?
Ans. : There are 74 operations in the 8085 microprocessor.
Q.4 List out the five categories of the 8085 instructions. Give examples of the
instructions for each group.
Ans. :
· Data transfer group - MOV, MVI, LXI.
· Arithmetic group - ADD, SUB, INR.
· Logical group -ANA, XRA, CMP.
· Branch group - JMP, JNZ, CALL.
· Stack I/O and Machine control group – PUSH, POP, HLT.
Q.6 What is the difference between the shift and rotate instructions?
Ans. : A rotate instruction is a closed loop instruction. That is, the data moved out at
one end is put back in at the other end. The shift instruction loses the data that is
moved out of the last bit locations.
Ans. : XCHG : This instruction exchanges the contents of the register H with that of
D and of L with that of E.
SPHL : This instruction copies the contents of HL register pair into the stack
pointer. The contents of H register are copied to higher order byte of stack pointer
and contents of L register are copied to the lower byte of stack pointer. This allows
indirect way of initializing stack pointer.
TM
Ans. : The sim instruction masks the interrupts as desired. It also sends out serial
data through the SOD pin.
Q.17 Write the operation carried out when 8085 executes RST0 instruction.
Dec.-07
Ans. : When 8085 executes RST0 instruction, the program control is transferred to
memory address 0000H. Before transfer of program control RST0 instruction saves the
current program counter contents on the stack and decrements stack pointer by 2.
Q.18 Write the difference between opcode and operand. May-08
Q.22 What are the use of CALL and RET instructions of 8085? Dec-09
Ans. : Refer section 2.2.6.
Q.23 Mention the instructions used for data transfer with I/O ports. May-10
Ans. : The instructions used for data transfer with I/O ports are : 1. IN addr 2. OUT
adder
TM
Ans. :
Q.25 What do you understand by the term 'program status word' and state how it
can be read ? Dec.-10
Q.26 What is the value of register A after each of the following instructions ?
MOV A, # 26H
RR A
RR A
RR A
RR A
SWAP A Dec.-10
Ans. : A = 26 H
qqq
TM
Contents
3.1 Looping, Counting and Indexing
3.2 Timers
3.3 Code Conversion
3.4 BCD Arithmetic
(3 - 1)
TM
In the last chapter we have seen the instruction set of 8085 and some simple assembly
language programs using it. We know that, the program is an implementation of certain
logic by executing group of instructions. To implement program logic we need to take help
of some common programming techniques such as looping, counting, indexing and code
conversion.
In this chapter, we are going to study how to implement these programming
techniques using 8085 assembly language and some programming examples using them.
This chapter also introduces the BCD arithmetic and programming techniques to
implement BCD arithmetic using 8085 assembly language.
TM
Start
Is Is
No looping looping Yes
over over
? ?
Yes No
End End
Flowchart 1 Flowchart 2
2. The actual data manipulation occurs in the processing section. This is the section
which does the work.
3. The loop control section updates counters, indices (pointers) for the next iteration.
4. The result section analyzes and stores the results.
Note : The processor executes initialization section and result section only once, while it
may execute processing section and loop control section many times. Thus, the execution
time of the loop will be mainly dependent on the execution time of the processing section
and loop control section. The flowchart 1 shows typical program loop. The processing
section in this flowchart is always executed at least once. If you interchange the position of
the processing and loop control section then it is possible that the processing section may
not be executed at all, if necessary. Refer flowchart 2.
TM
Program Examples
Lab Experiment 22 : Calculate the sum of series of numbers.
Statement : Calculate the sum of series of numbers. The length of the series is in memory
location 2200H and the series itself begins from memory location 2201H.
a. Assume the sum to be 8 bit number so you can ignore carries. Store the sum at
memory location 2300H.
b. Assume the sum to be 16 bit number. Store the sum at memory locations 2300H
and 2301H.
a. Sample problem :
2200H = 04H
2201H = 20H
2202H = 15H
2203H = 13H
2204H = 22H
Result = 20 + 15 + 13 + 22 = 6AH
\ 2300H = 6AH
Flowchart :
Start
Sum=0
Pointer = 2201H
Count = (2200H)
Pointer = Pointer +1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
SUB A ; sum = 0
LXI H, 2201H ; Initialize pointer
BACK : ADD M ; SUM = SUM + data
INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
STA 2300H ; Store sum
HLT ; Terminate program execution
b. Sample problem :
2200H = 04H 2201H = 9AH
2202H = 52H 2203H = 89H 2204H = 3EH
Result = 9AH + 52H + 89H + 3EH = 1B3H
\ 2300H = B3H Lower byte 2301H = 01H Higher byte
Flowchart :
Start
Sum high = 0
Sum low = 0
Pointer = 2201H
Count = (2200H)
No Is
Carry 1
?
Yes
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum low
(2301H) = Sum high
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
LXI H, 2201H ; Initialize pointer
SUB A ; Sumlow = 0
MOV B, A ; Sumhigh = 0
BACK : ADD M ; Sum = sum + data
JNC SKIP
INR B ; Add carry to MSB of SUM
SKIP : INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; Check if counter ¹ 0 repeat
STA 2300H ; Store lower byte
MOV A, B
STA 2301H ; Store higher byte
HLT ; Terminate program execution
Lab Experiment 23 : Data transfer from memory block B1 to memory block B2.
Statement : Transfer ten bytes of data from one memory to another memory block. Source
memory block starts from memory location 2200H where as destination memory block
starts from memory location 2300H.
Flowchart :
Start
Initialize counter = 10
No Is
Count = 0
?
Yes
End
TM
Source program :
MVI C, 0AH ; Initialize counter
LXI H, 2200H ; Initialize source memory pointer
LXI D, 2300H ; Initialize destination memory pointer
BACK : MOV A, M ; Get byte from source memory block
STAX D ; Store byte in the destination memory block
INX H ; Increment source memory pointer
INX D ; Increment destination memory pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
HLT ; Terminate program execution
Sample problem :
(2200H) = 03H
(2201H) = B2H
Result = B2H + B2H + B2H
= 216H
(2300H) = 16H
(2301H) = 02H
Note : In 8085 multiplication can be done by repetitive addition.
Flowchart :
Start
Initialize second
number as a counter
Result = 0
Decrement counter
No Is
count = 0
?
Yes
End
TM
Source program :
LDA 2200H
MOV E, A
MVI D, 00 ; Get the first number in DE register pair
LDA 2201H
MOV C, A ; Initialize counter
LXI H, 0000H ; Result = 0
BACK : DAD D ; Result = result + first number
DCR C ; Decrement count
JNZ BACK ; If count ¹ 0 repeat
SHLD 2300H ; Store result
HLT ; Terminate program execution
Flowchart :
Start
Quotient = 0
Quotient = quotient + 1
Is
No dividend <
divisor
Yes
Remainder = dividend
End
TM
Sample problem :
(2200H) = 60H
(2201H) = A0H
(2202H) = 12H
Result = A060H/12H = 8E8H Quotient
and 10H remainder
(2300H) = E8H
(2301H) = 08H
(2302H) = 10H
(2303H) = 00H
Source program :
LHLD 2200H ; Get the dividend
LDA 2202H
MOV C, A ; Get the divisor
LXI D, 0000H ; Quotient = 0
BACK : MOV A, L
SUB C ; Subtract divisor
MOV L, A ; Save partial result
JNC SKIP ; If CY ¹ 1 jump
DCR H ; Subtract borrow of previous subtraction
SKIP : INX D ; Increment quotient
MOV A, H
CPI, 00 ; Check if dividend < divisor
JNZ BACK ; If no repeat
MOV A, L
CMP C
JNC BACK
SHLD 2302H ; Store the remainder
XCHG
SHLD 2300H ; Store the quotient
HLT ; Terminate program execution
TM
Flowchart :
Start
Neg number = 0
Pointer = 2201H
Count = (2200H)
No Is
MSB =1
?
Yes
Neg number =Neg number+ 1
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Neg number
End
Sample problem :
(2200H) = 04H
(2201H) = 56H
(2202H) = A9H
(2203H) = 73H
(2204H) = 82H
Result = 02 since 2202H and 2204H contain numbers with a MSB of 1.
Source program :
LDA 2200H
MOV C, A ; Initialize count
MVI B, 00 ; Negative number = 0
LXI H, 2201H ; Initialize pointer
BACK : MOV A, M ; Get the number
ANI 80H ; Check for MSB
JZ SKIP ; If MSB = 1
TM
Flowchart :
Start
Count = 2200H
Pointer = (2201H)
Max = 0
No Is
Max < (Pointer)
?
Yes
Max = (Pointer)
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Max
End
TM
Sample problem :
(2200H) = 04
(2201H) = 34H
(2202H) = A9H
(2203H) = 78H
(2204H) = 56H
Result = (2202H) = A9H.
Source program :
LDA 2200H
MOV C, A ; Initialize counter
XRA A ; Maximum = Minimum possible value = 0
LXI H, 2201H ; Initialize pointer
BACK : CMP M ; Is number > maximum
JNC SKIP
MOV A, M ; Yes, replace maximum
SKIP : INX H
DCR C
JNZ BACK
STA 2300H ; Store maximum number
HLT ; Terminate program execution
Source program :
MVI B, 00H
MVI C, 08H
MOV A, D
BACK : RAR
JNC SKIP
INR B
SKIP : DCR C
JNZ BACK
HLT
TM
Flowchart :
Start
Initialize count = 0
Initialize counter = 8
Rotate contents of
accumulator so that
LSB will go in carry
No Is
carry = 1
?
Yes
Increment count
Decrement counter
No Is
counter = 0
?
Yes
Stop
Source program :
MVI B, 09 ; Initialize counter 1
START : LXI H, 2200H ; Initialize memory pointer
MVI C, 09H ; Initialize counter 2
BACK : MOV A, M ; Get the number
INX H ; Increment memory pointer
CMP M ; Compare number with next number
JC SKIP ; If less, don’t interchange
JZ SKIP ; If equal, don’t interchange
MOV D, M
MOV M, A
DCX H
TM
MOV M, D
INX H ; Interchange two numbers
SKIP : DCR C ; Decrement counter 2
JNZ BACK ; If not zero, repeat
DCR B ; Decrement counter 1
JNZ START ; If not zero, repeat
HLT ; Terminate program execution
Flowchart :
Start
Initialize counter 1 = 09
Is
No (Pointer – 1) >(Pointer)
?
Interchange contents
Decrement counter 2
Increment memory pointer
No Is
counter 2 = 0
?
Yes
Decrement counter 1
Is
counter 1 = 0
?
Stop
TM
Sample problem :
2200H = 4H
2201H = 20H
2202H = 15H
2203H = 13H
2204H = 22H
Result = 20 + 22 = 42H
\ 2210H = 42H
Flowchart :
Start
Sum = 0
Pointer = 2201H
Count = (2200H)
Is
(Pointer) = even No
number
?
Yes
Pointer = Pointer +1
Count = Count – 1
No Is
carry = 0
?
Yes
(2300H) = Sum
End
TM
Source program :
LDA 2200H
MOV C, A ; Initialize counter
MVI B, 00H ; sum = 0
LXI H, 2201H ; Initialize pointer
BACK : MOV A, M ; Get the number
ANI 01H ; Mask Bit1 to Bit7
JNZ SKIP ; Don’t add if number is ODD
MOV A, B ; Get the sum
ADD M ; SUM = SUM + data
MOV B, A ; Store result in B register
SKIP : INX H ; Increment pointer
DCR C ; Decrement counter
JNZ BACK ; If counter 0 repeat
STA 2210H ; Store sum
HLT ; Terminate program execution
Sample program :
2200H = 4H
2201H = 9AH
2202H = 52H
2203H = 89H
2204H = 3FH
Result = 89H + 3FH = C8H
\ 2300H = 61H Lower byte
2301H = 01H Higher byte
Source program :
LDA 2200H
MOV C, A ; Initialize counter
LXI H, 2201H ; Initialize pointer
MVI E, 00 ; Sumlow = 0
MOV D, E ; Sumhigh = 0
BACK : MOV A, M ; Get the number
TM
Flowchart :
Start
Sum = 0
Pointer = 2201H
Count = (2200H)
Is
(Pointer) = odd No
number
?
Yes
Pointer = Pointer +1
Count = Count – 1
No Is
carry = 0
?
Yes
(2300H) = Sum
End
TM
Flowchart :
Start
Lookup Table
Initialize source memory pointer
Initialize destination memory pointer Address Digit Square
6100H 0 0H
Get the number
6101H 1 1H
6103H 3 9H
Store square in the
destination memory location 6104H 4 10H
6105H 5 19H
Increment source memory pointer
Increment destination memory pointer 6106H 6 24H
6107H 7 31H
Is 6108H 8 40H
No
last number
?
6109H 9 51H
Yes
Stop
TM
Source program :
LXI H, 2000H ; Initialize memory pointer
MVI B, 52H ; Initialize counter
BACK : MOV A, M ; Get the number
CMP C ; Compare with the given byte
JZ LAST ; Go last if match occurs
INX H ; Increment memory pointer
DCR B ; Decrement counter
JNZ B ; If not zero, repeat
LXI H, 0000H
SHLD 2200H
JMP END ; Store 00 at 2200H and 2201H
LAST : SHLD 2200H ; Store memory address
END : HLT ; Stop
Flowchart :
Start
Is
(Pointer) = Yes
Search byte
?
No
Yes
Store 00 as a result
Stop
TM
Flowchart :
Start
Set carry = 0
Check
No for last
digit
Yes
Stop
TM
Source program :
LXI H,6000H ; Initialize pointer1 to first number
LXI D,6100H ; Initialize pointer2 to second number
LXI B,6200H ; Initialize pointer3 to result
STC
CMC ; Carry = 0
BACK : LDAX D ; Get the digit
ADD M ; Add two digits
DAA ; Adjust for decimal
STAX B ; Store the result
INX H ; Increment pointer1
INX D ; Increment pointer2
INX B ; Increment result pointer
MOV A, L
CPI 06H ; Check for last digit
JNZ BACK ; If not last digit repeat
HLT ; Terminate program execution
Lab Experiment 35 : Add each element of array with the elements of another array.
Statement : Add 2 arrays having ten 8-bit numbers each and generate a third array of
result. It is necessary to add the first element of array1 with the first element of array-2
and so on. The starting addresses of array1, array2 and array3 are 2200H, 2300H and
2400H, respectively.
Source program :
LXI H, 2200H ; Initialize memory pointer 1
LXI B, 2300H ; Initialize memory pointer 2
LXI D, 2400H ; Initialize result pointer
BACK : LDAX B ; Get the number from array 2
ADD M ; Add it with number in array 1
STAX D ; Store the addition in array 3
INX H ; Increment pointer1
INX B ; Increment pointer2
INX D ; Increment result pointer
MOV A, L ;
CPI 0AH ; Check pointer1 for last number
JNZ BACK ; If not, repeat
HLT ; Stop
TM
Flowchart :
Start
Pointer 1 = Pointer 1 + 1
Pointer 2 = Pointer 2 + 1
Pointer 3 = Pointer 3 + 1
Yes Is
Pointer 1 <10
?
No
Stop
Source program :
LXI H, 2200H ; Initialize memory pointer1
LXI D, 2300H ; Initialize memory pointer2
MVI C, 32H ; Initialize counter
BACK : MOV A, M ; Get the number
ANI 01H ; Check for even number
JNZ SKIP ; If ODD, don’t store
MOV A, M ; Get the number
STAX D ; Store the number in result list
TM
Is No
number = even
?
Yes
(Pointer 2) number
Pointer 2 = pointer 2 + 1
Pointer 1 = pointer 1 + 1
Counter = counter – 1
No Is
counter = 0
?
Yes
End
TM
Source program :
Two blocks (3000 – 30FF and 3050 – 314F) are overlapping. Therefore it is necessary to
transfer last byte first and first byte last.
MVI C, FFH ; Initialize counter
LXI H, 30FFH ; Initialize source memory pointer
LXI D, 314FH ; Initialize destination memory pointer
BACK : MOV A, M ; Get byte from source memory block
STAX D ; Store byte in the destination memory
; block
DCX H ; Decrement source memory pointer
DCX ; Decrement destination memory pointer
DCR C ; Decrement counter
JNZ BACK ; If counter ¹ 0 repeat
HLT ; Stop execution
Solution :
Step 1 : Move bytes from location 10 till the end of array by four bytes downwards.
TM
Solution : Shift bytes from location 14 till the end of array upwards by 4 characters i.e.
from location 10 on words.
LXI H, 210DH ; Initialize source memory pointer at the 14th
; location of the array.
LXI D, 2109H ; Initialize destination memory pointer at the
; 10th location of the array.
MOV A, M ; Get the character
STAX D ; Store character at new location
INX D ; Increment destination pointer
INX H ; Increment source pointer
MOV A, L ; [ check whether desired
CPI 32H ; bytes are shifted or not]
JNZ REPE ; If not repeat the process.
HLT ; Stop
TM
Flowchart :
Start
No Is
Parity odd
?
Yes
Increment memory
Pointer
Decrement character
counter
Is
character No
counter 0?
Yes
End
TM
Source program :
LXI H, 2040H
MOV C, M ; Counter for character
REPEAT : INX H ; Memory pointer to character
MOV A, M ; Character in accumulator
ORA A ; ORing with itself to check parity.
JPO PEREVEN
ORI 80H ; If odd parity place even parity in
; D7(80).
PEREVEN : MOV M, A ; Store converted even parity character.
DCR C ; Decrement counter.
JNZ REPEAT ; If not zero go for next character.
HLT ; Terminate program execution
Lab Experiment 41 : Find the number of negative, zero and positive numbers.
Statement : A list of 50 numbers is stored in memory, starting at 6000H. Find number of
negative, zero and positive numbers from this list and store these results in memory
locations 7000H, 7001H, and 7002H respectively.
Source program :
LXI H, 6000H ; Initialize memory pointer
MVI C, 00H ; Initialize number counter
MVI B, 00H ; Initialize negative number counter
MVI E, 00H ; Initialize zero number counter
BEGIN : MOV A, M ; Get the number
CPI 00H ; If number = 0
JZ ZERONUM ; Goto zeronum
ANI 80H ; If MSB of number = 1 i.e. if
JNZ NEGNUM ; Number is negative goto NEGNUM
INR D ; Otherwise increment positive number Counter
JMP LAST ;
ZERONUM : INR E ; Increment zero number counter
JMP LAST
NEGNUM : INR B ; Increment negative number counter
LAST : INX H ; Increment memory pointer
INR C ; Increment number counter
MOV A, C
CPI 32H ; If number counter = 5010 then
JNZ BEGIN ; Store otherwise check next number
TM
Flowchart :
Start
Yes Is
number = 0
?
No
Is
number <0? Yes
Increment zero
number counter MSB=1
No Increment negative
number counter
Increment positive number
counter
Numbers = Numbers + 1
No Is
number =50
?
Yes
Stop
TM
Lab Experiment 42 : Multiply two eight bit numbers with shift and add method.
Statement : Multiply the 8-bit unsigned number in memory location 2200H by the 8-bit
unsigned number in memory location 2201H. Store the 8 least significant bits of the result
in memory location 2300H and the 8 most significant bits in memory location 2301H.
Sample problems :
(2200) = 1100 (0CH)
(2201) = 0101 (05H)
Multiplicand Multiplier Result
1100 (1210) 0101 (510) 12 ´ 5 = 6010
For simplicity, Multiplicand and Multiplier are taken 4-bit each.
B 7 B 6 B5 B4 B3 B2 B1 B 0 CY B 3 B 2 B1 B 0
0 0 0 0 0 0 0 0 0 0 1 0 1 Initial stage
Step 2 0 0 0 0 0 0 0 0 1 0 1 0 0 Shift
Source program :
LXI H, 2200H ; Initialize the memory pointer
MOV E, M ; Get multiplicand
MVI D, 00H ; Extend to 16-bits
INX H ; Increment memory pointer
MOV A, M ; Get multiplier
LXI H, 0000H ; Product = 0
MVI B, 08H ; Initialize counter with count 8
MULT : DAD H ; Product = product ´ 2
RAL
JNC SKIP ; Is carry from multiplier 1 ?
TM
Flowchart :
Start
Product = 0
Count = 8
Multiplicand = (2200H)
Multiplier = (2201H)
Product = 2 X product
( Shift left 1 bit )
Multiplier = 2 X multiplier
(Shift left 1 bit )
Is
No carry from
Multiplier 1
?
Yes
Count = count – 1
Yes
No Is
count = 0
?
Yes
End
Lab Experiment 43 : Divide 16-bit number with 8-bit number using shifting technique.
Statement : Divide the 16-bit unsigned number in memory locations 2200H and 2201H
(most significant bits in 2201H) by the 8-bit unsigned number in memory location 2300H
store the quotient in memory location 2400H and remainder in 2401H.
Assumption : The most significant bits of both the divisor and dividend are zero.
TM
Sample problem :
For simplicity, Dividend and divisor are taken 8-bit and 4-bit respectively
Dividend = 0110 0001 (61H) Divisor = 0111 (07H)
B7 B 6 B5 B4 B3 B2 B 1 B0 B3 B 2 B1 B 0
0 1 1 0 0 0 0 1 0 0 0 0 Initial stage
Source program :
MVI E, 00 ; Quotient = 0
LHLD 2200H ; Get dividend
LDA 2300 ; Get divisor
MOV B, A ; Store divisor
MVI C, 08 ; Count = 8
NEXT : DAD H ; Dividend = Dividend ´ 2
MOV A, E
RLC
MOV E, A ; Quotient = ´ 2
MOV A, H ;
SUB B ; Is most significant byte of Dividend
; > divisor
JC SKIP ; No, go to Next step
MOV H, A ; Yes, subtract divisor
INR E ; and Quotient = Quotient + 1
TM
Flowchart :
Start
Dividend = Dividend X 2
Quotient = Quotient X 2
Is
Divisor < = Yes
8 MSBS of
Dividend
?
8 MSBS of dividend = 8 MSBS of
No dividend – divisor
Quotient = Quotient + 1
Count = count –1
No Is
count = 0
?
Yes
(2400H) = Quotient
(2401 H) = Remainder
End
TM
Flowchart :
Start
Is
number > 9 No
or AC = 1
?
Yes
Is
number > 9 No
or CY = 1
?
Yes
End
Sample Problem :
Let us see the execution of DAA instruction.
1. If the value of the low order four bits (D 3 -D 0 ) in the accumulator is greater than 9
or if auxiliary carry flag is set, the instruction adds 6 (06) to the low-order four
bits.
TM
2. If the value of the high-order four bits (D7-D4) in the accumulator is greater than 9
or if carry flag is set, the instruction adds 6(06) to the high-order four bits.
Note : To check auxiliary carry flag it is necessary to get the flag register contents in
one of the registers and then we can check the auxiliary carry flag by checking bit 4 of
that register. To get the flag register contents in any general purpose register we require
stack operation and therefore stack pointer is initialized at the beginning of the source
program.
Source program :
LXI SP, 27FFH ; Initialize stack pointer
MOV E, A ; Store the contents of accumulator
ANI 0FH ; Mask upper nibble
CPI 0AH ; Check if number is greater than 9
JC SKIP ; If no go to skip
MOV A, E ; Get the number
ADI 06H ; Add 6 in the number
JMP SECOND ; Go for second check
SKIP : PUSH PSW ; Store accumulator and flag contents
; in stack
POP B ; Get the contents of accumulator in B
; register and
; flag register contents in C register
MOV A, C ; Get flag register contents in
; accumulator
ANI 10H ; Check for bit 4
JZ SECOND ; If zero, go for second check
MOV A, E ; Get the number
ADI 06 ; Add 6 in the number
SECOND : MOV E, A ; Store the contents of accumulator
ANI F0H ; Mask lower nibble
RRC
RRC
RRC
RRC ; Rotate number 4 bit right
CPI 0AH ; Check if number is greater than 9
JC SKIP1 ; If no go to skip 1
MOV A, E ; Get the number
ADI 60H ; Add 60 H in the number
TM
Source Program :
LXI H, 4000H ; Initialize memory pointer
BACK : MVI M, FFH ; Writing ‘1’ into RAM
MOV A, M ; Reading data from RAM
CPI FFH ; Check for ERROR
JNZ ERROR ; If yes go to ERROR
INX H ; Increment memory pointer
MOV A, H
CPI 50H ; Check for last check
JNZ BACK ; If not last repeat
LXI H, 4000H ; Initialize memory pointer
BACK1 : MVI M, 00H ; Writing ‘0’ into RAM
MOV A, M ; Reading data from RAM
CPI 00H ; Check for ERROR
INX H ; Increment memory pointer
MOV A, H
CPI 50H ; Check for last check
JNZ BACK1 ; If not last, repeat
HLT ; Stop execution
TM
Algorithm
1. Read number a.
2. Find a2 by performing a ´ a and store result 1.
3. Read number b.
4. Find b2 by performing b ´ b and store result 2.
5. Result = Result 1 + Result 2
Flowchart
See flowchart on next page.
Program
MVI E, Number a ; Get the number a
CALL MULTIPLY ; Call multiply subroutine
SHLD 2200H ; Store result 1
MVI E, Number b ; Get the number b
CALL MULTIPLY ; Call multiply subroutine
XCHG ; Store result 2 in DE
LHLD 2200H ; Get the result 1 in HL
DAD D ; HL ¬ HL + DE
HLT ; Stop
MULTIPLY : MVI D, 00 ; Extend to 16-bit
MOV A, E ; Multiplier = multiplicand
LXI H, 0000H ; Product = 0
MVI B, 08H ; Initialize counter with count 8
TM
Start
Read a
Multiplicand = a
Multiplier = a
Call Multiply
Store result 1
Read b
Multiplicand = b
Multiplier = b
Call multiply
Store result 2
Stop
TM
Lab Experiment 48 : Program to count given data in a set of numbers Dec.-08, Marks 6
Statement : Write a program to count the number of times the data 02 is present in a set of
20 numbers.
It is assumed that set of 20 numbers are stored from memory location 2000H.
Statement : Write an assembly language program in 8085 to multiply two 16-bit numbers.
LXI SP, 27FFH ; Initialize stack pointer
LXI H, 0000H ; Result = 0 (Lower word)
SHLD 2000H ; Result = 0 (Higher word)
LXI D, number 1 ; Multiplicand
LXI B, number 2 ; Multiplier as a counter
BACK : DAD D ; Result (HL) = HL + DE
JNC NEXT ; If no carry goto NEXT
PUSH H ; Save HL register
LHLD 2000H ; Get the higher word
INX H ; Increment word by 1
SHLD 2000H ; Save higher word
TM
3.2 Timers
In the real time applications, such as traffic light control, digital clock, process control,
serial communication, it is important to keep a track with time. For example in traffic light
control application, it is necessary to give time delays between two transitions. These time
delays are in few seconds and can be generated with the help of executing group of
instructions number of times. This software timers are also called time delays or software
delays. Let us see how to implement these time delays or software delays.
As you know microprocessor system consists of two basic components, Hardware and
software. The software component controls and operates the hardware to get the desired
output with the help of instructions. To execute these instructions, microprocessor takes fix
time as per the instruction, since it is driven by constant frequency clock. This makes it
possible to introduce delay for specific time between two events. In the following section
we will see different delay implementation techniques.
In this program, the instructions DCR C and JNZ BACK execute number of times
equal to count stored in the C register. The time taken by this program for execution can
be calculated with the help of T-states. The column to the right of the comments indicates
the number of T-states in the instruction cycle of each instruction. Two values are specified
for the number of T-states for the JNZ instruction. The smaller value is applied when the
condition is not met, and the larger value applied when it is met. The first instruction
MVI C, count executed only once and it requires 7 T-states. There are count – 1 passes
through the loop where the condition is met and control is transferred back to the first
instruction in the loop (DCR C). The number of T-states that elapse while C is not zero are
(count - 1) ´ (4+10). On the last pass through the loop, the condition is not met and the
loop is terminated. The number of T states that elapse in this pass are 4 + 7.
\ Total T-states required to execute the given program
= 7 + (count–1) ´ (4 + 10 ) + (4 + 7)
MVI C Loops Last loop
For count = 5
Number of T-state = 7 + (5 –1) ´ (14) + (11)
= 7 + 56 + 11
= 74
Assuming operating frequency of 8085A is 2 MHz,
1
Time required for 1 T-state =
2 MHz
= 0.5 msec
Total time required to execute the given program = 74 ´ 0.5 msec.
= 37 msec.
Maximum delay possible with 8-bit count.
The maximum count that can be loaded in the 8 bit register is FFH (255) so the
maximum delay possible with 8 bit count, assuming operating frequency 2 MHz
= (7 + (255 – 1) ´ (14) + (11)) ´ 0.5 msec.
= 1787 msec.
With these calculations, it can be noticed that delay with 8 bit count suitable for small
delays and not for large delays.
TM
In this program, the instructions DCX B, MOV A, C, ORA B and JNZ BACK execute
number of times equal to count stored in BC register pair. The instruction LXI B, count is
executed only once. It requires 10 T-states. The number of T-states required for
one loop = 6 + 4 + 4 + 10 = 24 T-states. The number of T-states required for
last loop = 6 + 4 + 4 + 7 = 21 T-states. So total T-states required for execution of given
program are
= 10 + (count–1) ´ 24 + 21
LXI B Loops Last loop
for count = 03FFH (102310 )
Number of T-states = 10 + (1022) ´ 24 + 21
= 24559
Assuming operating frequency of 8085A as 2 MHz, the time required for,
T state = 0.5 msec.
\ Total time required to execute the given program
= 24559 ´ 0.5 msec
= 12279.5 msec
= 12.2795 msec
= 1 ´ 10 6
Delay Program :
LXI B, count ; 16-bit count
BACK : DCX B ; Decrement count
MOV A, C
ORA B ; Logically OR B and C
JNZ BACK ; If result is not zero repeat
1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
æ 1 ´ 10 6 – 31 ö
count = ç ÷ + 1 » 41666
ç 24 ÷ 10
è ø
count = 41666 10
= A2C2H
Delay Subroutine :
Delay : LXI D, count ; Initialize count
BACK : DCX D ; Decrement count
MOV A, E ;
ORA D ; Logically OR D and E
JNZ BACK ; If result is not 0 repeat
RET ; Return to main program
Flowchart :
Start
Initialize counter = 00
Delay
Call display
Initialize counter
Call delay
Decrement counter
Increment counter
No Is
counter = 0
?
No Is
count > FFH Yes
?
RET
Yes
Stop
1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
TM
æ 1 ´ 10 6 – 31 ö
count = ç ÷+1 » 4166610
ç 24 ÷
è ø
count = 4166610 = A2C2H
Lab Experiment 52 : Generate and display BCD up counter with frequency 1 Hz.
Statement : Write a program for displaying BCD up counter. Counter should count
numbers from 00 to 99H and it should increment after every 1 sec. Assume operating
frequency of 8085 equal to 3 MHz. Display routine is available.
Solution :
LXI SP, 27FFH ; Initialize stack pointer
MVI C, 00H ; Initialize counter
BACK : CALL Display ; Call display subroutine
CALL Delay ; Call delay subroutine
MOV A, C ;
ADI , 01 ; Increment counter
DAA ; Adjust it for decimal
MOV C, A ; Store count
CPI ,00 ; Check count is > 99
JNZ BACK ; If not, repeat
HLT ; Stop
Delay Subroutine :
Delay : MVI B, Multiplier-count ; Initialize multiplier count
BACK1: LXI D, Initialize Count
BACK : DCX D ; Decrement count
MOV A, E ;
ORA D ; Locally OR D and E
JNZ BACK ; If result is not 0, repeat
DCR B ; Decrement multiplier count
JNZ BACK1 ; If not zero, repeat
RET ; Return to main program.
Operating frequency : 3 MHz
1
\ Time for one T-state = = 0.333 msec
3 MHz
TM
Flowchart :
Start
Initialize counter = 0
Call display
Delay
Call delay
Initialize counter
Increment counter
Decrement counter
No Is
counter = 0
?
No Is
count > 99 Yes
?
RET
Yes
Stop
Required Time
\ Number of T-states required = = 3 ´ 10 6
Time for 1- T state
1 sec
=
0.333 msec
Let us take multiplier count = 3.
3 ´ 10 6
\ Number of T-states required by inner loop = = 1 ´ 10 6
3
\ 1 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
» 4166610
count = 4166610 = A2C2H
Lab Experiment 53 : Generate and display BCD down counter with frequency 1 Hz
Statement : Write a program for displaying BCD down counter. Counter should count
numbers from 99 to 00 and it should decrement after every 1 sec. Assume display and
delay routines are available.
TM
Solution : Flowchart :
Start
Initialize counter = 99
Call display
Call delay
No Is
count < 0
?
Yes
End
Source program :
MVI C, 99H ; Initialize counter
BACK : MOV A, C ;
ANI 0F ; Mask higher nibble
CPI 0F
JNZ SKIP
MOV A, C
SUI 06 ; Subtract 6 to adjust decimal count
MOV D, A
SKIP : MOV A, C
OUT 05 ; send count on output port
CALL Delay ; Wait for 0.5 seconds
DCR C ; decrement count
MOV A, C
CPI FF
JNZ BACK ; If not zero, repeat
HLT ; Stop execution
Delay subroutine :
Delay : LXI D, Count
Back : DCX D ; 6 T-states
MOV A, D ; 4 T-states
TM
ORA E ; 4 T-states
JNZ Back ; 10 T-states
RET
1 1
1 T-state = =
Operating frequency 3.072 ´ 10 6
= 3.2552 ´ 10 –7
0.5 sec
Number of T-states required = = 1.536 ´ 10 6
3.2552 ´ 10 –7
1.536 ´ 10 6 = 10 + (count – 1) ´ 24 + 21
1.536 ´ 10 6 – 31
Count = + 1 = 63999.708
24
= 64000
= FA00H
Lab Experiment 55 : Identify the error and correct the given delay routine.
Statement : The delay routine given below is in infinite loop, identify the error. Correct
the program, give the machine cycles and T states of each instruction and also find the
maximum delay generated. Assume 1 "T" state = 320 ns.
DELAY : LXI H, N
L1 : DCX H
JNZ L1
Solution : 1) The fault in the above program is at instruction JNZ L1. This condition
always evaluates to be true hence loops keeps on executing and hence infinite loop.
2) Reason for infinite looping : - The instruction DCX H decrease the HL pair count
one by one but it does not affect the zero flag. So when count reaches to 0000H in HL pair
zero flag is not affected and JNZ L1 evaluates to be true and loop continues. Now HL
again decrements below 0000H and HL becomes FFFFH and thus execution continues.
3) The modification in the program is as follows :
No. of T states
DELAY : LXI H, N ; Load 16 bit count ® 10 T-states
L1 : DCX H ; Decrement count ® 6 T-states
MOV A, L ; ® 4 T-states
ORA H ; logically OR H and L ® 4 T-states
JNZ L1 ; If result is not 0 repeat ® 10 T-states
\ Total number of T states required for program execution are
= 10 + (count – 1) ´ 24 + 21
TM
= 0.50407904 seconds
= 3CH + 7 = 43H
TM
Sample Problem :
(2200H) = 67H
(2300H) = 6 ´ 0AH + 7 = 3CH + 7 = 43H
Source Program :
LDA 2200H ; Get the BCD number
MOV B, A ; Save it
ANI 0FH ; Mask most significant four bits
MOV C, A ; Save unpacked BCD1 in C register
MOV A, B ; Get BCD again
ANI F0H ; Mask least significant four bits
RRC ; Convert most significant four
RRC ; bits into unpacked BCD2
RRC ;
RRC ;
Flowchart :
Start
Multiply BCD2
number by 10
Add BCD1
Store result
Stop
TM
To represent the number in BCD requires twelve bits or three BCD digits as shown
below
12310 = 0001 0010 0011
Digit2 digit1 digit0
Step 1 : If the number is equal to or greater than 100, divide number by 100 (i.e.
subtract 100 repeatedly until the remainder is less than 100). The quotient
gives the most significant digit, digit 2 of the BCD number. If number is less
than 100 go to step 2.
Step 2 : If the number i.e. remainder of first division is equal to or greater than 10
divide number by 10 repeatedly until the remainder is less than 10. The
quotient gives the digit 1. If number is less than 10, go to step 3.
Source Program :
LXI SP, 27FFH ; Initialize stack pointer
LDA 6000H ; Get the binary number in accumulator
CALL BIN TO BCD ; Call subroutine BIN TO BCD
HLT ; Terminate program execution
Flowchart :
Start
Check if Yes
number
is > 100
Divide number by 100
No
Check if Yes
reminder
is > 10
Divide number by 10
No
Digit 0 = reminder
Stop
TM
Source Program :
LXI H, 6200H ; Initialize lookup table pointer
LXI D, 6000H ; Initialize source memory pointer
LXI B, 7000H ; Initialize destination memory pointer
TM
Flowchart :
Start
Lookup Table
1 06
Get the number
2 5B
3 4F
Find the 7-segment code
4 66
7 07
Increment source memory pointer
Increment destination memory pointer 8 7F
9 6F
Is
No last
number
?
Yes
Stop
TM
Therefore, by adding 30H we can convert number into its ASCII equivalent and by adding
37H we can convert letter to its ASCII equivalent. Let us see the program for binary to
ASCII code conversion.
Flowchart :
Start
Initialize count = 5
Is Yes
number > A
Get the number ?
No
CALL ASCII Number = number + 30 Number = number + 37
Decrement counter
No Is
count = 0
?
Yes
End
TM
Sample Problem :
(2000H) = 1
(2001H) = 2
(2002H) = 9
(2003H) = A
(2004H) = B Result (2200H) = 31
(2201H) = 32
(2202H) = 39
(2203H) = 41
(2204H) = 42
Subroutine Documentation :
Subroutine ‘ASCII’ converts a hexadecimal digit to ASCII.
Passing parameter : The digit is passed using accumulator.
Return value : In the accumulator.
Register used : Accumulator.
Stack used : From 27FEH to 27FDH
Source Program :
LXI SP, 27FFH ; Initialize stack pointer
LXI H, 2000H ; Source memory pointer
LXI D, 2200H ; Destination memory pointer
MVI C, 05H ; Initialize the counter
BACK : MOV A, M ; Get the number
CALL ASCII ; Call subroutine ASCII
STAX D ; Store result
INX H ; Increment source memory pointer
INX D ; Increment destination memory pointer
DCR C ; Decrement count by 1
JNZ BACK ; if not zero, repeat
HLT ; Stop program execution subroutine ASCII
ASCII : CPI, 0AH ; Check if number is 0AH
JNC NEXT ; If yes goto next otherwise continue
ADI 30H ;
JMP LAST
NEXT : ADI 37H
LAST : RET ; Return to main program
TM
The addition is carried out as in normal binary addition and the sum is 1 0 0 1, which
is BCD code for 9.
The sum 1 1 1 0 is an invalid BCD number. This has occurred because the sum of the
two digits exceeds 9. Whenever this occurs the sum has to be corrected by the addition of
six (0110) in the invalid BCD number, as shown below
6 0110 ¬ BCD for 6
+ 8 1000 ¬ BCD for 8
-------- ----------
14 1110 ¬ Invalid BCD number
+ 0110 ¬ Add 6 for correction
-----------------------------
0001 0100 ¬ BCD for 14
1424 3 12
4 4 3
1 4
After addition of 6 carry is produced into the second decimal position.
TM
In this, case, result (0001 0001) is valid BCD number, but it is incorrect. To get the
correct BCD result correction factor of 6 has to be added to the least significant digit sum,
as shown.
8 1000 ¬ BCD for 8
+ 9 1001 ¬ BCD for 9
------- -------------------
17 00010001 ¬ Incorrect BCD result
+ 00000110 ¬ Add 6 for correction
--------------------------------------
00010111 ¬ BCD for 17
Going through these three cases of BCD addition we can summarize the BCD addition
procedure as follows :
1. Add two BCD numbers using ordinary binary addition.
2. If four-bit sum is equal to or less than 9, no correction is needed. The sum is in
proper BCD form.
3. If the four-bit sum is greater than 9 or if a carry is generated from the four-bit
sum, the sum is invalid.
4. To correct the invalid sum, add 01102 to the four-bit sum. If a carry results from
this addition, add it to the next higher-order BCD digit.
The 8085 supports DAA (Decimal Adjust Accumulator) instruction for adjusting the
result of addition to the BCD number. (See chapter 2 for DAA instruction).
Sample problem :
Get the first BCD number
(2200H) = 39
(2201H) = 45
Get the second BCD number
Result = (2300H) = 39 + 45
= 7E + 6 = 84
(lower nibble is greater than 9 so add 6) Add two BCD numbers
Source program :
LXI H, 2200H ; Initialize pointer Adjust result to valid BCD number
TM
Sample problem :
(HL) = 3629
(DE) = 4738
Flowchart :
Start
End
TM
Source program :
MOV A, L ; Get lower 2 digits of no. 1
ADD E ; Add two lower digits
DAA ; Adjust result to valid BCD
STA 2300H ; Store partial result
MOV A, H ; Get most significant 2 digits of no. 2
ADC D ; Add two most significant digits
DAA ; Adjust result to valid BCD
STA 2301H ; Store partial result
HLT ; Terminate program execution.
Source Program :
MVI A,99H
SUB E ; Find the 99's complement of subtrahend
INR A ; Find 100's complement of subtrahend
TM
Source Program :
MVI C, Multiplier ; Load BCD multiplier
MVI B, 00 ; Initialize counter
LXI H, 0000H ; Result = 0000
MVI E, multiplicand ; Load multiplicand
MVI D, 00H ; Extend to 16-bits
BACK : DAD D ; Result ¬ Result + Multiplicand
MOV A, L ; [ Get the lower
ADI, 00H ; byte of the result
DAA ; Adjust it to BCD and
MOV L, A ; store it]
MOV A, H ; [ Get the higher
ACI, 00H ; byte of the result
DAA ; Adjust it to BCD and
MOV H, A ; store it]
MOV A, B ; [ Increment
ADI 01 H ; counter
DAA ; adjust it to BCD and
MOV B, A ; store it]
CMP C ; Compare if count = multiplier
JNZ BACK ; if not equal repeat
HLT ; Stop
Review Questions
Section 3.1
Q.1 Explain the loop structure with counting and indexing in 8085 programming.
May-10, Marks 8
TM
Q.3 Write an assembly language program in 8085 to add N one byte binary numbers
stored from location X + 1, where N is stored at location X, store the result in location
Y and Y + 1. Display the result in address field. June-09, Marks 8
Q.4 Write an assembly language program in 8085 to find the maximum number from the
given n numbers. June-09, Marks 8
Q.5 Write an 8085 program to count the number of even and odd numbers in a given set
of numbers. Dec.-09, Marks 10
Q.6 Write an 8085 program to find the largest set of a n 8-bit numbers.
Dec.-09, Marks 6
Q.7 Write an assembly language program for sorting 'n' elements in an array.
May-10, Marks 8
Q.8 Write an assembly program to multiply a number by 8 program using the rotate
instruction. June-09
Q.9 Write an ALP to add 5 data bytes stored in memory locations starting at 4500H and
display the sum in next memory location. Dec.-10
Section 3.2
Q.1 Write a program to count from 0 to 9 with one second delay between each count. At
the count of 9, the counter should reset itself to 0 and repeat the sequence
continuously. Assume the clock frequency is 1 MHz. Dec.-11, Marks 8
Section 3.3
Q.1 Why do we need look-up table ? Dec.-11, Marks 2
Q.2 List the common code conversions required in the microprocessor systems.
Q.3 Explain BCD to Binary code conversion technique and write 8085 assembly language
program for the same.
Q.4 Explain Binary to BCD code conversion technique and write 8085 assembly language
program for the same.
Q.5 Explain BCD to Seven segment code conversion technique and write 8085 assembly
language program for the same.
Q.6 Explain Binary to ASCII code conversion technique and write 8085 assembly language
program for the same.
Q.7 Explain ASCII to Binary code conversion technique and write 8085 assembly language
program for the same.
TM
Section 3.4
Q.1 Explain the procedure for addition of two BCD numbers.
Q.2 Explain the procedure for subtraction of two BCD numbers.
qqq
TM
Notes
TM
Contents
4.1 Necessity of Interrupts . . . . . . . . . . . . . . . . . . May/June-06, 09
4.2 8085 Interrupt Structure and Operation . . . . . . April/May-04, 05, 08, 10;
. . . . . . . . . . . . . . . . . . May/June-07, 09; Nov./Dec.-09
(4 - 1)
TM
When you have one or more I/O devices connected to a microprocessor system, any
one of them may demand service at any time. The microprocessor can service these
devices in one of the two ways. One way is to use the polling routine. The other way is to
use an interrupt. In the following section, we will see both ways.
In polling, the microprocessor’s software simply checks each of the I/O devices every
so often. During this check, the microprocessor tests to see if any device needs servicing.
Fig. 4.1 shows the flowchart for polling subroutine.
This is a simple program which services I/O ports A, B, and C. The polling routine
checks the status of I/O ports in proper sequence. It first transfers the status of I/O port A
into the accumulator. Then the polling routine block checks the contents of accumulator to
see if the service request bit is set. If it is, I/O port A service routine is called. After
completion of service routine for I/O port A, the polling routine moves on to test port B
and the process is repeated. This test and service procedure continues until all the I/O
port status registers are tested and all the I/O ports requesting service are serviced. Once
this is done, the microprocessor continues to execute the normal programs.
The polling routine assigns priorities to the different I/O devices. Once the polling
routine is started, the service request bit at port A is always checked first. Once port A is
checked, port B is checked, and then port C. However, the order can be changed by
simply changing the polling routine and thus the priorities.
TM
Start I/O
polling routine
Is
service request Yes Call the I/O port A
bit set service routine
?
No
Is
service request Yes Call the I/O port B
bit set service routine
?
No
Get I/O port C
status register
Is
service request Yes Call the I/O port C
bit set service routine
?
No
End
TM
An analogy to the interrupt concept is the classroom, where the professor will serve as
the CPU and the students as I/O ports. The classroom scenario for this interrupt analogy
will be such that the professor is busy in writing on the blackboard and delivering his
lecture. The student raises his hand when he wants to ask a question (student requesting
for service). The professor then completes his sentence and acknowledges student’s request
by saying “Yes” (Professor acknowledges the interrupt request). After acknowledgement
from the professor, student asks the question and professor gives answer to the question
(professor services the interrupt) and resumes his respective work.
4.2 8085 Interrupt Structure and Operation April/May-04, 05, 08, 10;
May/June-07, 09; Nov./Dec.-09
TM
3 RST
6.5 003416
Level triggered
M 6.5 003016
4 RST
5.5 002C16
Level triggered
M 5.5 002816
1 TRAP 002416
Both +ve edge
and level triggered 002016
EI
S Q 001816
DI Interrupt
Reset R enable 001016
Any interrupt recognized
Get
RST 000816
code
5 INTR from 000016
external
Level triggered hardware
In 8085, all interrupts except TRAP are maskable. When logic signal is applied to a
maskable interrupt input, the 8085 is interrupted only if that particular input is enabled.
These interrupts can be enabled or disabled under program control. If disabled, 8085
disables an interrupt request. The interrupt TRAP is non-maskable which means that it is
not maskable by program control. The Fig. 4.2 shows the interrupt structure of 8085. The
figure indicates that, the 8085 is designed to respond to edge triggering, level triggering or
both.
TM
TRAP
CALL 0024H
1 D Q
TRAP
Q
RESETIN
TRAP
ACKNOWLEDGE
Fig. 4.3 Interrupt circuit for trap interrupt
As shown in the Fig. 4.3, the positive edge of TRAP signal sets the D flip-flop.
However, due to the AND gate, it is necessary to sustain high level on the TRAP input.
There are two ways to clear TRAP interrupt :
1. By resetting microprocessor i.e. giving a low signal on RESETIN pin
(External signal).
2. By giving a high TRAP ACKNOWLEDGE (Internal signal).
After recognition of TRAP interrupt, 8085 internally generates a high TRAP
ACKNOWLEDGE which clears the flip-flop. Once the TRAP is acknowledged, the 8085
completes its current instruction. It then pushes the address of the next instruction i.e.
return address onto the stack and loads PC with the fixed vector address 0024H. Due to
this, 8085 starts execution of instructions from address 0024H which is the starting address
of an interrupt service routine for TRAP.
RST 7.5 : The RST 7.5 interrupt is a maskable interrupt. It has the second highest
priority. As shown in Fig. 4.2, it is positive edge triggered and the positive edge trigger is
stored internally by the D flip-flop until it is cleared by software reset using SIM
instruction or by internally generated ACKNOWLEDGE signal.
The positive edge signal on the RST 7.5 pin sets the D flip-flop. If the mask bit
M 7.5 is 0 i.e. RST 7.5 is unmasked then 8085 completes its current instruction. It then
pushes the address of the next instruction onto the stack and loads PC with the fixed
vector address 003CH. Due to this, 8085 starts execution of instructions from address
003CH which is the starting address of an interrupt service routine for RST 7.5.
RST 6.5 and RST 5.5 : The RST 6.5 and RST 5.5 both are level triggered. These
interrupts can be masked using SIM instruction. The RST 6.5 has the third priority whereas
RST 5.5 has the fourth priority. The vector addresses of RST 6.5 and RST 5.5 are 0034H
and 002CH respectively. After recognition of RST 6.5 or RST 5.5 interrupt, 8085 completes
its current instruction; pushes the address of next instruction onto the stack and loads PC
with corresponding vector address.
INTR : INTR is a maskable interrupt, but not the vector interrupt. It has the lowest
priority. The following sequence of events occur when INTR signal goes high.
1. The 8085 checks the status of INTR signal during execution of each instruction.
2. If INTR signal is high, then 8085 completes its current instruction and sends an
active low interrupt acknowledge signal (INTA) if the interrupt is enabled.
TM
3. In response to the INTA signal, external logic places an instruction OPCODE on the
data bus. In the case of multibyte instruction, additional interrupt acknowledge
machine cycles are generated by the 8085 to transfer the additional bytes into the
microprocessor.
4. On receiving the instruction, the 8085 saves the address of next instruction on stack
and executes received instruction.
Note : Theoretically, the external logic can place any instruction code on the data bus in
response to the INTA. However, only CALL and RST codes save the contents of the PC on
the stack and branch program control to the subroutine address.
Response for RST instruction : If the external device places an opcode for any one of
the RST instruction (RST 0 - RST 7), then 8085 pushes the contents of PC onto the stack. It
then branches the program control to the vector address of the corresponding RST
instruction.
Response for CALL instruction : If the external device places an opcode for CALL
instruction then 8085 generates two additional interrupt acknowledge cycles.
1. It sends an active low interrupt acknowledge signal second time.
2. In response to second INTA signal, external logic places the lower byte address for
the CALL instruction.
3. After receiving lower byte address, 8085 sends the third interrupt acknowledge
signal.
4. In response to third INTA signal, external logic places the higher byte address for
the CALL instruction.
5. After receiving sixteen bit address for CALL, 8085 pushes the contents of the PC
onto the stack and branches the program control to the subroutine whose address
is received from the external logic.
Example : The Fig. 4.4 shows the diagram of external logic that gives the RST 7
instruction opcode on interrupt acknowledge.
8085A
Microprocessor
AD0-AD7 8
8
Three - state
INTA buffer
R
5V
INTR 5V
Reset
Q D
Request from
CLK I/O device for
an interrupt
Flip-flop
Fig. 4.4 External logic that gives the RST 7 instruction opcode
TM
External logic controls a tri-state buffer with the INTA signal in order to place an
opcode for RST 7 instruction. The INTA signal from the microprocessor is used as an
Output Enable signal for the buffer as well as reset signal for D flip flop. The request from
the I/O device is routed through the D flip-flop to the INTR. The D flip flop is used to
hold the INTR signal high until 8085 gives interrupt acknowledge signal. The INTA signal
that is generated enables the tri-state buffer whose data inputs are hardwired to the value
equal to the opcode for RST 7 (FFH) instruction. The 8085 receives this opcode during
interrupt acknowledge cycle. After receiving the opcode 8085 pushes the contents of
program counter onto the stack, thus saving the return address. It then branches the
program control to the address 0038H (Vector address of RST 7). Table 4.1 shows the
summary of hardware interrupts in 8085.
Table 4.1
TM
EI : Enable Interrupt
The EI instruction sets the interrupt enable flip-flop, as shown in Fig. 4.2. Thus
RST 7.5, RST 6.5, RST 5.5 and INTR are enabled using EI instruction.
It is important to note that when any interrupt is acknowledged, interrupt enable
flip-flop resets and disables all interrupts. To enable interrupt in further process it is
necessary to execute EI instruction within interrupt service routine.
DI : Disable Interrupt
The DI instruction resets the interrupt enable flip-flop, as shown in Fig. 4.2. Thus it
disables RST 7.5, RST 6.5, RST 5.5 and INTR interrupts.
Example 1 : Write a program to display real time clock. Assume that a periodic signal is
interrupting RST 7.5 signal after every 0.5 seconds.
TM
Main program
MVI C, 00H ; Initialize counter
LXI H, 0000H ; Initialize seconds, and minutes
MVI D, 00H ; Initialize hours
MVI A, 0BH ;
SIM ;
EI ; Enable RST 7.5 interrupt
HERE : JMP HERE ; Wait for interrupt.
Start
Counter = Counter + 1
Is
No
counter = 2
?
Yes
Counter = 0
Is
No
Sec = 60
?
Yes
Seconds = 0
Is
No
Min = 60
?
Yes
Min = 0
Is
No
Hour = 24
?
Hour = 0
Enable interrupt
Ret
Fig. 4.5 Flowchart for interrupt subroutine
TM
Review Questions
Section 4.1
Q.1 What are interrupts ? June-09, Marks 2
Section 4.2
Q.1 Explain the 8085 interrupts system in detail. May-04, Marks 10
Q.3 With necessary diagrams, write short note on interrupt structure of 8085.
May-08,10, Dec.-11, Marks 6
Ans. : The type of interrupts where microprocessor pins are used to receive interrupt
requests, are called hardware interrupts. In software interrupts, the cause of the
interrupt is an execution of the instruction. These are special instructions supported by
the microprocessor. After execution of these instructions microprocessor completes the
execution of the instruction it is currently executing and transfers the program control
to the subroutine program.
Q.4 Name the vectored and nonvectored interrupt of 8085 system. May-05
Ans. : Vectored interrupt of 8085 are : RST0 - RST7, TRAP, RS7.5, R6.5 and RST 5.5.
The only nonvectored interrupt of 8085 system is INTR.
Q.5 What are all the hardware interrupts ? June-09
qqq
TM
Contents
5.1 Instruction Cycle,
Machine Cycle and T-State . . . . . . . . . . . . . . . April/May-08, Nov./Dec.-09
5.2 Representation of Signals
5.3 Signal Timings
5.4 8085 Machine Cycles and their Timings. . . . . . April/May-04, Nov./Dec.-05
5.5 Timing Diagrams for 8085 Instructions
(5 - 1)
TM
We have seen the instruction set of the 8085 microprocessor. For clear understanding
of the each instruction, it is necessary to see how microprocessor executes these
instructions, which signals are activated to execute a particular instruction and at what
instant. All this information can be plotted graphically. The graphical representation of the
instruction execution in steps with respect to the time (clock signal) is called ‘timing
diagram.’
In this chapter, we will first see what do we mean by instruction cycle, machine cycles
and T- states. After clear understanding of these things we will see the rules and standards
used in the timing diagrams and then we will see the timing diagrams of all 8085
instructions.
Instruction cycle
Fig. 5.1 Relation between instruction cycle, machine cycle and T-state
There are seven different types of machine cycles in the 8085A. Three status signals
IO/M, S1 and S0 identify each type as shown in Table 5.1. These signals are generated at
the beginning of each machine cycle and remain valid for the duration of the cycle.
TM
IO/M S1 S0 RD WR INTA
Opcode Fetch 0 1 1 0 1 1
Memory Read 0 1 0 0 1 1
Memory Write 0 0 1 1 0 1
I/O Read 1 1 0 0 1 1
I/O Write 1 0 1 1 0 1
INTR Acknowledge 1 1 1 1 1 0
Bus Idle 0 0 0 1 1 1
Clock Signal :
The 8085 divides the clock frequency provided at x 1 and x 2 inputs by 2, which is
called operating frequency. All the operations within the 8085 are synchronized with this
operating frequency. Therefore in the timing diagram operating frequency clock is shown
on the top and then the signals are shown with reference to operating frequency clock.
Ideally, the clock signal should be square wave with zero rise time and fall time, as shown
in the Fig. 5.2. But in practice, we do not get zero rise time and fall time. Therefore the
clock and other signals are always shown with finite rise and fall times. Fig. 5.2 shows the
practical way of representing clock signal.
T-state T-state Tr
Tf
1 Clock cycle
TM
Logic 1 Logic 1
Tri-state
Logic 0 Logic 0
Tr Tf
Tri state
State change Valid state
In the group representation individual state is not considered, but the group state is
considered. Change in state of single signal changes the state of group. It is represented by
the cross as shown the Fig. 5.4. The tri-state condition of the group signals is shown by
dotted lines. Two straight lines represent valid state/stable state.
In microprocessor systems, activation of signal/signals depends on the state of other
signal/signals. Such situations are shown in the timing diagrams with the help of specific
symbols. There are four possibilities :
Activation of a signal with the change in state of other signal.
Activation of a signal with the change in state of other signals.
Activation of signals with the change in state of other signal.
Activation of signals with the change in state of other signals.
Fig. 5.5 shows the representation of dependence of the signal/signals, in the timing
diagram.
Other signal
Activated
signal
(a) Activation of a signal with (b) Activation of signals with
the change in state of other signal the change in state of other signal
Fig. 5.5
TM
Fig. 5.5
5.3 Signal Timings
In 8085 microprocessor, signals are activated at specific instant for specific time period.
Once we understand this, it is very easy to draw timing diagrams. The following section
explains when the signals are activated and for what period they remain in active state.
T1 T2 T3 T4 T1 T2 T3
ALE
TM
T1 T2 T3 T4 T1 T2 T3
AD0 - AD7
A0 - A7 A0 - A7
T1 T2 T3 T1 T2 T3
(a) (b)
TM
T1 T2 T3 T4 T1 T2 T3
A8 - A15
A8 - A15 A8 - A15
T1 T2 T3 T4 T1 T2 T3
IO/M = 0, S0 = 1, S1 = 1 IO/M = 0, S0 = 0, S1 = 1
RD and WR : These signals decide the direction of the data transfer. When RD signal is
active, data is transmitted from memory or I/O device to the microprocessor and when
WR signal is active, data is transmitted from microprocessor to the memory or I/O device.
Both signals are never active at a time.
As we know data transfer in 8085 takes place during T2 and T3, these signals are
activated during T2 and T3, as shown in the Fig. 5.11.
T1 T2 T3 T4 T1 T2 T3
RD
TM
Step 1 : (State T1) In T1-state, the 8085 places the contents of program counter on the
address bus. The high-order byte of the PC is placed on the A8-A15 lines. The low-order
byte of the PC is placed on the AD0 - AD7 lines which stays on only during T1. Thus
microprocessor activates ALE (Address Latch Enable) which is used to latch the low-order
byte of the address in external latch before it disappears.
In T1, 8085 also sends status signals IO/M, S1 and S0. IO/M specifies whether it is a
memory or I/O operation, S1 status specifies whether it is read/write operation; S1 and S0
together indicate read, write, opcode fetch, machine cycle operation or whether it is in
HALT state. In opcode fetch machine cycle status signals are : IO/M = 0, S1 = 1, S0 = 1.
Step 2 : (State T2) In T2, low-order address disappears from the AD0 - AD7 lines.
(However A0 - A7 remain available as they were latched during T1). In T2, 8085 sends RD
signal low to enable the addressed memory location. The memory device then places the
contents of addressed memory location on the data bus (AD0 - AD7).
Step 3 : (State T3) During T3, 8085 loads the data from the data bus in its Instruction
Register and raises RD to high which disables the memory device.
Step 4 : (State T4) In T4, microprocessor decodes the opcode and on the basis of the
instruction received, it decides whether to enter state T5 or to enter state T1 of the next
TM
Opcode fetch
Instruction B C T1 T2 T3 T4
register
(IR) D E
H L CLK
SP
PC
Instruction
Microprocessors and Microcontroller
decoder A15
(ID) High-order memory address Unspecified
A8
AD7
AD7 AD0
Low-order Opcode
Timing AD0
ALE Memory address
and Latch
TM
5-9
control
A7 A0 ALE
Memory
IO / M
Status IO / M = 0, S0 = 1, S1 = 1 Opcode fetch
A15 A8
Memory
read
Data bus
Fig. 5.12 (a) Data (opcode) flow from memory to micrprocessor Fig. 5.12 (b) Opcode fetch machine cycle
Microprocessors and Microcontroller 5 - 10 8085 Timing Diagrams
machine cycle. One byte instructions those operate on eight bit data (8-bit operand) are
executed in T4.
For example : MOV A, B, ANA D, ADD B, INR L, DCR C, RAL and many more.
Note : For one byte instructions which operate on eight bit data, data is always available
in the internal memory of 8085 i.e. registers.
Step 5 : (States T5 and T6) States T5 and T6, when entered, are used for internal
microprocessor operations required by the instruction. During T5 and T6, 8085 performs
stack write, internal 16-bit, and conditional return operations depending upon the type of
instruction. One byte instructions those operate on sixteen bit data (16-bit operand) are
executed in T5 and T6. For example : DCX H, PCHL, SPHL, INX H etc.
Step 1 : (State T1) In T1-state, microprocessor places the address on the address lines
from stack pointer, general purpose register pair or program counter and activates ALE
signal in order to latch low-order byte of address.
During T1, 8085 sends status signals : IO/M = 0, S1 = 1 and S0 = 0 for memory read
machine cycle.
Step 2 : (State T2) In T2, 8085 sends RD signal low to enable the addressed memory
location. The memory device then places the contents of addressed memory location on the
data bus (AD0 - AD7).
Step 3 : (State T3) During T3, 8085 loads the data from the data bus into specified
register (F, A, B, C, D, E, H and L) and raises RD to high which disables the memory
device.
TM
ID
A15 - A8 Memory address
AD7 AD0
Timing ALE
and Latch
ALE
TM
control
5 - 11
A15 A8 IO / M, S1, S0 IO / M = 0, S1 = 1, S0 = 0
Memory
read Data bus
Fig. 5.13 (a) Data flow from memory to microprocessor Fig. 5.13 (b) Memory read machine cycle
8085 Timing Diagrams
Memory Write
A B C T1 T2 T3
IR
D E
H L CLK
SP
PC
ID
Microprocessors and Microcontroller
AD7 AD0
Timing
ALE ALE
and Latch
control
TM
AD7 - AD0 A 7 - A0 Data from CPU
5 - 12
A7 A0
Memory
IO / M IO / M = 0, S1 = 0, S0 = 1
A15 A8
Memory
write
Data bus
Fig. 5.14 (a) Data flow microprocessor to memory Fig. 5.14 (b) Memory write machine cycle
8085 Timing Diagrams
Microprocessors and Microcontroller 5 - 13 8085 Timing Diagrams
Step 1 : (State T1) In T1-state, the 8085 places the address on the address lines from
stack pointer or general purpose register pair and activates ALE signal in order to latch
low-order byte of address. During T1, 8085 sends status signals :
IO/M = 0, S1 = 0 and S0 = 1 for memory write machine cycle.
Step 2 : (State T2) In T2, 8085 places data on the data bus and sends WR signal low for
writing into the addressed memory location.
Step 3 : (State T3) During T3, WR signal goes high, which disables the memory device
and terminates the write operation.
TM
A B C T1 T2 T3
IR
D E
H L
CLK
SP
PC
Microprocessors and Microcontroller
ID ALE
AD7 AD0
A15 - A8 I/O Addr
Timing
TM
ALE
and Latch
5 - 14
RD
A15 - A8
Fig. 5.15 (a) Data flow from input device to microprocessor Fig. 5.15 (b) I/O read memory cycle
8085 Timing Diagrams
I / O Write
A B C T1 T2 T3
IR
D E
H L
CLK
SP
PC
Microprocessors and Microcontroller
ID
ALE
AD7 AD0
A15 - A8 I/O Addr
Timing
TM
ALE
and Latch
5 - 15
WR
Output OR
IO / M, S1, S0 IO / M = 1, S1 = 0, S0 = 1
device
I/O A7 - A0
write Data bus
Fig. 5.16 (a) Data flow from microprocessor to output device Fig. 5.16 (b) I/O write machine cycle
8085 Timing Diagrams
Microprocessors and Microcontroller 5 - 16 8085 Timing Diagrams
Restart Instruction
M1 M2 M3
T1 T2 T3 T4 T5 T6 T1 T2 T3 T1 T2 T3
CLOCK
ALE
INTR
INTA
RD
WR
IO/M=0,S1=0,S0=1 IO/M=0,S1=0,S0=1
Data
T3
PCH
M5
T2
PCL
T1
Fig. 5.18 Timing diagram of INTA machine cycle and execution of call instruction
T3
Data
PCH
M4
T2
PCL
T1
IO/M=1,S1=1,S0=1
address byte
Higher-order
T3
Data
M3
T2
T1
IO/M=1,S1=1,S0=1
address byte
Higher-order
T3
Data
M2
T2
T1
Unspecified
T6
IO/M = 1, S1 = 1, S0 = 1
T5
T4
M1
T3
Opcode
address byte
Higher-order
T2
Address
T1
AD0 - AD7
A8 - A15
ALE
CLOCK
IO/M,S1,S0
INTA
WR
TM
after execution of opcode fetch machine cycle, DAD instruction requires 6 extra T-states to
add 16-bit contents of a specified register pair to the contents of HL register pair. These
extra T-states which are divided into two machine cycles do not involve any memory or
I/O operation. These machine cycles are called BUS IDLE machine cycles. Fig. 5.19 shows
Bus Idle Machine Cycle for DAD instruction.
CLOCK
ALE
RD
WR
INTA
In the case of DAD, these Bus Idle cycles are similar to memory read cycles, except RD
and ALE signals are not activated.
2. During internal opcode generations, for TRAP and RST interrupts, 8085 executes Bus
Idle Machine Cycles. Fig. 5.20 shows the Bus Idle Machine Cycle for TRAP. In response to
TRAP interrupt, 8085 enters into a Bus Idle Machine Cycle during which it invokes restart
instruction, stores the contents of PC onto the stack and places 0024H (Vector address of
TRAP) onto the program counter.
TM
CLOCK
TRAP
IO/M
S1,S0
IN OUT OUT IN
AD0 - AD7 PCL (SP-1)L PCH
ALE
INTA
RD
WR
READY
The number of machine cycles required to fetch complete instruction depends on the
instruction type :
1. One byte 2. Two byte or 3. Three byte.
One byte instruction does not require any additional machine cycle. Two byte
instruction requires one additional memory read machine cycle, whereas three byte
instruction requires two additional memory read machine cycles.
The number of machine cycles required to execute the instruction depends on the
particular instruction. The total number of machine cycles required varies from one to five.
It is possible that memory read and memory write machine cycles occur more than once in
a single instruction cycle. The following examples illustrate the timing diagrams and
machine cycles used for few 8085 instructions.
ß Example 5.1 : Draw the timing diagram for instruction MVI A, 30H which is stored at
address 2000H.
2000H 3E
2001H 30
TM
Solution : This instruction consists of two bytes; the first is the opcode for MVI A
instruction and the second is the data byte. The 8085 needs to read these bytes first from
memory and thus requires at least two machine cycles. The first machine cycle is opcode
fetch and second is memory read. Fig. 5.21 shows the timing diagram for this instruction.
This instruction cycle is described in the following paragraphs.
1. The first machine cycle is an opcode fetch machine cycle. In T1, the microprocessor
places the 16-bit memory address (2000H) from the program counter on the address bus,
20H on the A15-A8 and 00H on AD7 - AD 0 and increments program counter to 2001H to
point to the next memory location. It activates ALE signal (active high) to latch the
low-order address 00H from the bus AD7 - AD 0 . It also gives the status signals (IO/M, S1
and S0) 011 to indicate that it is an opcode fetch machine cycle. In T2, the 8085 activates
RD (active low) and reads the contents of memory location 2000H i.e. 3EH. Then 8085
places the opcode in the instruction register and disables the RD signal during T3. During
T4, the 8085 decodes the opcode and recognizes that it needs memory read machine cycle
to read second byte of the instruction. In T4, the contents of the bus A15-A8 are unknown,
and the data bus AD7-AD0 goes into high impedance state.
T1 T2 T3 T4 T1 T2 T3
CLK
High-order High-order
A15 - A8 20H Unspecified 20H
memory address memory address
Low-order Low-order
ALE
IO / M Status IO / M=0, S1=1, S0=1 Opcode fetch IO / M=0, S1=1, S0=0 Status
S1, S0
RD
2. After opcode fetch machine cycle 8085 executes memory read machine cycle. In T1
of memory read machine cycle, 8085 places the address 2001H on the address bus and
increments the program counter to point the next memory location (2002H). 8085 then
activates ALE signal and sends status signals (IO/M, S1 and S0) 010 to indicate memory
read machine cycle. During T2 and T3, 8085 activates RD signal and reads the 8-bit data
from memory location 2001H (30H). The 8085 then stores this data into the accumulator.
Solution : For instruction shown in Fig. 5.22, three machine cycles are required to fetch
the instruction and one additional machine cycle is required to store the contents of
accumulator into the memory. So following machine cycles are required for STA
instruction.
INSTRUCTION CYCLE
MACHINE
CYCLE M1 M2 M3 M4
T-STATE T1 T2 T3 T4 T1 T2 T3 T1 T2 T3 T1 T2 T3
CLOCK
TYPE OF
MACHINE OPCODE FETCH MEMORY READ MEMORY READ MEMORY WRITE
CYCLE
The address ( contents of The address The address The address is
ADDRESS the program counter) (PC+1) points to (PC+2) points to the direct
BUS Points to the first byte the second byte the third byte address accessed
(Opcode) of the instruction of the instruction of the instruction in M2 and M3
DATA BUS Instruction Opcode (of STA) Low-order byte High-order byte Contents of the
of the direct of the direct Accumulator
address address
TM
ß Example 5.3 :
instruction.
Indicate machine cycles and T-states required for execution of LXI
Solution : For instruction shown in Fig. 5.23, three machine cycles are required to fetch
the instruction. As it is an immediate instruction, operand i.e. immediate 16-bit data is
given within the instruction, no further machine cycle is required.
INSTRUCTION CYCLE
MACHINE
CYCLE M1 M2 M3
T-STATE T1 T2 T3 T4 T1 T2 T3 T1 T2 T3
CLOCK
TYPE OF
MACHINE OPCODE FETCH MEMORY READ MEMORY READ
CYCLE
The address ( contents of The address The address
ADDRESS the program counter) (PC+1) points to (PC+2) points to
BUS points to the first byte the second byte the third byte
(Opcode) of the instruction of the instruction of the instruction
DATA BUS Instruction Opcode (of LXI) Low-order byte High-order byte
of the direct of the direct
address address
ß Example 5.4 : Indicate machine cycles and T-states required for execution of LHLD
instruction.
Solution : For instruction shown in Fig. 5.24, three machine cycles are required to fetch
the instruction and two additional machine cycles are required to load 16-bit contents from
two consecutive memory locations into HL register pair. So following machine cycles are
required for LHLD instruction.
TM
MACHINE
T-STATE T1 T2 T3 T4 T1 T2 T3 T1 T2 T3 T1 T2 T3 T1 T2 T3
CLOCK
TYPE OF
TM
MACHINE OPCODE FETCH MEMORY READ MEMORY READ MEMORY WRITE MEMORY WRITE
5 - 23
CYCLE
The address ( contents of The address The address The address is The address is
ADDRESS the program counter) (PC+1) points to (PC+2) points to the direct the direct
Instruction Opcode (LHLD) Low-order byte High-order byte Byte pointed by Byte pointed by
DATA BUS of the direct of the direct direct address the direct address
address address
Timing diagram 1 :
MVI A, data ...... MVI L, data
These instructions directly load a specified register with a data byte specified within
the instruction. They require the following machine cycles.
1. Opcode fetch : Program counter gives the memory address on low-order and
high-order address bus. This machine cycle is required for reading the opcode into the
microprocessor and decode it. Program counter is incremented by one.
2. Memory read : This machine cycle reads the data from addressed memory location
into specified register of the microprocessor.
Fig. 5.25 shows the timings required for different signals. Table 5.2 gives the
instructions for which the timing diagrams are same. Only difference is in opcode.
T1 T2 T3 T4 T1 T2 T3
CLK
PCH PC = PC + 1 PCH PC = PC + 1
High Order High Order ACI, data (8) CE
A15 - A8 Unspecified ADI, data (8) C6
Memory Address Memory Address
ANI, data (8) E6
CPI, data (8) EF
PCL PCL
ORI, data (8) F6
Low-Order Opcode Low-Order SBI, data (8) DE
AD7 - AD0 Memory Memory Data
Address (*) Address SUI, data (8) D6
XRI, data (8) EE
MVI A, data 3E
MVI B, data 06
ALE MVI C, data 0E
MVI D, data 16
MVI E, data 1E
MVI H, data 26
MVI L, data 2E
IO / M S1 - S0 IO / M=0, S0=1, S1=1 IO / M=0, S0=0, S1=1
Table 5.2
RD
Fig. 5.25
TM
ACI, data.. ADI, data.. ANI, data.. CPI, data.. ORI, data.. SBI, data.. SUI, data.. XRI,
data..
These instructions perform logical operation specified in the instruction with the
contents of accumulator and the data within the instruction. They require the following
machine cycles.
1. Opcode fetch : Program counter gives the memory address on low-order and
high-order address bus. This machine cycle is required for reading the opcode into the
microprocessor and decode it. Program counter is incremented by one.
2. Memory read : This machine cycle reads the data from addressed memory location
and after performing specified logical operation result is stored in the accumulator.
Timing diagram 2 :
LXI rp, data (16)
This instruction loads immediate 16-bit data specified within the instruction into
register pair or stack pointer. It requires three machine cycles as explained below.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. In this machine cycle, the opcode of LXI rp (e.g. 01 of LXI B) is read into the
microprocessor and is decoded. Program counter is incremented by one.
2. Memory read : Program counter gives address on low-order and high-order address
bus. The low-order byte of the data specified within the instruction is read into
microprocessor (low-order register of rp) from this address. The program counter is
incremented by one.
3. Memory read : Program counter keeps address on low-order and high-order address
bus. The high-order byte of the data specified within the instruction is read into
microprocessor (high-order register of rp) from this address. The program counter is
incremented by one.
TM
Fig. 5.26 shows the timing diagram of LXI rp, data (16) instruction.
ALE
IO / M , S0-S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 =1
RD
Table 5.3
Fig. 5.26
Timing diagram 3 :
MVI M, data (8)
This instruction loads the 8-bit data specified within the instruction into memory
whose address is specified by HL register pair. It requires three machine cycles as
explained below.
1. Opcode fetch : Program counter gives the address on low-order and high-order
address bus. This machine cycle is required for reading the opcode (36H) into the
microprocessor. Program counter is incremented by one.
2. Memory read : Program counter gives the address on low-order and high-order
address bus. The data (given within the instruction) is read into the microprocessor in this
machine cycle. Program counter is incremented by one.
TM
3. Memory write : This machine cycle writes the data at the address given by HL
register pair. The contents of H register are the higher-order byte of the address and the
contents of L register are the lower-order byte of the address.
The timing diagram of MVI M, data (8) is shown in Fig. 5.27.
ALE
IO / M , S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 =0
RD
WR
MVI M, data (8)
Fig. 5.27
Timing diagram 4 :
Fig. 5.28 gives the timing diagram for the instructions given in the Table 5.4. These
instructions require only opcode fetch machine cycle. Program counter gives address on
low-order and high-order address bus. The opcode of the instruction is read into the
microprocessor from the addressed memory location and is decoded.
TM
Table 5.4
Fig. 5.28
Timing diagram 5 :
These instructions performs logical operation between the contents of accumulator and
the contents of memory location pointed by HL register pair. They require following
machine cycles.
1. Opcode fetch : Program counter gives address on low-order and high-order address
bus. The opcode of ADD (86H) is read into the microprocessor from the addressed
memory location and is decoded. Program counter is incremented by one.
2. Memory read : The contents of H register give the high-order address and contents
of L register give the low-order address. The data stored at this address is read into the
microprocessor and after performing the specified logical operation result is stored in
accumulator instruction (for example opcode for instruction ADD M is 86H). Fig. 5.29
gives the timing diagram for the instructions given in the Table 5.5.
TM
CLK
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
Fig. 5.29
Timing diagram 6 :
STA address :
This instruction stores the contents of A register at the address given within the
instruction. It requires following machine cycles :
1. Opcode fetch : Program counter gives the memory address on high-order and
low-order address bus. This machine cycle is required for reading the opcode (32H) into
the microprocessor and to decode it. Program counter is incremented by one.
2. Memory read : Program counter gives the memory address on high-order and
low-order address bus. In this machine cycle, the low-order byte of the address specified
within the instruction is read into the microprocessor from the addressed memory location.
Program counter is incremented by one.
3. Memory read : Program counter gives the memory address on high-order and
low-order address bus. In this machine cycle the high-order byte of the address specified
within the instruction is read into the microprocessor from addressed memory location.
Program counter is incremented by one.
4. Memory write : This machine cycle is required for writing the data from the
accumulator at the addressed memory location. This address is nothing but the data read
into the microprocessor in previous two memory read cycles. Fig. 5.30 gives the timing
diagram for STA address instruction.
TM
Timing diagram 7 :
T1 T2 T3 T4 T1 T2 T3 T1 T2 T3 T1 T2 T3
Microprocessors and Microcontroller
TM
5 - 30
ALE
Fig. 5.30
RD
instruction. The machine cycles required for this instruction are explained below.
8085 Timing Diagrams
low-order address bus. This machine cycle reads the opcode (3AH) into the microprocessor
1. Opcode fetch : Program counter gives the memory address on high-order and
This instruction loads the data into A register from the address given within the
Microprocessors and Microcontroller 5 - 31 8085 Timing Diagrams
IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
Contents
Address Higher Byte
Memory
T3
DATA
Memory Address
MEMORY READ
High-Order
T2
Address
Memory
T1
Address
PC=PC+1
DATA
Memory Address
T3
MEMORY READ
High-Order
T2
Low-Order
Address
Memory
PCH
PCL
T1
Lower Byte
Address
Memory Address
PC=PC+1
DATA
T3
MEMORY READ
High-Order
T2
LDA Address
PCH
Low-Order
Address
Memory
PCL
T1
Unspe-
cified
T4
IO / M = 0, S0 = 1, S1 = 1
OPCODE FETCH
PC=PC+1
Opcode
Memory Address
(LDA)
T3
High-Order
T2
Low-Order
Address
Memory
PCH
PCL
T1
IO / M S0 - S1
AD7 - AD0
ALE
RD
A15 - A8
Fig. 5.31
2. Memory read : Program counter gives address on low-order and high-order address
bus. In this machine cycle, the low order byte of the address specified within the
instruction is read into the microprocessor from the addressed memory location. Program
counter is incremented by one.
3. Memory read : Program counter gives the address on low-order and high-order
address bus. In this machine cycle, the high-order byte of the address specified within the
instruction is read into the microprocessor from the addressed memory location. Program
counter is incremented by one.
TM
4. Memory read : This machine cycle is required for reading the data into the A
register from the addressed memory location. The address is nothing but data read into
the microprocessor in previous two memory read cycles.
Timing diagram 8 :
STAX rp
This instruction stores the contents of A register in memory whose address is specified
by register pair (BC or DE). It requires the following machine cycles :
1. Opcode fetch : Program counter places the memory address on low-order and
high-order address bus. This machine cycle is required for reading the opcode of STAX rp
(e.g. 02H for STAX B) into the microprocessor and decode it.
2. Memory write : Higher-order address is obtained from higher-order register and
lower-address is obtained from lower-order register of the specified register pair. The
contents of the accumulator are stored into the addressed memory location. Thus memory
write machine cycle is required for writing the data from the microprocessor (A register)
to the addressed memory location. Fig. 5.32 gives the timing diagram for STAX rp.
PCH rpH
High-Order Unspe- High-Order
A15 - A8 Memory Address cified Memory Address
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
WR
Fig. 5.32
TM
Timing diagram 9 :
LDAX rp
This instruction loads A register with the contents of memory location whose address
is specified by register pair (BC or DE). It requires the following machine cycles.
1. Opcode fetch : Program counter places the memory address on low-order and
high-order address bus. This machine cycle is required for reading the opcode of LDAX rp
(e.g. 0A for LDAX B) into the microprocessor and decode it.
2. Memory read : This machine cycle is required for reading the data into the
accumulator. The address at which the data is stored is obtained from register pair
specified within the instruction. In this machine cycle higher-order register contents are
kept on higher-order address bus and lower-order register contents are kept on
lower-order address bus. The data is read into the microprocessor (register A) from the
addressed memory location. Fig. 5.33 gives the timing diagram.
PCH PC = PC + 1 rpH
High-Order Unspe- High-Order
A15 - A8 Memory Address cified Memory Address
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
LDAX rp
RD LDAX B 0A
LDAX D 1A
Table 5.7
Fig. 5.33
TM
Timing diagram 10 :
DAD rp
This instruction adds 16-bit data from specified register pair or stack pointer in the
contents of HL register pair and stores the result in the HL register pair. This instruction
requires three machine cycles.
CLK
PCH PC=PC+1
High-Order Unspe-
A15 - A8 Memory Address cified
Unspecified Unspecified
PCL
Low-Order Opcode
AD7 - AD0 Memory
Address (*)
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 = 0
RD
WR
Fig. 5.34
1. Opcode fetch : Program counter places the memory address on low-order and high.
order address bus. This machine cycle is required for reading the opcode of DAD
(e.g. 09 for DAD B) into the microprocessor and decode it.
2. and 3. Bus idle
These machine cycles are required to do the internal operation i.e. to perform 16-bit
addition. During these machine cycles buses are not in use.
TM
Timing diagram 11 :
DCX rp and INX rp
These instructions decrement/increment the contents of register pair (rp) specified
within the register by one and store result in the same register pair. It requires only
opcode fetch machine cycle. In this cycle, program counter gives address on low-order and
high-order address bus. The opcode of DCX rp (e.g. 0B of DCX B) is read into the
microprocessor and it decodes it. This machine cycle requires 6 T-states. Fig. 5.35 gives the
timing diagram of DCX rp. The instruction INX rp also has same timing diagram, the only
difference is the opcode.
T1 T2 T3 T4 T5 T6
CLK
PCH PC=PC+1
High-Order
A15 - A8 Memory Address Unspecified
PCL
Low-Order Opcode DCX rp INX rp
AD7 - AD0 Memory
Address (*) DCX B 0B INX B 03
DCX D 1B INX D 13
DCX H 2B INX H 23
DCX SP 3B INX SP 33
ALE
Table 5.9
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1
RD
Fig. 5.35
Timing diagram 12 :
INR M and DCR M
These instructions increment/decrement the contents of memory by one where
memory address is specified by HL register pair. It requires three machine cycles as
explained below.
1. Opcode fetch : Program counter gives the memory address on low-order and
high-order address bus. This type of machine cycle is required for reading the opcode
(e.g. 34H of INR M) into the microprocessor and to decode it.
TM
2. Memory read : The contents of HL register pair give the address of memory where
the data (which is to be incremented) is stored. In this machine cycle data is read into the
microprocessor from this memory location.
3. Memory write : The microprocessor writes the incremented data at the memory
location given by the contents of HL register pair.
Fig. 5.36 gives the number of T-states and timing required for each of the above
operation. The DCR M instruction decrements the contents of memory by one where
memory is specified by HL register pair. The timing diagram for DCR M is same as for
INR M. Only difference is in opcode.
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 =0
RD
WR INR M 34
DCR M 35
Table 5.10
Fig. 5.36
Timing diagram 13 :
XTHL
This instruction exchanges the contents of memory location pointed by the stack
pointer with the contents of L register and the contents of the next memory location with
the contents of H register. This instruction requires five machine cycles as explained below.
TM
TM
5 - 37
ALE
Fig. 5.37
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
RD
and is decoded in this machine cycle. The program counter is incremented by one.
8085 Timing Diagrams
address bus. The opcode of XTHL (E3H) is read from this address into the microprocessor
1. Opcode fetch : Program counter places address on low-order and high-order
Microprocessors and Microcontroller 5 - 38 8085 Timing Diagrams
2. Memory read : Stack pointer gives address on low-order and high-order address
bus. The data is read from this addressed memory location into the microprocessor. The
stack pointer is incremented by one.
3. Memory read : Stack pointer gives address on low-order and high-order address
bus. The data is read from this addressed memory location into the microprocessor.
4. Memory write : Stack pointer gives address on low-order and high-order address
bus. This machine cycle is required for writing the data from H register of microprocessor
into the addressed memory location. Stack pointer is again decremented by one.
5. Memory write : Stack pointer gives address on low-order and high-order address
bus. This machine cycle is required for writing the data from L register of microprocessor
into the addressed memory location.
Timing diagram 14 :
PCHL
This instruction loads the contents of HL register pair into the program counter. It
requires only opcode fetch machine cycle. In this, the program counter gives address on
low-order and high-order address bus. Microprocessor reads the opcode of PCHL (E9H)
from this memory address and decodes it. This requires 6 T-states.
Fig. 5.38 gives the timing diagram of PCHL instruction.
OPCODE FETCH
T1 T2 T3 T4 T5 T6
CLK
PCH PC HL
High-Order
A15 - A8 Memory Address Unspecified
Low-Order Opcode
AD7 - AD0 Memory
Address (PCHL)
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1
PCHL
RD
Fig. 5.38
TM
Timing diagram 15 :
SPHL
This instruction copies the contents of HL register pair into the stack pointer. It
requires only opcode fetch machine cycle. In this, program counter gives address on
low-order and high-order address bus. Microprocessor reads the opcode of SPHL (F9H)
from this addressed memory location and decodes it. Program counter is incremented by
one. This machine cycle requires 6 T-states. Fig. 5.39 gives the timing diagram of SPHL.
OPCODE FETCH
T1 T2 T3 T4 T5 T6
CLK
PCH PC = PC + 1
High-Order
A15 - A8 Memory Address Unspecified
PCL
Low-Order Opcode
AD7 - AD0 Memory
Address (SPHL)
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1
RD
SPHL
Fig. 5.39
Timing diagram 16 :
LHLD address
Fig. 5.40 gives the timing diagram of LHLD address instruction. This instruction loads
L register with the contents of memory location given within the instruction and loads H
register with the contents of memory location at address next to it. It requires the
following five machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. Microprocessor reads the opcode of LHLD (2AH) from this memory location
and decodes it. Program counter is incremented by one.
TM
TM
AD7 - AD0 Address (LHLD) Address Address Address Address
5 - 40
Fig. 5.40
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
2. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads data from the addressed memory location. This data is the
low-order byte of the address specified within the instruction. Program counter is
incremented by one.
3. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads data from the addressed memory location. This data is the
high-order byte of the address specified within the instruction.
4. Memory read : The data read in the previous two memory read cycles is placed on
the address bus. Microprocessor reads the contents of memory location and loads it in
L register. This memory address is incremented by one.
5. Memory read : Now the incremented address is present on the address bus.
Microprocessor reads the contents of memory location and loads it in H register.
Timing diagram 17 :
SHLD address
Fig. 5.41 gives the timing diagram of SHLD address instruction. This instruction stores
the contents of L register in the memory location given within the instruction and contents
of H register at address next to it. It requires the following five machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. Microprocessor reads the opcode of SHLD (22H) from this memory location
and decodes it. Program counter is incremented by one.
2. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads data from the addressed memory location. This data is the
low-order byte of the address specified within the instruction. Program counter is
incremented by one.
3. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads data from the addressed memory location. This data is the
high-order byte of the address specified within the instruction.
4. Memory write : The data read in the previous two memory read cycles is placed on
the address bus. Microprocessor writes the contents of L register at this memory address.
This memory address is incremented by one.
5. Memory write : Now the incremented address is present on the address bus.
Microprocessor writes the contents of H register at this memory address.
TM
TM
Addr
5 - 42
ALE
Fig. 5.41
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
WR
SHLD addr
8085 Timing Diagrams
Microprocessors and Microcontroller 5 - 43 8085 Timing Diagrams
Timing diagram 18 :
JMP address
This instruction loads the program counter with the address given within the
instruction and resumes the program execution from this location. It requires three
machine cycles as explained below.
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
JMP C3
JZ CA
JNZ C2 JMP J conditional (when condition is
JC DA valid) * for instruction JNZ addr, if
JNC D2 zero flag is not set at the time of
JP F2 execution of instruciton condition is valid.
JM FA
JPE EA
JPO E2
Table 5.11
Fig. 5.42
TM
3. Memory read : Program counter gives address on low-order and high-order address
bus. The data at this addressed memory location is read into the microprocessor. This data
is nothing but the high-order byte of the address specified within the instruction. The data
read into the microprocessor in these two memory read cycles is loaded into the program
counter. So the program execution starts from the address specified within the instruction.
Fig. 5.42 gives the timing diagram of JMP address instruction. The timing diagram of
J condition, when condition is valid is same as that of JMP address.
Timing diagram 19 :
J condition (When condition is not valid)
This instruction transfers program control to the next instruction written after this
instruction. Due to fetch execution overlap flag are checked in the T-state of the next
machine cycle. Hence it is not possible for 8085 to decide whether to jump or not at the
given address after opcode fetch machine cycle. As a result, it executes memory read
machine cycle. At T1 of this machine cycle 8085 checks the necessary flag and goes to the
next instruction if condition is not true.
PCH PC = PC + 1 PC = PC + 1
High-Order Unspe- High-Order
A15 - A8 Memory Address cified Memory Address
PCL
Low-Order Opcode Low-Order
AD7 - AD0 Memory
Address (*)
Memory
Address
Data
ALE JZ CA
JNZ C2
JC DA
JNC D2
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 JP F2
JM FA
JPE EA
JPO E2
RD
Table 5.12
Fig. 5.43
TM
Timing diagram 20 :
ALE
IO / M S0-S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
RD
WR
PUSH rp
PUSH B C5
PUSH D D5
PUSH H E5
Table 5.13
Fig. 5.44
PUSH rp
The timing diagram of PUSH rp is shown in Fig. 5.44. This instruction decrements
stack pointer by one and copies the higher byte of the register pair (rp) into the memory
location pointed by stack pointer. It then decrements the stack pointer again by one and
copies the lower byte of the register pair into the memory location pointed by stack
pointer. The machine cycles required for this instruction are explained below.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode of PUSH rp. (e.g. C5H of PUSH B) is read into the
microprocessor. It decodes it. Program counter is incremented by one. Stack pointer is
decremented by one.
2. Memory write : Stack pointer places address on low-order and high-order address
bus. Microprocessor writes contents of high-order register (e.g. B in BC) at this address.
Stack pointer is again decremented by one.
TM
3. Memory write : Stack pointer gives address on low-order and high-order address
bus.
Microprocessor writes contents of low-order register (e.g. C in BC) at this address.
Timing diagram 21 :
Clock
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
POP rp
POP B C1
POP D D1
POP H E1
Table 5.14
Fig. 5.45
POP rp
Fig. 5.45 shows the timing diagram of POP rp instruction. This instruction copies the
contents of memory location pointed by the stack pointer into the lower byte of the
specified register pair and increments the stack pointer by one. It then copies the contents
of memory location pointed by stack pointer into the higher byte of the specified register
pair and increments the stack pointer again by one.
This instruction requires the following machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode of POP rp (e.g. C1 of POP B) is read into the microprocessor and
is decoded in this machine cycle. The program counter is incremented by one.
TM
2. Memory read : The stack pointer gives address on low-order and high-order address
bus. The data present at this address is loaded into the lower byte of the specified register
pair (e.g. in C register for BC register pair). The stack pointer is incremented by one.
3. Memory read : The stack pointer gives address on low-order and high-order address
bus. The data present at this address is loaded into high order byte of the specified
register pair (e.g. in B register for BC register pair). The stack pointer is again incremented
by one.
Timing diagram 22 :
PUSH PSW
This instruction decrements stack pointer by one and copies the accumulator contents
into the memory location pointed by stack pointer. It then decrements the stack pointer
again by one and copies the flag register into the memory location pointed by the stack
pointer. The machine cycles required for this instruction are explained below.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode of PUSH PSW (F5H) is read into the microprocessor. It decodes
it. Program counter is incremented by one. Stack pointer is decremented by one.
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
RD
WR
PUSH PSW
Fig. 5.46
TM
2. Memory write : Stack pointer places address on low-order and high-order address
bus. Microprocessor writes contents of accumulator at this address. Stack pointer is again
decremented by one.
3. Memory write : Stack pointer gives address on low-order and high-order address
bus. Microprocessor writes contents of flag register at this address. The timing diagram of
PUSH PSW is shown in Fig. 5.46 (See Fig. 5.46 on previous page) .
Timing diagram 23 :
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
POP PSW
Fig. 5.47
POP PSW
Fig. 5.47 gives the timing diagram of POP PSW instruction. This instruction copies the
contents of memory location pointed by the stack pointer into the flag register and
increments the stack pointer by one. It then copies the contents of memory location
pointed by stack pointer into the A register and increments the stack pointer again by one.
This instruction requires the following machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode of POP PSW (C1H) is read into the microprocessor and is
decoded in this machine cycle. The program counter is incremented by one.
TM
2. Memory read : The stack pointer gives address on low-order and high-order address
bus. The data present at this address is loaded (read) into the flag register of
microprocessor. The stack pointer is incremented by one.
3. Memory read : The stack pointer gives address on low-order and high-order address
bus. The data present at this address is loaded (read) into the A register of microprocessor.
The stack pointer is again incremented by one.
Timing diagram 24 :
CALL address
Fig. 5.48 (See Fig. 5.48 on next page) gives the timing diagram of CALL address
instruction. This instruction is used to transfer program control to a subprogram or
subroutine. This instruction pushes the current program counter contents onto the stack
and loads the given address into the program counter. It requires five machine cycles as
explained below.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. Microprocessor reads the opcode of CALL (CDH) from this memory address
and decodes it. Program counter is incremented by one. This machine cycle requires
6 T-states.
2. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads the lower byte of address specified within the instruction from
the addressed memory location. Program counter is incremented by one.
3. Memory read : Program counter gives address on low-order and high-order address
bus. Microprocessor reads the higher byte of address specified within the instruction from
the addressed memory location. Program counter is incremented by one. Stack pointer is
decremented by one.
4. Memory write : Stack pointer places address on low-order and high-order address
bus. Microprocessor writes the high-order byte of program counter at this addressed
memory location. Stack pointer is again decremented by one.
5. Memory write : Stack pointer places address on low-order and high-order address
bus. Microprocessor writes the low-order byte of program counter at this addressed
memory location.
TM
PC = PC + 1
PCH PC = PC + 1 PCH PC = PC + 1 PCH SP = SP – 1 SPH SP = SP – 1 SPH
High-Order High-Order High-Order High-Order High-Order
A15 - A8 Memory Address Unspecified Memory Address Memory Address Memory Address Memory Address
TM
Lower Byte Higher Byte
5 - 50
ALE
Fig. 5.48
IO / M IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
S0 - S1
RD
Timing diagram 25 :
C condition (When condition is not valid)
This instruction transfers program control to the next instruction written after this
instruction. It is similar to J condition when condition is not valid.
PCH PC = PC + 1 PC = PC + 2
High-Order High-Order
A15 - A8 Memory Address Unspecified Memory Address CC DC
CNC D4
PCL CP F4
Low-Order Opcode Low-Order CM FC
AD7 - AD0 Memory
(CALL)
Memory Data
Address Address CPE EC
CPO E4
CZ CC
ALE CNZ C4
IO / M IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
S0 - S1
RD
CALL addr
Fig. 5.49
Timing diagram 26 :
RET
This instruction POPs the return address (address of the instruction next to call in the
main program) from the stack and loads program counter with this return address. Thus
transfers program control to the instruction next to CALL in the main program. It requires
three machine cycles as explained below.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. From this address microprocessor reads the opcode of RET (C9H) and
decodes it. Program counter is incremented by one.
2. Memory read : This machine cycle reads the “data” into the microprocessor from
the memory whose address is the contents of stack pointer. This “data” is the low-order
byte of the address to which the program control is to be transferred. Stack pointer is
incremented by one.
TM
3. Memory read : This machine cycle reads the “data” in the microprocessor from the
memory whose address is the contents of stack pointer. This “data” is the high-order byte
of the address to which the program control is to be transferred, Stack pointer is again
incremented by one. Fig. 5.50 gives the timing diagram of RET instruction.
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
RC D8 RM F8
RNC D0 RD F0 RET R conditional (when condition is valid)
RNZ C0 RPE E8 * for instruction RNZ, if zero flag is not set at the
RZ C8 RPO E0 time of execution of instruction condition is valid
Table 5.15
Fig. 5.50
The timing diagram of R condition , when condition is valid is same as that of RET
instruction.
Timing diagram 27 :
R conditional (When condition is not valid)
This instruction requires opcode fetch machine cycle.
Fig. 5.51 gives the timing diagram of this instruction. 6 T-states are required for this
opcode fetch machine cycle. This instruction transfers program control to next instruction
written after this instruction. It requires opcode fetch machine cycle. In this cycle, program
counter places address on low-order and high-order address bus. Opcode of Recondition
(e.g. C0 of RNZ) is read into the microprocessor from the addressed memory location.
Program counter is incremented by one.
TM
OPCODE FETCH
T1 T2 T3 T4 T5 T6
RC D8
PCH PC = PC + 1 RNC D0
RNZ C0
High-Order
A15 - A8 Memory Address Unspecified RZ C8
RM F8
PCL RD F0
RPE E8
Low-Order Opcode
AD7 - AD0 Memory
(*)
RPO E0
Address
Table 5.16
RD
Fig. 5.51
Timing diagram 28 :
RST n
This instruction transfers the program control to the specific memory address, called as
vector location. The processor multiplies the RST number (n) by 8 to calculate these vector
addresses. Before transferring the program control to the instruction following the vector
address, RST instruction saves the current program counter contents on the stack. This
instruction requires the following machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode at this memory location (e.g. C7H of RST0) is read into the
microprocessor and is decoded. 6 T-states are required for this machine cycle. The stack
pointer is decremented by one. Program counter is incremented by one.
2. Memory write : Stack pointer gives address on low-order and high-order address
bus. Microprocessor writes the high-order byte of the program counter at this addressed
memory location. Stack pointer is again decremented by one.
3. Memory write : Stack pointer gives address on low-order and high-order address
bus. Microprocessor writes the low-order byte of the program counter at this addressed
memory location. The timing diagram of RST n is shown in Fig. 5.52.
TM
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 1, S1 = 0 IO / M = 0, S0 = 1, S1 = 0
RD
WR
RST n RST 4 E7
RST 0 C7 RST 5 EF
RST 1 CF RST 6 F7
RST 2 D7 RST 7 FF
RST 3 DF
Table 5.17
Fig. 5.52
Timing diagram 29 :
MOV r, M
Fig. 5.53 shows the timing diagram of MOV r, M instruction. This instruction copies
the contents from memory location pointed by HL register pair into the register specified
within the instruction. This instruction requires two machine cycles.
1. Opcode fetch : Program counter places address on low-order and high-order
address bus. The opcode at this memory location (e.g. 7EH of MOV A, M) is read into the
microprocessor and is decoded. 4 T-states are required for this machine cycle. Program
counter is incremented by one.
2. Memory read : HL register pair gives address on low-order and high-order address
bus. The data at this addressed memory location is read into the specified register of the
microprocessor. Program counter is incremented by one.
TM
CLK
PCH PC = PC + 1 H reg.
High-Order Unsp- High-Order MOV A, M
A15 - A8 Memory Address ecified Memory Address MOV B, M
MOV C, M
PCL L reg. (Mem.) MOV D, M
Low-Order Low-Order MOV E, M
AD7 - AD0 Memory Opcode Memory Data
Address Address MOV H, M
r MOV L, M
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 0, S1 = 1
RD
Fig. 5.53
Timing diagram 30 :
MOV M, r
Fig. 5.54 shows the timing diagram of MOV M, r instruction. This instruction copies
the contents from the specified register into the memory location pointed by HL register
pair. This instruction requires two machine cycles.
OPCODE FETCH MEMORY WRITE
T1 T2 T3 T4 T1 T2 T3
CLK
PCH H reg.
High-Order Unspe- High-Order MOV M, A
A15 - A8 Memory Address Memory Address
cified MOV M, B
MOV M, C
PCL L reg. reg. MOV M, D
Low-Order Low-Order MOV M, E
AD7 - AD0 Memory Opcode Memory Data
Address Address MOV M, H
(Mem.) MOV M, L
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1 IO / M = 0, S0 = 1, S1 = 0
RD
WR
Fig. 5.54
TM
Timing diagram 31 :
IN addr
This instruction reads the data byte from the input port whose address is given within
the instruction. This instruction requires two machine cycles.
Instruction IN
M1 (Opcode Fetch) M2 (Memory Read) M3 (I/O Read)
T1 T2 T3 T4 T1 T2 T3 T1 T2 T3
Unspe-
A15 - A8 Higher Address
cified
Higher Address Port Address
ALE
IO / M
RD
MEMR
IOR
Fig. 5.55
TM
Timing diagram 32 :
OUT addr
This instruction writes the data byte from the accumulator into the output port whose
address is given within the instruction. This instruction requires two machine cycles.
Instruction Out
M1 (Opcode Fetch) M2 (Memory Read) M3 (I/O Write)
T1 T2 T3 T4 T1 T2 T3 T1 T2 T3
Unspe-
A15 - A8 Higher Address
cified
Higher Address Port Address
ALE
IO / M
RD
MEMR
MR
IOW
Fig. 5.56
TM
Timing diagram 33 :
HLT
This instruction halts the processor. It requires only opcode fetch machine cycle.
Program counter gives the memory address on low-order and high-order address bus.
Opcode of HLT is read and decoded by the microprocessor. Program counter is
incremented by one but all buses are tri-stated. The microprocessor can be restarted by a
valid interrupt or by applying a RESET signal. Fig. 5.57 gives the timing diagram of HLT
instruction
PCH
High-Order Unspecified
A15 - A8 Memory Address
PCL
Low-Order Opcode
AD7 - AD0 Memory
Address (HLT)
ALE
IO / M S0 - S1 IO / M = 0, S0 = 1, S1 = 1
RD
WR HLT
Fig. 5.57
Review Questions
Section 5.1
Q.1 Distinguish between an instruction cycle, a machine cycle and a clock cycle with an
example instruction. Dec-09, Marks 6
Section 5.2
Q.1 What is clock signal ?
TM
Section 5.3
Q.1 In which T-cycle the ALE signal is activated ?
Q.2 Explain how the multiplexed data/address bus is shared for data and address.
Section 5.4
Q.1 Draw the timing diagram for I/O read operation and explain.
May-04, Marks 6
Q.2 Draw and explain the timing diagram of opcode fetch machine cycle.
Q.3 Draw and explain the timing diagram of memory read machine cycle.
Q.4 Draw and explain the timing diagram of memory write machine cycle.
Q.5 Draw and explain the timing diagrams for I/O read and I/O write machine cycles.
Q.6 Explain the interrupt acknowledge cycle for RST instruction.
Q.7 Explain the interrupt acknowledge cycle for CALL instruction.
Q.8 Draw and explain the bus idle machine cycle.
Q.9 Indicate machine cycles and T-states required for execution of LDA instruction.
Q.10 Explain the use of Ready signal in 8085.
Q.11 Draw the timing diagram of opcode fetch machine cycle and I/O read cycle.
June-12, Marks 8
Q.12 With timing diagram, explain the memory read operation in 8085 microprocessor.
May-11, Marks 2
Section 5.5
Q.1 Illustrate the timing diagram for the execution of instruction MVI B, 08 in 8085.
Dec.-10, Marks 10
Q.2 Draw timing diagrams for the following instructions with appropriate control and
status signals. Do not explain.
i) INR M ii) RST.
Q.3 Draw timing diagrams for the following instruction with appropriate control and status
signal. Explain in brief CALL 2000.
Q.4 Draw timing diagram for the following instructions with appropriate control and status
signals. Explain in brief :
i) LXI H, 2000 H ii) DCR M.
TM
Q.5 Discuss with examples and their timing diagrams the following with respect to 8085
microprocessor.
i) Instruction with one machine cycle
ii) Instruction with two machine cycles
iii) Instruction with three machine cycles
iv) Instruction with four machine cycles.
Q.6 Draw and explain the timing diagram for the following instructions.
i) LDA 2000 ii) INR M.
Q.7 Draw and explain timing diagrams of the following instructions.
i) IN ii) STA 4000.
Q.8 Draw and explain timing diagrams of the following instructions.
i) LDAX rp ii) DCX rp.
Q.4 How many machine cycles are needed to execute STA 1800 ?
Dec.-05
Ans. : Four machine cycles are needed to execute STA 1800 instruction.
TM
Q.5 What are the different machine cycles in 8085 microprocessor ? May-08
Ans. : The different machine cycles in 8085 microprocessor are :
1. Opcode fetch 2. Memory read
3. Memory write 4. I/O read
5. I/O write 6. Interrupt acknowledge
7. Bus idle
qqq
TM
Notes
TM
Contents
6.1 Introduction
6.2 Features . . . . . . . . . . . . . . . . . . Nov./Dec.-06
6.3 Register Organization of 8086 . . . . . . . . . . . . . Nov./Dec.-06, 08
6.4 Architecture of 8086 . . . . . . . . . . . . . . . . . . April/May-03, 05;
. . . . . . . . . . . . . . . . . . Nov./Dec.-03, 04, 06, 07, 08;
. . . . . . . . . . . . . . . . . . May/June-07, 08
6.5 Addressing Modes . . . . . . . . . . . . . . . . . . Dec.-07,08; June-07, 09;
. . . . . . . . . . . . . . . . . . May/June-06, 08; Nov./Dec.-06, 07, 08
(6 - 1)
TM
6.1 Introduction
In 1978, Intel came out with the 8086 processor. The Intel 8086 is a 16-bit
microprocessor, implemented in N-channel, depletion load, silicon gate technology (HMOS)
and packaged it in a 40 pin dual in line package. In this chapter, we study features,
architecture, register organization, signal description, memory organization, bus operations,
I/O addressing capability, minimum and maximum modes of 8086 processor. We also
study the details of 8088 processor.
1. The 8086 is a 16-bit microprocessor. The term “16-bit” means that its arithmetic
logic unit, internal registers and most of its instructions are designed to work with
16-bit binary words.
2. The 8086 has a 16-bit data bus, so it can read data from or write data to memory
and ports either 16 bits or 8 bits at a time. The 8088, however, has an 8-bit data
bus, so it can only read data from or write data to memory and ports 8 bits at a
time.
20
3. The 8086 has a 20-bit address bus, so it can directly access 2 or 10,48,576 (1 MB)
memory locations. Each of the 10,48,576 memory locations is byte wide. Therefore,
a sixteen-bit words are stored in two consecutive memory locations. The 8088 also
20
has a 20-bit address bus, so it can also address 2 or 10,48,576 memory locations.
16
4. The 8086 can generate 16-bit I/O address, hence it can access 2 = 65536 I/O
ports.
5. The 8086 provides fourteen 16-bit registers.
6. The 8086 has multiplexed address and data bus which reduces the number of pins
needed, but does slow down the transfer of data (drawback).
7. The 8086 requires one phase clock with a 33 % duty cycle to provide internal
timing.
Range of clock rates (Refer Fig. 6.1) are :- T/3 2T/3
5 MHz for 8086
8 MHz for 8086-2 TON TOFF
10 MHz for 8086-1
T
8. The 8086 is possible to perform bit,
Fig. 6.1 Clock cycle
byte, word and block operations in
8086. It performs the arithmetic and logical operations on bit, byte, word and
decimal numbers including multiply and divide.
9. The Intel 8086 is designed to operate in two modes, namely the minimum mode
and the maximum mode. When only one 8086 CPU is to be used in a
microcomputer system, the 8086 is used in the minimum mode of operation. In
this mode the CPU issues the control signals required by memory and I/O devices.
In multiprocessor (more than one processor in the system) system 8086 operates in
TM
maximum mode. In maximum mode, control signals are generated with the help of
external bus controller (8288).
10. The Intel 8086 supports multiprogramming. In multiprogramming, the code for
two or more processes is in memory at the same time and is executed in a
time-multiplexed fashion.
11. An interesting feature of the 8086 is that it fetches upto six instruction bytes
(4 instruction bytes for 8088) from memory and queue stores them in order to
speed up instruction execution. Later we will discuss this in detail.
12. The 8086 provides powerful instruction set with the following addressing modes :
Register, immediate, direct, indirect through an index or base, indirect through the
sum of a base and an index register, relative and implied.
SP
15 8 7 0
AX AH AL CS BP
BX BH BL DS SI
CX CH CL ES DI
DX DH DL SS F IP
(a) General purpose registers (b) Segment registers (c) Flag register (d) Pointer and
index registers
TM
The general purpose registers are either used for holding data, variables and
intermediate results temporarily. They can also be used as a counters or used for storing
offset address for some particular addressing modes. The register AX is used as 16-bit
accumulator whereas register AL (lowerbyte of AX) is used as 8-bit accumulator. The
register BX is also used as offset storage for generating physical addresses in case of
certain addressing modes. On the other hand, the register CX is also used as a default
counter in case of string and loop instructions.
TM
The BIU always inserts zeros for the lower 4 bits (nibble) in the contents of segment
register to generate 20-bit base address. For example, if the code segment register contains
348AH, then code segment will start at address 348A0H.
Fig. 6.4 shows a block diagram of the 8086 internal architecture. It is internally divided
into two separate functional units. These are the Bus Interface Unit (BIU) and the
Execution Unit (EU). These two functional units can work simultaneously to increase
system speed and hence the throughput. Throughput is a measure of number of
instructions executed per unit time.
TM
Memory
interface
BIU C-bus
C-Bus
S
6
5 Instruction
B-bus 4 stream
3 byte
ES
2 queue
CS
SS 1
DS
IP
Control
system
EU A-bus
AX AH AL
BX BH BL
Arithmetic
CX CH CL logic unit
DX DH DL
SP
BP
SI Operands
DI Flags
TM
Instruction Queue
To speed up program execution, the fetches six instruction bytes ahead of time from
the memory. These prefetched instruction bytes are held for the execution unit in a group
of six registers called Queue. With the help of queue it is possible to fetch next instruction
when current instruction is in execution. For example, current instruction in execution is a
multiplication instruction. In 8086, operands for multiplication operations are within
registers. Still it requires 100 clock cycles to execute multiply instruction. Like
multiplication there are number of other instructions in 8086 which need a quite a large
number of clock cycles for execution. During this execution time the BIU fetches the next
instruction or instructions from memory into the instruction queue instead of remaining
idle. The BIU continues this process as long as the queue is not full. Due to this, execution
unit gets the ready instruction in the queue and instruction fetch time is eliminated. This is
illustrated in Fig. 6.5.
Time
saved
Sequential F1 D1 E1 F2 D2 E2
phases
BIU F1 F2 F3
Overlapping
phases
EU D1 E1 D2 E2 D3 E3
The queue operates on the principle first in first out (FIFO). So that the execution unit
gets the instructions for execution in the order they are fetched. In case of JUMP and
CALL instructions, instruction already fetched in queue are of no use. Hence, in these
cases queue is dumped and newly formed by loading instructions from new address
specified by JUMP or CALL instruction. Feature of fetching the next instruction while the
current instruction is executing is called pipelining.
TM
BIT 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
U U U U OF DF IF TF SF ZF U AF U PF U CF
1. Carry Flag (CF) : In case of addition this flag is set if there is a carry out of the
MSB. The carry flag also serves as a borrow flag for subtraction.
In case of subtraction it is set when borrow is needed.
TM
2. Parity Flag (PF) : It is set to 1 if result of byte operation or lower byte of the word
operation contain an even number of ones; otherwise it is zero.
3. Auxiliary Flag (AF) : This flag is set if there is an overflow out of bit 3 i.e., carry from
lower nibble to higher nibble (D3 bit to D4 bit). This flag is used
for BCD operations and it is not available for the programmer.
4. Zero Flag (ZF) : The zero flag sets if the result of operation in ALU is zero and
flag resets if the result is nonzero. The zero flag is also set if a
certain register content becomes zero following an increment or
decrement operation of that register.
5. Sign Flag (SF) : After the execution of arithmetic or logical operations, if the MSB
of the result is 1, the sign bit is set. Sign bit 1 indicates the result
is negative; otherwise it is positive.
6. Overflow Flag (OF) : This flag is set if result is out of range. For addition this flag is
set when there is a carry into the MSB and no carry out of the
MSB or vice-versa. For subtraction, it is set when the MSB needs
a borrow and there is no borrow from the MSB, or vice-versa.
ß Example 6.1 : Give the contents of the flag register after execution of following addition.
ß Example 6.2 : Give the contents of the flag register after execution of following subtraction
0110 0111 0010 1001
– 0011 0101 0100 1010
0011 0001 1101 1111
Solution : SF = 0, ZF = 0, PF = 1, CF = 0, AF = 1, OF = 0
The three remaining flags are used to control certain operations of the processor.
1. Trap Flag (TF) : One way to debug a program is to run the program one
instruction at a time and see the contents of used registers and
memory variables after execution of every instruction. This
process is called ‘single stepping’ through a program. Trap flag
is used for single stepping through a program. If set, a trap is
executed after execution of each instruction, i.e. interrupt service
routine is executed which displays various registers and memory
variable contents on the display after execution of each
instruction. Thus programmer can easily trace and correct errors
in the program.
TM
TM
Physical address
FFFFFH Highest address
64 K
64 K
64 K
64 K
00000H
Physical memory
2. It allows instruction code, data, stack, and portion of program to be more than
64 kB long by using more than one code, data, stack segment, and extra segment.
3. It facilitates use of separate memory areas for program, data and stack.
4. It permits a program or its data to be put in different areas of memory, each time
the program is executed .e. program can be relocated which is very useful in
multiprogramming.
TM
value must be offset from (added to) the segment base address in CS to produce the
required 20-bit physical address.
The contents of the CS register are multiplied by 16 (10 H) i.e. shifted by 4 positions to
the left by inserting 4 zero bits and then the offset i.e. the contents of IP register are added
to the shifted contents of CS to generate physical address. As shown in the Fig. 6.8, the
contents of CS register are 348A H, therefore the shifted contents of CS register are
348A0 H. When the BIU adds the offset of 4214 H in the IP to this starting address, we get
38AB4 H as a 20-bit physical address of memory. This is illustrated in Fig. 6.8 (b).
Physical addresses
(a) (b)
Fig. 6.8
We have seen that how 20-bit physical address is generated within the code segment.
In the similar way the 20-bit physical address is generated in the other segments.
However, it is important to note that each segment requires particular segment register
and offset register to generate 20-bit physical address.
TM
SP = 9F20H
Base Pointer, Source Index and Destination Index (BP, SI and DI)
These three 16-bit registers can be used as general purpose registers. However, their
main use is to hold the 16-bit offset of the data word in one of the segments.
Base pointer : We can use the BP register instead of SP for accessing the stack using
the based addressing mode. In this case, the 20-bit physical stack address is calculated
from BP and SS. Addressing modes are discussed in later section.
Source Index : Source index (SI) can be used to hold the offset of a data word in the
data segment. In this case, the 20-bit physical data address is calculated from SI and DS.
Destination Index : The ES register points to the extra segment in which data is
stored. String instructions always use ES and DI to determined the 20-bit physical address
for the destination.
TM
Example 1 :
1) MOV AL, [BP]
This instruction copies a byte from memory
3000 0 H SS
location to the AL register. The effective address for
+ the memory location is contained in the BP register.
001 0 H BP
Physical Address 3 0 0 1 0 H By default, an effective address is added to the stack
segment (SS) to produce the physical memory address
(30010 H).
2) MOV CX, [BX]
This instruction copies a word from memory
2000 0 H DS location to the CX register. The effective address is
+
002 0 H BX contained in the BX register. By default an effective
Physical Address 2 0 0 2 0 H address is added to the data segment (DS) to
produce the physical memory address (20020 H).
4) MOV CS : [BX], AL
This instruction copies a byte from the AL register
1000 0 H CS to a memory location. The effective address for the
+ memory location is contained in the BX register. By
002 0 H BX
Physical Address 1 0 0 2 0 H default an effective address in BX will be added to the
data segment (DS) to produce the physical memory
address. In this instruction, the CS: in front of [BX]
indicates that we want BIU to add the effective address to the code segment (CS) to
produce the physical address. The CS: is called segment override prefix.
We have seen how the 8086 fetches code bytes from memory by generating 20-bit
physical address with the help of IP and CS. We have also seen how the 8086 accesses the
stack using SS and SP. In this section we will see the different ways that an 8086 can
access the data. The different ways that a processor can access data are referred to as
addressing modes.
The addressing modes of any processor can be broadly classified as :
· Data addressing modes.
· Program memory addressing modes.
· Stack memory addressing modes.
TM
MOV AL, BL AL BL
MOV AL, 20 H AL 20 H
15 0
TM
20-bit physical address after shifting the contents of the desired segment register four bits
to the left and then adding the 16-bit EA to it.
There are six ways to specify effective address (EA) in the instruction.
a. Direct addressing mode b. Register indirect addressing mode
c. Based addressing mode d. Indexed addressing mode
e. Based indexed addressing mode f. String addressing mode.
AL
40 H
13001 H
MOV AL, [3000H] 60 H 60 H 13000 H
10000H+3000H
DS 1000
DS ´ (10H)+3000H
Memory
10 13001H
10000 H +3000 H
DS 1000
DS´(10 H)+3000 H
TM
Example :
MOV CL, [9823H] ; This instruction will copy the contents of the
; memory location, at a displacement of 9823H from the
; data segment base, into the CL register. Here, 9823H is
; the effective address (EA) which is written
; directly in the instruction.
Memory
20 120001H
MOV BX, [CX] 20 30 30 12000H
BH BL 11FFFH
BX
1000 0 H
DS 1000H
DS ´ (10H)
12000H
+
Physical address
2000H
CX 2000H
Effective
address
Example :
1. MOV [DI], BX ; The instruction copies the 16-bit contents of BX into a
; memory location offset by the value of EA specified in DI
; from the current contents in DS. Now, if [DS] = 7205H,
; [DI] = 0030H, and [] = 8765H, then after MOV [DI], BX,
; content of (8765H) is copied to memory locations
; 72080H and 72081H.
TM
3. Base-Plus-Index-Addressing :
Base-plus-index addressing is similar to indirect addressing because it indirectly
addresses memory data. This addressing uses one base register (BP or BX) and one index
register (DI or SI) to indirectly address memory. The base register often holds the
beginning location of a memory array, while the index register holds the relative position
of an element in the array. Remember that whenever BP addresses the memory data, the
contents of stack segment, BP and index register are used to generate physical address.
Memory
MOV CX, [BX+DI]
10H 12031H
CX
1000 0 H
DS 1000H
DS ´ (10H)
12000H 12030H
+ +
2000H
BX 2000H
DI 2000H
TM
30H
30H 40H 40H ARRAY+6
CH CL ARRAY+5
CX ARRAY+4 ARRAY + DI
ARRAY+3 DI Element Index
ARRAY+2
ARRAY+1
ARRAY
ARRAY
DS Segment base +
BX ARRAY base
Fig. 6.10
4. Register Relative Addressing :
Register relative addressing is similar to base-plus-index addressing. Here, the data in
a segment of memory are addressed by adding the displacement to the contents of a base
or an index register (BP, BX, DI or SI). Remember that displacement should be added to
the register within the [ ]. This is illustrated in the Fig. 6.11. Displacement can be any 8-bit
or 16-bit number.
MOV CX, [BX + 0003H] or MOV CX, [BX +3]
10H 61004H
10H 20H 20H 61003H
CH CL 61002H
CX
6000 0 H
DS 6000H +
DS ´ (10 H)
1000H 1003H
BX 1000H +
Base Displacement
03H
Fig. 6.11
TM
Note :
30H ARRAY+6
30H 40H 40H ARRAY+5
CH CL ARRAY+4
ARRAY+2
ARRAY+1
ARRAY
DS Segment base +
ARRAY
Displacement in the
segment register
Fig. 6.12
TM
20310H
AL 50 50H
0300H 0310H
BX 0100H + + +
10H
SI 0200H
DS 2000H 20000H
DS ´ (10H)
Fig. 6.13
Addressing arrays with base relative-plus-index :
As mentioned earlier this addressing mode is useful in addressing two dimensional
array. Two dimensional array usually stores records. For example, student record such as
its name, roll no etc. Therefore, each record contains number of data elements. To access
2
1
0 Record+3
AL 2
1 + Displacement
0
Record+2
2
1
0
Record+1 + SI
2
1
0
Record
DS
BX
Fig. 6.14
TM
data element from a particular record we use base register to hold the beginning address
of the array of records, index register to point a particular record in the array of records
and displacement to point a particular element in the record. This is illustrated in Fig. 6.14.
Example :
MOVS BYTE ; If [DF] = 0, [DS] = 3000H, [SI] = 0600H, [ES] = 5000H,
; [DI] = 0400H, [30600H] = 38H, and [50400H] = 45H, then
; after execution of the MOVS BYTE, [50400H] = 38H,
; [SI] = 0601H, and [DI] = 0401H.
6.5.1.3 Addressing Modes for Accessing I/O Ports (I/O Modes) Nov./Dec.-08
Standard I/O devices uses port addressing modes. For memory-mapped I/O, memory
addressing modes are used. There are two types of port addressing modes : direct and
indirect.
In direct port mode, the port number is an 8-bit immediate operand. This allows fixed
access to ports numbered 0 to 255.
Example :
OUT 05H, AL ; Sends the contents of AL to 8-bit port 05H.
IN AX, 80H ; Copies 16-bit contents of port 80H
In indirect port mode, the port number is taken from DX allowing 64 K 8-bit ports or
32 K 16-bit ports.
Example :
IN AL, DX ; if [DX] = 7890H, then it copies 8-bit content of port 7890H
; into AL.
IN AX, DX ; copies the 8-bit contents of ports 7890H and 7891H into AL
; and AH, respectively.
Note : The 8-bit and 16-bit I/O transfers must take place via AL and AX,
respectively.
TM
JMP 2000H EA 00 00 00 00
Fig. 6.15
Relative program memory addressing :
In this addressing mode, the term relative is restricted to instruction pointer (IP). For
example, if a JMP instruction skips the next 5 bytes of memory, the address in relation to
the instruction pointer is a 5 that adds to the instruction pointer. This generates the
address of the next program instruction. This is illustrated in Fig. 6.16.
Opcode
20000 H EB
JMP [05]
20001 H 05
20002 H –
Offset
20003 H –
20004 H –
20005 H –
20006 H –
20007 H
20008 H
Fig. 6.16
TM
It is important to note that in JMP instruction, opcode takes one byte and displacement
may take one or two byte. When displacement is one byte (8-bit), it is called short jump.
When displacement is two byte (16-bit), it is called near jump. In both (short and near)
cases only contents of IP register are modified; contents of CS register are not modified.
Such jumps are called intrasegment jumps because jumps are within the current code
segment.
The relative JMP and CALL instructions can have either an 8-bit or a 16-bit signed
displacement that allows a forward memory reference or a reverse memory reference.
Instruction Operation
JMP NEAR PTR [BX] Jumps to memory location addressed by the contents of the data
segment memory location addressed by BX within the current code
segment.
IP ¬ ([ BX + 1,] [ BX])
High byte Low byte
JMP NEAR PTR [DI + 2] Jumps to memory location addressed by the contents of the data
segment memory location addressed by DI plus 2 within the current
code segment.
IP ¬ ([DI + 3], [DI + 2])
High byte Low byte
JMP ARRAY [BX] Jumps to memory location addressed by the contents of the data
segment memory location addressed by ARRAY plus BX with the
current code segment.
IP ¬ ([ARRAY + BX + 1], [ARRAY + BX])
High byte Low byte
Table 6.2
6.5.3 Stack Memory Addressing Modes Nov./Dec.-07
The stack is a portion of read/write memory set aside by the user for the purpose of
storing information temporarily. When the information is written on the stack, the
operation is called PUSH. When the information is read from stack, the operation is called
a POP.
TM
SP = 9F20H
PUSH Operation :
The PUSH instruction decrements stack pointer by two and copies a word from some
source to the location in the stack where the stack pointer points. Here the source must be
a word (16 bit). The source of the word can be a general purpose register, a segment
register or memory. The Fig. 6.19 shows the map of the stack before and after execution of
PUSH AX and PUSH CX instructions.
AX 4455H
CX 1234H AX 4455H
End of stack segment End of stack segment
4FFFFH CX 1234H 4FFFFH
SP FFFFH
4FFFEH 44 H 4FFFEH
4FFFDH 55 H 4FFFDH
4FFFCH 12 H 4FFFCH
4FFFBH SP FFFBH 34 H 4FFFBH Top of stack
4FFFAH 4FFFAH
40003H 40003H
40002H 40002H
40001H 40001H
SS 4000H Start of stack segment SS 4000H Start of stack segment
40000H 40000H
BX DX BX 4455H DX 1234H
40003H 40003H
40002H 40002H
40001H 40001H
SS 4000H Start of stack segment SS 4000H Start of stack segment
40000H 40000H
TM
Review Questions
Section 6.2
Q.1 List the features of 8086 microprocessor.
Section 6.3
Q.1 Name the various segment registers and their usage in 8086 processor.
Dec.-06, Marks 4
Section 6.4
Q.1 Draw and explain in detail about the architecture of 8086.
May-03, Dec.-03,04, Marks 16; May-05, Marks 10, Dec.-06,08, Marks 12
Q.2 Draw the internal block diagram of 8086 and explain the bus interface unit and
execution unit. June-08, Dec.-11, Marks 8
Q.3 Give the significance of O flag, T flag and I flag, D flag of 8086. Dec.-06, Marks 4
Q.5 Draw and explain the flag register of 8086 in brief. Dec.-08, Marks 8
Q.7 If the stack segment register contains 3000h and stack pointer register contains 8434h,
what is the physical address of the top of the stack in 8086 microprocessor ?
Dec.-11, Marks 2
Q.9 What are the advantages of using memory segmentation in 8086 ? Dec.-06, Marks 2
Q.10 List the advantages of using segment registers in 8086. June-08, Marks 2
Section 6.5
Q.1 Explain the addressing modes of 8086 with examples.
June-06,07,09, Marks 16, Dec.-06,07,08, Marks 12
Q.2 Discuss in detail the data related addressing modes of 8086 with an example.
June-08, Marks 10
Q.3 Explain in detail the stack structure of 8086. Write a simple program to illustrate the
concept of programming the stack. Dec.-07, Marks 16
TM
Ans. :
1 2 4 B 0 H
+ 3 4 1 C H
1 5 8 C C H
Q.2 What is the role of TF and IF flags in the flag register of 8086 ?
Dec.-04, May-05
Ans. : Trap flag is used for single stepping through a program. If set, a trap is
executed after execution of each instruction, i.e. interrupt service routine is executed
which displays various registers and memory variable contents on the display after
execution of each instruction. Thus programmer can easily trace and correct errors in
the program.
IF flag is used to allow/prohibit the interruption of a program. If set, a certain
type of interrupt (a maskable interrupt) can be recognized by the 8086; otherwise,
these interrupts are ignored.
Ans. : D flag is used with string instructions. If DF = 0, the string is processed from
its beginning with the first element having the lowest address. Otherwise, the string is
processed from the high address towards the low address.
Ans. : Feature of fetching the next instruction while the current instruction is
executing is called pipelining.
The 8086 BIU fetches six instruction bytes ahead of time from the memory and
save prefetched instructions in queue to implement pipelining.
Q.5 How the 20-bit effective address is calculated in an 8086 processor ? Dec.-06
Ans. : The segment register contents are shifted by 4 position to the left by inserting 4
zero bits and then 16-bits offset is added to shifted contents to get 20-bit physical
address.
TM
Q.7 What are the advantages of using memory segmentation in 8086 ? Dec.-06
Ans. : The segment override prefix is an additional 8-bit code which is put in memory
before the code for the rest of the instruction. This additional code selects the alternate
segment register. The code byte for the segment override prefix as the format 001XX110.
The XX represents a 2 bits which are as follows : ES = 00, CS = 01, SS = 10 and DS = 11
In this instruction, the CS: in front of [BX] indicates that we want BIU to add the
effective address to the code segment (CS) to produce the physical address.
In this instruction, the CS: in front of [BX] indicates that we want BIU to add the
effective address to the code segment (CS) to produce the physical address.
Example : MOV CS : [BX], AL
Ans. : The microprocessor architecture which allows fetching the next instruction
while the current instruction is executing is called pipelined architecture.
Ans. : The segment registers of 8086 are : CS (Code segment), DS (Data segment),
ES (Extra segment) and SS (Stack segment)
Ans. : A flag is a flip-flop which indicates some condition produced by the execution
of an instruction or control certain operations of the EU. Such conditions are necessary
for programmer to develop the desired program logic.
TM
Ans. : The segment register is identified by the contents of the segres field in the
instruction code, as shown in the following table -
Segres Code
CS 0 1
DS 1 1
ES 0 0
SS 1 0
qqq
TM
Contents
7.1 8086 Signals . . . . . . . . . . . . . . . . . . Nov./Dec.-03, 06, 07,
. . . . . . . . . . . . . . . . . . May/June-06, 07, 08, 09
7.2 Addressing Memory . . . . . . . . . . . . . . . . . . Nov./Dec.-06, May/June-07
7.3 Addressing I/O . . . . . . . . . . . . . . . . . . Nov./Dec.-07
7.4 Minimum Mode 8086 System and Timings. . . . Nov./Dec.-05, 06, 07, 08,
. . . . . . . . . . . . . . . . . . May/June-06, 07, 08
7.5 Maximum Mode 8086 System and Timings . . . Nov./Dec.-05, 06, 07, 09,
. . . . . . . . . . . . . . . . . . May/June-06, 07, 08, 09
(7 - 1)
TM
Unlike 8085, 8086 and 8088 can be operated in two modes : Minimum mode and
Maximum mode. In this chapter we study the topics related to Minimum mode and
Maximum mode operation of 8086. Topics include clock generation, bus buffering, bus
latching, timings, minimum mode operation and maximum mode operation.
In order to implement many situations in the microcomputer system the 8086 and 8088
has been designed to work in two operating modes :
1. Minimum mode 2. Maximum mode
The minimum mode is used for a small systems with a single processor and
maximum mode is for medium size to large systems, which often include two or more
processors. Fig. 7.1 shows the pin diagram of 8086 and 8088 in minimum as well as
maximum mode. As a close comparison reveals, there is no much difference between two
microprocessors - both are packaged in 40-pin dual-in-line package (DIPs). As mentioned
in section 6.1, the 8086 is a 16-bit microprocessor with a 16-bit data bus, and the 8088 is a
16-bit microprocessor with an 8-bit data bus. The pin-out shows, the 8086 has pin
connections AD0-AD15, and the 8088 has pin connections AD0-AD7. There is one more
minor difference in one of the control signals. The 8086 has an M/IO pin and the 8088 has
(Max (Min
mode) mode)
GND 1 40 VCC GND 1 40 VCC
AD14 2 39 AD15 A14 2 39 A15
AD13 3 38 A16/S3 A13 3 38 A16/S3
AD12 4 37 A17/S4 A12 4 37 A17/S4
AD11 5 36 A18/S5 A11 5 36 A18/S5
AD10 6 35 A19/S6 A10 6 35 A19/S6
AD9 7 34 BHE/S7 A9 7 34 SS0 (HIGH)
AD8 8 33 MN/MX A8 8 33 MN/MX
(Min (Max
AD7 9 32 RD mode) AD7 9 32 RD mode)
AD6 10 8086 31 RQ/GT0 (HOLD) AD6 10 8088 31 HOLD (RQ/GT0)
CPU CPU
AD5 11 30 RQ/GT1 (HLDA) AD5 11 30 HLDA (RQ/GT1)
AD4 12 29 LOCK (WR) AD4 12 29 WR (LOCK)
AD3 13 28 S2 (M/IO) AD3 13 28 IO/M S2
AD2 14 27 S1 (DT/R) AD2 14 27 DT/R S1
AD1 15 26 S0 (DEN) AD1 15 26 DEN S0
AD0 16 25 QS0 (ALE) AD0 16 25 ALE (QS0)
NMI 17 24 QS1 (INTA) NMI 17 24 INTA (QS1)
an IO/M pin. The only hardware difference appears on pin 34 of both chips : on the 8086
it is a BHE/S7 pin, while on the 8088 it is a SS0 pin.
The 8086 signals can be categorized in three groups.
· Signals having common functions in both minimum and maximum modes.
· Signals having special functions for minimum mode.
· Signals having special functions for maximum mode.
S4 S3 Register
0 0 ES
0 1 SS
1 0 CS or none
1 1 DS
S5 gives the current setting of the interrupt flag (IF) and S6 is always zero.
3. BHE/S7 : BHE (Bus High Enable) : Low on this pin during first part of the
machine cycle, indicates that at least one byte of the current transfer is to be made
on higher order byte AD15-AD8; otherwise the transfer is made on lower order
byte AD7-AD0.
Status S7 is output during the later part of the machine cycle, but, presently, S7 has
not been assigned a meaning.
TM
7. RESET : It clears , IP, DS, SS, ES, and the instruction queue. It then sets CS to
FFFFH. This signal must be high for at least 4 clock cycles. When RESET is
removed, 8086 will fetch its next instruction from physical address FFFF0H.
8. READY : If this signal is low the 8086 enters into wait state. This signal is used
primarily to synchronize slower peripherals with the microprocessor.
9. TEST (Input) : This signal is only used by the WAIT instruction. The 8086 enters
into a wait state after execution of the WAIT instruction until a LOW signal on the
TEST pin. TEST signal is synchronized internally during each clock cycle on the
leading edge of the clock cycle.
10. RD (Output) : RD is low whenever the 8086 is reading data from memory or an
I/O device.
11. MN/MX (Input) : The 8086 can be configured in either minimum mode or
maximum mode using this pin. This pin is tied high for minimum mode.
ALE (Address Latch Enable) output : This signal is provided by 8086 to demultiplex
the AD0-AD15 into A0-A15 and D0-D15 using external latches.
DEN (Data Enable) output : This signal informs the transceivers that the CPU is ready
to send or receive data.
TM
DT/R (Data transmit/Receive) output : This signal is used to control data flow
direction. High on this pin indicates that the 8086 is transmitting the data and low
indicates that the 8086 is receiving the data.
M/IO output : It is used to distinguish memory data transfer, (M/IO = HIGH) and
I/O data transfer (M/IO = LOW).
WR : Write output : WR is low whenever the 8086 is writing data into memory or an
I/O device.
HOLD input, HLDA output : A HIGH on HOLD pin indicates that another master
(DMA) is requesting to take over the system bus. On receiving HOLD signal processor
outputs HLDA signal HIGH as an acknowledgment. At the same time, processor tristates
the system bus. A low on HOLD gives the system bus control back to the processor.
Processor then outputs low signal on HLDA.
0 1 First byte of an
1 0 Queue is empty
1 1 Subsequent byte of an
2. S2 , S1, S0 (output) : These three status signals indicate the type of transfer to be
take place during the current bus cycle.
0 1 1 Halt 1 1 1 Inactive-Passive
3. LOCK : This signal indicates that an instruction with a LOCK prefix is being
executed and the bus is not to be used by another processor.
TM
4. RQ/GT1 and RQ/GT0 : In the maximum mode, HOLD and HLDA pins are
replaced by RQ (Bus request)/GT0 (Bus Grant), and RQ/GT1 signals. By using bus
request signal another master can request for the system bus and processor
communicate that the request is granted to the requesting master by using bus
grant signal. Both signals are similar except the RQ/GT0 has higher priority than
RQ/GT1.
Note : To access odd addressed word two bus cycles are required.
TM
Every microprocessor based system has a memory system. Almost all systems contain
two basic types of memory, read-only memory (ROM) and random access memory (RAM)
or read/write memory. Read only memory contains system software and permanent
system data such as lookup tables, while Random Access Memory contains temporary data
and application software. ROMs/PROMs/EPROMs are mapped to cover the CPU’s reset
address, since these are non-volatile. When the 8086 is reset, the next instruction is fetched
from memory location FFFF0H. So in the 8086 systems, the location FFFF0H must be ROM
location.
The Fig. 7.3 shows memory map for 8086. Certain locations in 1 Mbyte memory are
reserved and some are dedicated for specific CPU operations. Locations from FFFF0H to
FFFF5H are dedicated to the initialization procedure of the 8086, while locations FFFF6H to
FFFFBH are dedicated to the initialization procedure of the 8089 input/output processor.
Locations 00000H to 00013H are dedicated to store the vector addresses of the dedicated
interrupts. The dedicated locations are used for processing of specific system initialization,
interrupt and reset function.
Intel has also reserved several locations for future hardware and software products.
Locations from 00014H to 0007FH and locations from FFFFCH to FFFFFH are reserved
locations. The locations from 00000H to 003FFH are used for interrupt vector table (IVT).
The interrupt vector table provides the starting location/address of the interrupt service
routine for the interrupt supported by 8086.
1 Mbytes
003FFH 003FEH
003FDH 003FCH
0007FH 0007EH
0007DH 0007CH
Reserved Interrupt
vector
table
00015H 00014H
128 bytes 00013H 00012H
00011H 00010H
Dedicated
00003H 00002H
00001H 00000H
Odd Bank Even Bank
TM
The 8086 can generate 16-bit of I/O address. Thus it can address upto 64 kbyte I/O
locations or 32 K word I/O locations. The 16-bit I/O address appears on A0 to A15
address lines; A16 to A19 lines are at logic 0 during the I/O operations. The 16-bit DX
register is used as 16-bit I/O address pointer to address upto 64 K devices in in-direct
addressing mode. The I/O instructions with direct addressing mode can directly address
one or two of the 256 I/O byte locations in page 0 of the I/O address space. See Fig. 7.4.
7 0
FFFFH
FFFEH
00FFH 64 K
00FEH I/O space
Reserved
00F8H
Page 0
00F7H
0001H
0000H
Fig. 7.4 I/O map for 8086
I/O ports are addressed in the same manner as memory locations. Even addressed
bytes are transferred on the D7-D0 bus lines and odd addressed bytes on D15-D8. Care
must be taken to assure that each register within an 8-bit peripheral located on the lower
portion of the bus be addressed as even. In the I/O space, Intel has reserved 00F8H to
00FF locations.
Latching
Fig. 7.5 shows the typical minimum mode configuration. As shown in the figure, (See
Fig. 7.5 on next page). AD0-AD15, A16/S3-A19/S6, and BHE/S7 signals are multiplexed.
These signals are demultiplexed by external latches and ALE signal generated by the
processor. This is accomplished by using three latch ICs (Intel 8282/8283), two of them are
TM
+VCC
+VCC
MN/MX
CLK
R 8284A ALE STB
Clock READY
BHE BHE
Generator 8282
RESET
Address Address
RES A19-A16 latch Bus
C RDY
(3)
AD15-AD0 OE
WAIT
STATE
8086 CPU
GENERATOR
8286 Data
Transceiver Bus
(2)
DEN OE
DT/R T
required for a 16-bit address and three are needed if a full 20-bit address is used. In case
of 8088, only two external latches are required. One for demultiplexing AD0-AD7 and other
for demultiplexing A16/S3 and AD19/S6. Fig. 7.6 shows the internal block diagram of
8282/8283 latches (See Fig. 7.6 on next page). The 8282 provides noninverting outputs
while the 8283 version inverts the input data. In addition to their demultiplexing function,
these chips also buffer the address lines, providing increased output driving capability. The
output low level is specified as 0.45 V maximum with a sink current of 32 mA maximum.
The high level is specified as 2.4 V minimum while supplying a 5 mA maximum high
level load current.
Buffering
If a system includes several interfaces then to increase current sourcing/sinking
capacities it is necessary to use drivers and receivers (transceiver) for data bus also. The
Intel 8286 device is used to implement the transceiver block shown in Fig. 7.5. The 8286
contains 16 tristate elements, eight receivers, and eight drivers. Therefore two 8286s are
required to service 16 data lines of 8086. Fig. 7.7 shows the detailed connections of 8286.
TM
8282 8283
STB OE STB OE
AD0 A0 B0
DT/R signal is connected to the
AD1 A1 B1 T input, which controls the
AD2 A2 B2
direction of the data flow. When
AD3 A3 B3
8 Data Bus this signal is low, receivers are
AD4 A4 B4
AD5 A5 2 B5
8
enabled, so that 8086 can read data
AD6 A6 B6
AD7 A7 6 B7 from memory or input device. To
write data into memory or output
DEN OE
DT/R T
device, the 8086’s DT/R signal goes
8086
high. Due to this drivers are
AD8 A0 B0
enabled to transfer data from 8086
AD9 A1 B1 to the memory or the output
AD10 A2 B2
device. At the time of data transfer,
AD11 A3 B3
8 Data Bus to enable output of transceiver its
AD12 A4 B4
AD13 A5 2 B5
8 OE should be low. This is
AD1 4 A6 B6
AD15 A7 6 B7 accomplished by connecting DEN
signal of 8086 to the OE pin of
OE
T 8286, since DEN signal goes low
when CPU is ready to send or
Fig. 7.7 Connection details of 8286 receive data.
Clock generator
The third component, other than the processor that appears in Fig. 7.5 is an 8284 clock
generator. The 8284 clock generator does the following functions :
· Clock generation
· RESET synchronization
TM
· READY synchronization
· Peripheral clock generation.
The Fig. 7.8 shows the internal logic diagram of 8284.
RES D
Q RESET
CLK
X1
XTAL
X2 Oscillator
OSC
F/C
+3 +2 PCLK
SYNC SYNC
EFI
CSYNC
RDY1
AEN1 CLK
ASYNC
Fig. 7.8
The top half of the logic diagram represents the clock and reset synchronization section
of the 8284 clock generator. As shown in the logic diagram, the crystal oscillator has two
inputs : X1 and X2. If a crystal is attached to X1 and X2, the oscillator generates a
square-wave signal at the same frequency as the crystal. The output of oscillator is fed to
an AND gate and also to an inverter buffer that provides the OSC output signal. The F/C
signal selects one of the oscillator inputs. When F/C input is 1, the EFI input determines
the frequency; otherwise oscillator determines the frequency. When EFI input is used,
CSYNC signal is used for multiple processor system synchronization. If the internal crystal
oscillator is used, CSYNC signal is grounded. In both the cases the output clock frequency
is one third of the input frequency. The CLK signal is also buffered before it leaves the
clock generator. As shown in the Fig. 7.8, the output of the divide-by-3 counter generates
the timing for ready synchronization, a signal for another counter (divide-by-2), and the
CLK signal to the 8086/8088 microprocessors. The two cascaded counters (divide-by-3 and
divide-by-2) provide the divide-by-6 output at PCLK, which can be used to provide clock
input for peripherals. The address enable pins, AEN1 and AEN2 are provided to qualify
the bus ready signals, RDY1 and RDY2, respectively.
TM
The reset circuit of 8284 consists of a schmitt trigger buffer and a single D flip-flop
circuit. The D flip-flop ensures that the timing requirements of the 8086/8088 RESET input
are met. This circuit applies the RESET signal to the microprocessor on the negative edge
(1 to 0 transition) of each clock. The 8086/8088 microprocessors sample RESET at the
positive edge (0 to 1 transition) of the clocks; therefore, this circuit meets the timing
requirements of the 8086/8088.
The Fig. 7.9 shows the circuit connection for 8284 clock generator. The RC circuit
provides a logic 0 to the RES input pin when power is first applied to the system. After a
short time, the RES input becomes a logic 1 because the capacitor charges toward + 5.0 V
through the register. A push button switch allows the microprocessor to be reset by the
operator.
X1 X2
RESET RESET
F/ C
AEN1
AEN2 READY READY
CSYNC
+5 V
8284 8086
Clock Generator or
10 K 8088
RES
10 mF
PCLK
RDY1 RDY2
Other signals
The status on the M/IO, RD, and WR lines decides the type of data transfer, as listed
in the Table 7.1.
M/IO RD WR Operation
0 0 1 I/O read
0 1 0 I/O write
1 0 1 Memory read
1 1 0 Memory write
Table 7.1
TM
HOLD and HLDA signals are used to interface other bus masters like DMA controller.
Interrupt request (INTR) and interrupt acknowledge (INTA) are used to extend the
interrupt handling capacity of the 8086 with the help of interrupt controller.
7.4.2 Minimum Mode 8086 System
The Fig. 7.10 shows the typical minimum mode 8086 system. Here, interfacing of
memory and I/O devices are shown with the basic minimum mode 8086 configuration.
INTA
M/IO
BHE
WR
D15
A19
RD
D0
A0
O0
RD WRD0-D15 CS
Data
I/O
Y1
OE WR D0-D7 Addr CS
A0
RAM (EVEN)
Data
Y0
OE WRD8-D15 Addr CS
OE D0-D7 Addr CS
EPROM (EVEN)
A0
Data
Y7
OE D8-D15 Addr CS
EPROM (ODD)
Data
Transceivers
Latches
(2 or 3)
(2)
STB
OE
OE
T
O0
O1
O6
O7
Addr/Data
VCC
GND
Decoder
EN EN
8086 ALE
AD0-AD15
A16-A19
MN/MX
M/IO
INTA
RD
WR
BHE
DT/R
DEN
VCC
Ready
Addr
Reset
CLK
Y0
Y1
Y6
Y7
8284
Decoder
generator
generator
EN EN
state
Clock
Wait
Rdy
RES
VCC
M/IO
Addr
TM
For interfacing memory module to 8086, it is necessary to have odd and even memory
banks. This is implemented by using two EPROMs and two RAMs. Data lines D15-D8 are
connected to odd bank of EPROM and RAM, and data lines D7-D0 are connected to even
bank of EPROM and RAM. Address lines are connected to EPROM and RAM as per their
capacities. RD signal is connected to the output enable (OE) signals of EPROMs and
RAMs. WR signal is connected to WR signal of RAMs. Two separate decoders are used to
generate chip select signals for memory and I/O devices. These chip select signals are
logically ORed with either BHE or A0 to generate final chip select signals. For generating
final chip select signals for odd bank decoder outputs are logically ORed with BHE signal.
On the other hand to generate final chip select signals for even bank decoder outputs are
logically ORed with A0 signal.
The 16-bit I/O interface is shown in the Fig. 7.10. RD and WR signals are connected to
the RD and WR signals of I/O device. Data lines D15-D0 are connected to the data lines of
I/O device. The chip select signal for I/O device is generated using separate decoder
whose output is enabled only when M/IO signal is low.
CLK
Address, BHE OUT
A19/S6-A16/S3
Status OUT
and BHE/S7
TAVDV
AD15-AD0 Data IN
Address OUT
ALE
RD
TRLDV
DT/R
DEN
TM
CLK
Address, BHE OUT
A19/S6-A16/S3
Status OUT
and BHE/S7
TDVWH
ALE
TWLWH
WR
DT/R
DEN
TM
CLK
» »
HOLD
»
HLDA
AD15-AD0 Requesting
8086 master 8086
A19/S6, A16/S3
BHE/S7, M/IO
RD, WR, DT/R, DEN
Fig. 7.12 HOLD and HLDA signal timings
Fig. 7.13 shows the typical maximum mode configuration. In the maximum mode
additional circuitry is required to translate the control signals. The additional circuitry
converts the status signals (S2-S0) into the I/O and memory transfer signals. It also
generates the control signals required to direct the data flow and for controlling
8282 latches and 8286 transceivers. The Intel 8288 bus controller is used to implement this
control circuitry.
Fig. 7.14 shows that the 8288 is able to originate the address latch enable signal to the
8282’s, the enable and direction signals to the 8286 transceivers, and the interrupt
TM
STB
GND OE 1 MEGABYTE
AD0-AD15 ADDRESS BUS
ADDR/DATA 8282
A16-A19 LATCH
(2 OR 3)
BHE
T
OE 16-BIT
8286 DATA BUS
TRANSCEIVER
(2)
Latches
STB 8282s (3)
If there is no
8259A, this is
Clock an inverter
8284A
OE Transceiver
8286 (2)
T
CLK ALE
CEN DEN
+5 V
Control Control
AEN Logic Signal DT/R
Generator
8086/ IOB MCE/PDEN
8088
BUS MRDC
controller
8288 MWTC
Control
IQRC bus
S0 Command
S0 Signal IOWC
S1 Status Generator
S1 INTA
S2
Decoder
S2
INTA
Priority interrupt
controller 8259A
acknowledge signal to the interrupt controller. It also decodes the S2-S0 signals to generate
MRDC, MWTC, IORC, IOWC, MCE/PDEN, AEN, IOB, CEN, AIOWC, and AMWC signals.
MRDC (Memory Read Command) : It instructs the memory to put the contents of the
addressed location on the data bus.
MWTC (Memory Write Command) : It instructs the memory to accept the data on the
data bus and load the data into the addressed memory location.
IORC (I/O Read Command) : It instructs an I/O device to put the data contained in
the addressed port on the data bus.
IOWC (I/O Write Command) : It instructs an I/O device to accept the data on the
data bus and load the data into the addressed port.
AEN, IOB and CEN : These pins are used in multiprocessor system. With a single
processor in the system, AEN and IOB are grounded and CEN is tied high. AEN causes
the 8288 to enable the memory control signals. IOB (I/O bus mode) signal selects either
the I/O bus mode or system bus mode operation. CEN (control enable) input enables the
command output pins on the 8288.
The Fig. 7.15 shows the typical maximum mode 8086 system. Here interfacing of
memory and I/O devices are shown with the basic maximum mode configuration. The
connections for memory and I/O devices are similar to that of minimum mode
configuration. However, the generation of control signals from 8086 is done by external
bus controller 8288.
TM
S1 S1 AMWC ADVANCED MW
RES 8288
RESET S2 S2 BUS IORC I/O READ
RDY CTRLR
DEN IOWC I/O WRITE
8086
GND CPU DT/R AIOWC ADVANCED I/O W
INTA INTERRUPT
LOCK N.C. ALE
WAIT ACKNOWLEDGE
STATE
GENERATOR
STB
GND
TM
OE 1 MEGABYTE
AD0-AD15 ADDRESS BUS
ADDR/DATA 8282
7 - 19
A16-A19 LATCH
(2 OR 3)
BHE
Fig. 7.15
T
OE 16-BIT
8286 DATA BUS
TRANSCEIVER Y7 A0 Y6 Y0 A0 Y1
(2)
Y0
Y1 Y2
CLK
S2-S0 S2-S0
S2-S0 Inactive
BHE, A19-A16
Address/status Float
S7-S3
AND BHE/S7
Data IN D15-D0
Address/data A15-A0
(AD15-AD0)
TAVDV
* ALE
TRLDV
* MRDC
or IORC
* DT/R
* DEN
TM
CLK
S2-S0
S2-S0
S2-S0 Inactive
BHE, A19-A16
Address/status Float
S7-S3
AND BHE/S7
* ALE
TWLWHA
* AMWC
or AIOWC
* MWTC
or IOWC TWLWH
* DEN
* 8288 bus controller
outputs
a sequence of three
pulses. The RQ/GT pins
are sampled at the rising
»
RQ/GT
edge of each clock pulse
and if a request is
Master requests 8086 grants bus Master releases detected the 8086 will
bus access to requesting master bus
apply a grant pulse to the
Fig. 7.17 Timings for bus request and bus grant
signals RQ/GT if the following
conditions are met.
1. The previous bus transfer was not the low byte of a word to or from an odd
address if the CPU is an 8086. For an 8088, regardless of the address alignment,
the grant signal will not be sent until the second byte of a word reference is
accessed.
2. The first pulse of an interrupt acknowledgement did not occur during the previous
bus cycle.
3. An instruction with a LOCK prefix is not being executed.
TM
If condition 1 and 2 is not met, then the grant will not be given until the next bus
cycle, and if condition 3 is not met, the grant will wait until the locked instruction is
completed.
After activation of grant pulse, requesting master takes the control of buses. This
master may control the buses for only one bus cycle or for several bus cycles. When it is
ready to relinquish the buses it will send the processor release pulse over the same line
that it made its request.
Review Questions
Section 7.1
Q.1 Explain Min/Max mode of 8086 microprocessor.
Dec.-03, Marks 2; Dec.-06, Marks 10
Q.3 Explain the pin configuration of 8086. May-09, Marks 16, May-11, Marks 2
Q.4 Show the pin configuration and function of signals of 8086 microprocessor.
May-11, Marks 2
Section 7.2
Q.1 Explain in detail about the 8086 memory banks and associated signals for byte and
word operations. Dec.-06, Marks 12
Q.3 Explain in detail about memory access mechanism in 8086. May-07, Marks 8
Q.4 Show the memory organization and interfacing with 8086 microprocessor. Explain how
the memory is accessed. May-11, Marks 2
Section 7.3
Q.1 Calculate how many devices can be addressed by 8086. Dec.-07, Marks 2
Section 7.4
Q.1 Describe the system design using 8086. May-06, Marks 16
TM
Q.2 Describe the minimum mode 8086 system and its timing diagram.
May-06,07,09, Dec.-06,07,08, Marks 16
Section 7.5
Q.1 Write a brief note on 8086 based maximum mode and minimum mode CPU module
with a neat diagram. Dec.-05,06,07, May-06,08, Marks 16
Q.2 Draw and explain a block diagram showing 8086 in maximum mode configuration.
May-07, Dec.-09, Marks 12
Q.4 Explain the maximum mode of 8086 with the timing diagrams of memory read cycles,
memory write cycle and RQ GT timing.
May-08, Marks 16
Ans. : 8086 uses ALE signal to demultiplex the address/data bus and it uses DEN
(Data Enable) and DT/R (Data Transmit/Receive) signals to control the data bus
buffers.
Q.3 What is the use of latch signal on the AD0-AD15 bus in an 8086 system ?
Dec.-06
Ans. : The latch signal (ALE) is used to demultiplex (separate) the address bus and
data bus of the 8086.
Q.4 What is the need for MN/MX pin in 8086 microprocessor ? or why we use
MN / MX pin in 8086 ? Dec.-06, 07
Ans. : The 8086 provides MN/MX pin to select the mode of operation : MN/MX = 1
for minimum mode and MN/MX = 0 for maximum mode.
Q.5 What are the signals involved in memory bank selection in 8086
microprocessor ? May-07
Ans. : The signals BHE and A 0 are involved in memory bank selection in 8086
microprocessor. When BHE = 0 odd bank (Bank 1) is selected and when A 0 = 0 even
bank (Bank 0) is selected.
TM
Q.6 How clock signal is generated in 8086? What is the maximum internal clock
frequency of 8086 ? May-07
Ans. : The clock signal of 8086 is generated using 8284 clock generator. The crystal is
connected to the crystal oscillator inputs of 8284 to generate the clock signal. The
maximum internal clock frequency of 8086 is 5 MHz.
Ans. : BHE (Bus High Enable) : Low on this pin during first part of the machine cycle,
indicates that at least one byte of the current transfer is to be made on higher order
byte AD15-AD8; otherwise the transfer is made on lower order byte AD7-AD0.
LOCK : This signal indicates that an instruction with a LOCK prefix is being
executed and the bus is not to be used by another processor.
Q.8 What information is conveyed when Qs1 Qs0 bits are 01 ? May-08
Ans. : When QS 1 QS 0 bits are 01, they indicate that the queue is loaded with the first
byte of an opcode during the previous clock cycle.
Ans. : When DEN = 0, the transceivers are enabled to send or receive data. Since
DT R = 1, the transceivers are used to transmit data.
Q.10 What is the advance signal name of write command of MAX mode 8086?
May-09
Ans. : The advance signal name of write command of MAX mode 8086 is AMWC for
memory write and AIOWC for I/O write.
Ans. : The 8051 provides 16 bytes of a bit-addressable area. It occupies RAM byte
addresses from 20H to 2FH, forming a total of 128 (16 × 8) addressable bits.
An addressable bit may be specified by its bit address of 00H to 7FH, or 8 bits
may form any byte address from 20H to 2FH.
For example, bit address 4EH refers bit 6 of the byte address 29H.
qqq
TM
Contents
8.1 Introduction . . . . . . . . . . . . . . . . . . Nov./Dec.-05,07,08; May/June-08,09
8.2 Sources of Interrupts in 8086 . . . . . . . . . . . . . . Nov./Dec.-07; May/June-08
8.3 8086 Interrupt Types . . . . . . . . . . . . . . . . . . May/June-06,07,08; Nov./Dec.-06,08
8.4 Interrupt Priorities . . . . . . . . . . . . . . . . . . May/June-06
(8 - 1)
TM
An 8086 interrupt can come from any one the three sources :
· External signal
· Special Instruction in the program
· Condition produced by instruction
TM
INTERRUPT
SERVICE
PROCEDURE
PUSH REGISTERS
MAINLINE PUSH FLAGS
PROGRAM CLEAR IF
CLEAR TF
PUSH CS
PUSH IP
FETCH ISR ADDRESS
POP IP
POP CS
POP FLAGS
POP REGISTERS
IRET
1. It decrements stack pointer by 2 and pushes the flag register on the stack .
2. It disables the INTR interrupt input by clearing the interrupt flag in the flag
register.
3. It resets the trap flag in the flag register.
4. It decrements stack pointer by 2 and pushes the current code segment register
contents on the stack.
5. It decrements stack pointer by 2 and pushes the current instruction pointer
contents on the stack.
TM
6. It does an indirect far jump at the start of the procedure by loading the CS and IP
values for the start of the interrupt service routine (ISR).
An IRET instruction at the end of the interrupt service procedure returns execution to
the main program.
Now the question is “How to get the values of CS and IP register ?” The 8086 gets the
new values of CS and IP register from four memory addresses. When it responds to an
interrupt, the 8086 goes to memory locations to get the CS and IP values for the start of
the interrupt service routine. In an 8086 system the first 1 Kbyte of memory from 00000H
to 003FFH is reserved for storing the starting addresses of interrupt service routines. This
block of memory is often called the interrupt vector table or the interrupt pointer table.
Since 4 bytes are required to store the CS and IP values for each interrupt service
procedure, the table can hold the starting addresses for 256 interrupt service routines.
Fig. 8.2 shows how the 256 interrupt pointers are arranged in the memory table. (See
Fig. 8.2 on next page)
Each interrupt type is given a number between 0 to 255 and the address of each
interrupt is found by multiplying the type by 4 e.g. for type 11, interrupt address is
11 ´ 4 = 44 10 = 0002CH
Only first five types have explicit definitions such as divide by zero and non maskable
interrupt. The next 27 interrupt types, from 5 to 31, are reserved by Intel for use in future
microprocessors. The upper 224 interrupt types, from 32 to 255, are available for user for
hardware or software interrupts.
When the 8086 responds to an interrupt, it automatically goes to the specified location
in the interrupt vector table to get the starting address of interrupt service routine. So user
has to load these starting addresses for different routines at the start of the program.
TM
ADDRESS
3FFH
TYPE 255 POINTER :
(AVAILABLE)
3FCH
AVAILABLE INTERRUPT
POINTERS (224)
TYPE 33 POINTER :
(AVAILABLE)
084H
TYPE 32 POINTER :
080H (AVAILABLE)
07FH
TYPE 31 POINTER :
(RESERVED)
RESERVED INTERRUPT
POINTERS (27)
TYPE 5 POINTER :
(RESERVED)
014H
TYPE 4 POINTER :
OVERFLOW
010H
TYPE 3 POINTER :
1-BYTE INT INSTRUCTION
00CH
DEDICATED INTERRUPT TYPE 2 POINTER :
POINTERS (5) NON-MASKABLE
008H
TYPE 1 POINTER :
SINGLE-STEP
004H
TYPE 0 POINTER : CS BASE ADDRESS
DIVIDE ERROR IP OFFSET
000H
16 BITS
To reset the trap flag we have to reset Bit 8. This can be done by using AND [BP + 0],
0FEFFH instruction instead of OR [BP + 0], 0100H.
TM
TM
ALE
LOCK
INTA
FLOAT
AD0-AD15
Interrupt
type
Fig. 8.3 Interrupt acknowledge machine cycle
2. Once the 8086 receives the interrupt type, it pushes the flag register on the stack,
clears TF and IF, and pushes the CS and IP values of the next instruction on the
stack.
3. The 8086 then gets the new value of IP from the memory address equal to 4 times
the interrupt type (number), and CS value from memory address equal to 4 times
the interrupt number plus 2.
As far as the 8086 interrupt priorities are concerned, software interrupts (All interrupts
except single step, NMI and INTR interrupts) have the highest priority, followed by NMI
followed by INTR. Single step has the least priority.
Interrupt Priority
TM
Review Questions
Section 8.1
Q.1 Write a short note on interrupts and interrupt service routines.
Dec.-08, Marks 8
Q.2 Why is an interrupt driven I/O more efficient than programmed I/O for
8086 microprocessor ? Dec.-05, Marks 2
Section 8.2
Q.1 Discuss in detail the interrupts and interrupt service routine in 8086 processor.
Dec.-07,08, May-08, Marks 8
Q.2 Describe the action taken by 8086 when INTR pin is activated. May-07, Marks 6
TM
Section 8.3
Q.1 Describe the action taken by 8086 when NMI pin is activated. Dec.-06, Marks 4
Q.2 Briefly describe the conditions which cause the 8086 to perform each of the following
types of interrupts : Type 0, Type 1, Type 2, Type 3 and Type 4.
Q.3 Explain interrupt structure of 8086.
Q.4 What are software interrupt ? How 8086 responds to software interrupts ?
Q.5 Draw and explain the interrupt acknowledge cycle of 8086.
Q.6 What is the storage space required to store the interrupt vectors of 8086 ?
May-08, Marks 2
Section 8.4
Q.1 What do you mean by interrupt priorities ?
Q.2 State the interrupt priorities for 8086 interrupts.
Q.1 Why is an interrupt driven I/O more efficient than programmed I/O for
8086 microprocessor ? Dec.-05
Q.2 An interrupt device based on 8086 microprocessor sends 03H onto AD0 through
AD7 data bus when INTA is low. Where should the interrupt jump address
located in the vector table? Dec.-04
TM
Ans. :
Interrupt Priority
Ans. : The 8086 INT instruction can be used to cause the 8086 to do one of the 256
possible interrupt types. The interrupt type is specified by the number as a part of the
instruction. Such interrupts are called software interrupt in 8086.
Ans. : The INTR interrupt of 8086 can be masked or unmasked by making IF flag
equal to 0 or 1, respectively. IF can be set STI instruction and it can be reset by CLI
instruction.
Q.8 What is the storage space required to store the interrupt vectors of 8086 ?
May-08
qqq
TM
Notes
TM
Contents
9.1 Introduction . . . . . . . . . . . . . . . . . . Nov./Dec.-04,05; May/June-06,
. . . . . . . . . . . . . . . . . . April/May-08
9.2 Terminology and Operations
9.3 Memory Structure and its Requirements
9.4 Basic Concepts in Memory
Interfacing with 8085 . . . . . . . . . . . . . . . . . . Nov./Dec.-07; April/May-10
(9 - 1)
TM
Storage Cells
Address Address
0 0
1 1
2 1 0 0 0 1 0 01 2 1 0 0 0 Reading Data
Writing Data
3 3
4 4
5 5
6 6
n-1 n-1
n n
TM
Input
data
WR
Input buffer
CS EPROM
4096 x 8
A10 A11
Internal decoder
Internal decoder
R/W
Memory
2048 x 8
A0 A0
CS
Output buffer Output buffer
RD RD
Output Output
data data
(a) Logic diagram for RAM (b) Logic diagram for EPROM
Fig. 9.2
= 4096
= 8-bit
TM
Example 2 : If memory has 8192 memory locations, then it has 13 address lines.
The Table 9.1 summarizes the memory capacity and address lines required for memory
interfacing.
Table 9.1
As shown in the Fig. 9.2 (a) memory chip has 12 address lines A0-A11, one chip select
(CS), and two control lines, read (RD) to enable output buffer and write (WR) to enable the
input buffer. The internal decoder is used to decode the address lines. Fig. 9.2 (b) shows
the logic diagram of a typical EPROM (Erasable Programmable Read-Only Memory) with
4096 (4 K) registers. It has 12 address lines A0-A11, one chip select (CS), one Read control
signal. Since EPROM is a read only memory, it does not require the (WR) signal.
For interfacing memory devices to microprocessor 8085, following important points are
to be kept in mind.
1. Microprocessor 8085 can access 64 kbytes memory since address bus is 16-bit. But
it is not always necessary to use full 64 kbytes address space. The total memory
size depends upon the application.
2. Generally EPROM (or EPROMs) is used as a program memory and RAM (or
RAMs) as a data memory. When both, EPROM and RAM are used, the total
address space 64 kbytes is shared by them.
3. The capacity of program memory and data memory depends on the application.
4. It is not always necessary to select 1 EPROM and 1 RAM. We can have multiple
EPROMs and multiple RAMs as per the requirement of application.
TM
For example :
We have to implement 32 kbytes of program memory and 4 kbytes EPROMs are
available. In this case, we can connect 8 EPROMs in parallel
(4 kbytes ´ 8 = 32 kbytes) with different chip select for each EPROM.
6. We can place EPROM/RAM anywhere in full 64 kbytes address space. But
program memory (EPROM) should be located from address 0000H since reset
address of 8085 microprocessor is 0000H.
7. It is not always necessary to locate EPROM and RAM in consecutive memory
addresses. For example : If the mapping of EPROM is from 0000H to 0FFFH, it is
not must to locate RAM from 1000H. We can locate it anywhere between 1000H
and FFFFH. Where to locate memory component totally depends on the
application.
The memory interfacing requires to :
· Select the chip
· Identify the register
· Enable the appropriate buffer.
Microprocessor system includes memory devices and I/O devices. It is important to
note that microprocessor can communicate (read/write) with only one device at a time,
since the data, address and control buses are common for all the devices. In order to
communicate with memory or I/O devices, it is necessary to decode the address from the
microprocessor. Due to this each device (memory or I/O) can be accessed independently.
The following section describes common address decoding techniques.
Absolute decoding
In absolute decoding technique, all the higher address lines are decoded to select the
memory chip, and the memory chip is selected only for the specified logic levels on these
high-order address lines; no other logic levels can select the chip. Fig. 9.3 shows the
memory interface with absolute decoding. This addressing technique is normally used in
large memory systems.
Memory Map :
TM
Table 9.2
D0
D7
A0
A7
VCC
A8
A15
G
WR A Y5 IOR
RD B Y6 IOW
IO/M C A Y1 MEMR
Y2 MEMW
G1 G2
74LS138
D 7D 0 A 9 A8 A7 A0 OE D7D0 A9 A 8 A7A0 OE WR
EPROM (1 K) RAM (1 K)
VCC
CS CS
G
A13 A Y0
A14 B Y1
A15 C B
G1 G2
74LS138
A10
A12 A11
TM
Linear decoding
In small systems, hardware for the decoding logic can be eliminated by using
individual high-order address lines to select memory chips. This is referred to as linear
decoding. Fig. 9.4 shows the addressing of RAM with linear decoding technique. This
technique is also called partial decoding. It reduces the cost of decoding circuit, but it has
a drawback of multiple addresses (shadow addresses).
Fig. 9.4 shows the addressing of RAM with linear decoding technique. A15 address
line, is directly connected to the chip select signal of EPROM and after inversion it is
connected to the chip select signal of the RAM. Therefore, when the status of A15 line is
‘zero’, EPROM gets selected and when the status of A15 line is ‘one’ RAM gets selected.
The status of the other address lines is not considered, since those address lines are not
used for generation of chip select signals.
D0
D7
A0
A7
VCC
A8
A15
G
WR A Y5 IOR
RD B Y6 IOW
IO/M C
Y1 MEMR
Y2 MEMW
G1 G2
74LS138
D 7D 0 A9 A8 A7 A0 OE D 7D 0 A9 A8 A7 A0 OE WR
EPROM (1 K) RAM (1 K)
CS CS
A15
Memory Map :
Table 9.3
TM
ß Example 9.1 : Design memory system for the 8085 microprocessor such that it should
contain 8 kbyte of EPROM (Erasable Programmable Read Only Memory) and 8 kbyte of
RAM ( Read/Write Memory).
Solution : Fig. 9.5 shows the desired memory system using IC 2764 (8 K) EPROM and
6264 (8 K) RAM. Memory requires 13 address lines (A0-A12) since 213 = 8 K. The
remaining address lines (A 13 - A 15 ) are decoded to generate chip select ( CS ) signals.
IC 74LS138 is used as decoder. When ( A15 - A13 ) address lines are zero, the Y0 output of
decoder goes low and selects the EPROM. This means that A 15 - A 13 address lines must
be zero to read data from EPROM. The address lines A12 - A0 select the particular memory
location in the EPROM when A 15 - A 13 lines are zero. Similarly, when address lines
A 15 - A 13 are 001, the Y1 output of decoder goes low and selects the RAM. The Table 9.4
shows the memory map for the designed circuit.
D0
D7
A0
A7
VCC
A8
A15
G
WR A Y5 IOR
RD B Y6 IOW
IO/M C
Y1 MEMR
Y2 MEMW
G1 G2
74LS138
D7D0 A12 A11 A10 A9 A8 A7A0 OE D7D0 A12 A11 A10 A9 A8 A7A0 OE WR
EPROM (8 K) RAM (8 K)
VCC 2764 6264
CS CS
G
A13 A Y0
A14 B Y1
A15 C
74LS138
G1 G2
Fig. 9.5 Memory system using IC 2764 (8 K) EPROM and 6264 (8 K) RAM
TM
Memory Map :
ß Example 9.2 : Design a microprocessor system for the 8085 microprocessor such that it
should contain 16 kbyte of EPROM and 4 kbyte of RAM using two 8 kbyte EPROMs
(2764) and two 2 kbyte RAMs (6116).
Solution : Fig. 9.6 (See on page no. 9 - 10) shows the desired memory system using two
(8 K ´ 8) EPROM and two (2 K ´ 8) RAMs. EPROM memory is 8 K, so it requires 13
address lines (A12 - A 0 ) whereas RAM memory is 2 K, so it requires 11 address lines
(A10 - A0). The remaining higher address lines (A15 - A13) are used to generate chip-select
(CS) signals. Table 9.5 shows the memory map for the designed circuit.
Memory Map :
ß Example 9.3 : Interface 2 kbyte RAM to 8085 using 2114 (1 K ´ 4) chips, 74LS138
decoder and full address decoding and give the address map (memory map).
Solution : 2114 RAM is 1 K ´ 4 i.e. it has 1 K (1024) memory locations, each of which is
of 4 bits. 8085 is an 8 bit processor. To interface byte RAM, we require two nibble wide
RAMs, connected together to form byte wide RAM. To form 2 K ´ 8 RAM we require two
sets of 1 K´ 4 + 1 K ´ 4 RAM chips. So in all we require four 1 K ´ 4 RAM chips.
TM
A15
G
WR A Y5 IOR
RD B Y6 IOW
IO/M C
Y1 MEMR
Y2 MEMW
G1 G2
74LS138
TM
D7D0 A12 A11 A10 A9 A8 A7A0 OE D7D0 A12 A11 A10 A9 A8 A7A0 OE D7D0 A10 A9 A8 A7A0 OE WR D7D0 A10 A9 A8 A7A0 OE WR
9 - 10
Fig. 9.6 Memory system using two (8 K × 8) EPROMs and two (2 K × 8) RAMs
Memory Interfacing
Microprocessors and Microcontroller 9 - 11 Memory Interfacing
(Fig. 9.7 see page no. 9 - 12) shows the interface. The IC 74LS138 is used to generate chip
select (CS) signals. Table 9.6 shows the memory map.
Memory Map :
ß Example 9.4 : Design a microprocessor system for the 8085 microprocessor such that it
should contain 2 kbyte of EPROM and 2 kbyte of RAM with starting addresses 0000H and
6000H respectively.
Solution : Fig. 9.8 (See on page no 9 - 13) shows the desired memory system using
2 kbyte EPROM and 2 kbyte RAM. Both EPROM and RAM are 2 K, so they require
11 address lines (A 10 - A 0 ). The remaining higher address lines (A 15 - A 11 ) are used to
generate chip select ( CS) signals.
The chip selection logic is designed to have starting address of EPROM, 0000H and
starting address of RAM, 6000H. This is implemented by selecting EPROM only when
higher address lines (A15 - A11) are all zero, and selecting RAM only when higher address
lines (A15 - A11) are 01100 (Binary). The Table 9.7 shows the memory map for the designed
circuit.
Memory Map :
TM
A15
G
WR A Y5 IOR
RD B Y6 IOW
IO/M C
Y1 MEMR
Y2 MEMW
G1 G2
74LS138
D 7D 4 A9 A8 A7A0 OE WR D 3D 0 A9 A8 A7A0 OE WR D 7D 0 A9 A8 A7A0 OE WR D 3D 0 A9 A8 A7A0 OE WR
TM
RAM (1 K) RAM (1 K) RAM (1 K) RAM (1 K)
9 - 12
VCC
CS CS CS CS
2114 2114 2114 2114
G
A13 A Y0
A14 B
Y1
A15 C
MEMW
MEMR
IOW
IOR
A15
D0
D7
A0
OE WR
RAM (2 K)
D7-D0 A10-A0
CS
OE
EPROM (2 K)
D7-D0 A10-A0
CS
74LS245
Y0
Y3
B
U
ENABLE
E
F
F
+5 V
DIR
G1
G2 GND
O
D
R
E
E
+5V
VCC
O
D
R
E
E
74LS244
74LS373
G2
G1
C
B
A
G1
1G 2G
C
H
A
T
L
G1
B
U
R
E
F
F
C
A
B
CLK
A13
A14
A15
A12
A11
15 pF
AD7
AD0
ALE
A8
A15
WR
IO/M
RD
+5V
X2
VCC
8085
READY
RESET
X1
C
R
SW
Fig. 9.8 Memory system using 2 kbyte EPROM and 2 kbyte RAM
ß Example 9.5 : Design a microprocessor system for the 8085 microprocessor such that it
should contain 4 kbyte of EPROM and 2 kbyte of RAM using two 2 kbytes of EPROMs
and two 1 kbytes RAM. Draw the complete interfacing diagram with buffers, latches, and
chip select logic used.
Solution : Fig. 9.9 shows the desired memory system using two 2 kbyte EPROMs and
two 1 kbyte RAMs.
TM
The uni-directional buffer is used to drive the high order address bus and
bi-directional buffer is used to drive the data bus. The direction pin of the bi-directional
buffer is controlled by the RD signal from the microprocessor. EPROM memory is 2 kbyte,
so it requires 11 address lines (A10-A0) whereas RAM memory is 1 K, so it requires
10 address lines (A9-A0). The remaining higher address lines (A15-A11) are used to generate
chip-select (CS) signals. Table 9.8 shows the memory map for the designed circuit.
Memory Map :
MEMW
MEMR
IOW
IOR
A15
D0
D7
A0
WR
WR
OE
OE
RAM 1
RAM 2
(1 K)
(1 K)
A9-A0
A9-A0
D7-D0
D7-D0
CS
CS
OE
OE
EPROM 1
EPROM 2
(2 K)
(2 K)
A10-A0
A10-A0
D7-D0
D7-D0
CS
CS
74LS245
Y0
Y1
Y2
Y3
B
U
ENABLE
E
F
F
+5 V
DIR
G1
GND
O
D
R
E
E
VCC
+5 V
O
D
R
E
E
74LS244
74LS373
G2
G1
G2
C
B
A
G1
1G 2G
C
H
A
T
L
B
U
R
E
F
F
G1
C
A
B
CLK
A12
A13
A14
A15
A11
15 pF
A8
AD7
AD0
A15
ALE
WR
IO/M
RD
+5 V
VCC
8085
READY
RESET
C
R
SW
Fig. 9.9 Memory system using 2 kbyte EPROMs and two 1 kbyte RAMs
TM
ß Example 9.6 : Design a microprocessor system for the 8085 microprocessor such that it
should contain 8 kbyte of EPROM and 8 kbyte of RAM. Use linear addressing technique
and give the detailed address map.
Solution : Fig. 9.10 shows the desired memory system using 8 kbyte EPROM and
8 kbyte RAM. Both EPROM and RAM are 8 K, so they require 13 address lines (A12-A0).
As problem says to design microprocessor system using linear addressing technique,
A 15 address line is used to generate chip select (CS) signals. When A 15 address line is
low, it selects EPROM and when it is high, it selects RAM. The table 9.9 shows the
memory map for the designed circuit.
Memory Map :
TM
READY 1G 2G
B
VCC
R U
A15 F
F
E
R
A8
74LS244
C RESET
L
Microprocessors and Microcontroller
SW AD7
AD0 A
T A0
CLK C
ALE A15
H
74LS373 B
U
F
8085 F D0
E
DIR R
D7
+5 V ENABLE
TM
74LS245
D
9 - 16
RD A E
C MEMW
WR B O MEMR
address decoding
D
IO/M C IOR
E
R
IOW
X1 X2 G1 G2
74LS138
CS CS
A15
Fig. 9.10 Memory system using 8 kbyte EPROM and 8 kbyte RAM with linear
Memory Interfacing
Microprocessors and Microcontroller 9 - 17 Memory Interfacing
1. All higher address lines are decoded to Few higher address lines are decoded to
select the memory or I/O device. select the memory or I/O device.
2. More hardware is required to design Hardware required to design decoding logic
decoding logic. is less and sometimes it can be eliminated.
3. Higher cost for decoding circuit. Less cost for decoding circuit.
Table 9.10
Review Questions
Section 9.2
Q.1 What is address ?
Q.2 What is memory capacity.
Section 9.3
Q.1 Explain the memory structure and its requirements.
Q.2 How much address lines are required to interface 4 kbytes of memory.
Section 9.4
Q.1 With necessary diagrams, write short notes RAM memory interfacing and ROM
memory interfacing May-08, Marks 16
Q.3 Explain the interfacing of memory with 8085 microprocessor. May-10, Marks 10
Q.4 Interface a 8KX8 EPROM IC and 2KX8 RAM IC with 8085 such that the starting
address assigned to them are 0000H and 4000H respectively using address decoder
having NAND gate and inverters. Dec.-07, Marks 8
Q.6 With necessary diagram, explain the interfacing of a RAM memory IC-6116 (2Kx8)
with 8085. May-11, Marks 8
Q.7 List the steps involved in interfacing a memory to the 8085 microprocessor.
Dec.-10, Marks 2
TM
Q.5 Justify your choice between UV-EPROM and flash EPROM for an external
ROM in an 8051 microcontroller application.
Ans. : Flash EPROMS can be erased electrically with selective erase facility. However,
UV-EPROMS cannot be erased electrically, they need ultraviolet light source. EPROMS
need around 20 minutes to erase and entire EPROM is erased at a time. Thus Hash
EPROM is more preferable during development stage. However, once the product is
ready we can use EPROM as an external memory.
TM
MEMW
MEMR
IOW
IOR
A0-A15
D0-D7
OE WR
RAM (2K)
A10-A0
CS
D7-D0
OE
EPROM (8K)
A12-A0
CS
D7-D0
BUFFER
EN
DIR
+5 V
G2
O
D
R
E
E
1G 2G
C
H
A
T
L
G1
B
U
R
E
F
F
C
A
B
CLK
15 pF
AD0
AD7
ALE
A15
WR
IO/M
RD
A8
+5 V
X2
VCC
8085
A11
A12
A14
A15
A14
A13
Ready
Reset
X1
C
R
SW
D
Fig. 9.11
Q.6 List the steps involved in interfacing a memory to the 8085 microprocessor.
Dec.-10
Notes
TM
Contents
10.1 Introduction . . . . . . . . . . . . . . . . . . April/May-08
10.2 I/O Interfacing Techniques in 8085. . . . . . . . . April/May-04,05; Nov./Dec.-04, 05, 09
. . . . . . . . . . . . . . . . . . May/June-06,07
10.3 Data Transfer Schemes . . . . . . . . . . . . . . . . . Nov./Dec.-08
(10 - 1)
TM
Input port :
It is used to read data from the input
device such as keyboard. The simplest form
D0-D7 of input port is a buffer. The input device is
Input connected to the microprocessor through
Port buffer, as shown in the Fig. 10.1. This buffer
Data bus (Tri-state buffer) Data from input
device is a tri-state buffer and its output is available
(Keyboard) only when enable signal is active. When
Enable
microprocessor wants to read data from the
input device (keyboard), the control signals
Fig. 10.1 from the microprocessor activates the buffer
by asserting enable input of the buffer. Once
the buffer is enabled, data from the input device is available on the data bus.
Microprocessor reads this data by initiating read command.
Output port : It is used to send data to the output device such as display from the
microprocessor. The simplest form of output port is a latch. The output device is
connected to the microprocessor through latch, as shown in the Fig. 10.2. When
microprocessor wants to send data to the output device it puts the data on the data bus
and activates the clock signal of the latch, latching the data from the data bus at the
output of latch. It is then available at the output of latch for the output device.
D0-D7
Output
Device
Data bus (Latch)
To output device
(Display)
CLK
Fig. 10.2
TM
In this chapter, we will see the interfacing concepts of these I/O devices and different
methods of I/O transfer.
The most of the microprocessors support isolated I/O system. It partitions memory
from I/O, via software, by having instructions that specifically access (address) memory,
and others that specifically access I/O. When these instructions are decoded by the
microprocessor, an appropriate control signal is generated to activate either memory or
I/O operation. In 8085, IO/M signal is used for this purpose. The 8085 outputs a logic ‘1’
on the IO/M line for an I/O operation and a logic ‘0’ for memory operation. In 8085, it is
possible to connect 64 kbyte memory and 256 I/O ports in the system since 8085 sends
16 bit address for memory and 8-bit address for I/O. I/O devices can be interfaced to an
8085A system in two ways :
1. I/O Mapped I/O 2. Memory mapped I/O
0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0
A7 A6 A5 A4 A3 A2 A1 A0 Device
Address
A15 A14 A13 A12 A11 A10 A9 A8
0 1 1 0 0 0 0 0 60H
The instruction IN inputs data from an input device (such as keyboard) into the
accumulator and the instruction OUT sends the contents of the accumulator to an output
device such as LED display. These are two byte instructions. The second byte of the
instruction specifies the address or the port number of an I/O device. As it is a byte, the
TM
address or port number can be any of the 256 combinations of eight bits, from 00H to
FFH. Therefore, the 8085 can communicate with 256 different I/O devices. When we want
to interface an I/O device, it is necessary to assign a device address or a port number.
Before going to see this device address logic, we will examine how the 8085 executes IN
and OUT instructions.
Buffer
or To
A0 Data I/O
Latch
A1 bus device
Decoder
A2
OE
A3
A4 G1 IO/M
A5 G2 IOR/IOW
A6
A7 G
RD/WR
Y0
74LS138
TM
To generate device select signal (Y0) low, the address on the address bus must be as
given below :
A2 A1 A0 = 000H ; Activates Y0 output
A3 A4 A5 A6 = 0000H ; makes G1 and G2 low to enable output of decoder
A7 = 1H ; Makes G high to enable output of decoder
Note : Decoder output is enabled only when control signals G1 and G2 are low and
control signal G is high. Therefore the address of this I/O device is 80H as shown in the
Table 10.1.
A7 A6 A5 A4 A3 A2 A1 A0 Address
1 0 0 0 0 0 0 0 80H
Table 10.1
10.2.3 Interfacing Input and Output Devices with Examples
Interfacing Input Device :
The microprocessor 8085 accepts 8-bit data from the input device such as keyboard,
sensors, transducers etc. Fig. 10.4 shows the circuit diagram to interface input port (buffer)
which is used to read the status of 8 switches. The address for this input device is 80H as
device select signal goes low when address is 80H.
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.
The following program checks whether the switch 2 is pressed or not.
Program :
IN 80H ; Read status of all switches
ANI 02H ; Mask bit positions for other switches
JZ NEXT ; if program control is transferred to label
; NEXT, then switch 2 is pressed otherwise not.
Interfacing Output Device :
The microprocessor 8085 sends 8-bit data to the output device such as 7 segment
displays, LEDs, printer etc. Fig 10.5 shows the circuit diagram to interface output port
(latch) which is used to send the signal for glowing the LEDs. LED will glow when output
pin status is low. The IC 74LS138 and 3 input OR gate is used to generate device select
signal. The latch enable signal is active high. So NOR gate is used to generate latch enable
signal, which goes high when Y1 and IOW both are low.
The following program glows the LEDs L1, L3 and L6.
L8 L7 L6 L5 L4 L3 L2 L1
1 1 0 1 1 0 1 0 = DAH
The code (data) DAH must be sent on the latch to glow LEDs L1, L3 and L6.
TM
+5 V
R R R R R R R R = 10 K
74LS244
S0 O0 O0
S1 O1 O1
S2 O2 O2
S3 O3 O3
Tri-state Data bus
DIP S4 O4 buffer O4 of 8085
switches
S5 O5 O5
S6 O6 O6
S7 O7 O7
OE
A0 A
A1 D
B
A2 e
C
A3 c
A4 G1 o IO/M
A5 G2 d
e IOR
A6
A7 G r
RD
Y0
74LS138
L1 L8
330 W
A0 A O0
Data L
A1 B A
bus of
A2 Y1 8085 T
C C
A3
A4 G1 H
IO/M O7
A5 G2 LE
A6 330 W
A7 G IOW
WR
A7 A6 A5 A4 A3 A2 A1 A0
1 0 0 0 0 0 0 1 = 81 H
Program :
MVI A, DAH ; Loads the data in the accumulator.
OUT 81H ; sends the data on the latch.
The Fig. 10.6 shows the combined circuit for I/O interfacing. For this circuit the
address of input port is 80H and address of output port is 81H. The following program
displays the status of switches on the LEDs.
Program :
IN 80H ; Read status of all switches.
OUT 81H ; send status on the output port.
ß Example 10.1 : Refer Fig. 10.6 and write a program that will check the switch1 status
and do accordingly.
R R 10 K
74LS244
D0
B
Data bus U SW1
of 8085 F
F
E
R D7
RD IOR
SW8
OE
+5 V
IO/M
IOW Y0
L1 L8
WR
A0 A 74LS373
A1 D
B E Y0
A2 C 330 W
C L O0
A3 O
D Y1 A
A4 G1
E T
A5 G2 C
R
A6 H
A7 G O7
330 W
LE
Solution :
Input port address = 80H
Output port address = 81H
Flowchart :
Start
Is No
SW1 = 0
?
Yes
Source Program :
LXI SP, 27FFH ; Initialize stack pointer
START : IN 80H ; Read status of switches
ANI 01H ; Masks Bit 1 to Bit 7
JNZ HIGHER ; If sw1 status is not zero goto blink
; higher nibble
MVI A, F0H ; Load bit pattern to glow lower nibble
; LEDs
OUT 81H ; Send it to output port
CALL Delay ; Call delay subroutine
MVI A, FFH ; Load bit pattern to switch off all LEDs
TM
+5 V
R R 10 K
74LS244
D0
B
U
Data bus F SW1
of 8085 F
E
R
RD D7
MEMR
SW8
OE
+5 V
IO/M
MEMW Y0
WR L1 L8
A0 A 74LS373
A1 D
B E Y0
A3 A2 330 W
C C L O0
A4 O Y1 A
G1 T
A5 D C
A6 A7 G E H
R
G2 O7
330 W
A8 74LS138 LE
A9
A10
A11
A12
A13
A14
A15
TM
ß Example
below.
10.2 : Identify the port address and the mapping scheme for the Fig. 10.8 given
A6
A5
A4
A3
A7 IO/M
E1 E2 E3
O7 D7
MSB
A2
DATA OCTAL
A1 3 to 8 BUS LATCH
decoder To LED's
A0 D0
O0
LE OE
WR
Fig. 10.8
Solution :
Mapping scheme : I/O mapped I/O
10.2.4.1 Comparison between Memory Mapped I/O and I/O Mapped I/O in 8085
1. In this device address is 16-bit. Thus A0 to In this I/O device address is 8-bit. Thus A0
A15 lines are used to generate device to A7 or A8 to A15 lines are used to
address. generate device address.
2. MEMR and MEMW control signals are used IOR and IOW control signals are used to
to control read and write I/O operations. control read and write I/O operations.
3. Instructions available are LDA addr, Instructions available are IN and OUT.
STA addr, LDAX rp, STAX rp, MOV M,R,
MOV R,M, ADD, CMP M etc.
4. Data transfer is between any register and I/O Data transfer is between accumulator and I/O
device. device.
5. Maximum number of I/O devices are 65536 Maximum number of I/O devices are 256.
(theoretically).
TM
6. Execution speed using LDA addr, STA addr Execution speed is 10 T-states.
is 13 T-state and 7 T-states for MOV M, r
and MOV r, M instructions.
7. Decoding 16-bit address may require more Decoding 8-bit address will require less
hardware. hardware.
In I/O data transfer, the system requires the transfer of data between external circuitry
and the microprocessor. In this section, we will discuss different ways of I/O transfer.
a. Program controlled I/O or polling control.
b. Interrupt program controlled I/O or interrupt driven I/O.
c. Hardware controlled I/O.
d. I/O controlled by handshake signals.
e. I/O controlled by ready signal.
Review Questions
Section 10.1
Q.1 What do you mean by input port and output port ?
Section 10.2
Q.1 With suitable examples explain how I/O devices are connected using memory mapped
I/O and peripheral I/O. May-05, Marks 10
Q.2 Describe the comparision of I/O mapped and memory mapped I/O interfacing.
May-04,12, Marks 8
Q.3 Distinguish peripheral mapped I/O and memory mapped I/O technique.
May-07, Dec.-09, Marks 8
Q.4 Show the common anode seven segment LED configuration. How to switch it on and
off ? May-04, Marks 2
Q.5 State the disadvantages of memory mapped I/O scheme. Dec.-05 Marks 2
Q.6 Write the difference between memory mapped I/O and peripheral mapped I/O.
Dec.-04,10; May-06, Marks 2
Section 10.3
Q.4 With necessary diagrams, explain the different data transfer schemes in 8085.
Dec.-08, Marks 10
TM
Q.5 Write the difference between memory mapped I/O and peripheral mapped I/O.
Dec.-04,10; May-06
Ans. : Refer section 10.2.4.1.
Ans. : I/O ports allow to transfer data from input devices such as keyboard to the
microprocessor and from microprocessor to the output devices such as display devices.
Contents
11.1 Features of 8255A . . . . . . . . . . . . . . . . . . Nov./Dec.-04, May/June-06,09
11.2 Pin Diagram
11.3 Block Diagram . . . . . . . . . . . . . . . . . . May/June-06,09
11.4 Operation Modes . . . . . . . . . . . . . . . . . . April/May-04,08, Nov./Dec.-04, 09
. . . . . . . . . . . . . . . . . . May/June-07
11.5 Control Word Formats . . . . . . . . . . . . . . . . . . April/May-04, Nov./Dec.-05,07,08
11.6 8255 Programming and Operation
11.7 Interfacing 8255 in I/O Mapped I/O
11.8 Parallel Communication between
Two MP Kits using Mode 2 of 8255 . . . . . . . . April/May-05
(11 - 1)
TM
The 8255 is a general purpose programmable I/O device used for parallel data
transfer. It has 24 I/O pins which can be grouped in three 8-bit parallel ports : Port A,
Port B and Port C. The eight bits of Port C can be used as individual bits or be grouped in
two 4-bit ports : C upper (CU ) and C lower (CL ).
The 8255, primarily, can be programmed in two basic modes : Bit Set/Reset (BSR)
mode and I/O mode. The BSR mode is used to set or reset the bits in Port C. The I/O
mode is further divided into three modes :
Mode 0 : Simple Input/Output
Mode 1 : Input/Output with handshake
Mode 2 : Bi-directional I/O data transfer
The function of I/O pins (input or output) and modes of operation of I/O ports can be
programmed by writing proper control word in the control word register. Each bit in the
control word has a specific meaning and the status of these bits decides the function and
operating mode of the I/O ports.
TM
b) In Mode 1, two groups each of 12 pins are formed. Group A consists of Port A
and the upper half of Port C while Group B consists of Port B and the lower half
of Port C. Ports A and B can be programmed as 8-bit Input or Output ports with
three lines of Port C in each group used for handshaking.
c) In Mode 2, only Port A can be used as a bidirectional port. The handshaking
signals are provided on five lines of Port C (PC 3 - PC7 ). Port B can be used in
Mode 0 or in Mode 1.
8. All I/O pins of 8255 has 2.5 mA DC driving capacity (i.e. sourcing current of
2.5 mA).
PA3 1 40 PA4
PA2 2 39 PA5
PA1 3 38 PA6
PA0 4 37 PA7
RD 5 36 WR
CS 6 35 RESET
GND 7 34 D0
A1 8 33 D1
A0 9 32 D2
PC7 10 31 D3
8255A
PC6 11 30 D4
PC5 12 29 D5
PC4 13 28 D6
PC0 14 27 D7
PC1 15 26 VCC(+5V)
PC2 16 25 PB7
PC3 17 24 PB6
PB0 18 23 PB5
PB1 19 22 PB4
PB2 20 21 PB3
TM
RD (Read) When this pin is low, the CPU can read the data in the ports or the status
word, through the data buffer.
WR (Write) When this input pin is low, the CPU can write data on the ports or in the
control register through the data bus buffer.
CS (Chip Select) This is an active low input which can be enabled for data transfer operation
between the CPU and the 8255.
RESET This is an active high input used to reset 8255. When RESET input is high,
the control register is cleared and all the ports are set to the input mode.
Usually RESET OUT signal from 8085 is used to reset 8255.
A0 and A1 These input signals along with RD and WR inputs control the selection of
the control/status word registers or one of the three ports. Table. 11.1
summarizes the status of A0 , A1, CS, RD and WR to access the control
word/ports. A0 and A1 are generally connected to the A0, A1 pins of the
address bus; the 8255 therefore occupies four consecutive locations in the
I/O space.
A1 A0 RD WR CS Operations
TM
Disable Function
X X X X 1 Data Bus Tri-stated
1 1 0 1 0 Illegal Condition
X X 1 1 0 Data Bus Tri-stated
Fig. 11.2 shows the internal block diagram of 8255A. It consists of data bus buffer,
control logic and Group A and Group B controls.
GROUP GROUP A PA
A PORT A
POWER +5 V CONTROL (8)
PA7-PA0
SUPPLIES GND
GROUP A PCU
PORT C
Upper
BI-DIRECTIONAL
(4) PC7-PC4
DATA BUS
D7-D0
DATA
BUS
BUFFER 8 BIT
INTERNAL GROUP B
DATA BUS PCL
PORT C
Lower
(4) PC3-PC0
RD
WR READ/ GROUP B PB
GROUP
A0 WRITE PORT B
B
CONTROL (8)
A1 CONTROL
LOGIC PB7-PB0
RESET
CS
TM
Port A : This has an 8-bit latched and buffered output and an 8-bit input latch. It can
be programmed in three modes: mode 0, mode 1 and mode 2.
Port B : This has an 8-bit data I/O latch/buffer and an 8-bit data input buffer. It can
be programmed in mode 0 and mode 1.
Port C : This has one 8-bit unlatched input buffer and an 8-bit output latch/buffer.
Port C can be separated into two parts and each can be used as control signals
for ports A and B in the handshake mode. It can be programmed for bit
set/reset operation.
TM
appears on the bus connecting it to the peripheral, only when the peripheral requests it.
The remaining lines of Port C i.e. PC0-PC2 can be used for simple I/O functions. The
Port B can be programmed in mode 0 or in mode 1. When Port B is programmed in mode
1, PC0-PC2 lines of Port C are used as handshaking signals.
A high on the RESET pin causes all 24 lines of the three 8-bit ports to be in the input
mode. All flip-flops are cleared and the interrupts are reset. This condition is maintained
even after the RESET goes low. The ports of the 8255 can then be programmed for any
other mode by writing a single control word into the control register, when required.
0 D6 D5 D4 D3 D2 D1 D0
BIT SET/RESET
1 - SET
0 - RESET
Don't care
BIT SELECT
0 1 2 3 4 5 6 7
0 1 0 1 0 1 0 1 B0
0 0 1 1 0 0 1 1 B1
0 0 0 0 1 1 1 1 B2
Set-Reset format (BSR) determine particular bit in PC 0 - PC7 being set or reset as per the
status of bit D0 . A BSR word is to be written for each bit that is to be set or reset. For
example, if bit PC 3 is to be set and bit PC 4 is to be reset, the appropriate BSR words that
will have to be loaded into the control register will be, 0 ´ ´ ´ 0 1 1 1 and 0 ´ ´ ´ 1 0 0 0,
respectively, where ´ is don’t care.
TM
The BSR word can also be used for enabling or disabling interrupt signals generated
by Port C when the 8255 is programmed for Mode 1 or 2 operation. This is done by
setting or resetting the associated bits of the interrupts. This is described in detail in next
section.
1 D6 D5 D4 D3 D2 D1 D0
GROUP B
PORT C (LOWER)
1 = INPUT
0 = OUTPUT
PORT B
1 = INPUT
0 = OUTPUT
MODE SELECTION
0 = MODE 0
1 = MODE 1
GROUP A
PORT C (UPPER)
1 = INPUT
0 = OUTPUT
PORT A
1 = INPUT
0 = OUTPUT
MODE SELECTION
00 =MODE 0
01 = MODE 1
1X = MODE 2
Source program :
MVI A, 98H ; Load control word
OUT 83H ; Send control word
1 0 0 1 1 0 0 0 = 98H
Port CL - Output
Port B - Output
Mode 0 Port B - Simple I/O
Port CU - Input
Port A - Simple input
Mode 0 Port A - Simple I/O
I/O Mode
1 0 1 0 1 1 1 0 = AEH
Port CL - Output
Port B
Mode 1 Port B - Handshake
Port CU
Port A
Mode 1 Port A - Handshake
I/O Mode
TM
Source program :
MVI A, AEH ; Load control word
OUT 23H ; Send control word
0 X X X 0 0 0 1 = 01H
Make Bit = 1
Bit 0 of Port C
Don't care
BSR Mode
0 X X X 0 0 0 0 = 00H
Make Bit = 0
Bit 0 of Port C
Don't care
BSR Mode
Flowchart :
Start
Call delay
Call delay
End
TM
Source program :
BACK : MVI A, 01H ; Load bit pattern to make PC0 high
OUT 83H ; Send it to control word register
CALL DELAY ; Call Delay subroutine
MVI A, 00H ; Load bit pattern to make PC0 Low
OUT 83H ; Send it to control word register
CALL Delay ; Call Delay subroutine
JMP BACK ; Repeat
1 0 0 0 0 0 0 1 = 81H
As mentioned earlier, this mode provides simple input and output operations for each
of the three ports. No handshaking is required, data is simply written to or read from a
specified port.
Input Mode : Fig. 11.6 shows the timing diagram for mode 0 input mode.
RD
Input
CS, A1, A0
D7-D0 Data
After initialization of 8255 in the input mode 0, CPU can read data through the input
port by initiating read command with proper port address. Read command activates RD
signal. Upon activation of RD signal CPU reads the data from the selected input port into
the CPU register.
Output Mode : Fig. 11.7 shows the timing diagram for mode 0 output mode.
After initialization of 8255 in the output mode 0, CPU can write data into the output
port by initiating write command with proper port address. CPU sends data on the data
bus and upon activation of WR signal, data on the data bus gets latched on the selected
output port.
WR
D7-D0
CS, A1, A0
Output
Mode 0 Configurations :
A B GROUP A GROUP B
TM
1. STB (Strobe Input) : This is an active low input signal for 8255 and output signal for
the input device. The input device activates this signal to indicate CPU that the data to be
read is already sent on the port lines of 8255 port. Upon activation of this signal 8255
loads the data from the input port lines into the input buffer of that port.
2. IBF (Input Buffer Full) : This is an active high output signal for 8255 and an input
signal for input device. This signal is generated by 8255 in response to STB signal as an
acknowledgment to input device. It also indicates to the input device that the input buffer
is full and it is not ready to accept next byte from the input device. Therefore input device
sends data on the port lines only when IBF signal is not active. The IBF signal is
deactivated when CPU reads the data from input buffer of the respective port by
activation of RD signal.
3. INTR (Interrupt Request) : This is an active high output signal generated by 8255. A
‘high’ on this output can be used to interrupt the CPU when an input device is requesting
TM
service. The 8255 sets the INTR when STB signal is ‘one’, IBF signal is ‘one’ and INTE is
‘one’, indicating CPU that the data from the input device is available in the input buffer.
This signal is reset by the falling edge of the RD signal i.e. immediately after reading the
data from the input buffer.
INTE (Interrupt Enable) flip flop is used to enable or disable INTR (Interrupt request)
signal. If INTE flip-flop is set, the interrupt request is generated depending on the status of
STB and IBF signals. If INTE flip flop is reset, the interrupt request is not generated,
allowing masking facility for the interrupt.
INTRA is an active high output signal which can be used to interrupt the CPU so that
the CPU can suspend its current operation and read the data written into Port A by the
peripheral. INTRA can be enabled or disabled by the INTEA flip-flop which is controlled
by Bit Set-Reset operation of PC 4 . INTRA is set (if enabled by setting the INTEA flip-flop)
after the STBA has gone high again, and if IBFA is high.
TM
STBA
IBFA
INTR
RD
DATA ON
PA0-PA7
On receipt of the interrupt, the CPU can be forced to read Port A. The falling edge of
the RD input resets IBFA and it goes low. This can be used to indicate to the peripheral
that the input buffer is empty and that data can again be loaded into it.
MODE 1(PORT B)
PB7-PB0 8
Control word
PC0 INTRB
RD
TM
otherwise not busy with other jobs, it can continuously poll (read) the status word to
check for an IBFA . This is often called Program Controlled I/O. The status word is
accessed by reading Port C (A 1A 0 must be 10, RD and CS must be low). The status word
format when Ports A and B are input ports in Mode 1, is shown in Fig. 11.10.
INPUT CONFIGURATION
D7 D6 D5 D4 D3 D2 D1 D0
GROUP A GROUP B
TM
The OBFA output (Output Buffer Full) goes low on the rising edge of the WR signal
(when the CPU writes data into the 8255). The OBFA output from 8255 can be used as a
strobe input to the peripheral to latch the contents of Port A. The peripheral responds to
MODE 1(PORT A)
PA7-PA0 8
Control word
D7 D6 D5 D4 D3 D2 D1 D0 PC7 OBFA
PC3 INTRA
WR 2
PC5-PC4 I/O
WR
OBFA
INTRA
ACKA
DATA
OUTPUT PREVIOUS DATA NEW DATA
ON
PA0-PA7
TM
the receipt of data by making the ACKA input of the 8255 low, thus acknowledging that it
has received the data sent by the CPU through Port A. The ACKA low sets the OBFA
signal, which can be polled by the CPU through OBFA of the status word to load the next
data when it is high again.
INTRA is an active high output of the 8255 which is made high (if the associated
INTEA flip-flop is set) when ACKA is made high again by the peripheral, and when OBFA
goes high again (See timing diagram in Fig. 11.11(b)). It can be used to interrupt the CPU
whenever the output buffer is empty. It is reset by the falling edge of WR when the CPU
writes data onto Port A. It can be enabled or disabled by writing a ‘1’ or a’0’ respectively
to PC 6 in the BSR mode.
MODE 1(PORT B)
PB7-PB0 8
Control word
D7 D6 D5 D4 D3 D2 D1 D0 PC1 OBFB
1 1 0 PC2 ACKB
INTE
B
PC0 INTRB
WR
TM
D7 D6 D5 D4 D3 D2 D1 D0
GROUP A GROUP B
Fig. 11.13 Mode 1 status word (Output)
D7 D6 D5 D4 D3 D2 D1 D0
PC2-PC0
1 = INPUT
0 = OUTPUT
PORT B
1 = INPUT
0 = OUTPUT
GROUP B MODE
0 = MODE 0
1 = MODE 1
TM
ACKA (Acknowledge)
This is an active low input signal (generated by the peripheral) which enables the
tri-state output buffer of Port A and makes Port A data available to the peripheral. In
Mode 2, Port A outputs are in tri-state until enabled.
INTE 1
This is the flip-flop associated with Output Buffer Full. INTE 1 can be used to enable
or disable the interrupt by setting or resetting PC 6 in the BSR Mode.
PC3 INTRA
PA7-PA0 8
PC7 OBFA
INTE
PC6 ACKA
1
Data from
CPU to 8255A
WR
tAOB
OBF
tWOB
INTR
tAK
ACK
tST
STB
tSIB
IBF
tPS
tAD tKD
Peripheral
bus
tPH tRBI
RD
Data from Data from
peripheral to 8255A 8255A to peripheral
Data from
8255A to 8080
D7 D6 D5 D4 D3 D2 D1 D0
GROUP A GROUP B
(DEFINED BY MODE 0
OR MODE 1 SELECTION)
TM
TM
D0 D0 PA0
D7 D7
A7 PA7
A0 A0
A6 A1 A1 PB0
8255
A5 IOR RD PB7
IOW WR
A4 PC0
RESET OUT RESET
A3 PC7
CS
A2
Port A 0 0 0 0 0 0 0 0 00H
Port B 0 0 0 0 0 0 0 1 01H
Port C 0 0 0 0 0 0 1 0 02H
Control Register 0 0 0 0 0 0 1 1 03H
The Fig. 11.19 shows a block diagram to implement the bidirectional communication
between the master and the slave processors. As shown in the Fig. 11.28, the data buses of
two processors are interconnected through the 8255, which servers as a peripheral device
of the master processor. Here, 8255 is used in bidirectional I/O mode (mode 2), in which
port A of the 8255 is used for bidirectional data transfer, and four signals from port C are
used for handshaking.
PA0
System data bus Data bus
PA7
8255
Chip
select OBF
CS PC7
Master logic Slave
ACK
processor PC6 processor
Reset IBF
PC5
RD STB
PC4
WR
PC3 INTR
Hardware
The Fig. 11.20 shows the detail hardware required for bidirectional communication
between two 8085 processors. It shows all the signals and the decoder circuitry for the
communication (See Fig. 11.20 on next page).
The NAND gate acts as a chip-select logic for 8255. The 8255 is selected when A2
through A7 address lines are high. Thus, the port addresses for 8255 are :
PORT A 1 1 1 1 1 1 0 0 FCH
PORT B 1 1 1 1 1 1 0 1 FDH
PORT C 1 1 1 1 1 1 1 0 FEH
Port A of 8255 is configured in mode 2. Here, out off five control signal four control
signals are used; INTR signal is not used and hence not shown. The master processor
checks the ACK and the STB signals by reading the status of respective bits in port C. The
other two control signals OBF and IBF are connected to bits D7 and D0 of the slave data
bus, respectively, through tri-state buffer so that they can be read by the slave processor.
TM
A7 A0 F A7
ALE CLK E CLK ALE
A1
A2 A3 A7 IBF R
PC5
OE
8255 A0
Y7 A
D
e A1
Master Slave
TM
B
8085 c A2 8085
ACK
11 - 26
PC6 Y5 o C
d
CS e
Fig. 11.20
r A5
G1
RD IOR A6
RD
STB Y0 G2
PC4 A7
IO/M G
IOW RD
WR
WR IOR
IO/M
IOW WR
PPI - 8255
Microprocessors and Microcontroller 11 - 27 PPI - 8255
A 3 : 8 decoder is used to generate ACK and STB signals and the output enable signal
for tri-state buffer with the help of external circuitry. Address lines A0 through A2 select
the output of the decoder and address lines A5, A6 and A7 decoder when A5 = A6 = 0 and
A7 = 1. Address lines A3 and A4 are don't care lines and are assumed at logic 0. Therefore
the output Y7 is selected when address is 87 H, the output Y5 is selected when address is
85 H and the output Y0 is selected when address is 80 H . This is illustrated in Table 11.2.
80H 1 0 0 X X 0 0 0 Y0 = 0 1 0 1 1 0
85H 1 0 0 X X 1 0 1 Y5 = 0 0 1 1 0 1
87H 1 0 0 X X 1 1 1 Y7 = 0 0 1 0 1 1
D7 D6 D5 D4 D3 D2 D1 D0
Port B
I/O operation PA : Mode 2 mode Port C : Bits 0, 1 and 2
Port B 1 = Input
I/O 0 = Output
Fig. 11.21
Here, we are not using port B and port C bits 0, 1 and 2. Hence bits D2 to D0 are
don't cares. Therefore, the required control word to configure port A in mode 2 is C0 H.
Flowchart
Start
Initialize 8255,
memory pointer and
byte counter Program
MVI A,C0H ; [Initialize 8255
HLT ; Stop
Is No
byte counter
zero ?
Yes
Stop
Fig. 11.23 Flowchart and program for master 8085 for transfer of data from
master to slave
TM
Flowchart
Start
Program
Initialize memory pointer
and byte counter
Read and store data JNZ AGAIN ; If all bytes are not
; transferred go back
Is No
byte counter
zero ?
Yes
Stop
Fig. 11.24 Flowchart and program for slave 8085 for transfer of data from master
to slave
TM
IOR
(master)
OBF
IOW
(master)
Note :8255 puts data on
TM
port A only when ACK
signal is activated
11 - 30
IOR
(slave)
ACK
Fig. 11.25 Timing diagram for the transfer data from master to slave
PPI - 8255
Microprocessors and Microcontroller 11 - 31 PPI - 8255
Flowchart
Read port C
to check IBF
Program
No Is
IBF = 1
? MVI A, C0H ; [Initialize 8255
OUT FFH ; in mode 2]
Yes
LXI H, 2000H ; Initialize memory pointer
Read byte from port A
MVI B, 0AH ; Initialize byte counter
AGAIN: IN FEH ; Read port C
ANI 20H ; Mask All bits except D5 i.e., IBF
Store byte
JZ AGAIN ; if IBF = 0, check it again
IN FCH ; Read byte from port A
INX H ; Increment memory pointer
Increment memory pointer
DCR B ; Decrement counter
JNZ AGAIN ; If all bytes are not
; transferred go back to
Decrement byte counter
; transfer the next byte
HLT ; Stop
Is No
byte counter
zero ?
Yes
Stop
Fig. 11.26 Flowchart and program for master 8085 for transfer of data from slave
to master
TM
Flowchart
Start
Initialize memory
pointer
and byte counter Program
Is
No
byte counter
zero
Yes
Stop
Fig. 11.27 Flowchart and program for slave 8085 for transfer of data from slave
to master
TM
Timing diagram
(master)
IOR
(Slave)
(Slave)
(master)
Data Bus
Data Bus
OE
IBF
STB
IOR
Fig. 11.28 Timing diagram for the transfer of data from slave to master
TM
Review Questions
Section 11.1
Q.1 What is known as PPI, what is the use of interfacing the same in 8085 ?
May-09, Marks 4
Section 11.2
Q.1 List the features of 8255.
Q.2 Explain the use of important signals of 8255.
Section 11.3
Q.1 Draw and explain the block diagram / architecture of 8255. May-06,09, Marks 12
Section 11.4
Q.1 Explain in detail about various operating modes of 8255 PPI.
Section 11.5
Q.1 Show the control word format of 8255 and explain how each bit is programmed.
May-04, Marks 4
Section 11.6
Q.1 Explain the mode 1 input mode operation of 8255 in detail. May-04, Marks 8
Q.2 With neat block diagram, explain the operating modes of 8255 PPI.
May-11, Marks 8
Q.3 Explain the operation of 8255 PPI Port A programmed as input and output in mode 1
with necessary handshaking signals. May-11, Marks 8
Section 11.7
Q.1 Explain how the 8255 A programmable peripheral interface chip can be used with the
8085 microprocessor for reading and writing parallel data from and to I/O devices.
Dec.-10, Marks 16
TM
Section 11.8
Q.1 Using model, write a program to communicate between two microprocessors using 8255.
May-05, Marks 10
Q.4 What are the signals used in input control signal and output control signal ?
Ans. : Input control signal :
1. STB (Strobe input)
2. IBF (Input buffer full)
3. INTR(Interrupt request)
Output control signal
1. OBF (Output buffer full)
2. ACK (Acknowledge input)
3. INTR(Interrupt request)
TM
Q.8 What is the purpose of control word written to control register in 8255 ?
Ans. : The control words written to control register specify an I/O function for each
I/O port. The bit D7 of the control word determines either the I/O function of the BSR
function.
TM
Q.13 Write the BSR control words to set PC0 and to reset PC4 in 8255. Dec.-07
0 X X X 0 0 0 1 = 01H
0 X X X 1 0 0 0 = 08H
Q.14 What is the bit set reset mode of 8255 PPI ? May-08, Dec.-09
Q.15 Write the format of control word for 8255 PPI. Dec.-08
Ans. : Refer section 11.5.
Q.16 Write the sequence of bit pattern for a four phase stepper motor in half
stepping mode of control. Dec.-08
Ans. : Refer sections 11.9.2 and 11.20.7.
Q.17 What are the ports available in 8255 ? What is the advantage of the third
port ? May-09
Ans. : The ports available in 8255 are : Port A, Port B and Port C. The advantages of
third port, Port C is that it can be programmed for bit set/reset operation.
Q.19 Specify the bit of a control word for the 8255, which differentiates between
I/O mode and the BSR mode.
Ans. : Bit D7 in the control word. D7 = 0 indicates bit set/reset mode and D7 = 1
indicates I/O mode.
TM
qqq
TM
Contents
12.1 8259A Programmable Interrupt Controller
12.2 Features of 8259A
12.3 Block Diagram of 8259A . . . . . . . . . . . . . . . . . Nov./Dec.-09
12.4 Interrupt Sequence with 8085
12.5 Priority Modes and Other Features
12.6 Programming the 8259A
12.7 8259A Interfacing with 8085
12.8 Cascading . . . . . . . . . . . . . . . . . . April/May-10
(12 - 1)
TM
AD0 D0 IR0
INTERRUPT
8085 8259 INPUTS
AD7 D7 IR7
INTA INT
INTR INTA
TM
6. With the help of 8259A user can get the information of pending interrupts,
in-service interrupts and masked interrupts.
7. The 8259A is designed to minimize the software and real time overhead in
handling multi-level priority interrupts.
Fig. 12.2 shows the internal block diagram of the 8259A. It includes eight blocks : data
bus buffer, read/write logic, control logic, three registers (IRR, ISR and IMR), priority
resolver, and cascade buffer.
INTA INT
CONTROL LOGIC
D7-D0 DATA
BUS
BUFFER
INTERNAL BUS
RD
READ/
WR WRITE IR0
LOGIC IR1
A0 INTERRUPT INTERRUPT IR2
SERVICE PRIORITY REQUEST IR3
CS REG RESOLVER REG IR4
(ISR) (IRR) IR5
IR6
CAS0 IR7
CASCADE
CAS1 BUFFER
COMPARATOR
INTERRUPT MASK REG (IMR)
CAS2
SP/EN
TM
Read/Write Logic
The RD and WR inputs control the data flow on the data bus when the device is
selected by asserting its chip select (CS) input low.
Control Logic
This block has an input and an output line. If the 8259A is properly enabled, the
interrupt request will cause the 8259A to assert its INT output pin high. If this pin is
connected to the INTR pin of an 8085 and if the 8085 Interrupt Enable (IE) flag is set, then
this high signal will cause the 8085 to respond INTR as explained earlier.
Priority Resolver
The priority resolver determines the priorities of the bits set in the IRR. The bit
corresponding to the highest priority interrupt input is set in the ISR during the INTA
input.
TM
CAS 0 - CAS 2
For a master 8259, the CAS0-CAS2 pins are output pins, and for slave 8259s, these are
input pins. When the 8259 is a master (that is, when it accepts interrupt requests from
other 8259s), the CALL opcode is generated by the master in response to the first INTA.
The vector address must be released by the slave 8259. The master sends an identification
code of three-bits to select one out of the eight possible slave 8259s on the CAS0-CAS2
lines. The slave 8259s accept these three signals as inputs (on their CAS0 - CAS2 pins) and
compare the code sent by the master with the codes assigned to them during initialisation.
The slave thus selected (which had originally placed an interrupt request to the master
8259) then puts the address of the interrupt service routine during the second and third
INTA pulses from the MPU.
third interrupt acknowledge pulse 8259 places a higher byte of the subroutine
address.
7. This completes the interrupt cycle. In the AEOI (Automatic End of Interrupt) mode
the ISR bit is reset at the end of the second INTA pulse. Otherwise, the ISR bit
remains set until the issue of an appropriate EOI command at the end of the
interrupt subroutine.
TM
i) Automatic Rotation
In this mode, a device, after being serviced, receives the lowest priority. The device
just been serviced, will receive the seventh priority. Here IR 3 has just been serviced.
IR0 IR1 IR2 IR3 IR4 IR5 IR6 IR7
4 5 6 7 0 1 2 3
TM
I X X X X W2 W1 W0
TM
ICW1
ICW2
Is
NO (SNGL = 1) CASCADE
MODE
?
YES (SNGL = 0)
ICW3
Is
NO (ICW4 = 0) ICW4
NEEDED
?
YES (IC4 = 1)
ICW4
READY TO ACCEPT
INTERRUPT REQUESTS
TM
A0 D7 D6 D5 D4 D3 D2 D1 D0
1 = ICW4 NEEDED
0 = NO ICW4 NEEDED
1 = SINGLE
0 = CASCADE MODE
A7-A5 OF INTERRUPT
VECTOR ADDRESS
(MCS - 80/85 MODE ONLY)
TM
1 S7 S6 S5 S4 S3 S2 S1 S0
TM
A0 D7 D6 D5 D4 D3 D2 D1 D0
1 = 8086/8088 MODE
0 = MCS - 80/85 MODE
1 = AUTO EOI
0 = NORMAL EOI
1 = SPECIAL FULLY
NESTED MODE
0 = NOT SPECIAL FULLY
NESTED MODE
1 M7 M6 M5 M4 M3 M2 M1 M0
INTERRUPT MASK
1 = MASK SET
0 = MASK RESET
A0 D7 D6 D5 D4 D3 D2 D1 D0
0 R SL EOI 0 0 L2 L1 L0
IR LEVEL TO BE
ACTED UPON
0 1 2 3 4 5 6 7
0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1
0 0 0 0 1 1 1 1
1 = POLL COMMAND
0 = NO POLL COMMAND
RESET SET
NO
SPECIAL SPECIAL
ACTION
MASK MASK
ß Example 12.1 : Write the initialization instructions for 8259A interrupt controller to meet
the following specifications :
TM
Solution :
ICW1
A7 A6 A5 1 LTM ADI SNGL IC4
1 0 0 1 0 1 1 0 = 96H
ICW2
In an 8085 system, ICW2 is used to tell the 8259A the higher byte of the interrupt
service routine address to be sent in response to an interrupt signal on the IR0 input.
B7 B6 B5 B4 B3 B2 B1 B0
0 1 1 0 0 0 1 0 = 62H
ICW3
Since we are not using a slave in our example, we don't need to send an ICW3.
OCW1
An OCW1 must be sent to an 8259A to unmask any IR inputs. For our example we
want to mask IR1 and IR3, so we put 1s in these two bits and 0s in the rest of the bits.
M7 M6 M5 M4 M3 M2 M1 M0
0 0 0 0 1 0 1 0 = 0AH
Program :
MVI A, 96H ; Edge triggered, single, interval 4, ICW4 not needed
OUT 40H ; Send ICW1
MVI A, 62H ; Higher byte of ISR.
OUT 41H ; Send ICW2
MVI A, 0AH ; OCW1 to mask IR1 and IR3
OUT 41H ; Send OCW1
ß Example 12.2 : Write the initialization instructions for master and slave configuration to
meet the following specifications :
1. The INTR of slave is routed through IR2 of the master 8259A to the 8085.
2. Master and slave are both level triggered.
3. Master Interrupt vector address for IR0 is 6280H.
4. Slave Interrupt vector address for IR0 is 7280H.
5. Modes : automatic rotation and auto end of interrupt.
6. Addresses of the master are 40H and 41H and the slave are 80H and 81H.
7. Buffers are not used.
TM
Solution :
ICW1 (master)
1 0 0 1 1 0 0 1 = 99H
ICW2 (master)
B7 B6 B5 B4 B3 B2 B1 B0
0 1 1 0 0 0 1 0 = 62H
ICW3 (master)
S7 S6 S5 S4 S3 S2 S1 S0
0 0 0 0 0 1 0 0 = 04H
ICW4 (master)
0 0 0 0 0 0 1 0 = 02H
Program :
MVI A,99H ; level triggered, cascaded, ICW4 needed
OUT 40H ; send ICW1 (master)
MVI A,62H ; Higher byte of ISR
OUT 41H ; send ICW2 (master)
MVI A,04H ; slave at IR2
OUT 42H ; send ICW3 (master)
MVI A,02H ; ICW4, 8085 mode, and set AEOI
OUT 41H ; send ICW4 (master)
MVI A,99H ; level triggered, cascaded, ICW4 needed
OUT 80H ; send ICW1 (slave)
MVI A,72H ; Higher byte of ISR
OUT 81H ; send ICW2 (slave)
MVI A,02H ; ID for slave connected to IR2
OUT 81H ; send ICW3 (slave)
MVI A,02H ; ICW4, 8085 mode
OUT 81H ; send ICW4
TM
+5 V
A7 VCC
.
.
. G2 Y0
Address
A4
bus 74LS138
A3 C
A2 B
A0
A1 A
IO/M G
GND G1 +5 V
SP/EN VCC
Control IR0
CS
bus IR1
A0
IR2
RD RD IR3
WR WR IR4
INTR INT IR5
INTA IR6
INTA
IR7
D0 D0
D1 D1 8259A
D2 D2
Data D3 D3
bus D4 D4 CAS0
D5 D5 CAS1
D6 D6 CAS2
D7 D7
GND
TM
Addressing of 8259A :
A7 A6 A5 A4 A3 A2 A1 A0
1 1 1 1 0 0 0 X
F 0/1
= F0H
= F1H
The 74LS138 address decoder will assert the CS input of the 8259A when an I/O base
address is F0H or F1H on the address bus. The A0 input of the 8259A is used to select one
of the two internal addresses in the device. A0 of the 8259A is connected to system line
A0. So the system addresses for the two internal addresses are F0H and F1H. The data
lines of an 8259A are connected to the AD0-AD7 of the system data bus, RD and WR
signals are connected to the system RD and WR lines. The interrupt request signal INT
from the 8259A is connected to the INTR input of the 8085 and INTA from the 8085 is
connected to INTA of the 8259A. As we are using single 8259A in the system, SP/EN pin
is tied high and CAS0-CAS2 lines are left open. The eight IR inputs are available for
interrupt signals.
Note : Unused IR inputs should be tied to ground so that a noise pulse cannot
accidently cause an interrupt.
TM
+5 V
A7 VCC
G2 Y0
A4 Y1
Address
bus Y2
A3 C
74LS138
A2 B
A0
A1 A
IO/M G
GND G –5 V
1
SP/EN VCC
Control IR0
CS
bus A0 A0 IR1
RD RD IR2
WR WR IR3
INT INT
IR4
INTA INTA
D0 D0 IR5
8259-1
D1 D1 Master IR6
D2 D2 IR7
Data D3 D3
bus D4 D4 CAS0
D5 D5 CAS1
D6 D6 CAS2
D7 D7
GND
+5 V
VCC
CS IR0
A0 IR1
RD IR2
WR IR3
IR4
INTA
IR5
D0 8259-2
D1 Slave IR6
D2 IR7
D3
D4 CAS0
D5 CAS1
D6 CAS2
D7
INT
GND
+5 V
A0 RD WR INTA D0 D1 D2 D3 D4 D5 D6 D7 CS
VCC
8259-3 Slave
SP/EN
CAS0 CAS1 CAS2 INT
GND
No A7 A6 A5 A4 A3 A2 A1 A0 Address
8259A-1 1 1 1 1 0 0 0 X F0H
F 0/1 F1H
8259A-2 1 1 1 1 0 0 1 X F2H
F 2/3 F3H
8259A-3 1 1 1 1 0 1 0 X F4H
F 4/5 F5H
Review Questions
Section 12.1
Q.1 List the interrupt related signals of 8085 that are connected to 8259 IC.
June-11, Marks 2
Section 12.2
Q.1 List the features of 8259.
Section 12.3
Q.1 With a neat diagram, discuss the functional organization of a programmable interrupt
controller. Dec.-09,11 Marks 16
TM
Q.2 State the use of ISR and PR registers in 8259 PIC. Dec.-11, Marks 2
Section 12.4
Q.1 Explain the processing of interrupt using 8259.
Section 12.5
Q.1 What are the different ways to end the interrupt execution in 8259 programmable
interrupt controller ? May-11, Marks 2
Section 12.6
Q.1 Explain the ICWs and OCWs with the help of example.
Section 12.7
Q.1 Draw and explain the interfacing of 8259 with 8085.
Section 12.8
Q.1 Draw and explain the interfacing of cascaded 8259s with 8085.
TM
Ans. : The mode in which 8259s are interconnected to get multiple interrupt is called
cascaded mode.
In cascade mode one 8259A is configured in Master mode and other should be
configured in the Slave mode. The 8259A connected directly to the 8085 INTR pin is
referred to as the master. The INT pins from other 8259As are connected to the IR
inputs of the master 8259A. These cascaded 8259As are referred to as slave.
Q.4 What are the different ways to end the interrupt execution in 8259
programmable interrupt controller ? May-11
qqq
TM
Contents
13.1 Serial Communication Supported by 8085
13.2 Features of 8251A (USART) . . . . . . . . . . . . . April/May-04, Nov./Dec.-05
13.3 Pin Diagram of 8251A
13.4 Block Diagram . . . . . . . . . . . . . . . . . . Nov./Dec.-07, April/May-08, 10
13.5 8251A Control Words . . . . . . . . . . . . . . . . . . Nov./Dec.-07
13.6 8251A Status Word . . . . . . . . . . . . . . . . . . Nov./Dec.-07
13.7 Data Communication Types . . . . . . . . . . . . . . April/May-04, 05, Nov./Dec.-07
13.8 Interfacing 8251A in I/O Mapped I/O
13.9 Programming 8251A
(13 - 1)
TM
Flowchart
Start
Initialize counter = 8
Call delay
Count = Count – 1
No Is
Count = 0
?
Yes
End
TM
Source program
MVI C, 08H ; Initialize count with 8
BACK : MOV A,B ;
RRC ; Rotate B register contents right
MOV B,A ; Save contents of register B
JNC SKIP ; If no carry skip
MVI A,C0H
SIM ; If carry, send high on SOD
JMP NEXT
SKIP: MVI A,40H
SIM ; If no carry, send low on SOD
NEXT: CALL DELAY ; Wait for specific time
DCR C ; Decrement count by 1
JNZ BACK ; if count = 0, if not repeat
HLT ; Stop program execution
Source program
LXI SP,27FFH ; Initialize stack pointer
LXI B,1388H ; Initialize counter with count 5000.
BACK: MVI A,C0H
SIM ; Send high on SOD pin
CALL DELAY ; Wait for 0.5 msec (Refer delay program of program 46)
MVI A, 40H ; Send low on SOD pin
CALL DELAY ; Wait for 0.5 msec
TM
Start
Count = Count – 1
No Is
Count = 0
?
Yes
End
TM
Flowchart
Start
Initialization
Is
No
start bit
received
?
Yes
Store bit in
appropriate position
No Is
Count = 0
?
Yes
Is No
Stop bit
?
Yes Error
End
TM
Source program
LXI SP, 27FFH
LXI H, 2000H ; Memory pointer
RIM ; Read SID
ANI 80H ; Check D7 bit of Accumulator
CALL Delay ; 1/2 bit time delay for stop bit
MVI B,08H ; Initialize bit counter
MVI D,00H ; Clear data register
UP1: ALL Delay ; 1bit time
RIM ; Read SID line
ANI 80H ; Mask bits B 6 - B 0
ORA D ; OR data bit with previous bits
RRC
MOV D,A ; Store data bit at appropriate position
DCR B
JNZ UP1
RLC ; Shift left to correct result
MOV M,A ; Store result
RIM ; Read stop bit
ANI 80H
CZ error ; If not stop bit call error
HLT ; Terminate program.
From above examples it is clear that 8085 has two pins for serial communication.
However, it does not support built-in serial to parallel converter and parallel-to-serial
converter. To transmit byte data it is necessary to convert byte into eight serial bits. This
can be done by using the parallel to serial converter. Similarly at the reception these serial
bits must be converted into parallel 8 bit data. The serial to parallel converter is used to
convert serial data bits into the parallel data.
In 8085, we have to achieve this with the help of programming as done in previous
examples.
The Intel has designed special devices for this purpose are called Universal
Asynchronous Receiver-Transmitter (UART). The devices which provide synchronous as
well as asynchronous transmission and reception are called Universal Synchronous
Asynchronous Receiver-Transmitter. A good example of UART is 8250 and USART is 8251.
These devices are software programmable for number of data bits, parity and number of
stop bits. In the next section we discuss IC 8251 (USART).
TM
TM
D2 D1
D3 D0
RxD VCC
GND RxC
D4 DTR
D5 RTS
D6 DSR
8251 A
D7 RESET
TxD CLK
WR TxD
CS TxEmpty
C/D CTS
RD SYN DET / BD
RxRDY Tx RDY
Data Bus : Bi-directional, tri-state, 8-bit Data Bus. This pin allow transfer of bytes
between the CPU and the 8251A.
RD (Read) : A low on this input allows the CPU to read data or status bytes from
8251A.
WR (Write) : A low on this input allows the CPU to write data or command word to the
8251A.
CLK (Clock) : The CLK input is used to generate internal device timing. The frequency
of CLK must be greater than 30 times the receiver or transmitter data bit rates.
RESET : A high on this input forces the 8251A into an “Idle” mode. The device will
remain at “Idle” until a new set of control words is written into the 8251A to program its
functional definition.
C/D (Control /Data) : This input in conjunction with the WR and RD inputs, informs the
8251A that the word on the Data Bus is either a data character, control word or status
information as shown in table.
TM
C/D RD WR Operation
0 0 1 CPU reads data from USART
0 1 0 CPU writes data to USART
1 0 1 CPU reads status from USART
1 1 0 CPU writes command to USART
X 1 1 USART Bus floating
CS (Chip Select) : A low on this input allows communication between CPU and 8251A
DSR (Data Set Ready) : This input signal is used to test modem conditions such as
Data Set Ready.
DTR (Data Terminal Ready) : This output signal is used to tell modem that Data
Terminal is ready.
CTS (Clear to Send) : A low on this input enables the 8251A to transmit serial data if
the TxE bit in the command byte is set to a “one”.
Note : The modem control signals are general purpose in nature and can be used for
functions other than modem control, if necessary. The DSR can be used as an inverted
input port and DTR can be used as an inverted output port.
Transmitter Signals
TxD : (Transmit data) : This output signal outputs a composite serial stream of data on
the falling edge of TxC.
TxRDY (Transmitter Ready) : This output signal indicates the CPU that the transmitter
is ready to accept a data character.
TxE (Transmitter Empty) : This output signal indicates that the transmitter has no
character to transmit.
TxC (Transmitter Clock) : This clock input controls the rate at which the character is to
be transmitted.
TM
Receiver Signals
RxD (Receiver data) : This input receives a composite serial stream of data on the rising
edge of RxC.
RxRDY (Receiver Ready) : This output indicates that the 8251A contains a character
that is ready to be input to the CPU.
RxC (Receiver Clock) : This clock input controls the rate at which the character is to be
received.
Fig. 13.2 shows the block diagram of IC 8251A. It includes : Data bus buffer,
Read/Write control logic, Modem control, Transmit buffer, Transmit Control, Receiver
Buffer and Receiver control.
Data Transmit
D7-D0 bus buffer TxD
buffer (P-S)
RESET
CLK Read/write
C/D control TxRDY
RD
Transmit
logic TxE
control
WR TxC
CS
DSR Receive
DTR Modem buffer RxD
CTS control (S-P)
RTS
Internal R´RDY
data bus Receive
control RxC
SYNDET
Data Bus Buffer : This tri-state, bi-directional, 8-bit buffer is used to interface 8251 to
the system data bus. Along with the data, control word, command words and status
information are also transferred through the Data Bus Buffer.
Read/Write control logic : This functional block accepts inputs from the system control
bus and generates control signals for overall device operation. It decodes control signals on
the 8085 control bus into signals which controls the internal and external I/O bus. It
contains the control word register and command word register that stores the various
control formats for the device functional definition.
Transmit Buffer : The transmit buffer accepts parallel data from the CPU, adds the
appropriate framing information, serializes it, and transmits it on the TxD pin on the
falling edge of TxC.
It has two registers : A buffer register to hold eight bits and an output register to
convert eight bits into a stream of serial bits. The CPU writes a byte in the buffer register,
which is transferred to the output register when it is empty. The output register then
transmits serial data on the TxD pin.
In the asynchronous mode the transmitter always adds START bit; depending on how
the unit is programmed, it also adds an optional even or odd parity bit, and either 1, 1 1 2,
or 2 STOP bits. In synchronous mode no extra bits (other than parity, if enable) are
generated by the transmitter.
Transmit Control
It manages all activities associated with the transmission of serial data. It accepts and
issues signals both externally and internally to accomplish this function.
TxRDY (Transmit Ready ) : This output signal indicates CPU that buffer register is
empty and the USART is ready to accept a data character. It can be used as an interrupt to
the system or, for polled operation, the CPU can check TxRDY using the status read
operation. This signal is reset when a data byte is loaded into the buffer register.
TxE (Transmitter Empty) : This is an output signal. A high on this line indicates that
the output buffer is empty. In the synchronous mode, if the CPU has failed to load a new
character in time, TxE will go high momentarily as SYNC characters are loaded into the
transmitter to fill the gap in transmission.
TxC (Transmitter Clock) : This clock controls the rate at which characters are
transmitted by USART. In the synchronous mode TxC is equivalent to the baud rate, and
is supplied by the modem. In asynchronous mode TxC is 1, 16, or 64 times the baud rate.
The clock division is programmable. It can be programmed by writing proper mode word
in the mode set register.
TM
Receiver Buffer : The receiver accepts serial data on the RxD line, converts this serial
data to parallel format, checks for bits or characters that are unique to the communication
technique and sends an “assembled” character to the CPU.
When 8251A is in the asynchronous mode and it is ready to accept a character, it
looks for a low level on the RxD line. When it receives the low level, it assumes that it is a
START bit and enables an internal counter. At a count equivalent to one-half of a bit time,
the RxD line is sampled again. If the line is still low, a valid START bit is detected and the
8251A proceeds to assemble the character. After successful reception of a START bit the
8251A receives data, parity and STOP bits, and then transfers the data on the receiver
input register. The data is then transferred into the receiver buffer register.
In the synchronous mode the receiver simply receives the specified number of data bits
and transfers them to the receiver input register and then to the receiver buffer register.
Receiver Control
It manages all receiver-related activities. Along with data reception, it does false start
bit detection, parity error detection, framing error detection, sync detection and break
detection.
RxRDY (Receiver Ready) : This is an output signal. It goes high (active), when the
USART has a character in the buffer register and is ready to transfer it to the CPU. This
line can be used either to indicate the status in the status register or to interrupt the CPU.
This signal is reset when a data byte from receiver buffer is read by the CPU.
RxC (Receiver Clock) : This clock controls the rate at which the character is to be
received by USART in the synchronous mode. RxC is equivalent to the baud rate, and is
supplied by the modem. In asynchronous mode RxC is 1, 16, or 64 times the baud rate.
The clock division is programmable. It can be programmed by writing proper mode word
in the mode set register.
Modem Control
The 8251 has a set of control inputs and outputs that can be used to simplify the
interface to almost any modem. It provides control circuitry for the generation of RTS and
DTR and the reception of CTS and DSR. In addition, a general purpose inverted output
and a general purpose input are provided. The output is labeled DTR and the input is
labeled DSR. DTR can be asserted by setting bit 2 of the command instruction; DSR can be
sensed as bit 7 of the status register. When used as a modem control signal DTR indicates
that the terminal is ready to communicate and DSR indicates that it is ready for
communication.
TM
The control words defines the complete functional definition of 8251A and they must
be loaded before any transmission or reception. The control words of 8251A are split into
two formats :
1. Mode instruction 2. Command instruction
Mode Instruction : Fig. 13.3 shows the mode instruction format.
D7 D6 D5 D4 D3 D2 D1 D0
Baud rate factor
00 SYN mode
01 ASYN´1
10 ASYN´16
11 ASYN´64
Character length
00 5 bits
ASYN (D1D0 = 00) 01 6 bits
10 7 bits
11 8 bits
TM
Command Instruction
After the mode instruction, command character should be issued to the USART. It
controls the operation of the USART within the basic frame work established by the mode
instruction. Fig. 13.4 shows command instruction format.
D7 D6 D5 D4 D3 D2 D1 D0
Internal reset
Data terminal ready
1 = Resets
1 = Enable DTR
8251 to mode
Receive enable
Request to send
1 = Enable
1 = Enable RTS
0 = Disable
It does function such as : Enable Transmit/Receive, Error Reset and Modem Control.
In the data communication systems it is often necessary to examine the “status” of the
transmitter and receiver. It is also necessary for CPU to know if any error has occurred
during communication. The 8251A allow the programmer to read above mentioned
information from the status register any time during the functional operation. Fig. 13.5
shows the format of status register. (See Fig. 13.5 on next page)
Error Definitions
Parity Error : At the time of transmission of data an even or odd parity bit is inserted in
the data stream. At the receiver end, if parity of the character does not match with the
pre-defined parity, parity error occurs.
Overrun Error : In the receiver section received character is stored in the receiver buffer.
The CPU is supposed to read this character before reception of the next character. But if
CPU fails in reading the character loaded in the receiver buffer, the next received character
replaces the previous one and the OVERRUN error occurs.
TM
D7 D6 D5 D4 D3 D2 D1 D0
SYNDET/
DSR FE OE PE T´EMPTY R´RDY T´RDY
BRKDET
Note 1
Same definitions as I/O pins
Parity Error
The PE flag is set when a parity error is
detected. It is reset by the ER bit of the
command instruction. PE does not inhibit
operation of the 8251A.
Overrun Error
The OE flag is set when the CPU does
not read a character before the next one
becomes available. It is reset by the ER
bit of the command instruction.OE does
not inhibit operation of the 8251 A. However,
the previously overrun character is lost.
TM
Operation :
When transmitter is ready to transfer data on TxD line, 8251A transfers characters
serially out on the TxD line at the falling edge of the TxC. The first character usually is the
SYNC character.
Once transmission has started, the data stream at the TxD output must continue at the
TxC rate. If CPU does not provide 8251A with a data character before transmitter buffers
become empty, the SYNC characters will be automatically inserted in the TxD data stream,
as shown in the Fig. 13.8. In this case, the TxEMPTY pin is raised high to indicate CPU
that transmitter buffers are empty. The TxEMPTY pin is internally reset when CPU writes
data character in the transmitter buffer.
TxEMPTY
TM
External SYNC
In the external SYNC mode, synchronization is achieved by applying a high level on
the SYNDET pin, thus forcing the 8251A out of the HUNT mode.
VCC
X1 X2 L
AD0 A A0
T
AD7 C A7
H OE
RESET ALE CLK
74LS373
D0
D7
VCC VCC
R 8085
VCC G
3:8
Y1
RD A MEMW
READY D Y2
WR B E
C MEMR
C O
Y5
IO/M IOW
D Y6
E
IOR
G1 R G2
74LS138
D7-D0 A0 RD WR M DSR
O DTR
RESETOUT RESET D
8251 A CTS
CLKOUT CLK E
CS Receiver Transmitter M RTS
VCC
RxC RxD TxC TxE TxD
RxRDY
TxRDY
SYNDET
VCC G
A6 A D
A5 B E
A4 C Y0
C O 3:8
A3 G1 D
A2 E
G2 R
A1
A7 G
The address line A0 is connected to the C/D input of the 8251A. The RESET and CLK
signals are driven from the RESETOUT and CLKOUT signals of the 8085, respectively.
TM
I/O MAP :
A7 A 6 A 5 A 4 A 3 A2 A1 A0
Solution : In the example, number of stop bits and baud rate is specified, therefore, it is
necessary to initialize 8251A in the asynchronous mode.
Mode word for given specification is as follows :
0 1 1 1 0 1 1 1 = 77H
0 0 1 1 0 0 1 1 = 33 H
Program :
MVI A, 00H
OUT 81H ; Dummy mode word
OUT 81H
OUT 81H
MVI A, 40H ; Reset command word
OUT 81H ; Reset 8251A
MVI A, 77H ; Mode Word initialization
OUT 81H
MVI A, 33H ; Command word initialization
OUT 81H
Note : Before initialization of the 8251A, the dummy mode word and the reset command
are sent to the control register. Initially control register may have any random word;
therefore, it is a good practice to reset the 8251A. However, it expects the instruction as a
mode word followed by the command word. Therefore, the reset command is sent after
sending three dummy mode words, which are recommended to avoid problems when it is
turned on.
Solution : CRT terminal uses normal RS 232C standard serial communication interface.
Therefore to transmit data to CRT it is necessary to have RS 232C interface at the sending
end. Fig. 13.12 shows the interfacing of 8251 with RS 232C to 8085.
As shown in the Fig. 13.12 three RS-232C signals (TxD, RXD are Ground) are used for
serial communication between the CRT terminal and the 8085 system. Line drivers and
receivers are used to transfer logic levels from TTL logic to RS-232C logic. For RS-232C the
voltage level +3 V to +15 V is defined as logic 0 and voltage level from –3 V to –15 V is
defined as logic 1. The line driver, MC 1488, converts logic 1 of TTL to approximately –9 V
and logic 0 of TTL to approximately +9 V. These levels at the receiving end are again
converted by the line receiver, MC1489, into TTL compatible logic.
I/O Map :
A7 A 6 A 5 A 4 A 3 A2 A1 A0
TM
+5 V
26
8 VCC
D7 D7 4
7 19 6 3
D6 TxD 1488 o 3
6 Transmit Receive
D5 5
5
D4
Terminal port
2 Line driver RS-232C
RS-232C
D3 cable
1 DCE
D2
28 3 8 10 2
D1 RxD 1489 o 2
27 Receive Transmit
D0 D0
5V Line receiver
o
7 7
8085 10 K 8251 A
A7
11
74LS30 CS
A1
A0 12 C/D RxC From pulse generator
IOR 13 RD or timer
TxC
IOW 10
WR
RESET(OUT) 21
RESET
CLK (OUT) 20
CLK
CTS GND
17 4
B7 B6 B5 B4 B3 B2 B1 B0
1 1 0 0 1 0 1 0 = CA H
Fig. 13.13
Command word necessary for the given specification is as follows :
B7 B6 B5 B4 B3 B2 B1 B0
X 0 X 1 X 0 X 1 = 11 H
Fig. 13.14
TM
To transmit characters on the TxD line it is necessary for 8085 check whether
transmitter is ready or not. This can be checked by reading status word as shown in the
Fig. 13.15.
B7 B6 B5 B4 B3 B2 B1 B0
X X X X X X X 1 = 01 H
Transmitter
ready
Fig. 13.15 Status word
If bit 0 of the status word is logic ‘1’ then transmitter is ready to accept the character.
Flowchart :
Start
Initialize 8251
Read status
Is
No
transmitter
ready
?
Yes
Send character to
transmitter
Decrement counter
No Is
counter = 0
?
Yes
End
Program :
LXI H, 2200H ; Initialize memory pointer to point the message
MVI C, 32H ; Initialize counter to send 50 characters
TM
MVI A, 00H
OUT FFH
OUT FFH ; Dummy mode word
OUT FFH
MVI A, 40H ; Reset command word
OUT FFH ; Reset 8251A
MVI A, CAH ; Mode word initialization
OUT FFH
MVI A, 11H ; Command word initialization
OUT FFH
CHECK : IN FFH
ANI 01H ; Check TxRDY
JZ CHECK ; Is TxRDY 1 ? if not, check again
MOV A, M ; Get the character in accumulator
OUT FEH ; Send character to the transmitter
INX H ; Increment memory pointer
DCR C ; Decrement counter
JNZ CHECK ; if not zero, send next character
HLT ; Stop program execution
1 1 0 0 1 0 1 0 = CA H
X 0 X 1 X 1 X 0 = 14 H
To receive characters from RxD line it is necessary for 8085 to check whether receiver
is ready to give data or not. This can be checked by reading status word as shown in the
Fig. 13.18.
B7 B6 B5 B4 B3 B2 B1 B0
X X X X X X 1 X = 02H
Receiver
ready
Fig. 13.18
If bit 1 of the status word is logic ‘1’ then receiver is ready to give the character.
Flowchart :
Start
Initialize 8251
Read status
Is
No
receiver
ready
?
Yes
Decrement counter
No Is
counter = 0
?
Yes
End
TM
Program :
LXI H, 2300 H ; Initialize memory pointer
MVI C, FFH ; Initialize counter to accept 25 characters
MVI A, 00H
OUT FFH
OUT FFH ; Dummy mode word
OUT FFH
MVI A, 40H ; Reset command word
OUT FFH ; Reset 8251A
MVI A, CAH ; Mode word initialization
OUT FFH
MVI A, 14H ; Command word initialization
OUT FFH
CHECK : IN FFH
ANI 02H ; Check RxRDY
JZ CHECK ; Is RxRDY 1 ? If not, check again
IN FEH ; Get the character
MOV M, A ; save the character
INX H ; Increment memory pointer
DCR C ; Increment memory pointer
OUT FEH ; Send character to the transmitter
JNZ CHECK ; if not zero, accept next character
HLT ; Stop program execution
Review Questions
Section 13.1
Q.1 Compare parallel and serial type of data transfer.
Q.2 Write a short note on serial communication supported by 8085.
Section 13.2
Q.1 Explain the advantages of using the following chip in microprocessor based systems :
USART. May-04, Marks 5
Section 13.3
Q.1 Explain the important signals and 8251.
TM
Section 13.4
Q.1 With neat block diagram, explain the architecture of 8251 USART.
Dec.-07, May-08,11, Marks 16
Section 13.5
Q.1 Draw the 'Mode Word' and command word format of 8251 USART.
Dec.-11, Marks 2
Section 13.6
Q.1 Describe the status control words in 8251. Dec.-07, Marks 8
Section 13.7
Q.1 Explain the data communication types supported by 8251.
Q.2 Discuss how 8251 is used for serial communication of data. May-05, Marks 6
Section 13.8
Q.1 Explain with a neat diagram the interfacing of 8251 to 8085 microprocessor.
June-12, Marks 8
Section 13.9
Q.1 Explain the operation and programming of 8251 USART in detail.
May-10,11, Marks 16
TM
Q.7 Write the format in which data is transmitted in asynchronous mode by 8251.
Dec.-07
Q.8 Name the peripheral ICs used for parallel and serial data transfer. May-10
qqq
TM
Notes
TM
Contents
14.1 Keyboard Interfacing . . . . . . . . . . . . . . . . . . May/June-06, Nov./Dec.-07
14.2 Display Interfacing . . . . . . . . . . . . . . . . . . Nov./Dec.-04,05, April/May-05,
. . . . . . . . . . . . . . . . . . May/June-06
14.3 Features of 8279 . . . . . . . . . . . . . . . . . . April/May-04
14.4 Pin Description
14.5 Block Diagram . . . . . . . . . . . . . . . . . . Nov./Dec.-09
14.6 Operating Modes . . . . . . . . . . . . . . . . . . April/May-05
14.7 8279 Commands
14.8 Interfacing 8279 in I/O Mapped I/O
14.9 Applications
(14 - 1)
TM
We have seen that keyboard and display devices are the two main components of
microprocessor based system. Using them user can give and receive information from the
microprocessor based system. In this chapter we will discuss keyboard and display
interfacing in detail and study the programmable keyboard/display interface, 8279.
For interfacing keyboard to the microprocessor based systems, usually push button
keys are used. These push button keys when pressed, bounces a few times, closing and
opening the contacts before providing a steady reading, as shown in the Fig. 14.1. Reading
taken during bouncing period may be faulty. Therefore, microprocessor must wait until the
key reach to a steady state; this is known as key debounce.
Logic 1 Logic 1
+5 V
Output
Logic 0
Key Key
pressed pressed
The problem of key bounce can be eliminated using key debounce technique, either
hardware or software.
Key position a b Y c d Y
A 0 0 1 1 1 0
B 1 1 0 0 0 1
Table 14.1
Fig. 14.2 shows the circuit diagram of key debounce. It consists of flip-flop. The output
of flip-flop shown in Fig. 14.2 is logic 1 when key is at position A (unpressed) and it is
logic 0 when key is at position B, as shown in Table 14.1. It is important to note that,
when key is in between A and B, output does not change, preventing bouncing of key
output. In other words we can say that output does not change during transition period,
eliminating key debouncing.
TM
+5 V
a y
To input port
b
A
B
c y
+5 V
Fig. 14.2
Start
Are
No all keys
open
?
Yes
Wait for key
debounce (10 ms)
Read status
of keys
Is
No key
pressed
?
Yes
End
TM
In the software technique, when a key press is found, the microprocessor waits for at
least 10 ms before it accepts the key as an input. This 10 ms period is sufficient to settle
key at steady state. Fig. 14.3 shows the flowchart with key debounce technique.
+5 V
R R R R R R R R
K1
K2
D0 K3
K4
8 - bit Input
K5
data port
K6
D7 K7
K8
Here eight keys are individually connected to specific pins of input port. Each port pin
gives the status of key connected to that pin. When port pin is logic 1, key is open,
otherwise key is closed.
TM
This program reads status of all keys by getting data through port IN_PORT and
compares it with FFH to check whether all keys are open. If all keys are open, instruction
CPI sets the zero flag, and the program waits for key debounce. After waiting about 10 ms,
program checks the IN_PORT for key press. If key press is found, program waits for
another 10 ms as a key debounce period. After key debounce period, program reads the
keycode from port IN_PORT.
Key Keycode
D7 D6 D5 D4 D3 D2 D1 D0
K1 1 1 1 1 1 1 1 0
K2 1 1 1 1 1 1 0 1
K3 1 1 1 1 1 0 1 1
K4 1 1 1 1 0 1 1 1
K5 1 1 1 0 1 1 1 1
K6 1 1 0 1 1 1 1 1
K7 1 0 1 1 1 1 1 1
K8 0 1 1 1 1 1 1 1
Table 14.2
TM
Row 3
Row 2
Row 1
Row 0
+5 V
Column Column Column Column
3 2 1 0
R R R R
Row 3
Row 2
Data Input
bus port A
Row 1
Row 0
Output port B
Data bus
TM
1. Make all column lines zero by sending low on all output lines. This activates all
keys in the keyboard matrix. (Note : When scan lines are logic high, the status on
the return lines do not change, it will remain logic high.)
2. Read the status of return lines. If the status of all lines is logic high, key is not
pressed; otherwise key is pressed.
Check 2 :
1. Activate keys from any one column by making any one column line zero.
2. Read the status of return lines. The zero on any return line indicates key is pressed
from the corresponding row and selected column. If the status of all lines is logic
high, key is not pressed from that column.
3. Activate the keys from the next column and repeat 2 and 3 for all columns.
Lab Experiment 70 : Hardware and software for 64-key matrix keyboard interface
Statement : Interface 64-key matrix keyboard to the 8085 microprocessor using 8255. Write
an 8085 assembly language program to initialize 8255 and to read the key code.
Hardware : Fig. 14.7 shows a matrix keyboard with 64 keys connected to the 8085
microprocessor using 8255. A matrix keyboard reduces the number of connections, thus the
number of interfacing lines. In this example, the keyboard with 64 keys, is arranged in
8 ´ 8 (8 rows and 8 columns) matrix. This requires sixteen lines from the microprocessor to
make all the connections instead of 64 lines if the keys are connected individually. The
interfacing of matrix keyboard requires two ports : one input port and other output port.
Rows are connected to the input port, port A and columns are connected to the output
port, port B.
Source program :
MVI A, 90H ; Initialize Port A as input and Port B as
OUT CR ; Output
START : MVI A, 00
OUT PB ; Make all scan lines zero
BACK : IN PA
CPI FF ; Check for key release
JNZ BACK ; If not, wait for key release
CALL DELAY ; Wait for key debounce
BACK_1 : IN PA
CPI FF ; Check for key press
JZ BACK_1 ; If not, wait for key press
CALL DELAY ; Wait for key debounce
MVI L, 00H ; Initialize key counter
MVI C, 08H
TM
D0 D0 PA0 4
D1 D1 3
D2 D2 PA1
D3 D3 PA 2
2
D4 D4 PA3 1
D5 D5
Microprocessors and Microcontroller
D6 D6 PA 40
4
Return lines
D7 D7 PA5 39
PA 38
6
IOR RD
IOW WR PA7 37
A0 A0
TM
A1 A1
18
14 - 8
Flowchart
Start
Check
No for key
release
Yes
Check
No for key
press
Yes
Call delay for key debounce
If
key is Yes Call display
pressed
Is
No last
row
?
Yes
Is
No last
row
?
Yes
Fig. 14.9 Circuit for driving single seven segment LED display
TM
The value of the resistor in series with the segment can be calculated as follows :
We know, VCC – drop across LED segment – IR = 0
Drop across LED segment is nearly 1.5 V.
\ IR = VCC – 1.5 V
= 5 – 1.5 V
= 3.5 V
Each LED segment requires a current of between 5 and 30 mA to light. Let’s assume
that current through LED segment is 15 mA.
3.5V
\ R =
15mA
= 233 W
In practice, the voltage drop across the LED and the output of 7447 are not exactly
predictable and the exact current through the LED is not critical as long as we don’t
exceed its maximum current rating. Therefore, a standard value 220 W can be used.
The static display circuits work well for driving just one or two LED digits. However,
these circuits are not suitable for driving more LED digits, say 8 digits. When there are
more number of digits, the first problem is power consumption. For worst-case
calculations, assume that all eight digits with all segments are lit. Therefore, worst case
current required is
I = 8 (digits) ´ 7 (segment) ´ 15 mA (current per segment)
= 840 mA
A second problem of the static approach is that each display digit requires a separate
BCD to 7 segment decoder.
TM
+VCC
R
a a
b b
Segment bus
A 7 c c
Output B 4 d d
port
C 4
A e e
D 7
f f
GND g g
R Q4 R Q3 R Q2 R Q1
+5 V
Output
port
B
TM
Lab Experiment 71 : Hardware and software for interfacing 8-digit 7-segment display.
Statement : Interface an 8-digit 7-segment LED display using 8255 to the 8085
microprocessor system and write an 8085 assembly language routine to display message
on the display.
Hardware : Fig. 14.11 (See Fig. 14.11 on next page) shows the multiplexed eight
7-segment display connected in the 8085 system using 8255. In this circuit port A and port
B are used as simple latched output ports. Port A provides the segment data inputs to the
display and port B provides a means of selecting a display position at a time for
multiplexing the displays. A0-A7 lines are used to decode the addresses for 8255. For this
circuit different addresses are :
PA = 00H PC = 02H
PB = 01H CR = 03H.
The register values are chosen in Fig. 14.11 such that the segment current is 80 mA.
This current is required to produce an average of 10 mA per segment as the displays are
multiplexed. In this type of display system, only one of the eight display position is ‘ON’
at any given instant. Only one digit is selected at a time by giving low signal on the
corresponding control line. Maximum anode current is (7 - segments ´ 80 mA = 560 mA )
but the average anode current is 70 mA.
Software : Before going to write the software we must know the control word to
program 8255 according to hardware connections. For 8255 Port A and B are used as
output ports.
BSR Mode A PA PCU Mode B PB PCL
1 0 0 0 X 0 0 X = 80H
Fig. 14.12 Control word format for 8255
2.2 K 2N2907
D0 14 D 4 1 14
0
Microprocessors and Microcontroller
PA0 VCC
15 D 3 2 13
D1 1 PA1
16 2 3 12
D2 D2 PA2
1 4 11
17 D PA3
D3 3 PA4 40 5 10
13 D
D4 4 PA5 39 6 9
D5 12 D 38 7 8
5 PA6 2N2222
D6 11 D 37
6 PA7
10 D
D7 7 1K
8
TM
18 1 16
5 2 PB0
IOR RD
14 - 14
5 19 2 15
36 PB1
IOW WR 5 20 3 14
A0 9 A1 PB2
21 4 13
A1 8 A2 PB3
22 5 12
Reset out 35 Reset PB4
23 6 11
6 PB5
CS 24 7 10
PB6
26 8 9
PB7
1. IC 8279 provides a scanned interface to a 64-contact key matrix, with two more
keys CONTROL and SHIFT.
2. It provides three input modes for keyboard interface.
(i) Scanned Keyboard Mode (ii) Scanned Sensor Matrix Mode
(iii) Strobed Input Mode
3. It has built-in hardware to provide key debounce.
4. It allows key depressions in 2 key lockout or N-key rollover mode, which
eliminates software required to implement 2 key lockout and N-key rollover
modes.
5. The interrupt output of 8279 can be used to tell CPU that the keypress is detected.
This eliminates the need of software polling.
6. It provides 8 byte FIFO RAM to store keycodes. This allows to store 8 key board
inputs when CPU is busy in performing his own computation.
7. It provides multiplexed display interface with blanking and inhibit options.
TM
8. It provides sixteen byte display RAM to store display codes for 16 digits, allowing
to interface 16 digits.
9. In autoincrement mode, address of display RAM and FIFO RAM is incremented
automatically which eliminates extra command after each read/write operation to
access successive locations of display RAM and FIFO RAM.
10. It provides two output modes for display interface.
(i) Left Entry (typewriter type) (ii) Right Entry (calculator type)
11. Simultaneous keyboard and display operation facility allows to interleave keyboard
and display software.
RD RL5 6 35 SL3
WR 8 RL6 7 34 SL2
2 SL0 -3 4 Scan
RL7 8 33 SL1
7
CS 9 RESET 9 32 SL0
DB1 13 28 OUT B3
CLK
data
DB2 14 27 OUT A0
DB3 15 26 OUT A1
BD DB4 16 25 OUT A2
DB5 17 24 OUT A3
VSS DB6 18 23 BD
DB7 19 22 CS
Logic symbol
Vss 20 21 A0
Pin configuration
(a) Functional diagram (b) Pin diagram
Fig. 14.13
TM
RD : Read
It is an active low signal. When RD signal is low CPU reads the contents of selected
register (display RAM, status register or FIFO RAM) from 8279; depending on the type of
command and the status of the A0 signal.
WR : Write
It is an active low signal. When WR signal is low, CPU loads the data into selected
register (control register or display register) depending on the status of A0 signal.
A0 : Address line
When A0 is high, signals are interpreted as a command or status. When A0 is low
signals are interpreted as a data.
CS : Chip select
It is an active low signal. When low, enables the communication between CPU and
8279.
RESET : A high signal on this pin resets 8279. After being reset 8279 is configured in
the following mode.
1. Sixteen 8-bit character display-left entry
2. Encoded scan keyboard- 2 key lockout
3. The program clock prescaler is set to 31.
CLK : This signal is usually driven by the system clock and used to generate internal
timings.
the autoincrement flag is set to zero, or by the End interrupt command if the
auto-increment flag is set to one. The interrupt feature of 8279 eliminates the need of
polling the keyboard.
RL0-RL7 : Return lines : These input lines are used to interface matrix keyboard. These
lines have active internal pullups which keep their status high. When the key from the
matrix keyboard is pressed corresponding return line goes low. In the strobed input mode
these lines are used as 8 input lines.
SHIFT : It is a special key input line. Its status is stored along with the key position on
the key closure in the scanned keyboard modes. It has an active internal pullup to keep it
high until a switch closure pulls it low.
CNTL/STB : Control/strobe
For scanned keyboard mode this line is used as a control input. Like SHIFT key, its
status is stored along with the key position on the key closure. It also has an active
internal pullup to keep it high until a switch closure pulls it low.
In the strobed input mode this line is used as a strobe input. When activated, loads the
status of keyboard into the FIFO RAM.
OUT A3-A0 and OUT B3-B0 : These two four bit output ports, which can be considered
as an one 8-bit port. These are used for sending data to display drivers from display RAM
and connected to the segment inputs of 7 segment display or row inputs of dot matrix
displays. These lines are synchronized to the scan lines (SL0-SL3) for multiplexed digit
display. In other words we can say that when the data on the scan lines is 0000, ports will
have data from register 0 of display RAM and when the data on the scan lines is 1111,
ports will have data from register 15 of display RAM. The two 4-bit ports can be blanked
independently.
BD : Blank Display
This is an active low output used to blank the display during digit switching or by a
display blanking command.
TM
Fig. 14.14 shows the block diagram of 8279. It consists of four main sections :
DB0-DB7
WR A0 IRQ
RD CS
Display Timing
registers and
control Scan counter Return
8
Shift
-
OUT A0 A3 OUT B0 B3 - BD SL0-SL3 RL0-RL7 CNTL/ STB
Fig. 14.14
Data Buffers
The data buffers are 8-bit bi-directional buffers that connect the internal data bus to the
external data bus.
TM
I/O Control
The I/O control section uses the A0, CS, RD and WR signals to control data flow to
and from the various internal registers and buffers. The data flow to and from the 8279 is
enabled only when CS = 0; otherwise the 8279 signals are in a high impedance state. The
8279 interprets the data given or desired by the CPU with the help of A0, RD and WR
signals, as shown in Table 14.3. When A0 is logic 0 data is transferred and when A0 is
logic 1 command word or status word is transferred. RD and WR determine the direction
of data flow through the data buffers.
A0 RD WR Interpretation
Table 14.3
Timing Control
The timing control consists of the basic timing counter chain. The first counter is
divided by N prescaler that can be programmed to give an internal frequency of 100 kHz.
The other counters divide down the basic internal frequency to provide the proper
key scan, row scan, keyboard matrix scan, and display scan times. The internal frequency
of 100 kHz gives the internal timings as shown in the Table 14.4.
Parameter Timings
Keyboard scan time 5.1 msec
Keyboard debounce time 10.3 msec
Key scan time 80 msec
Display scan time 10.3 msec
Digit ON time 480 msec
Blanking time 160 msec
Internal clock cycle 10 msec
TM
Decoded mode
In the decoded mode, the internal decoder decodes the least significant 2 bits of binary
count and provides four possible combinations on the scan lines ( SC 3 - SC 0 ) : 1110, 1101,
1011 and 0111. Thus the output of decoded scan is active low. These four active low
output lines can be used directly to interface 4 digit 7 segment display, 8 ´ 4 matrix
keyboard, eliminating the external decoder.
Return buffers
The 8 return lines ( RL7 - RL 0 ) are buffered and latched by the return buffers during
each row scan in scanned keyboard or sensor matrix mode. In strobed input mode, the
contents of the return lines are transferred to the FIFO RAM on the rising edge of the
CNTL/STB line pulse.
TM
sensor RAM is loaded with the status of the corresponding row of sensor in the sensor
matrix.
FIFO/sensor RAM status
FIFO RAM status keeps track of the number of characters in the FIFO and whether it
is full or empty. The status logic also makes IRQ signal high when the FIFO is not empty,
which can be used to interrupt CPU telling that key press is detected and keycode is
available in FIFO RAM.
Display RAM
It is 16 ´ 8 RAM, which stores the display codes for 16 digits. It can be accessed
directly by CPU. In decoded mode, 8279 uses only first four locations of display RAM. In
encoded mode, 8279 uses first eight locations for 8 digit display and all 16 locations for 16
digits display.
Display registers
Display registers are two 4-bit registers A and B. They hold the bit pattern of character
to be displayed. The contents of display registers A and B can be blanked and inhibited
individually.
· Scanned keyboard
· Scanned sensor matrix
· Strobed input
TM
Scanned Keyboard
In this mode, keyboard can be scanned in two ways : Encoded scan and Decoded
scan.
Encoded scan : In the encoded scan, scan lines (SL 2 – SL 0 ) are decoded externally to
provide 8 scan lines. We know that 8279 provides 8 returns lines. Therefore, the maximum
size of keyboard matrix is 8 ´ 8 = 64. When the key is pressed, 8279 stores the encoded
status of scan lines and return lines along with the status of SHIFT and CNTL/STB keys
into the FIFO RAM, as shown in the Fig. 14.15.
B7 B6 B5 B4 B3 B2 B1 B0
Example 14.1 : Find the key code for condition given below :
CNTL/STB SHIFT keys are open.
The pressed key is connected to scan line 2 and return line 4.
Solution :
B7 B6 B5 B4 B3 B2 B1 B0
1 1 0 1 0 1 0 0
CNTL = 1
SHIFT = 1
Scan Code = 0 1 0 (scan line 2)
Return Code = 1 0 0 (return line 4)
\ Key code = D4H
Decoded scan : In the decoded mode, the internal decoder decodes the least significant
2 bits of scan lines internally to provide four possible combinations on the scan lines
(SC 3 – SC 0 ) : 1110, 1101, 1011 and 0111. Therefore maximum size of keyboard matrix is
8 ´ 4 = 32. In this mode, keycode is generated in similar way as in the encoded mode, only
bit 5 of keycode is always 0. Therefore, 8279 can recognize only 128 (27 ) characters.
The scanned keyboard mode allows key depressions in 2-key lockout or N-key rollover
mode with key debounce.
2-key lockout : In this mode, simultaneous key depression is not allowed. When any
key is depressed, the debounce logic is set and 8279 checks for any other key depress
TM
during next two scans. Now we will see how this mode reacts with three possible
conditions that can occur during debounce scanning.
Condition 1 : If other key depress not found during next two scans, it is a single key
depression and the key code is entered into FIFO RAM along with the status of CNTL and
SHIFT lines. If the FIFO was empty, IRQ will be set to signal the CPU that there is an
entry in the FIFO RAM. If the FIFO RAM was full, the key will not be entered and the
error flag will be set.
Condition 2 : If another key depress is encountered, no entry to the FIFO can occur. If
all other keys are released before first one, then it will be entered to the FIFO. If first key
is released before any other, it will be entirely ignored.
Condition 3 : If two keys are depressed within the debounce cycle, it is a simultaneous
depression. Neither key will be recognized until one key remains depressed alone. The last
key will be treated as a single key depression.
N-Key Rollover : In N-Key rollover, each key depression is treated independently from
all others. When a key is depressed, the debounce logic is set and 8279 checks for key
depress during next two scans. Now we will see how this mode reacts with three possible
conditions that can occur during debounce scanning.
Condition 1 : If key is still pressed then key is entered into the FIFO.
Condition 2 : If other keys are pressed, they are recognized and entered into the FIFO.
Condition 3 : If a simultaneous depression occurs, the keys are recognized and entered
according to the keyboard scan found them.
Scanned Sensor Matrix : In the sensor matrix mode, image of the sensor matrix is kept
in the sensor RAM. The status of the sensor switches are input directly to the sensor RAM.
8279 scans rows one by one and stores the status of each row in the corresponding
location in the sensor RAM. For example, when 8279 scans first row of sensor matrix it
stores the status of first row in the location 0 of the sensor RAM. At the end of sensor
matrix scan if any sensor value change is detected then 8279 sets ‘S’ bit in the status
register and activates the IRQ signal. In the autoincrement mode, the IRQ line is cleared by
issuing End of Interrupt command, otherwise it is cleared by the first data read operation.
When multiple changes in the sensor matrix occurs, multiple interrupts are generated. In
sensor matrix mode, the debounce logic is inhibited. Although it is inhibited, sensor matrix
mode has the advantage that CPU knows how long the sensor was closed and when it
was released. The scanned keyboard mode can only indicate validated key closure.
In encoded mode, size of sensor matrix is 8 ´ 8 whereas in decoded mode size of sensor
matrix is 8 ´ 4. In both the modes CNTL and SHIFT lines are ignored.
TM
Fig. 14.16
Entering characters from possible zero causes the display to fill from the left. The 17th
th
(9 in case 8 character display) character is entered back in the left most position and
filling again proceeds from there, as shown in the Fig. 14.17. The characters can be
displayed on the specific digit by loading character code in the corresponding location in
the display RAM.
TM
TM
TM
1 2 3 4 5 6 7 0
st
1 Entry Display RAM
1 Address
2 3 4 5 6 7 0 1
nd
2 Entry 1 2
3 4 5 6 7 0 1 2
rd
3 Entry 3 1 2
at location
5
4 5 6 7 0 1 2 3
th
4 Entry 3 4 1 2
5 6 7 0 1 2 3 4
th
5 Entry 3 4 5 1 2 3
0 0 0 D D K K K
TM
K K K Keyboard Modes
D D Display Modes
Example 14.2 : Give the command word to set keyboard / display mode for the following
configuration.
0 0 0 D D K K K
0 0 0 1 1 0 1 0 = 1AH
Example 14.3 : The microprocessor system has a configuration given below. Find the
keyboard / display command word.
lines can be used directly to interface 8 ´ 4 matrix keyboard and 4 digit display without
external decoder. Therefore, we should select keyboard and display modes as:
Command word :
0 0 0 D D K K K
0 0 0 0 0 0 0 1 = 01H
0 0 1 P P P P P
Example 14.4 : Find the program clock command word if external clock frequency is 2 MHz.
Solution :
2 ´ 10 6
Prescaler value = = 20 = (10100) 2
100 ´ 10 3
TM
B7 B6 B5 B4 B3 B2 B1 B0
0 1 0 AI X A A A
Example 14.5 : Write a command word to read data from FIFO RAM.
Solution : We know that, in scan keyboard mode, the autoincrement flag (AI) and the
FIFO RAM address bits (AAA) are irrelevant. Therefore, command word to read data from
FIFO RAM is as given below.
AI D A A A
0 1 0 X X X X X = 40H
Note : X = Don’t care. Taking don’t cares equal to zeros we get command word to
read data from FIFO RAM is 40H.
Example 14.6 : Write a command word to read third location with autoincrement of the sensor
RAM in sensor matrix mode.
TM
Here, four least significant bits (AAAA) specify the address of the 16 byte display
RAM and bit B 4 , if 1, enables autoincrement mode. If the bit B 4 (AI) is set, display RAM
address is incremented after each read command to display RAM.
Example 14.7 : Write a command word to read fourth location with autoincrement of the
display RAM.
1 0 0 AI A A A A
Example 14.8 : Write a command word to write fifth location without autoincrement of the
display RAM.
1 0 1 X IW IW BL BL
A B A B
TM
The IW bits are used to mask nibble. A (4-bit port A) and nibble B (4-bit port B) in
applications requiring separate 4-bit display ports. By setting the IW flag (I/W = 1) for one
of the ports, the port can be masked so that entries to the display RAM from the CPU do
not affect other port.
The BL bits are used to blank the individual nibbles. This command loads the blank
code (All zeros, 20H, or All ones) determined by the last issued clear command, in the
display RAM to blank the display.
Note : After reset blank code is set to all zeros.
1 0 1 X 1 0 0 0 = A8H
CD bits (CD 0 - CD 1 ) are used to select the blanking code as given below
CD 1 CD 0 Blanking Code
TM
Example 14.10 : Write a command word to set blanking code for common anode display and to
clear the FIFO status.
Solution : Blanking code for common anode display is all ones and which can be set by
writing CD1 = 1 and CD 2
1 1 0 1 1 1 1 0 = DEH
OR
1 1 0 X 1 1 X 1 = CDH
B7 B6 B5 B4 B3 B2 B1 B0
1 1 1 E X X X X
Fig. 14.27 End interrupt / Error mode set command word format
For the N key rollover mode, if the E bit is programmed to ‘1’, the 8279 will operate
in the Special Error Mode. In the special error mode, if two keys are depressed during
single debounce, the error flag in the FIFO status word is set.
Example 14.11 : Write a command word to clear IRQ line in a sensor matrix mode.
Solution : Command word :
E
1 1 1 X X X X X = E0H
TM
B7 B6 B5 B4 B3 B2 B1 B0
DU S/E O U F N N N
Indicates number
of characters in FIFO
FIFO full
Error under run
Error overrun
Sensor closure / Error flag for multiple closures
Display unavailable
Fig. 14.28 FIFO status word
As shown in the Fig. 14.28, there are two types of possible errors : Overrun and
underrun. Overrun error occurs when the entry of another character into a full FIFO is
attempted. Underrun occurs when the CPU tries to read an empty FIFO.
During clear display or clear all command, display RAM is not available for user. This
is indicated DU bit in the FIFO status register.
In the sensor matrix mode, a S/E bit is set in the FIFO status word to indicate that at
least one sensor closure indication is contained in the sensor RAM.
In the special error mode, a S/E bit is set in the FIFO status word to indicate that a
simultaneous multiple closure error has occurred.
I/O Map :
A7 A6 A5 A4 A3 A2 A1 A0
TM
D0 D0 SHIFT
D7 D7
CNTL
A0 A0 RL0 Return lines
RL7
IOR RD 8
2 BD Blank display
IOW WR 7
S0
9 Scan lines
S3
RESET OUT RESET
A0
CLK OUT CLK A3
A7 Display
B0 lines
To RST 7.5 INT
A6 of 8085 B3
CS
A5
A4
A3
A2
A1
14.9 Applications
In this section we will discuss many useful applications with different modes of
keyboard and display interfacing. In addition to this we are going to see the software
requirement to control the interfacing circuits. All these applications are illustrated using
different examples.
Lab Experiment 72 : Hardware and software for 8 ´ 8 keyboard interface using 8279.
Statement : Interface an 8 ´ 8 matrix keyboard to 8085 through 8279 in 2-key lockout
mode and write an assembly language program to read keycode of the pressed key. The
external clock frequency is 2 MHz. Use I/O mapped I/O technique.
Solution : The 8 ´ 8 matrix keyboard can be interfaced to 8085 through 8279 in two ways.
1. Without interrupt signal
2. With interrupt signal (Interrupt driven Input)
We will see both the ways one by one.
TM
Scan lines
D0 D0 RL0
D7 D7
RL1
RL2
Return lines
A0 A0
RL3
IOR RD
RL4
IOW WR
RL5
RESET OUT RESET
RL6
CLK OUT CLK
RL7
8279 Y0
S0
S1 3:8
Decoder
S2
INT S3
Y7
A7
SHIFT
A6
CNTL
A5 CS
A4
A3
A2
A1
I/O Map :
A7 A6 A5 A4 A3 A2 A1 A0
Software :
0 0 0 D D K K K
0 0 0 X X 0 0 0 = 00H
0 0 1 1 0 1 0 0 = 34H
Step 3 : Find Read FIFO/sensor RAM command word we want to read first entry from
the FIFO RAM. Therefore command word is as given below.
AI A A A
0 1 0 0 X 0 0 0 = 40H
Flowchart :
Start
Initialize keyboard /
display mode of 8279
Initialize prescaler
count
Is
no. of
Yes characters
in FIFO = 0
?
No
End
Fig. 14.31
TM
Program :
MVI A, 00H
OUT 81H ; Initialize keyboard/display
; in encoded scan keyboard-2 keylockout mode
MVI A 34H
OUT 81H ; Initialize prescaler count
BACK : IN 81H ; Read FIFO status word
ANI 07H ; Mask bit B3 to B7
JZ BACK ; If 0, key is not pressed wait for key press
; otherwise read FIFO RAM
MVI A, 40H ; Initialize 8279 in read
OUT 81H ; FIFO RAM mode
IN 80H ; Read FIFO RAM (keycode)
HLT ; Stop program execution.
Hardware : Fig. 14.32 shows the interfacing of 8 ´ 8 matrix keyboard in interrupt driven
keyboard mode.
D0 D0 RL0
D7 D7
RL1
RL2
A0 A0
RL3
IOR RD
RL4
IOW WR
RL5
RESET OUT RESET
RL6
CLK OUT CLK
RL7
To RST 7.5 INT 8279
S0 A
A7
S1 B
A6 S2 C
3:8
A5 Decoder
A4 CS
A3
A2
A1
In the interrupt driven mode interrupt line from 8279 is connected to the one of the
interrupt input of 8085 except INTR. Here, INT line from 8279 is connected to the interrupt
RST 7.5 of 8085. Other signal connections are same as in the non interrupt mode.
Software : All the command words required to initialize 8279 are same as in the non
interrupt mode.
Flowchart :
Start Start
Enable interrupt
Initialize prescaler count
Return
Enable interrupt
Fig. 14.33
Main Program :
MVI A, 00H
OUT 81H ; Initialize keyboard/display in encoded
; scan keyboard 2 key lockout mode
MVI A, 34H
OUT 81H ; Initialize prescaler count
MVI A, 0BH ; Load mask pattern to enable RST 7.5 and mask other interrupts
SIM
EI ; Enable Interrupt
HERE : JMP HERE ; Wait for the interrupt
Interrupt Subroutine :
MVI A, 40H ; Initialize 8279 in read FIFO
OUT 81H ; RAM mode
IN 80H ; Read FIFO RAM (keycode)
EI ; Enable Interrupt
RET ; Return to main program
In the interrupt driven keyboard, when key is pressed, key code is loaded into FIFO
RAM and interrupt is generated. This interrupt signal is used to tell CPU that there is a
keycode in the FIFO RAM. CPU then initiates read command with in the interrupt service
routine to read keycode from the FIFO RAM.
TM
Lab Experiment 73 : Hardware and software to interface 8 ´ 4 matrix keyboard using 8279
Statement : Interface an 8 ´ 4 matrix keyboard to 8085 through 8279.
Solution : Fig. 14.34 shows interfacing of an 8 ´ 4 matrix keyboard to 8085 through 8279.
D0 D0 RL0
D7 D7
RL1
RL2
A0 A0
RL3
IOR RD
RL4
IOW WR 8279
RL5
RESET OUT RESET
RL6
CLK OUT CLK
RL7
To RST 7.5 INT S0
S1
S2
S3
A7
SHIFT
A6
CNTL
A5 CS
A4
A3
A2
A1
As keyboard is having 8 rows and 4 columns, only 4 scan lines are required and we
can avoid external decoder to generate scan lines by selecting decoded scan keyboard
mode.
Main Program :
MVI A, 00H
OUT 81H ; Initialize keyboard/display in encoded
; scan keyboard 2 key lockout mode
MVI A, 34H
OUT 81H ; Initialize prescaler count
MVI A, 0BH ; Load mask pattern to enable RST 7.5
; and mask other interrupts
SIM
EI ; Enable Interrupt
HERE : JMP HERE ; Wait for the interrupt
TM
Interrupt Subroutine :
MVI A, 40H ; Initialize 8279 in read FIFO
OUT 81H ; RAM mode
IN 80H ; Read FIFO RAM (keycode)
EI ; Enable Interrupt
RET ; Return to main program
Lab Experiment 74 : Hardware and software to interface eight 7-segment digits using 8279.
Statement : Interface 8/7-segment digits (common cathode) to 8085 through 8279 and
write an 8085 assembly language program to display 1 to 8 on the eight seven segment
digits. External clock frequency is 3 MHz.
Solution : Fig. 14.35 (see Fig. 14.35 on next page) shows the interfacing of eight
7-segment digits to 8085 through 8279.
As shown in the Fig. 14.35, eight display lines (B 0 -B 3 and A 0 -A 3 ) are buffered with
the help of transistor and used to drive display digits. These buffered lines are connected
in parallel to all display digits. S 0 , S1 and S 2 lines are decoded and decoded lines are
used for selection of one of the eight digits.
Number h g f e d c b a Code
1 0 0 0 0 0 1 1 0 06
2 0 1 0 1 1 0 1 1 5B
3 0 1 0 0 1 1 1 1 4F
4 0 1 1 0 0 1 1 0 66
5 0 1 1 0 1 1 0 1 6D
6 0 1 1 1 1 1 0 1 7D
7 0 0 0 0 0 1 1 1 07
8 0 1 1 1 1 1 1 1 7F
D D K K K
0 0 0 0 0 0 0 0 = 00H
TM
h
a
Y0
Y7
+VCC
B 74138
G2
G
+VCC
+VCC
VCC
G1
C
S0 A
S1
S2
B0
A3
8279
CS
RESET
CLK
WR
INT
RD
A0
D0
D7
A0
CLK OUT
RESET OUT
IOR
IOW
D0
D7
A7
A6
A5
A4
A3
A2
A1
Fig. 14.35
Step 2 : Find program clock command word. External clock frequency is 3 MHz.
3 MHz
\ Prescaler value = = 30 = (11110) 2
100 MHz
TM
P P P P P
0 0 1 1 1 1 1 0 = 3EH
Step 3 : Find write display RAM command word. We want to write first eight locations
of display RAM with corresponding 7 segment codes. So we start from first location with
autoincrement mode by command word given below.
AI A3 A2 A1 A0
1 0 0 1 0 0 0 0 = 90H
Flowchart :
Start
Initialize Keyboard /
Display mode of 8279
Decrement counter
No Is
counter = 0
?
Yes
End
Fig. 14.36
TM
Program :
LXI H, 6200H ; Initialize lookup table pointer
MVI C, 08H ; Initialize counter
MVI A, 00H ; Initialize keyboard/display
OUT 81H ; Mode
MVI A, 3EH ; Initialize prescaler count
OUT 81H
MVI A, 90H ; Initialize 8279 in write Display
OUT 81H ; RAM mode
BACK : MOV A,M ; Get the 7-segment code
OUT 80H ; Write 7-segment code in display RAM
INX H ; Increment lookup table pointer
DCR C ; Decrement counter
JNZ BACK ; if count = 0 stop otherwise go to back
HLT ; Stop program execution
Lookup Table :
Memory Address Contents
6200 66
6201 5B
6202 4F
6203 66
6204 6D
6205 7D
6206 07
6207 7F
Solution : To roll above message we have to load 7-segment codes for characters within
the message and it is necessary to configure 8279 in right entry mode.
Character h g f e d c b a Code
H 0 1 1 1 0 1 1 0 76H
E 0 1 1 1 1 0 0 1 79H
L 0 0 1 1 1 0 0 0 38H
L 0 0 1 1 1 0 0 0 38H
0 0 0 1 1 1 1 1 1 3FH
1 0 0 0 0 0 1 1 0 06H
2 0 1 0 1 1 0 1 1 5BH
3 0 1 0 0 1 1 1 1 4FH
Table 14.8 7-segment codes for given message
TM
0 0 0 1 0 0 0 0 = 10H
1 1 0 1 0 0 0 0 = D0H
Flowchart :
Start
Initialize keyboard /
Display mode of 8279
Clear display
Decrement counter
No Is
counter = 0
?
Yes
End
Fig. 14.37
TM
Program :
LXI H,6200H ; Initialize lookup table pointer
MVI C,08H ; Initialize counter
MVI A,10H ; Initialize keyboard/display in right entry mode.
OUT 81H ; Mode
MVI A,34H ; Initialize prescaler count
OUT 81H
MVI A, D0H ; Clear display
OUT 81H
MVI A, 90H ; Initialize 8279 in write display
OUT 81H ; RAM mode
BACK : MOV A,M ; Get the 7-segment code
OUT 80H ; Write 7 segment code in display RAM
INX H ; Increment lookup table pointer
DCR C ; Decrement counter
JNZ BACK ; If count-0 stop; otherwise goto BACK
HLT ; Stop program execution.
Lookup table :
6200H 76H
6201H 79H
6202H 38H
6203H 38H
6204H 3FH
6205H 06H
6206H 58H
6207H 4FH
Lab Experiment 76 : Interface 4 ´ 4 matrix keyboard and 4 digit 7-segment display using 8279
Statement : Interface 4 ´ 4 matrix keyboard and 4 digit 7-segment display and write an
assembly language program to read keycode of the pressed key and display same key on
the 7 segment display.
Solution : Fig. 14.38 shows interfacing diagram. Here, only 4 scan lines are sufficient to
scan matrix keyboard and to select display digits. Hence decoded mode is used.
TM
5 MHz
A0
X1 A1 A0
X2
L A2 B0
A A3 +VCC
Hold AD0
T
AD7
A4
C A5 CS
H
Microprocessors and Microcontroller
A6
RST 5.5 A7
RST 6.5
D0
TRAP D7
A3
INTR
8085 D
RD Y6 IOR 8 S0
VCC A E RD
TM
WR C Y5 IOW 2 S1
B WR 7
14 - 48
IO/M O
C 9 S2
D
E S3
R
R
RESET
C
RESETOUT RESET RL0 0 4 8 C
Fig. 14.38 Keyboard and display interfacing in decoded mode using 8279
Keyboard and Display Controller - 8279
Microprocessors and Microcontroller 14 - 49 Keyboard and Display Controller - 8279
Software :
D D K K K
0 0 0 0 0 0 0 1 = 01H
P P P P P
0 0 1 1 1 0 0 1 = 39H
Step 3 : Find Read FIFO RAM command word. We want to read first entry from the
FIFO RAM. Therefore command word is as given below.
A AI A A A
0 1 0 0 X 0 0 0 = 40H
Start
Initialize keyboard /
display mode of 8279
Enable interrupt
Fig. 14.39
TM
We have to display at a time only single key number. Here mode is not required.
AI A3 A2 A1 A0
1 0 0 0 0 0 0 0 = 80H
Start
Return
Fig. 14.40
Program :
MVI A, 00H ; Initialize keyboard / display in
OUT 81H ; encoded scan keyboard 2-key lockout mode.
MVI A, 39H ; Initialize prescaler count
OUT 81H
MVI A, 0BH ; load mask pattern to enable RST 7.5
SIM ; and mask other interrupts
EI ; Enable interrupt
HERE : JMP HERE ; Wait for the interrupt
TM
Review Questions
Section 14.1
Q.1 What is keyboard interfacing ? May-12, Marks 2
Section 14.2
Q.1 Explain the seven segment LED interface with microprocessor.
Dec.-04, May-06, Marks 16
Q.2 Design an interface circuit needed to connect DIP switch as an input device and
display the value of the key pressed using a 7 segment LED display. Using 8085
system, write a program to implement the same. Dec.-05, Marks 16
Section 14.3
Q.1 Explain the advantages of using the following chips in microprocessor based systems :
i) Keyboard and ii) Display controller. May-04, Marks 6
Section 14.4
Q.1 Explain important signals of 8279.
Section 14.5
Q.1 Explain the operation of the keyboard/display controller with a neat diagram.
Dec.-09,11, Marks 8
Section 14.6
Q.1 What do you mean by encoded scan and decoded scan ?
Q.2 Explain the input modes provided by 8279.
Q.3 Explain the terms 2-key lockout and N-key rollover.
Q.4 Why maximum size of keyboard matrix is 8 ´ 8 = 64, when interfaced with 8279 ?
Q.5 Explain the display modes provided by 8279.
Section 14.7
Q.1 Explain various command word formats of 8279.
TM
Section 14.8
Q.1 Draw and explain the interfacing of 8279 with 8085.
Section 14.9
Q.1 Write a program using RST 5.5 interrupt to get an input from keyboard and display it
on the display system. May-05, Marks 6
Q.2 Using peripheral mapped I/O, design a seven segment LED output port with device
address of F2h using necessary control ICs. Draw the schematic and write 8085 ALP
for displaying digit 8. May-07, Marks 8
TM
Ans. : Mechanical switches are used as keys in most of the keyboards. When a key is
pressed the contact bounce back and forth and settle down only after a small time delay
(about 10 ms). Even though a key is actuated once, it will appear to have been actuated
several times. This problem is called key bouncing.
Q.8 How much current is needed to drive an LED? Draw a typical driver circuit
for it. May-05
Ans. : Around 15 mA current is needed to drive LED. The typical driver circuit for
LED is
+ VCC
LED
R
RB
Port pin
Fig. 14.41
Q.9 How keyboard debouncing is done by software ? Dec.-07
Ans. : In the software technique, when a key press is found, the microprocessor
waits for at least 10 ms before it accepts the key as an input. This 10 ms period is
sufficient to settle key at steady state. In this way key debouncing is done by software.
Q.10 What is the difference between two key lockout and N-key rollover modes in
8279 ? Dec.-10
Ans. : 2-key lockout : In this mode, simultaneous key depression is not allowed.
N-key rollover : In N-key rollover, each key depression is treated independently
from all others.
Q.12 What is the internal operating frequency of 8279 ? How can you derive it from
any available clock signal ?
Ans. : The internal operating frequency of 8279 is 100 kHz. We can derive the internal
operating frequency from any available clock signal using internal prescaler.
This prescaler divides the external clock by a programmable integer value given in
the program clock command word, to generate internal frequency.
Q.13 Describe the three major tasks needed to get meaningful information from
matrix keyboard.
Ans. :
1. Initialize keyboard in proper mode.
2. Initialize prescaler count.
3. Read keyboard entry from FIFO RAM.
Q.14 What is the necessity for a separate 8279 keyboard display controller ?
Ans. : In software approach, to drive multiplexed displays CPU has to look after digit
selection in synchronism with the data for specific digit and these displays need
continuous refreshing. This puts a lot of burden on the CPU. In keyboard interface to
provide facilities such as 2-key lockout, N-key Rollover CPU needs to execute necessary
programs which further increases the burden on CPU and hence it is necessary to have
separate 8279 keyboard/display controller.
qqq
TM
Contents
15.1 Features
15.2 Block Diagram
15.3 Operational Description
15.4 Mode Definition . . . . . . . . . . . . . . . . . . May/June-07
15.5 Programming Examples
15.6 Interfacing of 8253/54 in I/O Mapped I/O
(15 - 1)
TM
The 8253/54 solves one of the most common problem in any microcomputer system -
the generation of accurate time delays under software control. Instead of setting up timing
loops in system software, the programmer configures the 8253/54 to match his
requirements, initializes one of the counters of the 8253/54 with the desired quantity, then
upon command the 8253/54 will count out the delay and interrupt the CPU when it has
completed its tasks. It is easy to see that the software overhead is minimum and that
multiple delays can be easily be maintained by assignment of priority levels.
The 8253/54 includes three identical 16 bit counters that can operate independently. To
operate a counter, a 16-bit count is loaded in its register and, on command, it begins to
decrement the count until it reaches 0. At the end of the count, it generates a pulse that
can be used to interrupt the CPU. The counter can count either in binary or BCD. In
addition, a count can be read by the CPU while the counter is decrementing. In this
chapter, we are going to study two timer ICs 8253 and 8254. The 8254 is a superset of
8253. The functioning of these two ICs are almost similar along with the pin configuration.
Only the differences are :
4. Reads and writes of the same Reads and writes of the same
counter cannot be interleaved. counter can be interleaved.
In this chapter, where the things are common 8253/54 is mentioned and 8254 is
mentioned specifically for giving only information about 8254.
15.1 Features
1) Three independent 16-bit down counters.
2) 8254 can handle inputs from DC to 10 MHz (5 MHz 8254-5 8 MHz 8254 10 MHz
8254-2) where as 8253 can operate upto 2.6 MHz.
3) Three counters are identical, presettable and can be programmed for either binary
or BCD count.
TM
D7 1 24 VCC
D6 2 23 WR
D5 3 22 RD
D4 4 21 CS
D3 5 20 A1
D2 6 19 A0
8254
D1 7 18 CLK2
D0 8 17 OUT2
CLK0 9 16 GATE2
OUT0 10 15 CLK1
GATE0 11 14 OUT1
GND 12 13 GATE1
Data Bus Buffer : This tri-state, bi-directional, 8-bit buffer is used to interface the
8253/54 to the system data bus. The Data bus buffer has three basic functions.
1. Programming the 8253/54 in various modes.
2. Loading the count registers.
3. Reading the count values.
TM
CLK0
D7 Data Counter
8 bus GATE0
D0 buffer 0
OUT0
RD CLK1
Read/ Counter
WR
write GATE1
A0 logic 1
A1 OUT1
CS
CLK2
Control Counter
word GATE2
register 2
OUT2
Internal bus
Read/Write Logic : The Read/Write logic has five signals : RD, WR, CS and the address
lines A0 and A1. In the peripheral I/O mode, the RD, and WR signals are connected to
IOR and IOW, respectively. In memory-mapped I/O, these are connected to MEMR and
MEMW. Address lines A0 and A1 of the CPU are usually 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 and A1.
A1 A0 Selection
0 0 Counter 0
0 1 Counter 1
1 0 Counter 2
Control Word Register : This register is accessed when lines A0 and A1 are at logic 1. It
is used to write a command word which specifies the counter to be used (binary or BCD),
its mode and either a read or write operation.
TM
Counters : These three functional blocks are identical in operation. Each counter
consists of a single, 16 bit, pre-settable, down counter. The counter can operate in either
binary or BCD and its input, gate and output are configured by the selection of modes
stored in the control word register. The counters are fully independent. The programmer
can read the contents of any of the three counters without disturbing the actual count in
process.
D7 D6 D5 D4 D3 D2 D1 D0
SC1 SC0 RW1 RW0 M2 M1 M0 BCD
1 0 Read / Write most significant byte only 1 Binary coded decimal (BCD)
Counter (4 Decades)
Read / write least significant byte first,
1 1 then most significant byte
Note : Don't care bits (´) should be 0 to ensure compatibility with future Intel products
WRITE Operation :
1. Write a control word into control register.
2. Load the low-order byte of a count in the counter register.
3. Load the high-order byte of count in the counter register.
READ Operation :
In some applications, especially in event counters, it is necessary to read the value of
the count in process. This can be done by three possible methods :
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.
3. Read-Back Command (Available only for 8254) : The third method uses the
Read-Back command. This command allows the user to check the count value,
programmed Mode, and current status of the OUT pin and Null count flag of the selected
counter(s). Fig. 15.4 shows the format of the control word register for Read-Back command.
A0, A1 = 11 CS = 0 RD = 1 WR = 0
D7 D6 D5 D4 D3 D2 D1 D0
D7 D6 D5 D4 D3 D2 D1 D0
D7 1 = OUT pin is 1.
0 = OUT pin is 0.
D6 1 = NULL count.
0 = Count available for reading.
D5-D0 = Counter programmed mode. (see Fig. 15.3)
a) Normal Operation :
1) The output will be initially low after the mode set operation. 2) After the count is
loaded into the selected count Register the output will remain low and the counter will
count. 3) When the terminal count is reached the output will go high and remain high
until the selected count is reloaded.
TM
b) Gate Disable :
1) Gate = 1 enables counting.
2) Gate = 0 disables counting.
Note : Gate has no effect on OUT.
c) New Count :
If a new count is written to the counter, it will be loaded on the next CLK pulse and
counting will continue from the new count
CW = 10 LSB = 4 (a) Normal operation
WR
CLK
GATE
OUT
0 0 0 0 0 FF FF
N N N N
4 3 2 1 0 FF FE
CW = 10 LSB = 3 (b) Gate disable
WR
CLK
GATE
OUT
0 0 0 0 0 0 FF
N N N N
3 2 2 2 1 0 FF
CLK
GATE
OUT
0 0 0 0 0 0 FF
N N N N
3 2 1 2 1 0 FF
a) Normal operation :
1) The output will be initially high.
2) The output will go low on the CLK pulse following the rising edge at the gate
input.
TM
3) The output will go high on the terminal count and remain high until the next
rising edge at the gate input.
b) Retriggering :
The one shot is retriggerable, hence the output will remain low for the full count after
any rising edge of the gate input.
c) New count :
If the counter is loaded during one shot pulse, the current one shot is not affected
unless the counter is retriggered. If retriggered, the counter is loaded with the new count
and the one-shot pulse continues until the new count expires.
WR
CLK
GATE
OUT
0 0 0 0 FF 0 0
N N N N N
3 2 1 0 FF 3 2
CW = 12 LSB = 2 (b) Retriggering
WR
CLK
GATE
OUT
0 0 0 0 0 0 0
N N N N N
3 2 1 3 2 1 0
CW = 12 LSB = 3 LSB = 4 (c) New count
WR
CLK
GATE
OUT
0 0 0 FF FF 0 0
N N N N N
2 1 0 FF FE 4 3
b) Gate disable :
1) If Gate = 1 it enables a counting otherwise it disables counting (Gate = 0 ).
2) If Gate goes low during an low output pulse, output is set immediately high. A
trigger reloads the count and the normal sequence is repeated.
c) New count :
The current counting sequence does not affect when the new count is written. If a
trigger is received after writing a new count but before the end of the current period, the
new count will be loaded with the new count on the next CLK pulse and counting will
continue from the new count. Otherwise, the new count will be loaded at the end of the
current counting cycle.
WR
CLK
GATE
OUT
0 0 0 0 0 0 0
N N N N
3 2 1 3 2 1 3
WR
CLK
GATE
OUT
0 0 0 0 0 0 0
N N N N
3 2 2 3 2 1 3
CW = 14 LSB = 4 LSB = 5 (c) New count
WR
CLK
GATE
OUT
0 0 0 0 0 0 0
N N N N
4 3 2 1 5 4 3
Note : A GATE transition should not occur one clock prior to terminal count.
a) Normal operation :
1) Initially output is high.
2) For even count, counter is decremented by 2 on the falling edge of each clock
pulse. When the counter reaches terminal count, the state of the output is changed
and the counter is reloaded with the full count and the whole process is repeated.
TM
3) If the count is odd and the output is high the first clock pulse (after the count is
loaded) decrements the count by 1. Subsequent clock pulses decrement the clock
by 2. After timeout, the output goes low and the full count is reloaded. The first
clock pulse (following the reload) decrements the count by 3 and subsequent clock
pulse decrement the count by two. Then the whole process is repeated. In this
way, if the count is odd, the output will be high for (n+1)/2 counts and low for
(n-1)/2 counts.
c) New count : The current counting sequence does not affect when the new count is
written. If a trigger is received after writing a new count but before the end of the current
half-cycle of the square wave, the counter will be loaded with the new count on the next
CLK pulse and counting will continue from the new count. Otherwise, the new count will
be loaded at end of the current half-cycle.
CW = 16 LSB = 4 (a) Even count
WR
CLK
GATE
OUT
0 0 0 0 0 0 0 0 0 0
N N N N
4 2 4 2 4 2 4 2 4 2
WR
CLK
GATE
OUT
0 0 0 0 0 0 0 0 0 0
N N N N
4 2 0 4 2 4 2 0 4 2
WR
CLK
GATE
OUT
0 0 0 0 0 0 0 0 0 0
N N N N 2
4 2 4 2 2 2 4 4 2
Note : A GATE transition should not occur one clock prior to terminal count.
a) Normal operation :
1) The output will be initially high.
2) The output will go low for one CLK pulse after the terminal count (TC).
b) Gate Disable : If Gate is one the counting is enabled otherwise it is disabled. The
Gate has no effect on the output.
c) New count : If a new count is written during counting, it will be loaded on the next
CLK pulse and counting will continue from the new count. If the count is two byte then
1) Writing the first byte has no effect on counting.
2) Writing the second byte allows the new count to be loaded on the next CLK pulse.
CLK
GATE
OUT
0 0 0 0 FF FF FF
N N N N
3 2 1 0 FF FE FD
WR
CLK
GATE
OUT
0 0 0 0 0 0 FF
N N N N
3 3 3 2 1 0 FF
CW = 18 LSB = 3 LSB = 2 (c) New count
WR
CLK
GATE
OUT
0 0 0 0 0 0 FF
N N N N
3 2 1 2 1 0 FF
a) Normal operation :
1) The output will be initially high.
2) The counting is triggered by the rising edge of the Gate.
3) The output will go low for one CLK pulse after the terminal count (TC).
b) Retriggering : If the triggering occurs on the Gate input during the counting, the
initial count is loaded on the next CLK pulse and the counting will be continued until the
terminal count is reached.
c) New count : If a new count is written during counting, the current counting sequence
will not be affected. If the trigger occurs after the new count is written but before the
terminal count, the counter will be loaded with the new count on the next CLK pulse and
counting will continue from there.
WR
CLK
GATE
OUT
0 0 0 0 FF 0
N N N N N
3 2 1 0 FF 3
CW = 1A LSB = 3 Retriggering
WR
CLK
GATE
OUT
0 0 0 0 0 0 FF
N N N N N N
3 2 3 2 1 0 FF
CW = 1A LSB = 3 LSB = 5 New count
WR
CLK
GATE
OUT
0 0 0 0 FF FF 0 0
N N N N N
3 2 1 0 FF FE 5 4
1 –– i) Initiates counting ––
5 –– Initiates counting ––
D7 D6 D5 D4 D3 D2 D1 D0
1 0 1 1 0 0 0 0 = B0H
Source Program :
MVI A, B0H
OUT 0BH ; Loads control word (B0H)in the control register.
MVI A, lowbyte (30H)
OUT 0AH ; Loads lower byte of the count.
MVI A, higher byte (C0H) ; Loads higher byte of the count.
OUT 0AH
TM
Control word :
D7 D6 D5 D4 D3 D2 D1 D0
0 1 1 1 0 1 1 1 = 77H
Source program :
MVI A, 77H
OUT 0BH ; Loads control word (77H) in the control register.
MVI A, Lowerbyte(00) ; Loads lower byte of the count
OUT 09H
MVI A, Higherbyte(10) ; Loads higher byte of the count
OUT 09H
D0 D0 CLK0
GATE0
D7 D7 OUT0
A0 A0
A1 A1 CLK1
GATE1
IOR RD OUT1
IOW WR
CLK2
8253/54 GATE2
OUT 2
A2
A3
A4
A5 CS
A6
A7
ports or control register. The remaining address lines (A7-A2 or A15-A10) can be used to
generate chip select signal. Fig. 15.12 (See Fig. 15.12 on previous page) shows the
interfacing of 8253/54 with 8085. The 74LS138 IC (3:8 decoder) is used to generate chip
select signal. Another decoder can be used to generate IOR, IOW, and MEMW signals
from the RD, WR and IO/M signals of 8085.
Address Map :
A7 A6 A5 A4 A3 A2 A1 A0
Counter 0 0 0 0 0 0 0 0 0 00H
Counter 1 0 0 0 0 0 0 0 1 01H
Counter 2 0 0 0 0 0 0 1 0 02H
Review Questions
Section 15.1
Q.1 What is the necessity of the programmable interval timer?
Q.2 List the features of any programmable interval timer.
Q.3 List the differences between 8253 and 8254.
Section 15.2
Q.1 Draw and explain the functional block diagram of IC 8253/54.
Section 15.3
Q.1 Draw the control word of 8253 timer/counter and explain. Dec.-11, Marks 8
Section 15.4
Q.1 Discuss various operating modes of 8253 timer with necessary control words.
June-07, Dec.-11, Marks 16
Section 15.6
Q.1 Draw and explain the interfacing of 8253 with 8085.
TM
Q.2 What are the different types of write operations used in 8253?
Ans. : There are two types of write operations in 8253
1) Writing a control word register
2) Writing a count value into a count register
Q.3 8253's out signal is to be used as a clock input of the desired frequency to a
particular device. Is it possible ? How ? Dec.-09
Ans. : It is possible to use the out signal of 8253 as a clock input of the desired
frequency to a particular device. Because we can program 8253 to generate continuous
square wave signal of desired frequency on the out signal of it.
qqq
TM
Notes
TM
Contents
16.1 Digital to Analog Converter
16.2 DAC 1408
16.3 Interfacing DAC 1408 / 0808 with
Microprocessor using 8255 . . . . . . . . . . . . . . . Nov./Dec.-07, April/May-10
16.4 Analog to Digital Converter . . . . . . . . . . . . . . . May/June-06
16.5 ADC 0808/0809 Family
16.6 ADC 7109
16.7 Interfacing ADC 0808 with 8085 . . . . . . . . . . . April/May-04, 05, 08, 10;
. . . . . . . . . . . . . . . . . . Nov./Dec.-04, 05, 08, 09
16.8 Interfacing ADC 7109 with 8085
16.9 Temperature Control System . . . . . . . . . . . . . April/May-04,05
16.10 Asynchronous, Synchronous and
Interrupt Modes of Interfacing ADC
16.11 Sample and Hold Circuit and Multiplexer . . . . April/May-04, 05; Nov./Dec.-05
(16 - 1)
TM
b1 b2 b3 bn = Digital inputs
Fig. 16.1 (a) DAC circuit symbol
Fig. 16.1 (b) shows analog output voltage Vo is plotted against all 16 possible digital
input words.
VoFS 15 15
14 Dashed envelope of 14
13 output voltage Vs 13
digital input
12 12
Analog output voltage Vo
11 11
Vo Value in LSBs
10 10
9 9
8 8
7 7
6 6
5 5
4 4
Output value for
3 1 LSB input 3
2 2
1 1
0 0
b4
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
b3
b2
b1
TM
Note : Input A 1 through A 8 can be either 0 or 1. Therefore, for typical circuit full scale
current can be given as,
5 æ1 1 1 1 1 1 1 1 ö
Io = + + + + + + +
2.5 K è 2 4 8 16 32 64 128 256 ÷ø
ç
2 mA ´ 255
=
256
= 1.992 mA
TM
MSB LSB
A1 A2 A3 A4 A5 A6 A7 A8
5 6 7 8 9 10 11 12
1 Io
Current switches
Range 4
control
2 GND
R/2R ladder Basic circuit
(+) Vref
13 VCC
14
Reference
15 current
(–) Vref amplifier
16
Compensation
3
VEE
It shows that the full scale output current is always 1 LSB less than the reference
current source of 2 mA. This output current is converted into voltage by I to V converter.
The output voltage for full scale input can be given as
Vo = 1.992 ´ 2.5 K
= 4.98 V
Note : The arrow on the pin 4 shows the output current direction. It is inward. This
means that IC 1408 sinks current. At ( 0000 0000) 2 binary input it sinks zero current and at
(1111 1111) 2 binary input it sinks 1.992 mA.
TM
VCC
(MSB) 13
A1 2.5 K +5 V
14
Vref
A2 R14
A3 2.5 K
8-bit A4
Rf
digit
input A5 IC 1408 –
4
A6 Io
+ +
A7 Vo
–
A8
(LSB) 15
16 R15
3 1 2
15 pF 2.5 K
VEE
–15 V
The circuit shown in the Fig. 16.4 gives output in the unipolar range. When digital
input is 00H, the output voltage is 0 V and when digital input is FFH (1111 1111) 2 , the
output voltage is + 5 V. This circuit can be modified to give bipolar output.
Fig. 16.4 shows the circuit for giving output in the bipolar range. Here, resistor R B
(5 K) is connected between Vref and the output terminal of IC 1408. This gives a constant
current source of 1 mA.
The circuit operation can be observed for three conditions :
TM
VCC
+5 V
5 13 R14
A1 +5 V
14 Vref
6 A2 2.5 K
7
A3 IB If Rf
5K
8-bit 8 A4 RB
1 mA 5K
digital
9 IC 1408
input A5 –
4
10 Io A
A6
+ +
11 A Vo
7
–
12 A
8
15
COMP 16 R15
3 1 2
15 pF 2.5 K
VEE
– IB + Io + If = 0
– IB + Io + If = 0
TM
Therefore, the output voltage is + 5 V. In this way, circuit shown in the Fig. 16.4 gives
output in the bipolar range.
Fig. 16.5 shows the interfacing of DAC 0808 with microprocessor 8085. Here,
programmable peripheral interface, 8255 is used as parallel port to send the digital data to
DAC.
VCC
2.5 K
13 14 Vref
AD0 AD0 PA0
A8
AD7 AD7 2.5 K
A1 2.5 K
PA7
IOR RD
8 0
IOW WR
2 8 4
–
5 0 Io
From Reset Reset 8
5
8085 out 2.5 K + +
15 Vo
A0 A0 –
A1 A1
CS
16 3 1 2
15 pF
VEE
A2
A3
A4
A5
A6
A7
Port/Register Address
Port A 0 0
Port B 0 1
Port C 0 2
Control register 0 3
TM
Program :
MVI A, 80H ; Initialization control word for 8255 to
OUT 03 ; Configure all ports as output ports
MVI A, data ; Load 8-bit data to be sent at the input
; of 0808 DAC
OUT 00 ; Send data on port A.
We now see how different waveforms can be generated using this circuit.
Solution : To generate square wave first we have to output FF and then 00 an Port A of
8255. The output of 8255 (Port A) connected to DAC 0808. According to frequency
requirement delay is provided in between the two outputs.
Program
LXI SP, 27FFH ; Initialize Stack Pointer
MVI A, 80H ; Initialize 8255 to configure all
OUT 03 ; ports as output ports
LOOP : MVI A, 00 ; Load A with digital data
; corresponds
; to – 2.5 V output
OUT 00 ; Send digital data to the input of
; DAC 0808
CALL DELAY ; Wait for specified time.
MVI A, FF ; Load A with digital data
; corresponds
; to + 2.5 V output
OUT 00 ; Send digital data to the input of
; DAC 0808.
CALL DELAY ; Wait for specified time
JMP LOOP ; Repeat
TM
Delay Program
DELAY : MVI B,08 ; Load delay count in register
BACK : DCR B ; Decrement count
JNZ BACK ; Check if count = 0
; otherwise repeat
RET
Solution : To generate triangular wave we have to output data from 00 initially, and it
should be incremented upto FF. When it reaches FF it should be decremented upto 00.
LXI SP, 27FFH ; Initialize Stack Pointer
MVI A, 80H ; Initialize 8255 to configure all
; ports as output ports
OUT 03
MVI A, 00 ; Load accumulator with digital data
; corresponds to – 2.5 V output
OUT 00 ; Send digital to the input
; of DAC 0808.
LOOP_1: INR A ; Increment digital data in the
; accumulator
OUT 00 ; Send digital data to the input
; of DAC 0808.
CPI FF ; Check digital data for Peak output
JNZ LOOP_1 ; If no repeat,
LOOP_2: DCR A ; Decrement digital data in the
; accumulator
OUT 00 ; Send digital data to the input of
; DAC 0808.
JNZ LOOP_2
JMP LOOP_1
Solution : To generate sine wave we have to output digital equivalent values which
will represent sine wave as shown in the Fig. 16.6. Digital data 00H represents – 2.5 V.
7FH represents 0 V and FFH represents + 2.5 V.
TM
+2.5 V
0V 180º 360º
90º t
FFH
80H
96H
ABH
COH FEH
D2H
–2.5 V
E2H
EFH
F8H
Fig. 16.6
We know that sin 0º = 0 and sin 90º = 1. The range sin 0º to sin 90º is distributed over
digital range of 7FH to FFH i.e. (FFH – 7FH) 128 decimal steps. Therefore, taking 128 as a
offset we can write,
Digital equivalent value (DEV) for sin q = (128 + 128 ´ sin q)
where q is a angle in degrees and digital value is in decimal.
Lookup table shows the digital equivalent values for sine wave.
TM
Program :
LXI SP, 27FFH ; Initialize Stack Pointer
MVI A, 80H ; Initialize 8255 to configure
; all
OUT 03H ; Ports as output ports
START: MVI C, 25H ; Initialize counter
LXI H, Lookup table ; Initialize HL pointer to point
; to lookup table
BACK: MOV A, M ; Get the digital equivalent
; data
; from lookup table
OUT 00 ; Send digital data to DAC
INX H ; Increment lookup table pointer
DCR C ; Decrement counter
JNZ BACK ; If not zero, go to BACK
JMP START ; Repeat
TM
Ideal transition
7/8 111 –
6/8 110
Nominal quantized
5/8 101
1 LSB
Digital output
4/8 100
3/8 011
2/8 010
Ideally quantized
analog input
1/8 001 1/2 LSB
TM
Features
3 1 28 2
Analog
4 2 27 1 inputs
Analog
5 3 26 0
inputs
6 4 25 A
7 5 24 B Address
SOC 6 23 C
EOC 7 ADC 22 ALE
0808/
DB3 8 0809 21 DB7
9 20 DB6
OUTPUT CONTROL
10 19 DB5
CLK
11 18 DB4
VCC
REF + 12 17 DB0
GND 13 16 REF
DB1 14 15 DB2
A
B Address
C 50 ns
ALE
25 ms
SOC
EOC
DB0
Valid data
DB7
OE
Features
1. IC 7109 is a 12-bit dual slope A/D converter.
2. It has polarity and over range bits.
3. It has byte organized TTL compatible three-state outputs and UART handshake
mode for simple parallel or serial interfacing to microprocessor systems.
4. It has RUN/HOLD input and STATUS output, which can be used to monitor and
control conversion timing.
5. It has true differential input and differential reference
6. It has low noise-typically 15 mVp - p .
7. It has very low input current - 1 pA.
8. It operates at 30 conversions per second.
9. All inputs are fully protected against static discharge.
TM
+
GND 1 GND V 40 +5 V
2 STATUS REF IN – 39 –
TM
TM
PB3 21 ALE
A2 VCC 11 12 V
22
16 - 16
PB4 22
SOC
14 7 1K
PC0 EOC +REF 12
10 16
PC7 –REF
GND 2K
GND 13 7 3
+
7
– + LM308
6 2K
1 mF – 6.8 V
2
Fig. 16.12
A/D and D/A Converter Interfacing
Microprocessors and Microcontroller 16 - 17 A/D and D/A Converter Interfacing
ADC
ADB Address
ADA
50 ns
2.5 ms
Start of
conversion
(SOC)
EOC
Valid data
Fig. 16.13
I/O Map
Ports/CR A7 A6 A5 A4 A3 A2 A1 A0
Port A 0 0 0 0 0 0 0 0 00H
Port B 0 0 0 0 0 0 0 1 01H
Port C 0 0 0 0 0 0 1 0 02H
Control Word
I/O Mode A PA PCU Mode B PB PCL
1 0 0 1 1 0 0 1 = 99H
Address bus
A14 A15
From
Control bus
8085
IOR
Data bus
HBEN LBEN
B9-B12
6
POL,OR
Analog
input 7109 B1-B8
8
CE / LOAD
MODE RUN/HOLD
GND +5 V
TM
D0 D0 PA0
B1
D7 D7 PA7 B8
PB0
A0 A0 B9
8 B12 7109
A1 A1 PB5
2 OR Analog input
5 PB6 POL
IOR RD 5 STATUS
PC7
IOW WR RUN/HOLD
Reset Reset CE / LOAD
out MODE
CS HBEN LBEN
A2
A3
A4
A5
A6
A7
I/O Map :
Port/Register A7 A6 A5 A4 A3 A2 A1 A0 Address
Port A 0 0 0 0 0 0 0 0 00
Port B 0 0 0 0 0 0 0 1 01
Port C 0 0 0 0 0 0 1 0 02
Control Register 0 0 0 0 0 0 1 1 03
1 0 0 1 0 0 1 0 = 92H
TM
Flowchart
Start
Initialize 8255
Make RUN/HOLD
signal high
No Check if
STATUS = 0
Yes
Make RUN/HOLD
signal low
Stop
TM
Before going to study the temperature control system, let us see the block diagram of
microprocessor based process control system. It is capable of controlling more than one
physical parameter. Such systems are commonly known as data acquisition and control
systems. In these system, analog signals from various sensors are converted into digital
values. These digital values are read in and processed by the microcomputer. The
keyboard and display in the system allow the user to enter set point values, to read the
current values of process variables, and to issue commands. Relays, D/A converters,
solenoid valves, and other actuators are used to control process variables under program
direction.
As an example, let us study the microcomputer based temperature control system to
control the temperature between 0-100º C with ON/OFF control. The Fig. 16.16 shows the
temperature sensing and heater control circuitry using ON/OFF control. It includes
1. Sensing circuitry
2. Analog to digital converter
3. Circuit required to drive the controller.
TM
Display
Pressure sensor Relays
Flow sensor
Temperature sensor
Data
Input Micro Output D/A
acquisition
ports computer ports converters
system
Load cells
Keyboard Solenoid
PH meters valves
1. Sensing circuitry :
The sensing circuitry consists of instrumentation amplifier using transducer bridge.
RTD (Resistance Temperature Dependent) is used as transducer whose resistance is
changed as a function of temperature.
Rt = R 0 (1 + a D t)
Where Rt is the resistance of RTD at temperature t.
R 0 is the resistance of RTD at 0º C
a is temperature coefficient of RTD (0.0039)
D t is difference in temperature (t – t 0 )
At 0º C resistance of RTD is 100 W and at 100º C resistance of RTD is
– 5 æç
1000 1000 ö
= 5 ´ ÷
1100 è 1139 ø
= 0.1556 V
æR ö
Now the instrumentation amplifier gain çç f ÷÷ must be chosen to get 5.12 V at 100º C
è R1 ø
Rf 5.12 V
= = 32.89
R1 0.1556
TM
50 ns
ALE
2.5 ms
min
SOC
Stable
Address
Address
EOC
tEOC
Output
DATA
Tri state
TM
A0 A0 DATA3
A1 A1 40 18
PA4 DATA4 A INPUT4 2
IOR RD
8 19
2 PA5 39 DATA5 D INPUT5 3
C 1K
IOW WR 5 PA 38 20
6 DATA6 8 INPUT6 4
Reset Out Reset 5 21
PA7 37 DATA7 0 INPUT7 5
25 8 10
PB 18
0 ADA CLOCK
19 24 OUTPUT 9
PB1 ADB ENABLE C 0.001 mF
TM
A7
PB2 20 23 VCC
ADC
16 - 24
cs
PB 21 6
3 SOC
A2 VCC 11 12 V
PB4 22 22
ALE
14 7 1K
PC0 EOC +REF 12
10 16
PC7 –REF
GND 2K
GND 13 7 3
2K +
7
– +
NO 10 K
10 mF
230 AC solid
50 Hz tantalum
BC547
Heating
element
Fig. 16.18 Temperature sensing and heater control circuitry using ON/OFF control
A/D and D/A Converter Interfacing
Microprocessors and Microcontroller 16 - 25 A/D and D/A Converter Interfacing
Software :
It includes
1. Initialization of 8255
2. A/D conversion routine
3. Software required to take control action.
Flowchart :
Start Start
Call conversion
Wait for EOC
Is
Yes Is
temp < Setpt No
? EOC HIGH
Make Heater ON ?
No Yes
Make Heater OFF Read digital data
RET
Control Word :
1 0 0 1 0 0 0 1 = 91H
TM
I/O map :
A 7 A 6 A5 A 4 A 3 A 2 A1 A 0 Address Port
0 0 0 0 0 0 0 0 0 0 H Port A
0 0 0 0 0 0 0 1 0 1 H Port B
0 0 0 0 0 0 1 0 0 2 H Port C
0 0 0 0 0 0 1 1 0 3 H Port CR
Program :
MVI A, 91H ;
OUT CR ; Initialize 8255
BEGIN : CALL CONVERSION ; Call conversion subroutine
CPI 80H ; compare with SETPT (80H)
JC NEXT
MVI A,0EH ; Reset bit PC7 to switch off heater with
OUT CR ; Bit Set/Reset Mode
JMP BEGIN
NEXT : MVI A,0FH ; Set bit PC7 to switch ON heater with
OUT CR ; Bit Set/Reset Mode
JMP BEGIN
Subroutine Conversion
MVI A,00H
OUT PB ; Send address to select input 0
MVI A,08H
OUT PB ; Latch the given address by sending ALE high
MVI C,0AH
BACK : DCR C
JNZ BACK ; Give delay greater than 2.5 ms
MVI A,18H
OUT PB ; Make SOC high
MVI A,08H ; Make SOC low
OUT PB
MVI A,00H
OUT PB ; Make ALE low
TM
AGAIN : IN PC
ANI 01
JZ AGAIN ; Wait for EOC
IN PA
RET
Data
bus
Analog Start
ADC input
I/O
Microprocessor Activate SOC
ports
Start of conversion
(SOC)
CS End of conversion No EOC
(EOC) Activated
?
Address bus
Yes
Address
decoder
Read data
Stop
(a) ADC interface in asynchronous mode (b) Flowchart for asynchronous mode interface
Fig. 16.19
TM
Synchronous Mode
In asynchronous mode, the microprocessor waste time by waiting for the end of
conversion signal. The conversion time of ADCs is in the range of hundreds of
microseconds. Thus for every conversion cycle, this much time of the microprocessor is
wasted. In synchronous mode, microprocessor executes certain instructions so that
execution time of instructions is greater or equal to the conversion time, during conversion
process. After execution of these instructions, microprocessor reads data from ADC
without checking end of conversion signal. Here, microprocessor utilizes conversion time
in executing a part of the program, thus increasing throughput.
Data
bus
Start
ADC
I/O
Microprocessor Activate SOC
ports
Start of conversion
(SOC)
Execute instructions
CS such that
Execution time >
Address bus conversion time
Address
decoder
Read data
Stop
(a) ADC interface in synchronous mode (b) Flowchart for synchronous mode interface
Fig. 16.20
Interrupt Mode
Many times, it is inconvenient to write instructions whose execution time is nearly
equal to the conversion time. To overcome this difficulty we can use interrupt mode of
interfacing ADC. Here, end of conversion signal from ADC is connected as an interrupt
input of microprocessor. In this case, microprocessor continues instruction execution after
initiating the conversion process. Whenever conversion process is completed, ADC
activates EOC signals and microprocessor is interrupted. Microprocessor suspends its
program execution, saves the program status and then executes the interrupt service
routine (ISR). The interrupt service routine reads the data from ADC and returns program
control to the main program.
TM
Data
INTR bus
Analog
ADC input
I/O
Microprocessor
ports
Start of conversion
(SOC) Main program ISR
Start Start
CS
Address bus
Activate SOC Read data
Address
decoder
END Return
(a) ADC interface in interrupt mode (b) Flowchart for interrupt mode interface
Fig. 16.21
A sample and hold circuit is used to interface real-world signals. The purpose of this
circuit is to hold the analog value steady for a short time while the converter or other
following system performs some operation that takes a little time.
8
1 7.5(111)
7
1 0 6.5(110)
6
1 5.5(101)
0
1 5
0
4.5(100)
4
1 3.5(011)
0
3
1 0 2.5(010)
2
1 1.5(001)
0
1
Analog
input voltage 0 0.5(000)
0
Time
Fig. 16.22 Effect of changing input voltage
TM
For accurate analog to digital conversion the analog input voltage should be held
constant during the conversion cycle. If the analog input voltage changes by more than
± 1/2 LSB an error can occur in the digital output code. To illustrate the effect of a
changing analog input voltage on the conversion processor, let us consider a situation of a
successive approximation ADC with an analog input voltage that is initially zero, but there
happen to be a large change in voltage amplitude occurring during the conversion process.
Fig. 16.22 shows the changing input voltage and its effect on the successive approximation
conversion process.
As shown in Fig. 16.22 analog input voltage at start of conversion process is zero volts
and at the end of conversion process it is near to 1.5 volts, and the conversion process
result is 010 2 , i.e. 2.5 V. This result does not corresponds to the analog voltage at the start
of conversion or at the end of conversion. To minimise the occurrence of these errors it is
necessary to hold the value of the analog input voltage constant during the conversion
process. The sample and hold circuit does this task.
As its name implies, the sample and hold (S/H) circuit samples the value of the input
signal in response to a sampling command and hold it at the output until arrival of the
next command. It samples an analog input voltage in a very short period, generally in the
range of 1 to 10 ms, and holds the sampled voltage level for an extended period, which can
range from a few millisecond to several seconds. Fig. 16.23(b) shows input and output
response of the sample and hold circuit.
Analog
switch
Analog Analog
A1 A2
input output
Control Capacitor
signal
V
Analog output
Sampling
time
Analog input
o
Time
Fig. 16.23
TM
The sample and hold circuit uses to basic components analog switch and capacitor.
The Fig. 16.23(a) shows the basic sample and hold circuit. The circuit tracks the analog
signal until the sample command causes the digital switch to isolate the capacitor from the
signal, and the capacitor holds this analog voltage during A/D conversion.
The Fig. 16.24(a) illustrates a practical data acquisition system using an analog
multiplexer, a sample and hold circuit and an A/D converter. In such system,
microprocessor selects one of the input channels through multiplexer address lines. When
the Sample / hold signal is low, the sample and hold circuit tracks the analog input;
otherwise the sample and hold circuit holds the analog input. The stable analog signal is
then converted into its digital equivalent using A/D converter. Microprocessor reads this
output using input port.
I0
I1
Sample and A/D Data
Analog I2 Analog hold circuit converter out
inputs
multiplexer
IN
Sample/hold Start Data
of conversion ready
(SOC)
MUX address
(a) Practical data acquisition system
MUX address T1
Sample/hold
T2
A/D SOC
T3
A/D data ready
Fig. 16.24
TM
I0
Reset Reset out
I1 Sample A/D
and PA0 D0 D0
converter
Analog hold (8-bit) PA7 D7 D7
8
multiplexer circuit
Data 8255
I7 ready A0 A0
Sample/ PB0
S2 S1 S0 hold SOC A1 A1
PC4
From
PC5
8085
PC0 RD IOR
PC1
WR IOW
PC2
MUX address CS
A2
A3
A7
Port Addresses : Port A (00H), Port B (01H), Port C (02H), Control word (03H)
Fig. 16.25
According to ports used we require to configure 8255 as follows :
· Port A : input (mode 0)
· Port B : input (mode 0)
· Port C : output
Control Word
1 0 0 1 0 0 1 0 = 92 H
TM
OUT 03H
BACK : IN 01H ; Read port B
ANI 01H ; Check PB0
JZ Back ; if zero wait for Data Ready
IN 00H ; Read data through port A
HLT ; Stop
Sample 0
I0 and
hold circuit
Sample 1
I1 and A/D Data
Analog converter out
hold circuit multiplexer
SOC Data
Sample Ready
N
IN and
hold circuit
Sample/hold
Fig. 16.26 SH and MUX connections for sampling all channels at the same time
Review Questions
Section 16.1
Q.1 What is digital to analog converter ?
Section 16.3
Q.1 Explain the interfacing of D/A converter with microprocessor.
TM
Q.2 Interface an 8-bit DAC with 8085 microprocessor using 8255 and write assembly
language programs to generate square wave of 1 kHz and triangular wave of 100 Hz.
The crystal frequency connected to 8085 is 6 MHz. Dec.-07, Marks 16
Q.3 With neat sketches, Explain the interfacing of D/A converter with 8085
Microprocessor. May-10,11, Dec.-11, Marks 8
Section 16.4
Q.1 What is analog to digital converter ?
Q.2 Explain how to convert an analog signal into digital signal.
June-06, Marks 16
Q.3 Why do we need A/D converter and D/A converter ? Dec.-11, Marks 2
Section 16.7
Q.1 Explain the interfacing of A/D converter with microprocessor.
Q.2 Interface a ADC chip with 8085 processor through 8255 ports and write and ALP to
use BSR mode to START conversion and STATUS CHECK mode to read output
data. Explain the complete circuit and programs. Use I/O mapped I/O configuration.
May-04, Marks 16
Q.4 Using peripheral mapped I/O, design an interface circuit to connect an ADC 0808 to a
microprocessor. May-05, Marks 6
Q.5 Explain how 8085 can be connected to an A/D convertor. Describe the signals involved
in the process of conversion. Dec.-05, Marks 12
Q.6 What are the steps involved in interfacing an ADC with 8085. Dec.-09, Marks 4
Section 16.9
Q.1 Describe any typical automatic process control system using 8085. Use necessary block
diagrams, flow charts, algorithms and program to explain the whole system operation.
May-04, Marks 16
Section 16.10
Q.1 Discuss different modes of interfacing ADC to microprocessor.
TM
Section 16.11
Q.1 What is sample and hold circuit ? Explain the purpose of it.
Q.2 Explain the use of sample and hold circuit with multiplexer.
Q.3 Draw and explain the data acquisition system.
Q.4 Draw and explain the operation of a sample and hold circuit.
May-04,05, Marks 4
Q.2 Calculate the values of LSB, MSB and full scale output for 8-bit DAC for 0 to
10 V range. June-07
= 39.21 mV
\ LSB = 39.21 mV
Q.3 Draw the basic block diagram of ADC interfacing with 8085. Dec.-08
TM
Q.5 What is the significance of end of conversion signal while interfacing A/D
converter to a microprocessor ?
Ans. : End of conversion signal from ADC indicates that the conversion is completed.
So that microprocessors can read converted digital word through data bus by enabling
the output enable signal after EOC is activated.
Q.6 Why ADC and DAC is used in the microprocessor bused system ?
Ans. : Most of the information carrying signals such as voltage, current, charge,
temperature, pressure and time are available in the analog form. However, for
processing, transmission and storage purposes, it is often more convenient to express
such signals in the digital form. When expressed in the digital form, they provide better
accuracy and reduce noise.
Moreover, the development in the microprocessor technology has made it
compulsory to process data in the digital form and hence ADC is used in the
microprocessor based systems.
On the other hand, a digital to analog (D/A) converter is used when a binary
output from a digital system must be converted to some equivalent analog voltage or
current. For example, if in a particular system a computer is used as a controller, the
controlling signal produced by the computer is always digital. The system to be
controlled requires the analog signal. Hence in between the computer and the system
to be controlled the digital to analog converter is must.
Q.8 Draw the basic block diagram of ADC interfacing with 8085. Dec.-08
qqq
TM
Contents
17.1 Introduction to 8051 Microcontroller
17.2 Features of 8051 and 8051 Family Microcontrollers
17.3 Architecture of 8051
17.4 Pin Description of 8051
17.5 Internal and External Memories
17.6 Interfacing and Timing Diagrams for Memory Interfacing
17.7 Stack and Stack Pointer
(17 - 1)
TM
· Less hardware, reduces PCB size and increases reliability of the system.
2. It has many instructions to move data between It has one or two instructions to move data
memory and CPU. between memory and CPU.
3. It has one or two bit handling instructions. It has many bit handling instructions.
4. Access times for memory and I/O devices are Less access times for built-in memory and I/O
more. devices.
5. Microprocessor based system requires more Microcontroller based system requires less
hardware. hardware reducing PCB size and increasing
the reliability.
6. Microprocessor based system is more flexible Less flexible in design point of view.
in design point of view.
7. It has single memory map for data and code. It has separate memory map for data and
code.
8. Less number of pins are multifunctioned. More number pins are multifunctioned.
TM
Data memory (in Bytes) 128 RAM 128 RAM 256 RAM 128 RAM
I/O pins 32 32 32 32
Serial Port 1 1 1 1
maintains the source compatibility with 8051. This means that all programs written for the
8051 will run on 8052; however, reverse is not true.
The 8751 microcontroller has 4 K of EPROM instead of ROM. This allows to erase and
reprogram the contents of program memory within 8751. It takes around 20 minutes to
erase the 8751 before it can be programmed again. This feature is very useful in the
program development stage.
Register B
In addition to accumulator, an 8-bit B-register is available as a general purpose
register. It is used for the hardware multiply/divide operation.
TM
Buffer 1
7
ALU DPTR 0
Latch 2
Port 2 1 I/O
O/P Driver A8-A15
Buffer 2
7
TM
0 I/O
17 - 5
16 bit address
PSEN
ALE Timing EPROM/
CY AC F0 RS1 RS0 OV - P
Fig. 17.2
The 8051 consists of following flags.
· CY-Carry Flag : This flag is set if there is an overflow out of bit 7. The carry flag
also serves as a borrow flag for subtraction. In both the examples shown below,
the carry flag is set.
· AC-Auxiliary Carry Flag : This flag is set if there is an overflow out of bit 3 i.e. ,
ADDITION SUBTRACTION
TM
· OV-Over Flow Flag : This flag is set whenever the result of a signed number
operation is too large, causing the high-order bit to overflow into the sign bit.
· P-Parity Flag : Parity is defined by the number of ones present in the
accumulator. P = 0, if number of ones are even and P = 1, if number of ones are
odd.
Example : The status of CY, AC and P flags after the addition of 9BH and 65H is as
follows :
1 1 1 1 1 1 1 Carry
9 BH 1 0 0 1 1 0 1 1
+
65 H 0 1 1 0 0 1 0 1
1 0 0 0 0 0 0 0 0
Accumulator
CY = 1, AC = 1 and P = 0
There are instructions in 8051, that tests the condition of flags in the PSW register and
make decision based on the status of flags. Thus, programmer use these flags to perform
some arithmetic operations which involves carry or borrow, or to change the program
control (using conditional branching).
As mention earlier, programmer can select register bank by setting corresponding bits
in PSW.
TM
0F0H F7 F6 F5 F4 F3 F2 F1 F0 B
0E0H E7 E6 E5 E4 E3 E2 E1 E0 ACC
0D0H D7 D6 D5 D4 D3 D2 D1 D0 PSW
0B0H B7 B6 B5 B4 B3 B2 B1 B0 P3
0A0H A7 A6 A5 A4 A3 A2 A1 A0 P2
98H 9F 9E 9D 9C 9B 9A 99 98 SCON
90H 97 96 95 94 93 92 91 90 P1
88H 8F 8E 8D 8C 8B 8A 89 88 TCON
80H 87 86 85 84 83 82 81 80 P0
TM
Table 17.3 contains a list of all the SFRs and their addresses and their value in binary
at reset.
Symbol Name Address Value in Binary
*B B Register 0F0H 0 0 0 0 0 0 0 0
0B8H 8051 X X X 0 0 0 0 0
*IP Interrupt Priority Control
8052 X X 0 0 0 0 0 0
0A8H 8051 0 X X 0 0 0 0 0
*IE Interrupt Enable Control
8052 0 X 0 0 0 0 0 0
87H HMOS 0 X X X X X X X
PCON Power Control
CHMOS 0 X X X 0 0 0 0
Table 17.3 List of all SFRs ( * – Bit addressable, + – 8052 only )
* before register name indicates that it is a bit addressable.
+ before register name indicates that it is supported by only 8052.
TM
P1.0 1 40 VCC + 5V
P1.1 2 39 P0.0 (AD0)
P1.2 3 38 P0.1 (AD1)
P1.3 4 37 P0.2 (AD2)
Port 1
P1.4 5 36 P0.3 (AD3)
Port 0
P1.5 6 35 P0.4 (AD4)
P1.6 7 34 P0.5 (AD5)
P1.7 8 33 P0.6 (AD6)
RST 9 32 P0.7 (AD7)
P3.0 (RXD) 10 8051 31 EA (VPP)
P3.1 (TXD) (40-pin)
11 DIP 30 ALE (PROG)
P3.2 (INT0)
12 29 PSEN
P3.3 (INT1) P2.7 (A15)
13 28
Port 3 P3.4 (T0)
14 27 P2.6 (A14)
P3.5 (T1) 15 26 P2.5 (A13)
The 8051 has 32 I/O pins configured as four eight-bit parallel ports (P0, P1, P2
and P3). All four ports are bidirectional i.e. each pin will be configured as input or output
(or both). All port-pins are multiplexed except the pins of port 1. Each port consists of a
latch, an output driver and an input buffer.
TM
Port 1 (Pins 1 - 8)
Port 1 pins can be used only as I/O pins.
Table 17.4
TM
TM
Program memory
FFFFH FFFFH
EA = 0
60 kbytes Access
External External
memory 64 kbytes
OR External
1000H
0FFFH
4 kbytes EA = 1
Internal Access
0000 Internal 0000
memory
Data memory
(SFRs)
FFH FFFFH
Accessible by
Accessible by
indirect
Upper direct
addressing
128 addressing
only
AND 64 kbytes
80H external
7FH memory
Accessible by
Lower direct & indirect
128 addressing
0 0000H
Fig. 17.5
TM
Byte
Address Byte
Address
1F R7 7F
1E R6
1D R5
1C R4
Bank 3
1B R3
1A R2
19 R1
18 R0
17 R7
16 R6
15 R5
14 R4
Bank 2
13 R3
12 R2
11 R1
10 R0 B7 B6 B5 B4 B3 B2 B1 B0
0F R7 7F 7E 7D 7C 7B 7A 79 78 2F
0E R6 77 76 75 74 73 72 71 70 2E
0D R5 6F 6E 6D 6C 6B 6A 69 68 2D
0C R4 67 66 65 64 63 62 61 60 2C
Bank 1
0B R3 5F 5E 5D 5C 5B 5A 59 58 2B
0A R2 57 56 55 54 53 52 51 50 2A
09 R1 4F 4E 4D 4C 4B 4A 49 48 29
08 R0 47 46 45 44 43 42 41 40 28
07 R7 3F 3E 3D 3C 3B 3A 39 38 27
06 R6 37 36 35 34 33 32 31 30 26
05 R5 2F 2E 2D 2C 2B 2A 29 28 25
04 R4 27 26 25 24 23 22 21 20 24
Bank 0
03 R3 1F 1E 1D 1C 1B 1A 19 18 23
02 R2 17 16 15 14 13 12 11 10 22
01 R1 0F 0E 0D 0C 0B 0A 09 08 21
00 R0 07 06 05 04 03 02 01 00 20
30
Register Bit Addresses Byte General Purpose
Bank Addresses
0 0 Bank 0
0 1 Bank 1
1 0 Bank 2
1 1 Bank 3
On reset, the bank 0 is selected and hence it is a default register bank. Register banks
when not selected can be used as general purpose RAM.
TM
increase the memory capacity. We also know that ROM is used as a program memory and
RAM is used as a data memory. Let us see how 8051 accesses these memories.
Program memory
FFFFH FFFFH
EA = 0
60 kbytes Access
External External
memory 64 kbytes
OR External
1000H
0FFFH
4 kbytes EA = 1
Internal Access
0000 0000
Internal
memory
P0 D0
P1
D7
EA ROM/EPROM
L
8051 A A0
T
C A7
ALE
CLK H Addr.
P3 A8
P2
A15
PSEN OE
TM
As shown in the Fig. 17.8, the port 0 is used as a multiplexed address/bus. It gives
lower order 8-bit address in the initial T-cycle and later it is used as a data bus. The 8-bit
address is latched using external latch and ALE signal generated by 8051. The port 2
provides the higher order 8-bit address. Fig. 17.9 shows the timing waveforms for external
program memory read cycle.
ALE
PSEN
INSTR
PORT 0 A0 - A7 IN
A0 - A 7
Fig. 17.9 Timing waveforms for external program memory read cycle
The lower part of program memory stores the vector addresses for various interrupt
service routines. Fig. 17.10 shows the vector address map. Each interrupt is assigned with
a fixed location in program memory. For example, external interrupt 0 is assigned to
location 0003H. The interrupt service locations are spaced at 8-byte intervals such as 0003H
for External Interrupt 0, 000BH for Timer 0, 0013H for External Interrupt 1, 001BH for
Timer 1, etc. If interrupt is going to be used, its service routine must begin at
0033 H
002B H
Timer 1 001B H
Interrupt 8 Bytes
Locations External Interrupt 1 0013 H
Timer 0 000B H
RESET 0000 H
Fig. 17.10 Interrupt/Vector locations in the lower part of program memory
TM
corresponding location. If the interrupt is not going to be used, its service location is
available as general purpose program memory.
Mnemonic Operation
Table 17.5
Data memory
(SFRs)
FFH FFFFH
Accessible by
Accessible by
indirect
Upper direct
addressing
128 addressing
only
AND 64 kbytes
80H external
7FH memory
Accessible by
Lower direct & indirect
128 addressing
0 0000H
TM
Fig. 17.12 shows the circuit diagram for connecting external data memory. The
multiplexed address/data bus provided by port 0 is demultiplexed by external latch and
ALE signal. Port 2 gives the higher order address bus. The RD and WR signals from 8051
selects the memory read and memory write operation, respectively.
D0
P1 P0
D7
+VCC
EA RAM
L
A A0
T
A7
C
8051 ALE H
CLK ADDR
P3 P2 PAGE
BITS
RD I/O
WR WR OE
ALE
PSEN
RD
Fig. 17.13 (a) Timing waveforms for external data memory read cycle
TM
ALE
PSEN
WR
Fig. 17.13 (b) Timing waveforms for external data memory write cycle
Mnemonic Operation
Table 17.6
TM
Table 17.7
As shown in the Fig. 17.14 (a) memory chip has 11 address lines A10-A0, one chip
select (CS) and two control lines. Read (RD) to enable output buffer and write (WR) to
enable the input buffer. The internal decoder is used to decode the address lines.
Fig. 17.14 (b) shows the logic diagram of a typical EPROM (Erasable Programmable Read
Only Memory) with 4096 (4 K) registers. It has 12 address lines A11-A0, one chip select
(CS), one read control signal. Since EPROM is a read only memory, it does not require the
(WR) signal.
The memory interfacing requires to :
· Select the chip.
· Identify the register.
· Enable the appropriate buffer.
TM
Input
data
WR
Input buffer EPROM
CS
4096 x 8
A10
Internal decoder
Internal decoder
A11
R/W
Memory
2048 x 8
A0 (N x M) A0
CS
Output buffer Output buffer
RD RD
Output Output
data data
(a) Logic diagram for RAM (b) Logic diagram for EPROM
Fig. 17.14
Microprocessor/microcontroller system includes memory devices and I/O devices. It is
important to note that microprocessor can communicate (read/write) with only one device
at a time, since the data, address and control buses are common for all the devices. In
order to communicate with memory or I/O devices, it is necessary to decode the address
from the microprocessor/microcontroller. The following section describes common address
decoding techniques.
Absolute decoding
In absolute decoding technique, all the higher address lines are decoded to select the
memory chip, and the memory chip is selected only for the specified logic levels on these
high-order address lines; no other logic levels can select the chip. Fig. 17.15 shows the
memory interface with absolute decoding. This addressing technique is normally used in
large memory systems.
TM
D0
D7
A0
A7
A8
A15
RD
WR
PSEN
D 7 - D 0 A 9 A8 A 7 - A 0 OE D7-D0 A9 A8 A7 - A0 OE WR
EPROM (1 K) RAM (1 K)
VCC
CS CS
G
A13 A Y0
A14 B Y1
A15 C B
G1 G2
74LS138
A10
A12 A11
Memory Map
Table 17.8
TM
Linear decoding
In small systems, hardware for the decoding logic can be eliminated by using
individual high-order address lines to select memory chips. This is referred to as linear
decoding. Fig. 17.16 shows the addressing of RAM with linear decoding technique. This
technique is also called partial decoding. It reduces the cost of decoding circuit, but it has
a drawback of multiple addresses (shadow addresses).
D0 - D7
A0 - A 7
A8 - A15
RD
WR
PSEN
EPROM (1 K) RAM (1 K)
CS CS
A15
Fig. 17.16 shows the addressing of RAM with linear decoding technique. A15 address
line, is directly connected to the chip select signal of EPROM and after inversion it is
connected to the chip select signal of the RAM. Therefore, when the status of A15 line is
‘zero’, EPROM gets selected and when the status of A15 line is ‘one’ RAM gets selected.
The status of the other address lines is not considered, since those address lines are not
used for generation of chip select signals.
Memory Map :
Table 17.9
TM
ß Example 17.1 : An 8051 based system requires external memory of four 4 kbytes of
SRAM each and two chips of EPROM of size 2 kbytes. The EPROM starts at address
2000H. SRAM address map follows EPROM map. Give the complete interface.
Solution :
A0-A15
D0-D7
PSEN
Reset
ALE
WR
RD
EPROM 1
EPROM 0
A1-A14
2 K (EPROM)
EPROM 1
D8-D15
CS
A11
A11
OE
A10-A0
2 K (EPROM)
EPROM 0
CS
D7-D0
OE
RAM 4
OE D7-D0 A11-A0 WR
RAM 3
RAM 2
4 K (RAM)
RAM 1
RAM 1
RAM 2
RAM 3
RAM 4
CS
C
H
A
T
L
Y2
Y3
Y4
Y5
Y6
X2 P2.0
P2.7
P0.0
P0.7
PSEN
WR
RD
ALE
GND
O
D
R
E
E
8051
G1
G2
C
A
B
RST
EA
X1
VCC
A12
A14
A13
R
C
Fig. 17.17
TM
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2000H
EPROM0
0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 27FFH
0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 2800H
EPROM1
0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 2FFFH
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 3000F
RAM0
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3FFFH
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4000H
RAM1
0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 4FFFH
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 5000H
RAM2
0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 5FFFH
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 6000H
RAM3
0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 6FFFH
ß Example 17.2 : Interface two 8255's to 8051 with stating address of 0F000H. Show the
hardware design. Write the instruction sequence to initialize all ports of first 8255 as output
ports in mode 0 and in the second 8255 port A as input in mode 1 and other ports as input
in mode 0.
Solution : See Fig. 17.18 on next page.
Address Map :
CR 0F003H 0F007H
TM
VCC L
A
P0.0 T A0-A15
C P0.7 C
RST H
R 8051 D0-D7
Microprocessors and Microcontroller
PSEN PSEN
EA WR WR
RD RD
ALE
ALE
Reset
TM
Reset WR OE D7-D0 A1-A0 Reset WR OE D7-D0 A1-A0
17 - 27
Fig. 17.18
CS CS
A3
8051 Microcontroller
Microprocessors and Microcontroller 17 - 28 8051 Microcontroller
ß Example 17.3 : Give the complete block schematic of an 8051 based system having
following specifications.
64 kB of program memory.
64 kB of data memory.
Make use of 16 K ´ 8-bit memory chips and 74 LS 138 decoders.
Indicate clearly, the addresses selected for the memory chips.
Solution : See Fig. 17.19 on next page.
Memory Map :
Table 17.10
TM
VCC L
A
P0.0 T A0-A15
C P0.7 C
RST H
Microprocessors and Microcontroller
R 8051 D0-D7
PSEN PSEN
EA WR WR
RD RD
ALE ALE
Reset
TM
17 - 29
Fig 17.19
EPROM EPROM EPROM EPROM RAM RAM RAM RAM
16 K x 8 16 K x 8 16 K x 8 16 K x 8 16 K x 8 16 K x 8 16 K x 8 16 K x 8
+5V
1 2 3 4 1 2 3 4
CS CS CS CS CS CS CS CS
A14 A VCC G Y0
08 09
09
SP 07 08
Data 08
06 SP 07
Stack pointer SP SP+1 07
Data 1 09 Data 2 09
SP Data 2 08 Read 08
Data 3 07 SP SP–1 07
Stack pointer
Fig. 17.20
The stack may overwrite data in the register banks, bit-addressable RAM and
scratch-pad RAM. Thus to avoid conflict with the register, bit-addressable RAM and
scratch-pad RAM data, the stack is initialized at a higher location in the internal RAM.
Review Questions
Section 17.1
Q.1 Distinguish between microprocessor and microcontroller.
TM
Section 17.2
Q.1 List the features of 8051 microcontroller ?
Q.2 Compare the 8051, 8031 and 8751 microcontroller.
Q.3 List out the hardware resources available in 8051. Dec.-04
Q.4 What are the main features of 8051 microcontroller ? May-12, Marks 2
Section 17.3
Q.1 Give the details of PSW of 8051. May-10
Q.2 Quantify the number of register banks in 8051 and say how the CPU knows which
bank is currently in use. Dec.-10
Q.3 Explain the functional block diagram of 8051 in detail.
Dec.-04,09, May-09,10 Marks 10
Q.4 Describe the architecture of 8051 with neat diagram. May-06,08,11,12, Marks 16
Q.5 List the on-chip peripherals of 8051 microcontroller. Dec.-11, Marks 2
Q.6 Mention the size of DPTR and stack pointer in 8051 microcontroller.
May-11, Marks 2
Q.7 What is program status word of 8051 ? May-12, Marks 2
Section 17.4
Q.1 Draw the pin diagram of 8051 microcontroller and explain its port structure.
Dec.-11, Marks 8
Q.2 List the alternative functions assigned to Port 3 pins of 8051 microcontroller.
May-11, Marks 2
Section 17.5
Q.1 What do you understand by bit addressable RAM in 8051 microcontroller ? Dec.-10
Q.2 Discuss the internal memory organization of the 8051 microcontroller.
Dec.-10, Marks 16
Q.3 Discuss about the organization of internal RAM and special function registers of 8051
microcontroller in detail. May-11, Marks 8
Q.4 What is the internal memory capacity of microcontroller 8051 ? May-11, Marks 8
Q.5 Explain the program memory and data memory structure of 8051 microcontroller.
Dec.-11, Marks 8
TM
Section 17.6
Q.1 Explain program memory interfacing in 8051 microcontroller. May-12, Marks 8
Q.2 Design an 8051 based system with 16 kbytes of program ROM and 16 kbytes of data
ROM. Dec.-10, Marks 16
Q.3 Explain in detail the different methods of memory address decoding in 8051.
Dec.-10, Marks 8
Section 17.7
Q.1 Explain the operation of stack in 8051.
Q.2 Define SP.
TM
Q.12 State the function of RS1 and RS0 bits in the flag register of Intel 8051
microcontroller ?
Ans. : RS1 and RS0 are bank selection bits. They are used to select working register
bank of 8051 as given below :
· 0 0 Bank 0
· 0 1 Bank 1
· 1 0 Bank 2
· 1 1 Bank 3
Q.13 Give the alternate functions for the port pins of port3 ?
Ans. : Refer Table 17.4.
TM
Q.16 Explain the 16-bit registers DPTR of 8051 or what is a function of DPTR ?
Ans. : DPTR : It stands for data pointer. DPTR consists of a high byte (DPH) and a
low byte (DPL). Its function is to hold a 16-bit address. It may be manipulated as a
16-bit data register or as two independent 8-bit registers. It serves as a base register in
indirect jumps, lookup table instructions and external data transfer.
TM
Q.20 What is the maximum frequency of the clock signal that can be counted by
8051 counter ?
Ans. : The maximum frequency of the clock signal that can be counted by 8051
counter is 1 12 ´ crystal frequency.
Q.21 What are the features of ROM and RAM in 8051 microcontroller ?
Ans. : The 8051 has 128-byte internal RAM. It is accessed using RAM address register.
The internal RAM of 8051 is organized into three distinct areas :
· Register Bank
· Bit addressable
· General purpose.
The 8051 has 4 kbyte of internal ROM with address space from 0000H to 0FFFH. It
is programmed by manufacturer when the chip is built. This part cannot be erased or
altered after fabrication. This is used to store final version of the program. It is
accessed using program address register.
Q.25 A given 8051 chip has a speed of 16 MHz. What is the range of frequency that
can be applied to the XTAL 1 and XTAL 2 pins ?
Ans. : The range of frequencies that can be applied to the XTAL 1 and XTAL 2 pins is
1 MHz to 16 MHz.
TM
Q.28 What are on-chip resources ? List those available in the 8051 microcontroller.
Dec.-10
Ans. : The advance microcontrollers are supported with on-chip peripherals such as
program memory, data memory, parallel ports, PWM output, ADC, RTC (Real time
clock), Timers/counters, Serial ports, I2C interface and so on. These are known as
on-chip resources. The resources available in 8051 are :
· 4096 byte on-chip program memory
· 128 bytes on chip data memory
· 32-bit bi-directional I/O lines
· Multi-mode serial port
· Two multi-mode 16-bit timers/counters
Q.29 Quantify the number of register banks in 8051 and say how the CPU knows
which bank is currently in use. Dec.-10
Ans. : Refer section 17.3.5.
Q.30 Justify your choice between UV-EPROM and flash EPROM for an external
ROM in an 8051 microcontroller application. Dec.-10
Ans. : Flash EPROMS can be erased electrically with selective erase facility. However,
UV-EPROMS cannot be erased electrically, they need ultraviolet light source. EPROMS
need around 20 minutes to erase and entire EPROM is erased at a time. Thus flash
EPROM is more preferable during development stage. However, once the product is
ready we can use EPROM as an external memory.
qqq
TM
Contents
18.1 8051 Addressing Modes
18.2 Classification of Instruction Set of 8051
18.3 Data Transfer Instructions
18.4 Byte Level Logical Instructions
18.5 Arithmetic Instructions
18.6 Bit Level Logical Instructions
18.7 Rotate and Swap Instructions
18.8 Jump and CALL Instructions
18.9 Time Delay for 8051
18.10 Introduction to Assembly Language Programming
18.11 Program Examples
(18 - 1)
Microprocessors and Microcontroller 18 - 2 8051 Instruction Set and Programming
Destination register
Address of memory
within the instruction
Data from
selected memory
location
Direct addressing can access any on-chip variable or hardware register. i.e. on-chip
RAM and special function register. The most significant bit of the address decides whether
it is a location within on-chip RAM (MSB = 0) or in special function register (MSB = 1).
TM
Memory
Register
Destination register
Contents of register are
used to point memory
Data from
selected memory
location
R0 and R1 are the only registers that can be used for pointers in register indirect
addressing mode.
Example : ADD the contents of memory location addressed by register 1 to the contents
of RAM location pointed by register 0.
MOV A, @R0 ; load the contents pointed by R0 in A
ADD A, @R1 ; Add the contents of A and the contents pointed by R1
Destination register
Data specified
in the instruction
TM
Example :
SWAP A ; Swap nibbles within the Accumulator
18.1.6 Index
Only program memory can be accessed in the index addressing. Either the DPTR or
PC can be used as an index register.
DPTR Register
Program memory
+
Data from Address of
selected memory memory
Contents of register A
Examples :
PUSH 04 ; Push R4 onto stack
PUSH 06 ; Push R6 onto stack
TM
Mnemonic Operation
MOV <dest-byte>, <src-byte> Copy the byte variable indicated by 'src-byte' into
the 'dest-byte' location
MOV direct, direct Copy the contents of the address specified within
instruction to the address specified within
instruction.
TM
MOV direct, @Ri Copy the contents of the address given by register
Ri of selected register bank to the address specified
within instruction.
MOV direct, #data Load data given within instruction to the address
specified within instruction.
MOV @Ri, #data Load the data specified within instruction to the
address specified by register Ri of selected register
bank.
Description : The byte variable indicated by the second operand is copied into the
location specified by the first operand. The source byte is not
affected. No other register or flag is affected.
This is by far the most flexible operation. Fifteen combinations of
source and destination addressing modes are allowed.
MOV A, Rn
MOV A, direct
TM
MOV A, @Ri
MOV A, #data
Example : MOV A, # 30H ; This instruction copies data given within instruction
(30H) into the accumulator.
MOV Rn, A
Example : MOV R1, 40H ; This instruction copies the contents at memory
address 40H into the R1 register of the selected register bank.
Example : MOV R2, #20H ; This instruction loads 20H in the registers R2 of
selected register bank.
MOV direct, A
MOV direct, Rn
TM
Example : MOV 20H, 40H ; This instruction copies the contents of memory
location whose address is 40H to the memory location whose address
is 20H.
Example : MOV 20H, @R3 ; This instruction copies the contents of memory
location whose address is given by register R3 of selected register
bank into the memory location whose address is 20H.
Example : MOV 30H, #12H ; This instruction copies data given within
instruction (12H) into the memory location whose address is 30H.
MOV @Ri, A
Example : MOV @ R2, 30H ; This instruction copies the contents of memory
location whose address is given within the instruction (30H) into the
memory location whose address is specified by register R2 of selected
register bank.
TM
Example : MOV @R2, #30H ; This instruction loads 30H into the memory
location whose address is specified by register R2 of selected register
bank.
MOV A, ACC is not a valid instruction.
Description : The data pointer is loaded with the 16-bit constant indicated. The
16-bit constant is loaded into the second and third bytes of the
instruction. The second byte (DPH) is the high-order byte, while the
third byte (DPL) holds the low-order byte. No flags are affected.
This is the only instruction which moves 16 bits of data at once.
Operation : MOV
(DPTR) ¬ #data 15-0
DPH ¬ #data15-8 DPL ¬ #data7 - 0
Mnemonic Operation
Table 18.2
Some examples of the instructions listed in Table 18.2 are given below.
Example 1 : MOVX A, @ R0
This instruction copies data from the 8-bit address in R0 to A.
Example 2 : MOVX A, @ DPTR
This instruction copies data from the 16-bit address in DPTR to A.
TM
Description : The MOVX instructions transfer data between the Accumulator and a
byte of external data memory, hence the “X” appended to MOV.
No flags are affected.
MOVX A, @Ri This instruction copies the contents of external memory whose
address is given by register into the accumulator.
MOVX A, @DPTR : This instruction copies the contents of external memory whose
address is given by DPTR register into the accumulator.
MOVX @Ri, A : This instruction copies the contents of accumulator into the external
memory whose address is given by the register.
MOVX @DPTR, A : This instruction copies the contents of accumulator into the external
memory whose address is given by the DPTR register.
· All external data moves with external RAM involve the A register.
· While accessing external RAM, Rp can address 256 bytes and DPTR can address
64 kbytes.
· MOVX instruction is used to access external RAM or I/O addresses.
TM
Mnemonic Operation
Table 18.3
Example 1 : Let the contents of DPTR are 1200 and the contents of A are 61H.
MOVC A, @A + DPTR
This instruction copies the code byte found at the external ROM
address formed by adding A and the DPTR, i.e. at an address
(1200H + 61H) 1261H to A.
Example 2 : Let the contents of PC are 4000H and contents of A are 50H.
MOVC A, @A + PC
This instruction copies the code byte found at the external ROM
address formed by adding A and the PC, i.e. at an address
(4000H + 50H) 4050H to A.
The MOVC instructions can be explained completely with example as
given below.
Description : The MOVC instructions load the Accumulator with a code byte, or
constant from program memory. The address of the byte fetched is
the sum of the original unsigned eight-bit Accumulator contents and
the contents of a sixteen-bit base register, which may be either the
Data Pointer or the PC. In the latter case, the PC is incremented to
the address of the following instruction before being added with the
Accumulator; otherwise the base register is not altered. No flags are
affected.
MOVC A,@A + DPTR : This instruction loads the accumulator from the contents of
program memory whose address is given by the sum of the
contents of accumulator and contents of DPTR register.
TM
MOVC A,@A + PC This instruction loads the accumulator from the contents of
program memory whose address is given by the sum of the
contents of accumulator and the contents of program counter. The
current contents of program counter are incremented by 1 before
summation.
Example : PUSH B
This instruction increments the stack pointer by one and stores the
contents of register B to the internal RAM location addressed by the
stack pointer (SP).
TM
Mnemonic Operation
XCH A, direct Exchange data bytes between address directly given within instruction and A.
TM
Operation : ®
(A) (R n )
¬
Operation : ®
(A) (direct)
¬
Operation : ®
(A 3 - 0 ) (R i 3 - 0 )
¬
1 1 1 1 0 0 0 0 Masking pattern
X X X X 0 0 0 0 Result
Masked bits
+ 1 1 1 1 0 0 0 0 Setting pattern
1 1 1 1 X X X X Result
Set bits
TM
X X X X X X X X Result
Inverted bits
· The Table 18.5 gives the list of byte level logical operations.
TM
ANL A, @R1
ANL A, #data
Example : ANL A, # 50H ; logically ANDs contents of A with 50H and stores
result in A.
ANL direct, A
Example : ANL 20H, #20H ; Logically ANDs the contents of memory location
20H with data 20H and stores result in memory location 20H.
TM
Description : ORL performs the bitwise logical-OR operation between the indicated
variables, storing the results in the destination byte. No flags are
affected.
ORL A, Rn
ORL A, direct
Example ORL A, 20H ; logically ORs the contents of A and memory location
20H and stores result in A.
ORL A, @Ri
Example ORL A, @R2 ; Logically ORs the contents of A and memory location
whose address is given by register R2 and stores result in A.
ORL A, #data
Example ORL A, #32H ; Logically ORs the contents of A with 32H and stores
result in A.
ORL direct, A
Example ORL 20H, #30H ; Logically ORs the contents of memory location 20H
and data 30H and stores result at memory location 20H.
TM
Description : XRL performs the bitwise logical Exclusive-OR operation between the
indicated variables, storing the results in the destination. No flags are
affected.
XRL A, Rn
Example XRL A, R2 ; logically XOR the contents of A and R2 and stores result
in A.
XRL A, direct
XRL A, @Ri
Example XRL A,@R2 ; Logically XORs the contents of A and the memory
location whose address is given by R2 and stores result in A.
XRL A, #data
Example XRL A, #40H ; Logically XORs the contents of A with data 40H and
stores result in A.
XRL direct, A
Example XRL 20H, A ; Logically XORs the contents at 20H and the A and
stores the result at 30H.
TM
Example XRL 30H, #40H ; Logically XORs the contents at 30H and data 40H
and stores the result at 30H.
Operation : (A) ¬ 0
TM
Table 18.6
INC A
INC Rn
INC direct
TM
INC @Ri
DEC A
DEC Rn
DEC direct
DEC @Rn
TM
18.5.2 Addition
The table shows the list of addition instructions supported by 8051.
Addition Instructions
Description : ADD adds the byte variable indicated to the Accumulator, leaving
the result in the Accumulator. The carry and auxiliary-carry flags are
set, respectively, if there is a carry-out from bit 7 or bit 3, and
cleared, otherwise.
When adding signed integers, OV indicates a negative number
produced as the sum of two positive operands, or a positive sum
from two negative operands.
ADD A, Rn
ADD A, direct
ADD A, @Ri
TM
ADD A, #data
Description : ADDC simultaneously adds the byte variable indicated, the carry flag
and the Accumulator. respectively, if there is a carry-out from bit 7
or bit 3, and cleared otherwise.
Precaution : When we use ADDC in loop to add 8-bit numbers carry flag should
be cleared before first 8-bit addition.
ADDC A, Rn
Example ADDC A, R2 : Adds the contents of A, R2 and carry flag, and stored
result in A.
ADDC A, direct
ADDC A, @Ri
ADDC A, #data
Example ADDC A, #20H ; Adds the contents of A and carry flag and 20H
and stores result in A.
TM
18.5.3 Subtraction
The table shows the list of subtraction instructions supported by 8051.
Subtraction Instructions
Precaution : If the state of the carry is not known before starting a single or
multiple-precision subtraction, it should be explicitly cleared by a
CLR C instruction.
Example :
SUBB A, Rn
SUBB A, direct
Example SUBB A, 20H ; subtracts the contents of memory location 20H and
carry together from A stores result in A.
SUBB A, @Ri
Operation : (A) ¬ (A) – (C) – ((Ri))
TM
Division Instruction
Example : The Accumulator holds the value 55H (01010101B) representing the
packed BCD digits of the decimal number 55. Register 3 contains the
value 68H (01101000B) representing the packed BCD digits of the
decimal number 68. The carry flag is set. The instruction sequence.
ADDC A, R3
DA A
will first perform a standard two's-complement binary addition,
resulting in the value BEH (10111110) in the Accumulator. The carry
and auxiliary carry flags will be cleared.
The Decimal Adjust instruction will then alter the Accumulator to
the value 24H (00100100B), indicating the packed BCD digits of the
decimal number 24, the low-order two digits of the decimal sum of
55, 68, and the carry-in. The carry flag will be set by the Decimal
Adjust instruction, indicating that a decimal overflow occurred. The
true sum 55, 68 and 1 is 124.
Operation DA
AND
TM
TM
P2 A0 A0-A7
P3 B0 B0-B7
PSW D0 D0-D7
TCON 88 88-8F
SCON 98 98-9F
Description : The indicated bit is cleared (reset to zero). No other flags are
affected. CLR can operate on the carry flag or any directly
addressable bit.
Example : Port 1 has previously been written with FFH (11111111B). The
instruction,
CLR P1.2
will leave the port set to FBH (11111011B)
TM
CLR C
Operation : (C) ¬ 0
CLR bit
Operation : CLR
(bit) ¬ 0
Description : SETB sets the indicated bit to one. SETB can operate on the carry flag
or any directly addressable bit. No other flags are affected.
Example : The carry is cleared. Output Port 1 has been written with the value
34H (00110100B). The instruction,
SETB C
SETB P1.0
will leave the carry flag set to 1 and change the data output on
Port 1 to 35H (00110101B).
SETB C
Operation : (C) ¬ 1
SETB bit
Operation : (bit) ¬ 1
Example : Port 1 has previously been written with FFH (11111111B). The
instruction
CPL P1.1
will leave the port set to FDH (11111101B)
TM
CPL C
CPL bit
Example : Set the carry flag if, and only if, P1.0 = 1, ACC.7 = 1, and OV = 0:
MOV C, P1.0 ; LOAD CARRY WITH INPUT PIN STATE
ANL C, ACC.7 ; AND CARRY WITH ACCUMULATOR BIT 7
ANL C,/OV ; AND WITH INVERSE OF OVERFLOW FLAG
ANL C, bit
ANL C/ bit
Description : Set the carry flag if the Boolean value is a logical 1; leave the carry
in its current state otherwise slash (“/”) preceding the operand in
the assembly language indicates that the logical complement of the
addressed bit is used as the source value, but the source bit itself is
not affected. No other flags are affected.
Example : Set the carry flag if and only if P1.0 = 1, ACC.7 = 1, or OV = 0:
MOV C, P1.0 ; LOAD CARRY WITH INPUT PIN P10
ORL C, ACC.7 ; OR CARRY WITH THE ACC.BIT7
ORL C, /OV ; OR CARRY WITH THE INVERSE OF OV.
TM
ORL C, bit
ORL C,/bit
MOV <dest-bit>, <src-bit> Function : Move bit data Bytes : 2 Cycles : 1/2
Description : The Boolean variable indicated by the second operand is copied into
the location specified by the first operand. One of the operands must
be the carry flag; the other may be any directly addressable bit. No
other register or flag is affected.
Example : The carry flag is originally set. The data present at input Port 3 is
(11000101B). The data previously written to output Port 1 is C5H
(00110101B).
MOV P1.3, C
MOV C, P3.3
MOV P1.2, C will leave the carry cleared and change Port 1 to
39H (00111001B).
MOV C, bit
MOV bit, C
TM
Description : The eight bits in the Accumulator are rotated one bit to the left. Bit 7
is rotated into the bit 0 position. No flags are affected.
7 6 5 4 3 2 1 0
Example : The Accumulator holds the value C5H (11000101B). The instruction,
RL, A
leaves the Accumulator holding the value 8BH (10001011B) with
the carry unaffected.
Description : The eight bits in the Accumulator and the carry flag are together
rotated one bit to the left. Bit 7 moves into the carry flag; the original
state of the carry flag moves into the bit 0 position. No other flags
are affected.
C 7 6 5 4 3 2 1 0
Carry
flag
Example : The Accumulator holds the value C5H (11000101B), and the carry is
zero. The instruction,
RLC A
leaves the Accumulator holding the value 8BH (10001010B) with
the carry set.
TM
Description : The eight bits in the Accumulator are rotated one bit to the right.
Bit 0 is rotated into the bit 7 position. No flags are affected.
7 6 5 4 3 2 1 0
Example : The Accumulator holds the value C5H (11000101B). The instruction,
RR A
leaves the Accumulator holding the value E2H (11100010B) with
the carry unaffected.
Operation : (An) ¬ (A n + 1) = 0 – 6
(A7) ¬ (A0)
Description : The eight bits in the Accumulator and the carry flag are together
rotated one bit to the right. Bit 0 moves into the carry flag; the
original value of the carry flag moves into the bit 7 position. No
other flags are affected.
7 6 5 4 3 2 1 0 C
Carry
flag
Example : The Accumulator holds the value C5H (11000101B), the carry is zero.
The instruction
RRC A
leaves the Accumulator holding the value 62 (01100010B) with the
carry set.
TM
Description : Swap A interchanges the low and high-order nibbles (four-bit fields)
of the Accumulator (bits 3-0 and bits 7-4). The operation can also be
thought of as a four-bit rotate instruction. No flags are affected.
7 4 3 0
Higher nibble Lower nibble
Example : The Accumulator holds the value C5H (11000101B). The instruction
SWAP A
leaves the Accumulator holding the value 5CH (01011100B)
Operation : SWAP
®
(A 3 - 0 ) (A 7 - 4 )
¬
TM
18.8.2 Jump
The Table 18.11 shows the list of jump instructions supported by 8051.
Table 18.11
Jump Instructions
TM
Description : Add the eight-bit unsigned contents of the Accumulator with the
sixteen-bit data pointer, and load the resulting sum to the program
counter. This will be the address for subsequent instruction fetches.
Neither the accumulator nor the data pointer is altered. No flags are
affected.
Description : If all bits of the Accumulator are zero, branch to the address
indicated; otherwise proceed with the next instruction. The
accumulator is not modified. No flags are affected.
TM
Description : If the carry flag is set branch to the address indicated; otherwise
proceed with the next instruction. No flags are affected.
Description : If the carry flag is a zero, branch to the address indicated; otherwise
proceed with the next instruction. The carry flag is not modified.
Description : If the indicated bit is one, jump to the address indicated; otherwise
proceed with the next instruction. The bit tested is not modified. No
flags are affected.
JNB bit, rel Function : Jump if Bit Not set Bytes : 3 Cycles : 2
JBC bit, rel Function : Jump if Bit is set and Clear bit Bytes : 3 Cycles : 2
Description : If the indicated bit is one, branch to the address indicated; otherwise
proceed with the next instruction. The bit will not be cleared if it is
already a zero.
Description : CJNE compares the magnitudes of the first two operands, and
branches if their values are not equal. The carry flag is set if the
unsigned integer value of <dest-byte> is less than the unsigned
integer value of <src-byte>; otherwise, the carry is cleared. Neither
operand is affected.
TM
TM
Description : RET pops the high and low-order bytes of the PC successively from
the stack, decrementing the Stack Pointer by two program execution
continues at the resulting address. No flags are affected.
Description : RETI pops the high and low-order bytes of the PC successively from
the stack, and restores the interrupt logic to accept additional
interrupts at the same priority level as the one just processed. The
Stack Pointer is left decremented by two. No other registers are
affected; the PSW is not automatically restored to its per-interrupt
status. Program execution continues at the resulting address, which is
generally the instruction immediately after the point at which the
interrupt request was detected. If a lower-level or same-level
interrupt had been pending interrupt is processed.
TM
For 8051 operating frequency is one-twelfth (1/12) of the crystal frequency. Therefore,
one machine cycle lasts for 12 oscillator periods.
12
\ Machine cycle period =
Crystal frequency
For 8051, we know that how much machine cycle/s are required to execute the
particular instruction. Therefore, we can calculate the exact time for execution of that
instruction, as shown below.
ß Example 18.1 : Calculate the time delay produced by the following subroutine.
Delay : MOV R1, #30
HERE : DJNZ R1, HERE
NOP
NOP
RET
Solution : Let us assume the crystal frequency of 8051 is 11.0592 MHz. Therefore, the
period of the machine cycle will be
12
T = = 1.085 µsec
11.0592 ´ 10 6
= 69.44 µs
Here, (2 ´ 30) indicates that the instruction DJNZ R1, HERE is executed 30 times.
TM
Assembly Language
To make programming easier, usually programmers write programs in assembly
language. They then translate the assembly language program to machine language so that
it can be loaded into memory and executed. Assembly language uses two, three or four
letter words to represent each instruction types. These words are referred to as
mnemonics. The letters in an assembly language mnemonic are usually initials or a
shortened form of the English word(s) for the operation performed by the instruction. For
example, the mnemonic for addition is ADD, the mnemonic for logic AND operation is
AND, and the mnemonic for the instruction for copy data from one location to another is
MOV. Therefore, the meaning expressed by mnemonics help us to remember the operation
performed by the instruction.
Assembly language statements are usually written in a standard form and assembly
language has its own unique syntactical structure, such as requiring upper case or lower
case, or requiring colons after label definitions. Here we discuss the common features that
assembler shares.
The assembly text is usually divided into fields, separated by spaces and tabs. A
format for a typical line from assembly language program can be given as
The first field, which is optional, is the label field, used to specify symbolic labels. A
label is an identifier that is assigned to the address of the first byte of the instruction in
which it appears. As mentioned earlier, the presence of a label is optional, but if present,
the label provides a symbolic name that can be used in branch instructions to branch to
the instruction.
The second field is mnemonic, which is compulsory. All instructions must contain a
mnemonic. The third and following fields are operands. The presence of the operands
depends on the instruction. Some instructions have no operands, some have one, and some
have two. If there are two operands, they are separated by a comma.
The last field is a comment field. It begins with a delimiter such as the semicolon and
continues to the end of the line. The comments are for our benefits, they tell us what the
TM
program is trying to accomplish. The Fig. 18.4 shows a typical 8051 assembly language
instruction.
4. Programs have less execution time. Programs have less execution time.
TM
i.e. the CPU chip other than the 8051 to convert assembly language program to
machine language program.
4. Macro Assembler : A very useful facility provided by many assemblers is the use
of macro. A macro is a sequence of instructions to which a name is assigned.
When the macro is referenced by specifying its name, the macro assembler replaces
the macro call by the sequence of instructions that define the macro. The macro
assembler functions in a similar manner to the assembler described earlier.
However, it has to perform an additional task of macro expansion before the
assembly program is translated into an equivalent machine language program.
5. Compiler : A compiler is a program used to translate higher level language
program (program written using BASIC, Pascal or C) to machine code which can
be loaded into memory and executed.
6. Cross Compiler : A cross compiler is a compiler capable of creating executable
code for a platform other than the one on which the compiler is run. It is used to
generate executables for embedded system or multiple platforms.
7. Linker : A linker is a program used to join together several object files into one
large object file and produce a link file which contains the binary codes for all the
combined files.
8. Loader : A loader is a program that transfers the program to be executed from
secondary memory into the memory accessed by microprocessor or microcontroller.
TM
Editor
Assembly language
program text written
in any text editor
. asm
. lst
Assembler
Program listing
Converts assembly
instruction to object
code Error messages
. abs
0H Program
Converts object code
file into executable file
. hex
Executable file
TM
CODE : It assigns a name to the specified memory location in the program memory
(Range 0 - 65535). For example,
LIST CODE 1020H ; Memory location 1020H in the program memory is now
; referred to as LIST
DATA : It assigns a name to the specified location in the internal RAM of 8051.
(Range 0 - 255). For example,
TEMP DATA 52 H ; Register at address 52H is now
; named as TEMP
IDATA : It assigns a name to the memory location whose address is located in the
specified register. For example,
MARKS DATA 80 ; Register whose address is in register at
; address 80 is named as MARKS
TM
XDATA : It assigns name to the specified memory location in the external RAM
memory (Range 0 - 65535). For example,
RESULT XDATA 1000H ; Memory location 1000H in the
; external RAM memory is now referred
; to as RESULT
USING : This directive is used to define which register bank (Bank0 - Bank3) will be used
in the following program. For example,
USING 1 ; Bank 1 will be used.
TM
Program 7 : Unpack the packed BCD number stored in the accumulator and save the
result in R0 and R1 such that (R0)¬ LSB and (R1) ¬ MSB.
MOV B, A ; Save the packed BCD number
ANL A, #0FH ; Mask upper nibble of BCD number
MOV R0, A ; Save the lower digit
MOV A, B ; Get the packed BCD number
ANL A, #0F0H ; Mask lower nibble of BCD number
SWAP A ; Exchange the lower and upper nibbles
MOV R1,A ; Save the upper digit.
Program 8 : Subtract two 8-bit numbers and exchange digits.
MOV A, #9F ; Get the first number in A
MOV R0, #40 ; Get the second number in R0
CLR C ; Clear carry
SUBB A, R0 ; A ¬ A–(R0)
TM
Program Logic :
Step 1 : Divide number with 100 decimal and save quotient i.e. save hundred’s digit.
Step 2 : Make remainder as a new number.
Step 3 : Divide number with 10 decimal and save quotient i.e. save tens digit.
Step 4 : Save remainder as ones digit.
Sample Example :
Quotient Remainder
76H ¸ 100 = 1 12H
12H ¸ 10 = 1 8
76H = (118) 10
TM
Flowchart :
Start
Number ¸ 100
Number Remainder
Hund_digit Quotient
Number ¸ 10
Ten_digit Quotient
One_digit Remainder
Stop
Program :
MOV A, #76H ; Load the binary number in A
MOV B, #100 ; Load B with 100 decimal
DIV AB ; Divide number with 100
MOV R0 ,A ; Save the hundreds of the number
; (Quotient of the previous division)
MOV A, B ; Get the remainder
MOV B, #10 ; Load B with 10 decimal
DIV AB ; Divide number with 10
MOV R1, A ; Save the tens of the number
MOV R3, B ; Save the ones of the number
Statement : Write a program to convert a given 8-bit binary number into its Gray code
equivalent
Let us see the Binary - Gray conversion process.
· Get the binary number.
· MSB of result ¬ MSB of the binary number.
· XOR bits from left to right with their adjacent bits.
TM
Example :
0 + 0 + 1 + 0 + 1 + 1 + 0 + 1 Binary number
0 0 1 1 1 0 1 1 Gray number
Program Logic :
To XOR each bit with its adjacent bit we right shift the contents of original number
and then XOR the result with the original number.
Example :
0 0 1 0 1 1 0 1 Binary number
+
0 0 0 1 0 1 1 0 Right shifted binary number
0 0 1 1 1 0 1 1 Gray number
Flowchart :
Start
Stop
Program :
MOV A, #52H ; Load binary number
MOV R0, A ; Save binary number
CLR C ; Clear carry flag so that after shifting
; the contents we will get MSB = 0
RRC A ; right shift
XRL A, R0 ; XOR shifted contents with the original
; number
TM
Start
Stop
Program :
MOV DPTR, #1234H ; Load first number
MOV R0, #20H ; Load lower byte of second number
MOV R1, #30H ; Load higher byte of second number
MOV A, R0 ; Get the lower byte of second number
ADD A, DPL ; Add two lower bytes
DA A ; Adjust result to valid BCD
MOV DPL, A ; Store the sum of lower bytes
MOV A, R1 ; Get the higher byte of second number
ADDC A, DPH ; Add two higher bytes considering
; carry of lower byte addition
DA A ; Adjust result to valid BCD
MOV DPH, A ; Store the sum of higher bytes
TM
TM
Program 18 : Find the maximum number from a given 8-bit ten numbers.
Flowchart :
Start
Initialize pointer
to memory
Initialize counter
Maximum number = 0
Is
Number > Yes
Max. number
?
Max. number Number
No
Decrement counter
No Is
counter = 0
?
Yes
Stop
TM
Program :
MOV DPTR, #2000 ; Initialize pointer to memory where
; numbers are stored
MOV R0, #0A ; Initialize counter
MOV R3, #00 ; Maximum = 0
AGAIN : MOVX A, @DPTR ; Get the number from memory
CJNE A, R3, NE ; Compare number with maximum number
AJMP SKIP ; If equal go to SKIP
NE : JC SKIP ; If not equal check for carry, if
; carry go to SKIP
MOV R3, A ; Otherwise maximum = number
SKIP : INC DPTR ; Increment memory pointer
DJNZ R0, AGAIN ; Decrement count, if count = 0 stop
; otherwise go to AGAIN
Program 19 : Arrange the given ten 8-bit numbers in the ascending order.
Program :
MOV R0, #09 ; Initialize counter1
AGAIN : MOV DPTR, #2000H ; Initialize memory pointer
MOV R1, #09 ; Initialize counter2
BACK : MOV R2, DPL ; Save lower byte of memory address
MOVX A, @DPTR ; Get the number
MOV B, A ; Save the number
INC DPTR ; Increment memory pointer
MOVX A, @DPTR ; Get the next number
CJNE A,B,NE ; If not equal check for greater or
; less
AJMP SKIP ; Otherwise go to skip
NE : JNC SKIP ; If
MOV DPL, R2 ; [ Exchange
MOVX @DPTR, A ; the contents
INC DPTR ; of two
MOV A,B ; memory
MOVX @DPTR, A ; locations ]
SKIP : DJNZ R1, BACK ; If R1 not equal to 0 go to BACK
DJNZ R0, AGAIN ; If R0 not equal to 0 go to AGAIN
TM
Start
Initialize counter 1
Initialize counter 2
Increment
memory pointer
Is
(pointer) > Yes
(pointer + 1)
?
Interchange contents of
compared memory locations
No
Decrement counter 2
No Is
counter 2 = 0
?
Yes
Decrement counter 1
No Is
counter 1 = 0
?
Yes
Stop
TM
Program 20 : Find the number of negative and positive numbers in a given array.
Flowchart :
Start
Initialize pos_no_counter = 0
Initialize neg_no_counter = 0
No Is Yes
MSB = 1
?
pos_no_counter = Neg_no_counter =
pos_no_counter + 1 Neg_no_counter + 1
Is
No iteration
counter = 0
?
Yes
Stop
Program :
MOV R0, #00 ; Initialize counter = 0 for negative
; numbers
MOV R1, R0 ; Initialize counter = 0 for positive
; numbers
MOV R2, #0AH ; Initialize counter = 10
MOV DPTR, #2000H ; Initialize memory pointer
TM
Start
Initialize count = 0
Initialize counter = 8
Rotate contents of
accumulator so that
LSB will go in carry
No Is
carry = 1
?
Yes
Increment count
Decrement counter
No Is
counter = 0
?
Yes
Stop
TM
Program :
MOV R2, #0 ; Initialize one’s counter = 0
MOV R1, #08 ; Initialize iteration count
MOV R0, #56 ; Load number
MOV A, R0 ; Get the number in accumulator
BACK : RRC A ; Rotate A and CY ¬ LSB
JNC SKIP ; If carry is not zero go to skip
INC R2 ; Otherwise increment one’s counter
SKIP : DJNZ R1, BACK ; Decrement iteration count and if not
; zero repeat
Flowchart :
Start
Initialize count = 0
Initialize counter = 8
Rotate contents of
accumulator so that
LSB will go in carry
No Is
carry = 0
?
Yes
Increment count
Decrement counter
No Is
counter = 0
?
Yes
Stop
TM
Program :
MOV R2, #0 ; Initialize zero’s counter = 0
MOV R1, #08 ; Initialize iteration count
MOV R0, #56 ; Load number
MOV A, R0 ; Get the number in accumulator
BACK : RRC A ; Rotate A and CY ¬ LSB
JC SKIP ; If carry is zero go to skip
INC R2 ; Otherwise increment zero’s counter
SKIP : DJNZ R1, BACK ; Decrement iteration count and if not
; zero repeat
Start
Rotate contents of
accumulator so that
LSB will go in carry
No Is Yes
carry = 0
?
Decrement counter
No Is
counter = 0
?
Yes
Stop
TM
Program :
MOV R2, #0 ; Initialize one’s counter = 0
MOV R3, #0 ; Initialize zero’s counter = 0
MOV R1, #08 ; Initialize iteration count
MOV R0, #56 ; Load number
MOV A, R0 ; Get the number in accumulator
BACK : RRC A ; Rotate A and CY ¬ LSB
JC SKIP ; If carry is zero go to skip
INC R3 ; Otherwise increment zero’s counter
AJMP LAST ; Go to last
SKIP : INC R2 ; Increment one’s counter
LAST : DJNZ R1, BACK ; Decrement iteration count and if not
; zero repeat
AGAIN : DJNZ R1, AGAIN ; Decrement count and repeat the process
; until Count is zero
RET ; Return to main program
Statement : Calculate the sum of series of numbers. The length of the series is in
memory location 2200H and the series itself begins from memory location 2201H.
a. Assume the sum to be 8-bit number so you can ignore carries. Store the sum at
memory location 2300H.
b. Assume the sum to be 16-bit number. Store the sum at memory locations 2300H
and 2301H.
a. Sample problem
2200H = 04H
2201H = 20H
2202H = 15H
2203H = 13H
2204H = 22H
Result = 20 + 15 + 13 + 22 = 6AH
\ 2300H = 6AH
Flowchart :
Start
Sum=0
Pointer = 2201H
Count = (2200H)
Pointer = Pointer +1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum
End
TM
Program :
a) MOV DPTR, #2200H ; Initialize memory pointer
MOVX A, @DPTR ; Get the count
MOV R0, #10 ; Initialize the iteration counter
INC DPTR ; Initialize pointer to array of numbers
MOV R1, #00 ; Result = 0
BACK : MOVX A, @DPTR ; Get the number
ADD A, R1 ; A ¬ Result + A
MOV R1, A ; Result ¬ A
INC DPTR ; Increment the array pointer
DJNZ R0, BACK ; Decrement iteration count if not zero
; repeat
MOV DPTR, #2300H ; Initialize memory pointer
MOV A, R1 ; Get the result
MOVX @DPTR, A ; Store the result
b. Sample problem
2200H = 04H 2201H = 9AH
2202H = 52H 2203H = 89H 2204H = 3EH
Result = 9AH + 52H + 89H + 3EH = 1B3H
\ 2300H = B3H Lower byte 2301H = 01H Higher byte
Program :
b) MOV DPTR, #2200H ; Initialize memory pointer
MOVX A, @DPTR ; Get the count
MOV R0, #10 ; Initialize the iteration counter
INC DPTR ; Initialize pointer to array of numbers
MOV R2, #00 ; [Make
MOV R1, #00 ; result = 00H]
BACK : MOVX A, @DPTR ; Get the number
ADD A, R1 ; A ¬ Result + A
MOV R1, A ; Result ¬ A
ADDC R2, #00 ; If carry exists, add it to MSD
INC DPTR ; increment the array pointer
DJNZ R0, BACK ; Decrement iteration count if not zero
; repeat
MOV DPTR, #2300H ; Initialize memory pointer
MOV A, R1 ; Get the lower byte of result
TM
Start
Sum high = 0
Sum low = 0
Pointer = 2201H
Count = (2200H)
No Is
Carry 1
?
Yes
Pointer = Pointer + 1
Count = Count – 1
No Is
Count = 0
?
Yes
(2300H) = Sum low
(2301H) = Sum high
End
TM
Flowchart :
Start
Initialize counter = 10
No Is
Count = 0
?
Yes
End
Program :
MOV R2, #1 ; Initialize iteration counter
MOV R1, #20H ; Initialize source memory pointer
MOV R0, #30H ; Initialize destination memory pointer
BACK : MOV A, @R1 ; Get data
MOV @R0, A ; Store data
INC R1 ; Increment source memory pointer
INC R0 ; Increment destination memory pointer
DJNZ R2, BACK ; Decrement iteration count and if not
; zero repeat
TM
Flowchart :
Start
Initialize counter = 10
No Is
Count = 0
?
Yes
End
Program :
MOV R2, #10 ; Initialize iteration counter
MOV R1, #29H ; Initialize source memory pointer
MOV R0, #32H ; Initialize destination memory pointer
BACK : MOV A, @R1 ; Get data
MOV @R0, A ; Store data
DEC R1 ; Decrement source memory pointer
DEC R0 ; Decrement destination memory pointer
DJNZ R2, BACK ; Decrement iteration count and if not
; zero repeat
Statement : Search the given byte in the list of 50 numbers stored in the consecutive
memory locations 2200H and 2201H. Assume that byte is 76H. If byte is not found store 00
at 2200H and 2201H.
TM
Flowchart :
Start
Is
(Pointer) = Yes
Search byte
?
No
Yes
Store 00 as a result
Stop
Program :
MOV R1, #00 ; [load R1 and R2 with 00 so that if
MOV R2, #00 ; byte is not found we can load these
; contents at 2200H and 2201H
; locations]
MOV R0, #50 ; Initialize iteration counter
MOV DPTR, #2000H ; Initialize memory pointer
BACK : MOVX A, @DPTR ; Get the number
CJNE A, #76H, SKIP ; Search byte 76H if not equal
; go to SKIP
MOV R1, DPL ; [otherwise store
MOV R2, DPH ; the address of the byte]
SJMP LAST
SKIP : INC DPTR
TM
Statement : Multiply two 8-bit numbers stored in memory locations 2200H and 2201H.
Store the result in memory locations 2300H and 2301H.
Sample problem
(2200H) = B2H
(2201H) = 03H
Result = B2H + B2H + B2H
= 216H
(2300H) = 16H
(2301H) = 02H
Flowchart :
Start
Initialize second
number as a counter
Result = 0
Decrement counter
No Is
count = 0
?
Yes
End
TM
Program :
MOV DPTR, #2200H ; [Get the first
MOVX A, @DPTR ; number]
MOV R0, A ; store the number
INC DPTR ; [Get the second
MOVX A, @DPTR ; number]
MOV R1, A ; store it as a counter
MOV R2, #00 ; [Make
MOV R3, #00 ; result = 0]
BACK : MOV A, R2 ; Get result (lower byte)
ADD A, R0 ; Result = Result + number
MOV R2, A ; store result (lower byte)
ADDC R3, #00 ; If carry exists, add 1 to higher byte
DJNZ R1, BACK ; Decrement counter, if not zero repeat
MOV A, R2 ; Get the lower byte of result
MOV DPTR, #2300H ; [store the
MOVX @DPTR, A ; lower byte of result]
MOV A, R3 ; Get the higher byte of result
INC DPTR ; [store the
MOVX @DPTR, A ; higher byte of result]
Program :
MOV R2, #00 ; [Make
MOV R3, #00 ; result = 0]
MOV R0, #10 ; Initialize count
MOV R1, #20H ; Initialize memory pointer
BACK : MOV A, @R1 ; Get the number
ADD A, R2 ; Add lower byte
MOV R2 A ; save result
ADDC R3, #00 ; If carry exists add Carry to higher byte
INC R1 ; Increment memory pointer
DJNZ R0, BACK ; Decrement counter and if not zero
; repeat
TM
Start
Sum = 0
Initialize count = N = 10
Initialize memory pointer
Pointer Pointer + 1
Count Count – 1
No Is
count = 0
?
Yes
Quotient = 0
Quotient = Quotient + 1
No
Is
Sum < 10
Yes
Stop
N! = N × (N – 1) × (N – 2) × (N – 3) × … × 2 × 1
For example :
6! = 6 × 5 × 4 × 3 × 2 × 1 = 720
Flowchart :
Start
Result = 1
Number = 1
Increment Number
No Is
number > N
?
Yes
Stop
Program
MOV A,#01 ; Store 01 in A register
MOV B, A ; Store 01 in B register
CAL : MUL AB ; Multiply two numbers
INC B ; increment number
CJNE B, #07H, CAL ; Check whether number is 7. If number
; is 7 we have to stop
TM
Note :
Program :
MOV R0, #10 ; Initialize term count
MOV R1, #20H ; Initialize array pointer
MOV @R1, #0 ; Store 1st term as 0
INC R1 ; increment memory pointer
MOV @R1, #1 ; store 2nd term as 1
BACK : MOV A, @R1 ; Get the current term
DEC R1 ; Pointer to previous term
ADD A, @R1 ; Add current term and previous term
INC R1 ; [Point to
INC R1 ; next term]
MOV @R1, A ; Store next term
CJNE @R1, 10,BACK ; Check whether last term calculated
; if not repeat the process
TM
Start
Initialize array
pointer where we are
going to store terms
No Is
term counter = N
?
Yes
Stop
Solution :
MOV R0, #05 ; Initialize count = 5
MOV DPL, #00H ; Initialize memory pointer
MOV DPH, #80H ; Adjust the memory pointer to
; address the first memory
BACK : MOVX A, @DPTR ; Get data 1
TM
Program :
MOV R0, # 0A ; Initialize counter
MOV R1, #00H ; Initialize memory pointer
MOV R2, #40H
MOV R3, #00H ; Initialize even memory pointer
MOV R4, #50H
MOV R5, #00H ; Initialize odd memory pointer
MOV R6, #60H
BACK : MOV DPL, R1 ; Load memory address
MOV DPH, R2
MOVX A, @DPTR ; Get data
JB ACC.0, NEXT ; Check LSB of accumulator
; if 1 goto NEXT (odd)
MOV DPL, R3 ; Load even memory address
MOV DPH, R4
MOVX @DPTR, A ; Store even data
INC R3 ; Increment even memory pointer
SJMP NEXT1
NEXT : MOV DPL, R5 ; Load odd memory address
MOV DPH, R6
MOVX @DPTR, A ; Store odd data
INC R5 ; Increment odd memory pointer
TM
Program 36 : Write an assembly language program to realize following logic circuit using
Boolean instructions of 8051.
Solution : Let us assume that input a is connected to P1.0, input b is connected by P1.1
and output Y is connected at P2.0.
a
y
b
Program 37 : Write a program to load accumulator with values 55H and complement 70
times.
MOV R0, #70 ; Initialize iteration count
MOV A, # 55H ; load 55H in accumulator
HERE : CPL A ; Complement accumulator
DJNZ R0, HERE ; Repeat till R0 = 0
Program 38 : Program to count the number of ONE's and ZERO's in two consecutive
data memory locations.
Solution :
ORG 100 H ; Start at location 100 H
MOV R2, #0 ; Initialize one's counter = 0
MOV R3, # 0 ; Initialize zero's counter = 0
MOV R1, # 08 ; Initialize iteration count
MOV R0, 40H ; Load number from first memory location
MOV A, R0 ; Get the number in accumulator
TM
Program 39 : Write a program to save the status of bits P1.3 and P1.4 on RAM bit
location 5 and 6 respectively.
Solution :
JNB P1.3, NEXT ; Check bit P1.3, if 0 then goto NEXT
SETB 05 ; P1.3 = 1, therefore, set bit location 5 = 1
SJMP SKIP ; Skip next instruction
NEXT : CLR 05 ; Clear bit location 5
SKIP : JNB P1.4, NEXT 1 ; Check bit 1.4, if 0 then goto NEXT1
SETB 06 ; P1.4 = 1, therefore, set bit location 6 = 1
SJMP SKIP 1 ; Skip next instruction
NEXT 1 : CLR 06 ; Clear bit location 6
Skip 1 : -------
TM
Review Questions
Section 18.1
Q.1 What are the addressing modes supported by 8051 ? Dec.-09
Q.2 Explain the different addressing modes in 8051 in detail.
June-08, Dec.-07,08,11 Marks 8
Q.3 What are the addressing modes of 8051 microcontroller ? Dec.-11, Marks 2
Q.4 What is register indirect addressing mode of microcontroller 8051 ? Give example.
June-11, Marks 2
Section 18.2
Q.1 List the different types of 8051 instructions. May-10
Q.2 Give the classification of 8051 instruction set.
Section 18.3
Q.1 Write the I/O related instructions in microcontroller 8051. Dec.-08
Q.2 What is the operation carried out when 8051 executes the instruction MOVC A,
@ A + DPTR ? Dec.-07
Q.3 Mention the I/O instructions of 8051 microcontroller. Dec.-11, Marks 2
Q.4 Explain the data transfer instructions and program control instructions of 8051
microcontroller. May-11, Marks 8
Q.5 Explain the operations carried out when the following instructions are executed by
8051.
i) MOVX @ R0,A ii) MOVC A,@A + PC iii) RLC A iv) CJNE A, 50H, L2
v) XCH A, 30H where L2 and L3 are labels. Dec.-07, Marks 16
Section 18.4
Q.1 What is the operation of the given 8051 microcontroller instructions : XRL A, direct ?
May-11, Marks 2
Q.2 Explain the instruction set of 8051 microcontroller. May-10, Marks 10
Section 18.5
Q.1 What is the time taken to execute MUL instruction in 8031 ? May-05
Q.2 How can you perform multiplication using 8051 microcontroller ? May-08
Q.3 List the arithmetic instructions of microcontroller 8051. June-11, Marks 2
TM
Section 18.6
Q.1 Name any four bit manipulation instructions in microcontroller 8051. May-08
Q.2 What are the various operations performed by boolean variable instructions of 8051 ?
May-10
Q.3 What are the different operations performed by boolean variable instructions of 8051 ?
May-11, Marks 2
Section 18.8
Q.1 What are the uses of LCALL and LJUMP instructions of 8051 ? Dec-09
Q.2 Explain about the instruction DJNZ. June-09
Section 18.10
Q.1 What is machine language ?
Q.2 What is assembly language ?
Q.3 Give comparison between assembly language and machine language.
Q.4 Write a short note on the following
a) Assembler b) Compiler c) Linker d) Cross assembler e) Macro assembler.
Q.5 How assembly language program is created, assembled and made ready to run ?
Q.6 List files required during assembly programming.
Q.7 Explain the data type of 8051.
Q.8 What are assembler directives ? Explain.
Q.9 Describe DB, ORG, EQU, END directive of IC 8051 microcontroller.
Q.10 Explain the function of following directives.
i) DB ii) EQU iii) ORG
iv) DATA v) END vi) CODE
Section 18.11
Q.1 Write an 8051 based assembly language program for performing four basic arithmetic
operations on two data. May-10, Marks 6
May-05
Ans. : Refer section 18.5.4.
TM
Q.2 Give the PSW setting for making register bank 2 as default register bank in
8051 microcontroller ? June-07
Ans. : Refer program 9.
Q.3 What is the operation carried out when 8051 executes the instruction MOVC A,
@ A + DPTR ? Dec.-07
Ans. : Refer section 18.3.2.
Q.4 Name any four bit manipulation instructions in microcontroller 8051. May-08
Ans. : Refer section 18.6.
Q.5 How can you perform multiplication using 8051 microcontroller ? May-08
Ans. : Refer section 18.5.4.
MOV R0, A
XRL A, #3FH
XRL A, R0 June-09
Ans. : The contents of A register will be 3FH and contents of R0 will be the initial
contents of A.
Q.11 What are the uses of LCALL and LJUMP instructions of 8051? Dec.-09
Ans. : Refer section 18.8.
Q.13 What are the various operations performed by boolean variable instructions of
8051 ? May-10
Ans. : Refer section 18.6.
TM
Q.15 Write a program using 8051 assembly language to change the data 55H stored
in the lower byte of the data pointer register to AAH using rotate instruction.
Ans. :
MOV DPL, #55H
MOV A, DPL
RL A
Q.16 Specify the single instruction, which clears the most significant bit of B
register of 8051, without affecting the remaining bits.
Ans. : Single instruction, which clears the most significant bit of B register of 8051,
without affecting the remaining bit is CLR B.7.
Q.17 Explain the contents of the accumulator after the execution of the following
program segments:
Ans. :
MOV A, #3CH
MOV R4, #66H
ANL A, R4
ans : A = 3C
R4 = 66
A = 24
Q.18 Write a program to load accumulator A, DPH and DPL with 30H.
Ans. :
MOV A, #30
MOV DPH, A
MOV DPL, A
TM
Q.19 Write a program to subtract the contents of R1 of Bank0 from the contents of
R0 of Bank2.
Ans. :
MOV PSW, #10
MOV A, R0
MOV PSW, #00
SUBB A, R1
Q.20 List the 8051 instructions that affect the overflow flag.
Ans. : ADD, ADDC, DIV, MUL, SUBB
Q.21 List the 8051 instructions that always clear the carry flag.
Ans. : CLR C, DIV, MUL
Q.22 List the 8051 instructions that affect all the flags.
Ans. : ADD, ADDC and SUBB
qqq
TM
Notes
TM
Contents
19.1 8051 I/O Ports Structure
19.2 8051 I/O Port Programming
19.3 I/O Bit Manipulation Programming
19.4 8051 Timers
19.5 8051 Timer Modes and Programming
19.6 8051 Counter Programming
19.7 8051 Serial Port
19.8 8051 Interrupt Structure
19.9 Programming Interrupts
(19 - 1)
TM
Read latch
P0.X
Pin
Internal bus D Q
P0.X
Latch Mux
Write to latch CL Q
Control logic
Read pin
Port 1 (Pins 1 - 8)
(See Fig. 19.2 on next page)
Port 1 pins can be used only as I/O pins.
TM
VCC
Read latch
Internal
pull-up
P1.X
Internal bus D Q Pin
P1.X
Latch
Write to latch CL Q
Read pin
VCC
Addr bus
Control
Read latch
Internal
pull-up
Internal bus D Q
P2.X
P2.X
Latch
Pin
Write to latch CL Q
MUX
Control logic
Read pin
TM
VCC
Alternate
output
function Internal
Read latch
pull-up
P3.X
Pin
Internal bus D Q
P3.X
Latch
Write to latch CL Q
Read pin
Alternate
input
function
Table 19.1
TM
X1
Internal clock :
Crystal
The 8051 microcontroller has on chip clock
generator. Fig. 19.5 shows the crystal oscillator X2
circuit. It is the most stable circuit. The 30 pF 30 pF C C 30 pF
capacitors in the circuit are connected to assure
oscillator start-up at the correct frequency.
Fig. 19.5 Crystal oscillator
circuit
External clock :
External X1
oscillator signal
TM
IC 74LS373
P0.0 D Q A0
P0.1 A1
P0.2 A2
Multiplexed CLK
address P0.3 A3 Address
and P0.4 A4 lines
data lines
P0.5 A5
P0.6 A6
P0.7 A7
G OC
Enable Output control
ALE
D0
D1
D2
D3 Data
D4 lines
D5
D6
D7
multiplexed bus. In the remaining part of the machine cycle, ALE signal is disabled so
output of the latch (A0-A7) remains unchanged. To latch the address, in each machine
cycle, the 8051 microcontroller gives ALE signal high during T1 of every machine cycle.
TM
R 10 K
P0.0
P0.1
P0.2
P0.3
8051 Port 0
P0.4
P0.5
P0.6
P0.7
VCC
10K
8751/89C51
+ EA/VPP P0.0
10 mF P0.1
RST P0.2
30 mF
P0.3
8.2 K X1
P0.4
11.0592 MHz P0.5
X2 P0.6
30 mF P0.7
P3.0/RXD
P3.1/TXD P2.0
P3.2/INT0 P2.1
P3.3/INT1 P2.2
P3.4/T0 P2.3
P3.5/T1 P2.4
P1.0 P2.5
P1.1 P2.6
P1.2 P2.7
P1.3 PSEN
P1.4
ALE/PROG
P1.5
P1.6 P3.6 WR
P1.7 P3.7/RD
Fig. 19.10 Minimum system connections
TM
Solution :
BACK : MOV A, #0AAH ; Load AAH in the (A) accumulator
MOV P0, A ; Send contents of A to port 0
A CALL Delay ; Wait for some time
MOV A, #55H ; Load 55H in the accumulator
MOV P0, A ; Send contents of A to port 0
A CALL Delay ; Wait for some time
SJMP BACK ; Repeat
The same action can be implemented using following program code.
BACK : MOV P0, A ; Send contents of A on port 0
CPL A ; Complement contents of port 0
A CALL Delay ; Wait for some time
SJMP Back ; Repeat
Note : Like port 0, we can toggle all bits of P1, P2 or P3 by replacing the corresponding
port instead of P0 in the above programs.
TM
ß Example 19.2 : Write a program to read the content of P1 and save it in R6 and also
send it to P2.
Solution :
MOV A, # 0FFH ; A ¬ FFH
MOV P1, A ; Make P1 as an input port by writing all 1's to it
MOV A, P1 ; Read data from P1
MOV R6, A ; Save it in R6
MOV P2, A ; Send it to P2
Timer 1 Timer 0
Fig. 19.13 TMOD register
n Select Timer 0 to operate as a counter or timer
n Select Timer 1 to operate as a counter or timer
n Select the mode in which timer should operate.
TM
M1, M0 : These bits select the timer mode. There are four different modes of timer,
mode 0, mode 1, mode 2 and mode 3. All these modes are discussed in the further section.
M1 M0 Operating mode
C/T : This bit is cleared (C/T = 0) for selecting 'timer' operation and is set (C/T = 1)
for selecting 'counter' operation.
GATE : Gating control when set. Timer/Counter "x" is enabled only while "INTx" pin
is high and "TRx" control bit is set. When cleared Timer "x" is enabled whenever "TRx"
control bit is set.
(MSB) (LSB)
TF1 TCON.7 Timer 1 Overflow Flag. Set by hardware on timer/counter overflow. Cleared when
interrupt processed.
TR1 TCON.6 Timer 1 Run control bit. Set/cleared by software to turn timer/counter on/off.
TF0 TCON.5 Timer 0 Overflow Flag. Set by hardware on timer/counter overflow. Cleared when
interrupt processed.
TR0 TCON.4 Timer 0 Run control bit. Set/cleared by software to turn timer/counter on/off.
TM
IE1 TCON.3 Interrupt 1 Edge Flag. Set by hardware when external interrupt edge detected.
Cleared when interrupt processed.
IT1 TCON.2 Interrupt 1 Type control bit. Set/cleared by software to specify falling edge/low
level triggered external interrupts.
IE0 TCON.1 Interrupt 0 Edge Flag. Set by hardware when external interrupt edge detected.
Cleared when interrupt processed.
IT0 TCON.0 Interrupt 0 Type control bit. Set/cleared by software to specify falling edge/low
level triggered external interrupts.
Solution :
a. MOV TMOD, #00010000B : The effect of this instruction is to set Timer 1 in
mode 1 and Gate = 0 for internal clocking.
b. MOV TMOD, #00000001B : The effect of this instruction is to set Timer 0 in mode
1 and Gate = 0 for internal clocking.
c. MOV TMOD, #04 : The effect of this instruction is to select timer 0 to run in the
counter mode.
ß Example 19.4 : a.Perform the following operations using bit addressable instructions
Start Timer 1
b. Stop Timer 0
TM
Mode 0
OSC ¸12
TR1
GATE Control
INT1 PIN
Both Timers in Mode 0 are 8-bit Counters with a divide-by-32 prescaler. This 13-bit
timer is MCS-48 compatible. Fig. 19.15 shows the Mode 0 operation as it applies to
Timer 1. In this mode, the Timer register is configured as a 13-bit register. As the count
rolls over from all 1s to all 0s, it sets the Timer interrupt flag TF1. The counted input is
enabled to the Timer when TR1 = 1 and either GATE = 0 or INT1 = 1. (Setting GATE = 1
allows the Timer to be controlled by external input INT1, to facilitate pulse width
measurements.) TR1 is a control bit in the Special Function Register TCON GATE is in
TMOD.
The 13-bit register consists of all 8 bits of TH1 and the lower 5 bits of TL1. The upper
3 bits of TL1 are indeterminate and should be ignored. Setting the run flag (TR1) does not
clear the registers.
Mode 0 operation is the same for Timer 0 as for Timer 1. Substitute TR0, TF0 and
INT0 for the corresponding Timer 1 signals in Fig. 19.15. There are two different GATE
bits, one for Timer 1 (TMOD.7) and one for Timer 0 (TMOD.3).
Mode 1
Both Timers in Mode 1 are 16-bit Counters As the count rolls over from all 1s to all 0s,
it sets the Timer interrupt flag TF. The counted input is enabled to the Timer when TR = 1
and either GATE = 0 or INT1 = 1. (Setting GATE = 1 allows the Timer to be controlled by
external input INT1, to facilitate pulse width measurements.)
TM
TH0 TL0
OSC ¸12 (8 Bits) (8 Bits) TF0 Interrupt
C/T=0
TR0
GATE(TMOD.3) Control
INT0 PIN
A time delay can be generated using mode 1 of the timer 0 using following steps :
1. Load TMOD register indicating timer 0 is used and mode 1 is selected.
7 6 5 4 3 2 1 0
TMOD X X X X 0 0 0 1 = 01
TR0
Fig. 19.17 Timer 0 in mode 1, no external hardware is used to start and stop
timer
TM
TH1 TL1
OSC ¸12 (8 Bits) (8 Bits) TF1 Interrupt
C/T=0
TR1
GATE(TMOD.7) Control
INT1 PIN
A time delay can be generated using mode 1 of the timer 1 using following steps :
1. Load TMOD register indicating timer 1 is used and mode 1 is selected.
7 6 5 4 3 2 1 0
TMOD 0 0 0 1 X X X X = 10H
TR1
Fig. 19.19 Timer 1 in mode 1, no external hardware is used to start and stop
timer
TM
Mode 2
Mode 2 configures the Timer register as an 8-bit Counter (TL) with automatic reload,
as shown in Fig. 19.20. Overflow from TL only sets TF, but also reloads TL with the
contents of TH, which is preset by software. The reload leaves TH unchanged.
TL0
OSC ¸12 (8 Bits) TF0 Interrupt
C/T=0
TR0
GATE(TMOD.3) TH0
Control (8 Bits)
INT0 PIN
A time delay can be generated using mode 2 of the timer 0 using following steps :
1. Load TMOD register indicating timer 0 is used and mode 2 is selected.
7 6 5 4 3 2 1 0
TMOD X X X X 0 0 1 0 = 02H
TM
TR0
Fig. 19.21 Timer 0 in mode 2, no external hardware is used to start and stop
timer
Timer 1 Mode 2 Programming
The Fig. 19.22 shows the timer control logic for timer 1 in mode 2.
TL1
OSC ¸12 (8 Bits) TF1 Interrupt
C/T=0
TR1
GATE(TMOD.7) TH1
Control (8 Bits)
INT1 PIN
A time delay can be generated using mode 2 of the timer 1 using following steps :
1. Load TMOD register indicating timer 1 is used and mode 2 is selected.
7 6 5 4 3 2 1 0
TMOD 0 0 1 0 X X X X = 20H
TM
When start and stop of timer is done using software, no external hardware is needed
for the same. It is illustrated in the Fig. 19.23.
TR1
Fig. 19.23 Timer 1 in mode 2, no external hardware is used to start and stop
timer
Mode 3
Timer 1 in Mode 3 simply holds its count. The effect is the same as setting TR1 = 0.
Timer 0 in Mode 3 establishes TL0 and TH0 as two separate counters. The logic for
Mode 3 on Timer 0 is shown in Fig. 19.24. TL0 uses the Timer 0 control bits : C/T, GATE,
TR0, INT0, and TF0. TH0 is locked into a timer mode (counting machine cycles) and takes
over the use of TR1 and TF1 from Timer 1. Thus, TH0 now controls the : Timer 1
interrupt.
C/T=0 TL0
(8 Bits) TF 0 Interrupt
C/T=1
T0 PIN
TR0
GATE Control
INT0 PIN
TH0
1/12 f OSC (8 Bits) TF 1 Interrupt
Control
TR1
TM
As seen earlier, the C/T bit in the TMOD register decides the timer/counter
functioning as a counter or a timer. When C/T bit in the TMOD register is 0, the timer
mode is selected. When timer/counter is used as a timer, the 8051's crystal is used as a
source of the frequency. When C/T bit in the TMOD register is 1, the counter mode is
selected. When timer/counter is used as a counter, it gets its pulses from outside the 8051.
The pin P 3.4 (pin number 14) and pin 3.5 (pin number 15) of 8051 are used for applying
pulses counter 0 and counter 1 respectively. These two pins belong to port 3. The counter
counts up for each clock pulse applied at this pin. These pins are called T0 (timer 0 clock
input) and T1(timer 1 clock input).
Counter 0 in Mode 1
The Fig. 19.25 (a) shows the block diagram of counter 0 in mode 1 and the
Fig. 19.25 (b) shows the block diagram of counter 0 in mode 1 when GATE = 0 and
INT0 = 1. Here, counter 0 counts up when the logic signal on pin T0 goes from high level
to low level.
TH0 TL0
T0(P3.4) (8 Bits) (8 Bits) TF0 Interrupt
C/T=1
TR0
GATE(TMOD.3) Control
INT0 PIN
TR0
7 6 5 4 3 2 1 0
TMOD X X X X 0 1 0 1 = 05H
7 6 5 4 3 2 1 0
TCON 0 0 0 1 0 0 0 0 = 10H
TH1 TL1
T1(P3.5) (8 Bits) (8 Bits) TF1 Interrupt
C/T=1
TR1
GATE(TMOD.7) Control
INT1 PIN
TR1
7 6 5 4 3 2 1 0
TMOD 0 1 0 1 X X X X = 50H
7 6 5 4 3 2 1 0
TCON 0 1 0 0 0 0 0 0 = 40H
Fig. 19.27 (c) Counter 1 control register settings for mode 1 operation
Counter in Mode 2
In this mode, counter is used in auto-reload mode instead of 16-bit counter. Rest of the
operation is exactly same as that of Mode 1. The Fig. 19.28 (a) shows the block diagram of
counter 0 in Mode 2 and the Fig. 19.28 (b) shows the block diagram of counter 0 in
Mode 2 when GATE = 0 and INT0 = 1
TL0
T0(P3.4) (8 Bits) TF0 Interrupt
C/T=0
TR0
GATE(TMOD.3) TH0
Control (8 Bits)
INT0 PIN
TM
T0(P3.4) TL0
(8 Bits) TF0 Interrupt
C/T=0
TR0
ß Example 19.5 : Write a program for counter 1 in mode 2 to count the pulses and display
the state of TL1 count on port 2. Assume that clock input is connected to T1 pin (P 3.5).
Solution :
MOV TMOD, #01100000B ; Initialize counter 1 in
; Mode 2, C T=1
MOV TH1, #0 ; Clear TH1
SETB P3.5 ; Make T1 input
START : SETB TR1 ; Start the counter
BACK : MOV A, TL1 ; Get the count from TL1
MOV P2, A ; Sent it to port 2
JNB TF1, BACK ; If TF1 = 0 repeat
CLR TR1 ; Otherwise stop counter 1
CLR TF1 ; Make TF1=0
SJMP START ; Repeat
Note : When 8051 is powered up ports are configured as input ports. To make them
work as output port we have to send high output on it. Therefore, to behave T1 as input
P 3.5 is set.
ß Example 19.6 : Write a program to display counter 0 on 7-segment LEDs. Assume that
clock input is connected to pin (P 3.4).
Solution :
MOV TMOD, #00000110 ; Initialize counter 0 in
; Mode 2, C T=1
MOV TH0, #00H ; Reset counter value
SETB P3.4 ; Make T0 as input
START : SETB TR0 ; Start counter 0
BACK : MOV A, TL0 ; Get the count value
ACALL CONVBCD
TM
ß Example 19.7 : Assume that XTAL = 11.0592 MHz. Write a program to generate a
square wave of 2 kHz frequency on pin P1.5.
Solution : T = 1/f = 1/2 kHz = 500 µs is period of square wave. 1/2 of it for high and
low portion of the pulse is 250 µs. 250 µs/1.085 µs = 230 and 65536 – 230 = 65306 which
in hex is FF1AH.
Program is as follows
MOV TMOD, #10H ; timer 1, mode 1
AGAIN : MOV TL1, #1AH ; low byte of timer
MOV TH1, #0FFH ; high byte of timer
SETB TR1 ; Start timer 1
BACK : JNB TF1, BACK ; Stay until timer rolls over
CLR TR1 ; Stop timer 1
CPL P1.5 ; Complement P1.5
CLR TF1 ; Clear timer flag
SJMP AGAIN ; reload timer
ß Example 19.8 : Generate a square wave of frequency 1 kHz using timer 1 in mode 1, on
Pin P1.2. Explain the TMOD word used to configure the timer 1 for this application.
Show the necessary calculations to find the value of count to be loaded into TH1 and TL1
registers. Assume XTAL frequency = 11.0592 MHz.
Solution :
T of square wave = 1/f = 1/2 kHz = 500 ms
500 ms
TON = TOFF = = 250 ms
2
12
T of clock = = 1.085 ms
11.0592 ´ 10 6
TON 250 ms
Count = = = 230
T of clock 1.085 ms
TM
Program
MOV TMOD, #10H ; Timer 1, mode 1 (16-bit)
AGAIN : MOV TL1, #1AH ; Load lower byte of timer
MOV TH1, #0FFH ; Load higher byte of timer
SETB TR1 ; Start timer 1
BACK : JNB TF1, BACK ; Wait for timer rolls over
CLR TR1 ; Stop timer 1
CPL P1.2 ; Complement P1.2
CLR TF1 ; Clear timer flag1
SJMP AGAIN ; Reload timer 1 and continue
ß Example 19.9 : Explain the steps to program timers in model and write an 8051 program
to generate a square wave of 50 % duty cycle on the pin P1.5.
Solution : Square wave
MOV TMOD, #01 ; Timer 0 mode 1
Here : MOV TL0, #0F2H ; Load TL0 - 0F2
MOV TH0, #0FFH ; Load TH0 - 0FF
CPL P1.5 ; toggle input on P1.5
ACALL Delay
SJMP Here
Delay : SETB TR0 ; Start Timer 0
again : JNB TF0, again ; Monitor Timer 0 flag until it rolls over
CLR TR0
CLR TF0
RET
ß Example 19.10 : Write an ALP to generate square wave on pin P1.5 of 500 Hz
(approximately) with a subroutine to provide a time delay of 30.38 ms using timer 0.
Assume that crystal frequency of 8051 is 11.0592 Hz.
Solution :
12 12
T = = = 1.085 ms
Crystal frequency 11.0592 ´ 10 6
30.38 ms
Number of counts for roll over = = 28
1.085 ms
65536 – 28 = 65508 = FFE4H
\ To get a delay of 30.38 we have to load TH0 = FFH and TL0 = E4H
TM
1
For square wave T = = 2 ms
500
\ TON = TOFF = T 2 = 1 ms
ß Example 19.11 : Find the delay generated by timer 0 in the following code. Calculate the
delay generated excluding the instruction overhead. What count has to be loaded in TL0
and TH0 if delay has to be increased to 25 msec ?
CLR P2.3
HERE : MOV TMOD, #01
MOV TL0, #3Eh
MOV TH0, #0B8h
SETB TF0,
AGAIN: JNB TF0, AGAIN
CLR TF0
CLR TR0
CLR P2.3
Solution :
TM
12
T = = 1.085 ´ 10 - 6
11.0592 ´ 10 6
= 5A01 H
ß Example 19.12 : Find out Hex. number to be loaded in TH0, to produce delay of
4.096 msec in mode '0' operation. Assume clock frequency of 12 MHz.
Solution :
Timer clock frequency = Crystal frequency ¸ 12 = 12 MHz ¸ 12
= 1 MHz
\ Timer clock period = 1 µs
Maximum count in mode 0 is 1FFFH (8191) and we have to count for 4096 counts to
get a delay of 4.096 ms
\ Count to be loaded in timer is 1FFFH
(8191 in decimal) – 4096 = FFFH (4095 in decimal)
Therefore, we have to load 0FH in TH0 and FFH in TL0.
Program
MOV TMOD,#2H ; Timer 0, mode 2 (8-bit auto reload)
MOV TH0,#206 ; TH0 = 206
SETB TR0 ; Start timer 0
BACK : JNB TF1, BACK ; Stay till timer rolls over
CPL P1.0 ; Complement P1.0
CLR TF0 ; Clear timer flag 0
SJMP BACK ; Continue
TM
ß Example 19.14 : Write an assembly language program to make LED ON and OFF
connected to P 1.0 continuously with ON time 20 msec and off time 40 msec.
TM
RxD D
(P3.0) SBUF
Shift register CLK (Write only) Q TxD
(P3.1)
CLK
Fig. 19.29
The way in which SBUF is used for the transmission and reception of the data during
serial communication is explained below.
· Transmission : When a byte of data is to be transmitted via the TxD pin, the
SBUF is loaded with this data byte. As soon as a data byte is written into SBUF,
it is framed with the start and stop bits and transmitted serially via the TxD pin.
· Reception : When 8051 receives data serially via RxD pin of it, the 8051
deframes it. The start and stop bits are separated out from a byte of data. This
byte is placed in SBUF register.
(MSB) (LSB)
7 6 5 4 3 2 1 0
TM
TM
SMOD PCON.7 Serial baud rate modify bit. It is 0 at reset. It is set to 1 by program to double the
baud rate.
– PCON.6-4 Not defined
GF1 PCON.3 General purpose user flag bit 1. Set/cleared by program.
GF0 PCON.2 General purpose user flag bit 0. Set/cleared by program.
PD PCON.1 Power down bit. It is set to 1 by program to enter power down configuration
for CHMOS microcontrollers.
IDL PCON.0 Idle mode bit. It is set to 1 by program to enter idle mode configuration for
CHMOS microcontrollers.
Note : PCON is not bit addressable
Mode 1
In this mode, 10 bits are transmitted (through TxD) or received (through RxD) : A start
bit (0), 8 data bits (LSB first) and a stop bit (1). On receive, the stop bit goes into RB8 in
Special Function Register SCON. The baud rate is variable.
Mode 2
In this mode, 11 bits are transmitted (through TxD) or received (through RxD) : A start
bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). On
Transmit, the 9th data bit (TB8 in SCON) can be assigned the value of 0 or 1. Or, for
example, the parity bit (P, in the PSW) could be moved into TB8. On receive, the 9th data
bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored. The
1 1
baud rate is programmable to either or the oscillator frequency.
32 64
Mode 3
In this mode, 11 bits are transmitted (through TxD) or received (through RxD) : A start
bit (0), 8 data bits (LSB first), a programmable 9th data bit and a stop bit (1). In fact,
Mode 3 is the same as Mode 2 in all respects except the baud rate. The baud rate in
Mode 3 is variable.
TM
In all four modes, transmission is initiated by any instruction that uses SBUF as a
destination register. Reception is initiated in Mode 0 by the condition RI = 0 and REN = 1.
Reception is initiated in the other modes by the incoming start bit if REN = 1.
The Table 19.3 summarizes the four serial port modes provided by 8051.
3 th
11-bit (start bit + 8 data bit + programmable 9 Variable.
data bit + stop bit).
If SMOD = 0, then K = 1.
If SMOD = 1, then K = 2. (SMOD is the PCON register)
Most of the time the user knows the baud rate and needs to know the reload value for
TH1. Therefore, the equation to calculate TH1 can be written as :
K ´ Oscillator frequency
TH1 = 256 –
384 ´ Baud rate
TM
TH1 must be an integer value. Rounding off TH1 to the nearest integer may not
produce the desired baud rate. In this case, the user may have to choose another crystal
frequency.
Since the PCON register is not bit addressable, one way to set the bit is logical ORing
the PCON register. (i.e. ORL PCON, #80H). The address of PCON is 87H.
The Table 19.4 shows the values to be loaded into TH1 to get the corresponding baud
rate. It also shows that the baud rates are doubled when SMOD = 1.
FD 9600 19,200
FA 4800 9600
F4 2400 4800
E8 1200 2400
To obtain the reload value for RCAP2H and RCAP2L the above equation can be
rewritten as :
Oscillator frequency
RCAP2H, RCAP2L = 65536 –
32 ´ Baud rate
TM
To set the SMOD bit : ORL PCON, #80H. The address of PCON is 87H.
Note : By changing SMOD bit in PCON from 0 to 1 we can double the baud rate in
8051.
ß Example 19.16 : 8051 uses 11.0592 MHz crystal. To get 9600 hertz baud rate how will
you program it for serial transmission ?
Solution : When 11.0592 MHz crystal is used and a standard baud rate of 9600 hertz is
required then, the setting of TH1 can be found as,
k ´ Oscillator frequency
TH1 = 256 –
384 ´ Baud rate
1 ´ 11.0592 ´ 10 6
= 256 – = 253 = FDH
384 ´ 9600
Program : MOV TMOD, #020 ; Initialize timer 1 in mode 2
MOV SCON, #4CH ; Initialize serial mode 1
ORL PCON, #80H ; Make SMOD = 1
MOV TH1, #FDH ; Load count
TM
ß Example 19.17 : Write an 8051 assembly language program to transfer letter "A"
serially at 9600 baud rate, continuously.
Solution :
MOV TMOD, #20H ; timer 1, mode 2 (auto reload)
MOV TH1, #FDH ; 9600 baud rate
MOV SCON, #50H ; 8-bit, 1 stop REN enabled
SETB TR1 ; start timer 1
START: MOV SBUF, #"A" ; Letter "A" to be transferred
HERE: JNB TI, HERE ; Wait for the last bit to
; transfer
CLR TI ; Clear TI for the next character
SJMP START ; Go to send the character again
ß Example 19.18 : Write an 8051 assembly language program to transfer the message
"HELLO"serially at 9600 baud, 8-bit data, 1 stop bit.
Solution :
MOV TMOD,#20H ; timer 1, mode 2
MOV TH1, #FDH ; 9600 baud rate
MOV SCON,#50H ; 8-bit, 1 stop bit, REN enabled
SETB TR1 ; start timer 1
START: MOV A, #"H" ; transfer "H"
ACALL TRANS
MOV A, #"E" ; transfer "E"
ACALL TRANS
MOV A, #"L" ; transfer "L"
ACALL TRANS
MOV A, #"L" ; transfer "L"
ACALL TRANS
MOV A,#"O" ; transfer "O"
ACALL TRANS ; Serial data transfer subroutine
TRANS: MOV SBUF, A ; Load SBUF
HERE: JNB TI, HERE ; wait for the last bit to
; transfer
CLR TI ; Clear TI for the next
; character
RET
TM
ß Example 19.19 : Write an 8051 assembly language program to receive bytes serially with
baud rate 9600, 8-bit data and 1 stop bit. Simultaneously send received bytes to port 2.
Solution :
MOV TMOD, #20H ; timer 1, mode 2 (auto reload)
MOV TH1, #FDH ; 9600 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; start timer 1
HERE: JNB RI, HERE ; wait for character receive
; completely
MOV A, SBUF ; save the received character
MOV P2, A ; send character to port 2
CLR RI ; Get ready to receive next byte
TM
ß Example 19.20 : Write a program to receive message from PC to 8051. Message string
is "Hello". After this micro controller sends message to PC "Fine".
Solution : The Fig. 19.32 shows the connections between 8051 and PC.
8051
TxD
(P3.1)
To PC
COM Port
RxD
(P3.0)
Fig. 19.32
MOV TMOD, #20H ; Initialize timer 1 in mode 2
MOV TH1, #0FDH ; Load count to get 9600 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; Start timer 1
MOV DPTR, #2000H ; Initialize memory pointer to
; save received data
MOV R0,#05H ; Initialize counter to read
; 5 characters
TM
P 3.0
RxD 3
R1 OUT R1 IN
P 3.1
TxD 2
T1 IN T2 OUT
R2 OUT R2 IN DB-9P
connector
RS 232C
T2 IN T2 OUT
0 IT0
INT0 IE0
1
TF0
0 IT1 Interrupt
INT1 IE1 sources
1
TF1
TI
RI
The serial port interrupt is generated by the logical OR of RI and TI. Neither of these
flags is cleared by hardware when the service routine is vectored to service routine.
In fact, the service routine will normally have to determine whether it was RI or TI that
generated the interrupt and the bit will have to be cleared in software.
TM
ß Example 19.21 : Write a program to enable serial interrupt, Timer 1 interrupt and
external hardware interrupt 0 (EX0)
Solution :
1 0 0 1 1 0 0 1
TM
ß Example 19.23 : Write a program to enable Timer 0 interrupt using bit manipulation
instructions.
Solution :
SETB IE.7 ; Enable interrupts
SETB IE.1 ; Enable timer 0 interrupt
(MSB) (LSB)
- IP.7 (Reserved)
- IP.6 (Reserved)
- IP.5 (Reserved)
PX0 IP.0 External interrupt 0 Priority control bit. Set/cleared by software to specify
high/low priority interrupts for INT0.
TM
Thus within each priority level there is a second priority structure determined by the
polling sequence, as follows :
ß Example 19.24 : Write an 8051 ALP that continuously read 8-bit data from port 2 and
sends it to port 0. At the same time it should generate square wave of 500 ms period on
port 1.0. Assume the crystal frequency = 11.0592 MHz.
Solution : We will use timer 0 in autoreload mode, i.e. mode 2. To generate square wave
of 500 ms we have to toggle port 1.0 pin after every 250 ms.
TM
11.0592 ´ 10 6
Timer clock frequency = = 921.6 kHz
12
» 26 = 1 AH
Program :
0RG 0000H
LJMP MAIN ; Avoid using memory space
; allocated to interrupt vector table
TM
interrupt was level-activated, then the external requesting source is what controls the
request flag, rather than the on-chip hardware.
If ITx = 0, external interrupt x is triggered by a detected low at the INTx pin. If
ITx = 1, external interrupt x is edge-triggered. In this mode if successive samples of the
INTx pin show a high in one cycle and a low in the next cycle, interrupt request flag IEx
in TCON is set. Flag bit IEx then requests the interrupt.
Since the external interrupt pins are sampled once each machine cycle, an input high
or low should hold for at least 12 oscillator periods to ensure sampling. If the external
interrupt is transition-activated, the external source has to hold the request pin high for at
least one machine cycle, and then hold it low for at least one machine cycle to ensure that
the transition is seen so that interrupt request flag IEx will be set. IEx will be automatically
cleared by the CPU when the service routine is called.
If the external interrupt is level-activated, the external source has to hold the request
active until the requested interrupt is actually generated. Then it has to deactivate the
request before the interrupt service routine is completed, or else another interrupt will be
generated.
ß Example 19.25 : Write an 8051 ALP to glow LED for a fraction of second when external
interrupt INT0 is activated.
Solution :
ORG 0000H
LJMP MAIN ; Avoid using memory space
; allocated to interrupt vector table
ORG 0003H
SETB P1.0 ; Turn ON LED
BACK : MOVE R2, #0FFH ; Load count
DJNZ BACK ; Decrement count and if not zero repeat
CLR P1.0 ; Turn OFF LED
RETI ; Return to main program
routine will normally have to determine whether it was RI or TI that generated the
interrupt, and the bit will have to be cleared in software.
Here, we are discussing interrupt based serial communication. In this case, the 8051
can perform other tasks in addition to serial communication, i.e. sending and receiving
data from serial communication port.
We know from Chapter 6 that transmit interrupt (TI) flag is set (=1) when the last bit
of the framed data (stop bit) is transmitted. This indicates that the SBUF register is ready
to transmit the next byte. The receive interrupt (RI) flag is set (=1) when the complete
frame of data (with stop bit) is received. RI indicates that the received byte needs to be
picked up before it is lost by new incoming serial data.
All the above concepts are applied equally using polling or an interrupt. Only
difference is in serving the serial communication needs. In polling method, the flag (TI or
RI) is monitored. The 8051 can not do anything else until this flag is set to high. This
problem is solved using interrupt method. When 8051 has received a byte or is ready to
send the next byte, the RI or TI flag respectively is set. Any other work can be performed
while the serial communication needs are served. There is a single interrupt set aside for
serial communication. If IE register (IE.4) is enabled, when RI or TI is set (= 1), the 8051 is
interrupted. When interrupted, the ISR written at 0023h is executed by 8051. In ISR, the TI
and RI flags must be examined to check which one caused the interrupt and according to
flag the response is given.
ß Example 19.26 : Write an 8051 ALP that continuously read 8-bit data from port 2 and
sends it to port 0. At the same time it should read incoming data from serial port at baud
rate 9600 and send it to port 1. Assume that crystal frequency = 11.0592 MHz.
Solution :
ORG 0000H
LJMP MAIN ; Avoid using memory space
; allocated to interrupt vector table
ORG 0023H
JNB RI, SKIP ; If RI is low goto skip
MOV A , SBUF ; Otherwise receive serial data
MOV P1, A ; Send it to port 1
CLR RI ; Clear RI
RETI ; Return to main program
SKIP : CLR TI ; Clear TI
RETI ; Return to main program
ORG 100H
MAIN : MOV P2, #0FFH ; Configure P2 as an input port
TM
ß Example 19.27 : Write an 8051 ALP that continuously read 8-bit data from port 2 and
sends it to port 0. At the same time it should transmit the same data on serial port. Assume
that crystal frequency = 11.0592 MHz.
Solution :
ORG 0000H
LJMP MAIN ; Avoid using memory space
; allocated to interrupt vector table
ORG 0023H
JNB TI, SKIP ; If TI is low goto SKIP
MOV SBUF, A ; Transfer data serially
CLR TI ; Clear TI
RETI ; Return to main program
SKIP : CLR RI ; Clear RI
RETI ; Return to main program
ORG 100H
MAIN : MOV P2, 0FFH ; Configure P2 as an input port
MOV TMOD, #20H ; Initialize timer 1 in mode 2
MOV TH1, #FDH ; Load count to get 9600 baud rate
MOV SCON, #40H ; Select serial mode
MOV IE,#10010000B ; Enable serial interrupt
SETB TRI ; Start timer 1
MOV A, P2 ; Read data from port 2
MOV SBUF, A ; Send the first byte serially
BACK : MOV A, P2 ; Read data from port 2
MOV P0, A ; Send it to port 0
SJMP BACK ; Repeat
END
TM
Review Questions
Section 19.1
Q.1 Explain the I/O ports of 8051. Dec.-08, Marks 8
Section 19.4
Q.1 Explain the timer/counter functional unit of microcontroller 8051 with relevant
diagrams. May-08, Marks 16
Q.2 Discuss in detail the on chip timers supported by 8051, bringing out the various modes
of operation of these timers. Dec-09, Marks 16
Q.3 Draw the TMOD register format and explain. Dec.-11, Marks 4
Section 19.5
Q.1 Describe the different modes of operation of timers in 8051.
Dec.-07,11, May-10,11, June-11, Marks 8
Section 19.6
Q.1 Write a note on counter programming of 8051.
Section 19.7
Q.1 Discuss the serial interface of 8051. May-05, Marks 8
Q.2 Discuss in detail, the hardware and software support provided by 8051 for serial
communication. Dec-09, Marks 16
Q.4 Draw the flowchart for programming of serial port of 8051. Dec.-11, Marks 2
Q.5 Explain the different serial communication modes in 8051. June-07, Marks 8
Section 19.8
Q.1 Explain the register IE format of 8051.
Q.2 Explain the interrupt structure of 8051 microcontroller and explain how interrupts are
prioritized. June-07, Dec.-07, Marks 8
Q.3 Explain the interrupt structure with the associated registers in 8051 microcontroller.
May-11, Marks 8
Q.4 Write 8051 ALP to transmit "Hello World" to PC at 9600 baud for external crystal
frequency of 11.0592 MHz. June-07, Marks 8
TM
Q.2 What is the function of SM2 bit in the SCON register of 8051 ? Dec.-07
Q.4 Write a delay routine for 1 millisecond using timer 0 of 8051 for 12 MHz
crystal frequency. June-07
TM
Q.7 How the RS -232C serial bus is interfaced to TTL logic device ?
Ans. : The RS-232C signal voltage levels are not compatible with TTL logic levels.
Hence for interfacing TTL devices to RS-232C serial bus, level converters are used. The
popularly used level converters are MC 1488 & MC 1489 or MAX 232.
Q.10 What register keeps track of interrupt priority in the 8051 ? Explain.
Ans. : Interrupt priority control registers keeps track of interrupt priority in the 8051.
Each interrupt source can also be individually programmed to one of two priority
levels by setting or clearing a bit in interrupt priority control register.
A low-priority interrupt can itself be interrupted by a high-priority interrupt, but
not by another low priority interrupt. A high-priority interrupt can't be interrupted by
any other interrupt source.
Q.11 What is the function of SM2 bit in the SCON register of 8051 ? Dec.-07
Q.13 Write the vector address and priority sequence of 8051 interrupts ? June-07
qqq
TM
Notes
TM
Contents
20.1 Keyboard Interface
20.2 Display Interface
20.3 Closed Loop Control of Servomotor
20.4 Stepper Motor Control
20.5 Washing Machine Control
(20 - 1)
TM
Output
Logic 0
Key Key
pressed pressed
Fig. 20.1 Bouncing of key switch
The problem of key bounce can be eliminated using key debounce technique, either
hardware or software.
Key position a b Y c d Y
A 0 0 1 1 1 0
B 1 1 0 0 0 1
Table 20.1
Fig. 20.2 shows the circuit diagram of key debounce. It consists of flip-flop. The output
of flip-flop shown in Fig. 20.2 is logic 1 when key is at position A (unpressed) and it is
logic 0 when key is at position B, as shown in Table 20.1. It is important to note that,
when key is in between A and B, output does not change, preventing bouncing of key
output. In other words we can say that output does not change during transition period,
eliminating key debouncing.
TM
+5 V
a Y
To input port
b
A
B
c Y
+5 V
Fig. 20.2
R R R R R R R R
K1
K2
K3
8051 K4
Input K5
port
(P1) K6
K7
K8
TM
Start
Are
No all keys
open
?
Yes
Wait for key
debounce (10 ms)
Read status
of keys
Is
No key
pressed
?
Yes
End
Key Keycode
D7 D6 D5 D4 D3 D2 D1 D0
K1 1 1 1 1 1 1 1 0
K2 1 1 1 1 1 1 0 1
K3 1 1 1 1 1 0 1 1
K4 1 1 1 1 0 1 1 1
K5 1 1 1 0 1 1 1 1
K6 1 1 0 1 1 1 1 1
K7 1 0 1 1 1 1 1 1
K8 0 1 1 1 1 1 1 1
Table 20.2
TM
code received from the input port. The following section explains the steps required to
identify pressed key.
Row 3
Row 2
Row 1
Row 0
+5 V
Column Column Column Column
3 2 1 0
R R R R
Row 3
Row 2
Data Input
bus port A Row 1
Row 0
Output port B
Data bus
TM
2. Read the status of return lines. If the status of all lines is logic high, key is not
pressed; otherwise key is pressed.
Check 2 :
1. Activate keys from any one column by making any one column line zero.
2. Read the status of return lines. The zero on any return line indicates key is pressed
from the corresponding row and selected column. If the status of all lines is logic
high, key is not pressed from that column.
3. Activate the keys from the next column and repeat 2 and 3 for all columns.
We will see how matrix keyboard can be connected to the 8051, a single chip
microprocessor/microcontroller. Fig. 20.7 shows the 4 ´ 4 matrix keyboard connected to the
port 1 of 8051. 4 lines of port 1 (P14-P17) are used as a scan lines and remaining 4 lines
(P10-P13) are used as return lines.
+5V
8 VCC
19 P17 7
XTAL1 P16
18 6
P15 5 Return
XTAL2 P14 4 lines
P13 3
P12 2
P11 C 8 4 0
1
P10
P 1.0
28
P27
27 D 9 5 1
P26
26
9 P25 P 1.1
RST 25
P24
24
P23 E A 6 2
23
P22
22
8051 P21
21
P 1.2
P20
31 F B 7 3
P07 32
EA/VCC
P06 33 P 1.3
P05 34
10 P04 35
11 RXD 36
TXD P03
P02 37
P01 38
P00 39 P 1.7 P 1.6 P 1.5 P 1.4
13 INT1 PSEN
29 Scan lines
12 INT0 30
ALE
15 T1 16
WR
14 T0 17
RD
4. Wait for key closure. Ground all scan lines by writing ‘0’ and then check if at least
one of return lines shows ‘0’ level.
Key pressed ? No step 4
Yes step 5
5. Call debounce. (allow sufficient time for debounce)
6. Is key really pressed ? (Ground all scan lines by writing ‘0’ and then check if
at least one of the return lines shows ‘0’ level.)
No step 4
Yes step 7
7. Find key code and display the key pressed on 7-segment display.
(By grounding one scan line at a time and checking return lines for any one line
to go to ‘0’ level. )
8. Go to step 1.
Program :
org lookup_table_address
db 30h, 31h, 32h, 33h, 34h, 35h, 36h, 37h,
38h, 39h, 41h, 42h, 43h, 44h, 45h, 46h
org program_start_address
beg: mov P1 #0f h ; configure lower 4 lines of port 1
; as i/p
mov dptr,#lookup_table_address ; initialise dptr with
; lookup_table_addr.
aga: mov a, P1 ;
anl a, #0fh ;
cjne a, #0fh,aga ; check for key released
1call delay ; call delay routine for key debounce
agal: mov a, P1
anl a, #0fh
cjne a,#0fh, go ; check for key pressed
ljmp agal
go: 1call delay ; call delay routine for key debounce
mov a, P1
anl a, #0fh
cjne a,#0fh, go1 ; is key really pressed ?
ljmp agal
go1: mov r1, # 01h ; initialise counter 1
mov r0, #0efh ; store word for column selection
mov r3, #04h ; initialise column counter
TM
TM
VCC
P1.0
LED
VSS VSS
Fig. 20.8
· We can use driver transistor to solve the problem of current sinking and
sourcing. The Fig. 20.9 shows the LED interface using driver transistor.
· In Fig. 20.9 (a), the source current of 8051 port pin is amplified by the npn
transistor to drive the LED. In Fig. 20.9 (b), the pnp transistor is used to amplify
the sinking current.
V - VLED V - VBE
R = CC RB = out
I LED (I LED / b)
R R
LED LED
RB
npn pnp
P1.0 P1.0
transistor transistor
RB
8051 8051
TM
ß Example 20.1 : Write an ALP to flash the LED connected to port P2.0.
Program 1 :
ORG 0000H
BACK : SETB P2.0 ; Make P2.0 high
ACALL Delay ; Wait for some time
CLR P2.0 ; Make P2.0 low
ACALL Delay ; Wait for some time
SJMP BACK ; repeat
Program 2 :
ORG 0000H
BACK : CPL P2.0 ; Complement P2.0
ACALL Delay ; Wait for some time
SJMP BACK
TM
a a a a a
f b f b f b f b f b
g g g g g
e c e c e c e c e c
h h h h h
d d d d d
(0) (1) (2) (3) (4)
a a a a a
f b f b f b f b f b
g g g g g
e c e c e c e c e c
h h h h h
d d d d d
(5) (6) (7) (8) (9)
Fig. 20.11
+VCC
A B C D E F G
TM
Static display
Fig. 20.13 shows a circuit to drive a
single, seven-segment, common anode +5 V +5 V
LED display. For common anode, when R
a
anode is connected to positive supply, a
b
low voltage is applied to a cathode to A 7 c
turn it on. Here, BCD to seven-segment BCD B 4 d
inputs C 4
decoder, IC 7447 is used to apply low 7 e
D
voltages at cathodes according to BCD f
input applied to IC 7447. To limit the GND g
current through LED segments resistors
are connected in series with the segments.
This circuit connection is referred to as a
static display because current is being Fig. 20.13 Circuit for driving single
seven-segment LED display
passed through the display at all times.
The value of the resistor in series with the segment can be calculated as follows :
We know, VCC – drop across LED segment – IR = 0
Drop across LED segment is nearly 1.5 V.
= 3.5 V
Each LED segment requires a current of between 5 and 30 mA to light. Let’s assume
that current through LED segment is 15 mA.
3.5V
\ R = = 233 W
15mA
In practice, the voltage drop across the LED and the output of IC 7447 are not exactly
predictable and the exact current through the LED is not critical as long as we don’t
exceed its maximum current rating. Therefore, a standard value 220 W can be used.
The static display circuits work well for driving just one or two LED digits. However,
these circuits are not suitable for driving more LED digits, say 8 digits. When there are
more number of digits, the first problem is power consumption. For worst case
calculations, assume that all eight digits with all segments are lit. Therefore, worst case
current required is
I = 8 (digits) ´ 7 (segment) ´ 15 mA (current per segment)
= 840 mA
A second problem of the static approach is that each display digit requires a separate
BCD to 7-segment decoder.
TM
Multiplexed display
To solve the problems of the static display approach, multiplexed display method is
used. Fig. 20.14 shows the 4 seven-segment displays connected using multiplexed method.
Here, common anode seven-segment LEDs are used.
+VCC
R
a a
P1.0 b b
Segment bus
A 7 c c
Output P1.1 B
4 d d
port P1.2
C 4
P1 P1.3 e e
D 7
f f
GND g g
R Q4 R Q3 R Q2 R Q1
P3.3
Output P3.2 +5 V
port P3.1
P3 P3.0
BCD code for digit 2 is then output to the port 1, and bit pattern to turn on digit 2 is
output on port 3. After 2 ms, digit 2 is turned off and the process is repeated for digit 3
and digit 4. After completion of turn for each digit, all the digits are lit again in turn.
With 4 digits and 2 ms per digit we get back to digit 1 every 8 ms or about 125 times
a second. This refresh rate is fast enough that, to our eye and due to persistence of vision
all digits will appear to be lit all the time.
In multiplexed display, the segment current is kept in between 40 mA to 60 mA so
that they will appear as bright as they would if not multiplexed. Even with this increased
segment current, multiplexing gives a large saving in power and hardware components.
ß Example 20.2 : Interface an 8-bit 7-segment LED display to 8051 through port 1 and
port 3 and write an 8051 assembly language program to display message on the display.
Solution : Hardware
The Fig. 20.15 shows the multiplexed 8-digit 7-segment LED display connected in 8051
system using port 1 and port 3. In this circuit port 1 and port 3 are used as a latch, i.e.
output port. Port 1 provides the segment data inputs to the display and port 3 provides a
means of selecting a display position at a time for multiplexing the displays. Here, instead
of BCD to seven-segment decoder (IC 7447) transistors are used to drive the LED
segments. Due to this we can also display HEX characters on the display; however in this
case we have to send the proper 7-segment code of a particular digit that is to be
displayed on the port 1. (See Fig. 20.15 on next page)
Note : This subroutine must be called continuously to display the 7-segment coded
message stored in the memory from address 6000H.
TM
+5 V
g
d
e
b
a
c
f
Qa
Qb
Qc
Qd
Qe
Qf
Qg
P3.1
P1.1
P1.0
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
P3.2
P3.0
P3.4
P3.3
P3.6
P3.7
P3.5
8051
Fig. 20.15
TM
1 VSS - Ground
7-14 DB0-DB7 I/O This 8-bit data bus is used to send information to the
LCD or read the contents of the internal registers of
the LCD.
TM
P3.3
P3.4
TM
TM
data on port A and apply high to low pulse of at least 450 ns of duration on E pin of the
module. The DD RAM stores the characters in their ASCII code whereas CG RAM stores
the character in its internally generated character code. Let us see the 8051 assembly
language program to display 'WELCOME' message on the LCD module. Before sending
command or data it is necessary to check busy flag, i.e. whether LCD is reading or not.
MAIN Routine :
MOV 81H,#30H ; Initialise stack pointer
MOV A,#3CH ; [Send command code to set font
; = 5 ´ 10 dots,
LCALL COMMAND ; DL = 8-bits and N = 2 lines].
MOV A,#0EH ; [Send command code to set display
LCALL COMMAND ; and cursor ON]
MOV A,#01H ; [Send command code to
LCALL COMMAND ; clear LCD]
MOV A,#86H ; [Send command to set DD RAM
LCALL COMMAND ; address to the seventh location]
MOV A,#'W'
LCALL DISPLAY ; Display letter W
MOV A,#'E'
LCALL DISPLAY ; Display letter E
MOV A,#'L'
LCALL DISPLAY ; Display letter L
MOV A,#'C'
LCALL DISPLAY ; Display letter C
MOV A,#'O'
LCALL DISPLAY ; Display letter O
MOV A,#'M'
LCALL DISPLAY ; Display letter M
MOV A,#'E'
LCALL DISPLAY ; Display letter E
SJMP HERE:HERE ; Loop here after displaying
; message
COMMAND Routine :
LCALL READY ; Check whether LCD is ready ?
MOV P1, A ; Issue command code
CLR P3.2 ; Make RS = 0 to issue command
CLR P3.3 ; Make R/W = 0 to enable writing
SETB P3.4 ; Make E = 1
TM
READY Routine :
CLR P3.4 ; Disable display
CLR P3.2 ; Make RS = 0 to access command register
MOV P1,#0FFH ; Configure P1 as an input port
SETB P3.3 ; Make R/W = 1 to enable reading
READ: SETB P3.4 ; Make E = 1
JB P1.7,READ ; Check DB7 bit. If it is 1, LCD is busy
; hence check if until it is 0
CLR P3.4 ; Make E = 0 to disable display
RET ; Return
DISPLAY Routine :
LCALL READY ; Check whether LCD is ready ?
MOV P1, A ; Issue data
SETB P3.2 ; Make RS = 1 to issue data
CLR P3.3 ; Make R/W = 0 to enable writing
SETB P3.4 ; Make E = 1
CLR P3.4 ; Make E = 0
RET ; Return
TM
Microcontroller
DAC
f1
Digital Encoder
f2 Encoder
controller counter
Fig. 20.17 Microcontroller based closed loop servo motor control circuit
TM
Sequential encoders produce quadrature pulse trains, from which position, speed, and
direction of the motor rotation can be derived. The frequency is proportional to speed and
each transition of f1 and f2 represents an increment of position. The phase of the signals is
used to determine direction of rotation.
These encoder signals are usually decoded into Count Up and Count Down pulses.
These pulses are then routed to an N-bit, up/down counter whose value corresponds to
the position of the motor shaft. The decoder/counter may be implemented in hardware,
software, or a combination of the two.
Stepper
X1 motor
P1.0
7407 X2
Y1 Y2
P1.1 +12V
7407
P1.2
7407
P1.3
7407
phases, with center-tap winding. The center taps of these windings are connected to the
12 V supply. Due to this, motor can be excited by grounding four terminals of the two
windings. Motor can be rotated in steps by giving proper excitation sequence to these
windings. The lower nibble of port 1 of the 8051 is used to generate excitation signals in
the proper sequence.
The Table 20.5 shows typical excitation sequence. The given excitation sequence rotates
the motor in clockwise direction. To rotate motor in anticlockwise direction we have to
excite motor in a reverse sequence. The excitation sequence for stepper motor may change
due to change in winding connections. However, it is not desirable to excite both the ends
of the same winding simultaneously. This cancels the flux and motor winding may
damage. To avoid this, digital locking system must be designed. Fig. 20.19 shows a simple
digital locking system. Only one output is activated (made low) when properly excited;
otherwise output is disabled (made high).
X1
X'1
X2
X'2
Step X1 X2 Y1 Y2
1 0 1 0 1
2 1 0 0 1
3 1 0 1 0
4 0 1 1 0
1 0 1 0 1
TM
Step X1 X2 Y1 Y2
1 0 1 0 1
2 0 0 0 1
3 1 0 0 1
4 1 0 0 0
5 1 0 1 0
6 0 0 1 0
7 0 1 1 0
8 0 1 0 0
1 0 1 0 1
TM
ß Example 20.3 : Write an 8051 assembly language program to control stepper motor using
connections given in Fig. 20.18.
Solution :
MOV R0 # COUNT ; Initialize rotation count
AGAIN : MOV DPTR, #ETC ; Initialize pointer to
; excitation code table
MOV R1, #04 ; Initialize counter to excitation
; code sequence
ß Example 20.4 : Write assembly language program to control conveyer belt using stepper
motor and 8051 controller. Belt moves continuously at rate of 1 step/sec. but stops for 5 sec.
when external interrupt occurs and then continues to move.
Solution :
MAIN : MOV IE, #1000 0001B ; Enable external interrupt 0
AGAIN: MOV DPTR,#ETC ; Initialize pointer to
; excitation code table
MOV R1, #04 ; Initialize counter to excitation
; code sequence
BACK: MOVX A, @DPTR ; Get the excitation code
MOV P1, A ; send the excitation code
MOV A,#14H ; Initialize count = 20
LCALL DELAY ; Wait for 1sec
INC DPTR ; Increment pointer
DJNZ R1, BACK ; Decrement R1
; if not zero goto BACK
SJMP AGAIN ; Repeat
ORG 3000H
ETC DB 03H,06H, 09H, 0CH ; Code sequence for
; Clockwise rotation
TM
Delay Routine
DELAY: MOV TMOD, #01 ; Time 0, mode1 (16-bit mode)
MOV R0,A ; Read count and Initialize
BACK: MOV TL0,#B0H ; TL0=B0H, the low byte
MOV TH0,#3CH ; TH0=3CH, the high byte
SETB TR0 ; Start the timer 0
RERE: JNB TF0,REPE ; Check timer 0 flag until
; it rolls over
CLR TR0 ; Stop timer 0
CLR TF0 ; clear timer 0 flag
DJNZ R0, BACK ; Decrement counter and
RET ; if not zero repeat
TM
skip the second rinse. Finally, program advances directly to spin skipping the Graduated
spin.
The Fig. 20.21 shows the flowchart of washing machine operation.
Start
1
Prewash
0 Prewash
Main wash 1
0
Woolen
1 Main wash 2
Rinse 1
0
Woolen
1 Rinse 2
0
Woolen
1 Graduated spin
Spin
Stop
TM
Inputs
Start P0.0
Prewash Input P0.1
Cloth Types : 0 : Cotton 1 : Woolen P0.2
Output
Hardware Interface
VCC
Start
P0.0
R
VCC
P0.1
P0.2
R R
8051
Program :
SMRT : JNB P 0.0, START ; check for star
JNB P 0.1, SKIPPW ; check if prewash is activated
SETB P 1.0, ; if yes do prewash
CALL D_PREWASH ; wait for prewash
CLRB P 1.0 ; stop prewash
SKIP PW : SETB P 1.1 ; Do main wash 1
CALL D_MAINWASH 1 ; Wait for main wash 1
CLRB P 1.1 ; stop main wash 1
JNB P 0.2, SKIPMW 2 ; check if cloth type is cotton
SETB P 1.2 ; if yes do main wash 2
CALL . D_MAINWASH 2 ; wait for main wash 2
CLRB P 1.2 ; stop main wash 2
SKIP MW 2 :SETB P 1.3 ; Do rinse 1
CALL D_RINSE 1 ; wait for rinse 1
CLRB P 1.3 ; stop rinse 1
JNB P 0.2, SKIPRINSE 2 ; check for cloth type is cotton
SETB P 1.4 ; if yes do rinse 2
CALL D_RINSE 2 ; wait for rinse 2
CLRB P 1.4 ; stop rinse 2
JNB P 0.2, SKIP GS ; check for cloth type is cotton
SETB P 1.5 ; if yes do gradual spin
CALL D_GS ; wait for gradual spin
CLRB P 1.5 ; stop gradual spin
SKIP GS : SETB P 1.6 ; Do spin
CALL D_SPIN ; wait for spin
CLRB P 1.6 ; stop spin
LJMP START ; Go to start
Review Questions
Section 20.1
Q.1 With a neat circuit diagram explain how a 4 × 4 keypad is interfaced with 8051
microcontroller and write 8051 ALP for keypad scanning. May-07,08,11, Marks 16
Q.2 Interface an 8 × 8 keyboard using 8255 ports and write a program to read the code of a
pressed key. Dec.-10, Marks 8
TM
Section 20.2
Q.1 Interface a 20 × 2 LCD with 8051 microcontroller and write assembly language
program to display the following message in it at the middle.
HELLO ! ALL
ARE WELCOME Dec.-07, Marks 16
Q.2 Explain the LCD display interfacing with microcontroller 8051. Dec.-08, Marks 16
Section 20.3
Q.1 Explain the servomotor control using 8051 microcontroller. May-11, Marks 8
Section 20.4
Q.1 Draw the schematic for interfacing a stepper motor with 8051 microcontroller and
write 8051 ALP for changing speed and direction of motor. June-07, Marks 16
Q.3 Write an assembly program in 8051 to rotate the stepper motor in clock wise and anti
clockwise direction. June-09, Dec.-11, Marks 16
Q.4 Write a program to generate pulses to drive and for continuous operation of a stepper
motor. June-12, Marks 8
Section 20.5
Q.1 How 8051 is used in washing machine control ? Dec.-11, June-11, Marks 8
Q.2 Explain with a neat diagram the application of 8051 microcontroller in Washing
Machine control. June-12, Marks 16
Ans. : To isolate power circuit of stepper motor and control circuit opto-isolator is
needed. It physically separates control circuit ground and power circuit ground
connections. Due to such connection, the operation of control circuit is not affected by
high current spikes in the power circuit.
qqq
TM
Notes
TM