Professional Documents
Culture Documents
"ICHII tDVCATION
Microprocessors
and
Microcontrollers
Microprocessors
and
Microcontrollers
N Senthil Kumar
Professor
Department of Electrical and Electronics Engineering
Mepco Schlenk Engineering College
Sivakasi, Virudhunagar Dt.
Tamil Nadu
N\ Saravanan
Professor
Department of Electrical and Electronics Engineering
Thiagarajar College of Engineering
Madurai
Tamil Nadu
S Jeevananthan
Assistant Professor
Department of Electrical and Electronics Engineering
Pondicherry Engineering College
Puducherry
OXFORD
UNIVERSITY PRESS
OXFORD
UNIVERSITY PRESS
Published in India by
Oxford University Press
YMCA Library Building, 1 Jai Singh Road, New Delhi 110001, India
ISBN-13: 978-0-19-806647-7
ISBN-10: 0-19-806647-3
N. Senthil Kumar
M. Saravanan
S. JEEVANANTHAN
PREFACE
KEY FEATURES
• One-text resource covering the 8085, 8086, and 8051 and an introduction to the
8096 processors
• Exhaustive programming examples (assembly language codes), which would
not only help students hone their programming skills but also allow faculty
members to use select portions for their laboratory
• A section on C programming applied to the 8051 processor
• Application examples (case studies) on traffic light control, thermometer,
elevator control, and washing machine control to appreciate the general design
process
• Simple theoretical questions, programming questions, and application sets
(‘Think and answer’ type questions) at the end of all the relevant chapters
• Extensive coverage of advanced Intel processors including the Pentium
processors, PowerPC, and PIC16F877 microcontroller
• Accompanied by a CD containing ALP codes and simulation-based learning
for select examples discussed in the book
and programming of the 8085. The third part explains the hardware and software
details of Intel’s 8-bit 8051 microcontroller series. The hardware and software
details of Intel’s 16-bit microprocessor, the 8086, form the fourth part. The fifth
part includes the details of Intel’s 16-bit microcontroller, the 8096. The last part
covers the recent developments in microprocessor-based systems, other advanced
microprocessors, and the PIC16F877 microcontroller.
Chapter 1 of the book contains an introduction to microprocessors, a list
of various terms related to microprocessor technology, and the evolution of
microprocessors.
Chapter 2 introduces and explains the details of Intel’s 8085 microprocessor,
along with its basic architecture and pin details.
Chapter 3 details the complete instruction set of the 8085 processor and the
related instruction format, addressing modes, and classification.
Chapter 4 discusses numerous 8085 example programs.
Chapter 5 explains the basic data transfer mechanism between the processor
and the peripheral, along with the interrupt structure of the 8085 processor.
Chapter 6 covers the methods of interfacing the memory and input/output
devices with the 8085 processor.
Chapter 7 handles the interfacing of some of the programmable peripheral
devices manufactured by Intel with the 8085 processor.
Chapter 8 gives an overview of a complete 8085-based system, including the
address map, a general microcomputer system, and other supporting devices.
Chapter 9 introduces Intel’s 8-bit microcontroller, the 8051, with details of its
internal architecture and memory organization.
Chapters 10 and 11 cover the software and internal hardware details of the
8051 microcontroller.
Chapter 12 explains the interfacing of the 8051 with a few basic peripherals
and includes numerous solved examples. This chapter also covers RTC interfacing
using the I2C standard.
Chapter 13 introduces the architecture, memory locations, and pin details of
Intel’s 16-bit processor, the 8086.
Chapter 14 discusses the addressing modes, instruction set, and programming
of the 8086.
Chapters 15 and 16 cover the interrupt structure of the 8086 and memory and
I/O interfacing (inclusive of printer and CRT terminal) with the 8086.
Chapters 17 deals with multiprocessor configuration, its advantages, need, bus
arbitration, and interconnection topologies.
Chapter 18 describes complete 8086-based systems.
Chapter 19 presents the basic features of Intel’s 16-bit 8096 microcontroller
series.
Chapter 20 addresses the programming of the 8096 microcontroller.
Chapter 21 details the internal hardware of the 8096.
Chapter 22 discusses the recent trends and developments in microprocessor
technology and the use of high-level language programming with the 8051
microcontroller, along with a few examples in C language.
Vijj PREFACE
IN CHE CD
• Simulator for the 8085 processor, from http://gnusim8085.org. Students can
execute in this simulator the 8085 assembly language codes given in the book.
They can execute each instruction and see how it affects the various registers,
the memory, and the flags. This will help in understanding the instruction sets
and algorithms used in the programming examples.
• Assembly language codes for 8085 from Chapter 4, which can be readily
executed in the simulator
• Assembly language codes for 8051 and 8086, from Chapters 10 and 14,
respectively, which can be run in any executable environment
Students and faculty members are requested to go through the readme.mht file
(autorun enabled), which provides guidelines for installation and utilization of the
simulator.
ACKNOWLEDGEMENTS
The authors acknowledge the authorities of their respective institutions for having
permitted them to embark on this mission and also for providing the necessary
support. Special thanks to Mr Sridhar Ratnakumar, Activestate Software Inc.,
Canada, for having permitted us to use the 8085 simulator developed by him in
the CD accompanying the book. During the course of development of this book,
we were hardly able to spare any time for our families. Their support has made this
book possible. The support rendered by the editorial team of Oxford University
Press is gratefully acknowledged. We would also like to thank all the reviewers
who gave their valuable comments on the manuscript at its draft stage,It is with
devotion that we recall the blessings of the Almighty for having been with us in
our efforts to come out with this book.
While we have tried our best to make the book error-free, readers are encouraged
to write to us with feedback, suggestions, and comments for improving the contents
of the book.
N. Senthil Kumar
M. Saravanan
S.Jeevananthan
BRIEF CONTENTS
Preface v
1. Microprocessors—Introduction and Evolution 1
Preface v
Index 7^1
CHAPTER 1 |
MICROPROCESSORS—
INTRODUCTION AND EVOLUTION
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Importance of microprocessors
• Origin and evolution of microprocessors
• Classification of microprocessors and memories
• Common input and output devices for computers
• Bus structures used in computers and technology improvements
1.1 INTRODUCTION
The microprocessor is an electronic chip that functions as the central processing
unit (CPU) of a computer. In other words, the microprocessor is the heart of any
computer system. Microprocessor-based systems with limited resources are called
microcomputers. Today, microprocessors can be found in almost all consumer
electronic devices such as computer printers, washing machines, microwave ovens,
mobile phones, fax machines, and photocopiers and in advanced applications such
as radars, satellites, and flights. Any middle-class household will have about a
dozen microprocessors in different forms inside various appliances. The recent
developments in the electronics industry and the large-scale integration of devices
have led to rapid cost reduction and increased application of microprocessors and
their derivatives.
Typically, basic microprocessor chips have arithmetic and logic functional units
along with the associated control logic to process the instruction execution. Almost
all microprocessors use the basic concept of stored-program execution. Programs
or instructions to be executed by the microprocessor are stored sequentially in
memory locations. The microprocessor, or the processor in general, fetches the
instructions one after another and executes them in its arithmetic and logic unit. So
all microprocessors have a built-in memory access and management part as well
as some amount of memory.
A microprocessor can be programmed to perform any task that can be written
and programmed by the user. Without a'program, the microprocessor unit is
a piece of useless electronic circuit. The programmer must take care of all the
resources of the microprocessor and use them efficiently for implementing the
required functionality. So to work with the microprocessor, it is necessary for the
programmer to know about its internal resources and features. The programmer
2 MICROPROCESSORS AND MICROCONTROLLERS
must also understand the instructions that a microprocessor can support. Every
microprocessor has its own associated set of instructions; this list is given by all
microprocessor manufacturers. The instruction set for microprocessors is in two
forms—one in mnemonic, which is comparatively easy to understand and the other
in binary machine code, which the microprocessor works with and is difficult
for us to understand. Generally, programs are written using mnemonics called
assembly-level language and then converted into binary machine-level language.
This conversion can be done manually or using an application called assembler.
In general, programs are written by the user for the microprocessor to work with
real world data. Data are available in many forms and from many sources. To input
these data to the microprocessor, the microprocessor-based systems need some
input interfacing circuits and some electronic processing circuits. These circuits
include data converters and ports. After processing the real world data, the output
from the microprocessor must be taken out to give to the output devices or circuits.
This again needs interfacing circuits and ports. So a microprocessor-based system
will need a set of memory units and interfacing circuits for inputs and outputs.
The circuits, together with the microprocessor, make the microcomputer system.
The physical components of the microcomputer system are called hardware. The
program that makes this hardware useful is called software.
The semiconductor manufacturing technology for chips has developed from
transistor-transistor logic (TTL) to complementary metal-oxide-semiconductor
(CMOS). Microprocessor manufacturing also has gone through these technological
changes. The other semiconductor manufacturing technology available is emitter-
coupled logic (ECL). TTL technology is most commonly used for basic digital
integrated circuits; CMOS is favoured for portable computers and other battery-
powered devices because of its low power consumption.
Chip A chip or an integrated circuit is a small, thin piece of silicon with the
required circuits and transistors etched on it to perform a particular function.
Simpler processors may consist of a few thousand transistors etched onto a silicon
base just a few millimeters square.
Bit A bit means a single binary digit. The bit is also the fundamental storage
unit of computer memory. In binary form, a bit can have only two values, 0 or 1,
whereas a decimal digit can have 10 values, represented by symbols 0 through 9.
Bit size The bit size of a microprocessor refers to the number of bits that can be
processed simultaneously by the basic arithmetic circuits of the microprocessor.
Word A word is a number of bits grouped together for processing. In
microprocessors, a word refers to the basic data size or bit size that can be processed
MICROPROCESSORS—INTRODUCTION AND EVOLUTION 3
by the arithmetic and logic unit (ALU) of the processor. A 16-bit binary number is
called a word in a 16-bit processor.
Memory word The number of bits that can be stored in a register or memory
element is called memory word. Mostly, all memory units use eight bits for their
memory word.
Byte An 8-bit word is referred to as a byte.
Nibble A 4-bit word is referred to as a nibble.
Kilobyte A collection of 1024 bytes is called a kilobyte (210 bytes).
Megabyte A collection of 1024 kilobytes is called a megabyte (220 bytes).
RAM or R/W memory Random access memory or read/write memory is a type
of semiconductor memory in which a particular memory location can be erased
and written with new data at any time. These memory units are volatile, which
means that the contents of the memory are erased when the power to the chip is
disrupted. The access of the individual memory location can be done randomly. In
microprocessors, the RAM is used to store data.
DRAM Dynamic random access memory is a semiconductor memory in which
the stored contents need to be refreshed repeatedly at about thousands of times per
second. Without refreshing, the stored data will be lost. These memory chips are
preferred in a computer system as these are slower but economical.
SRAM Static random access memory chips keep the data stored in it as long as
power is available. There is no need for refreshing. In terms of speed, SRAM is
faster.
ROM Read only memory devices are memory devices whose contents are
retained even after removing the power supply.
Arithmetic and logic unit ALU is a digital circuit present in the microprocessor
to perform arithmetic and logic operations on digital data. The typical operations
performed by the ALU are addition, subtraction, logical AND, logical OR, and
comparison of binary data. Generally, the functions of the ALU of a microprocessor
will decide the processor’s functionality.
BIOS Basic input/output system is a set of programs that handles the input
and output functions and interacts with the hardware directly. A new hardware
installed must be provided with the corresponding BIOS routines.
Clock The circuit in the computer that generates the sequence of evenly spaced
pulses to synchronize the activities of the processor and its peripherals is called
clock. The clock speed determines the speed of the operation of the computer. The
computer with a high frequency clock works faster. Normally the clock frequency
is in the range of megahertz (MHz) or gigahertz (GHz).
MIPS Million instructions per second is a measure of the speed at which the
instructions are executed in a processor.
Tri-state logic It is the logic used by digital circuits. The three logic levels used
are high (1), low (0), and high impedance state (Z). The logic high state of a digital
circuit can source current and the logic low can sink current in a computer system,
but the high impedance state neither sources nor sinks current and so the other
devices connected to it are not affected.
Operating system The program that controls the entire computer and its
resources and enables users to access the computer and its resources is called
operating system. It is required for any computer system to become operational and
user friendly. Under the control of the operating system, the computer recognizes
and obeys commands typed by the user. In addition, the operating system provides
built-in routines that allow the user’s program to perform input/output operations
without specifying the exact hardware configuration of the computer. In low-level
microprocessor-based systems, the program that controls the hardware is called
monitor routine or monitor software.
as memory devices and I/O ports to connect the input and output devices. All
microprocessor-based systems need two types of memories—RAM and ROM.
RAM is used for storage of data while ROM is used for storage of programs,
especially the start-up program that runs when the microprocessor is powered on.
There are numerous microprocessors developed by many companies. The
evolution of microprocessors, from4-bit microprocessors to 64-bit microprocessors,
has been discussed later in this chapter. This book is devoted to the discussion
of two groups of microprocessors— Intel’s 8-bit 8085 microprocessor series and
16-bit 8086 series.
Microcontrollers are microprocessors designed specially for control applications.
Microcontrollers contain memory units and I/O ports inside a chip, in addition to
the CPU. Microcontrollers are otherwise called embedded controllers; they are
generally used to control and operate smart machines. Some of the machines using
microcontrollers are microwave ovens, washing machines, sewing machines,
automobile ignition systems, computer printers, and fax machines. You will be
amazed to know that out of 100 processor chips manufactured, 99 are embedded
processors; only one goes into a general computer! A plethora of semiconductor
companies are in the microcontroller market and any application development
engineer is flooded with a variety of microcontrollers to choose from. This book
is confined to Intel’s 8-bit 8051 series and 16-bit 8096 series microcontroller
families.
' ■ ■ ’ ■ ■- ■■ ■■ ■ -
Figure 1.2 shows a typical personal computer system. The interfacing of the
processor with the other parts of the microcomputer system needs a three-bus
architecture. The three buses are data bus, address bus, and control bus.
Each memory location or I/O port is identified by a specific address similar to
a postal address. In microprocessor systems, the addresses are all in binary, and in
general, represented in hexadecimal number format. The address is a unique pattern
MICROPROCESSORS—INTRODUCTION AND EVOLUTION 7
Primary memory is the storage area from which all the programs are executed.
All the programs and corresponding data for execution must be within the primary
memory. The primary memory is much larger than the processor memory and the
cache memory but its operating speed is slower. The primary memory in a system
varies from few KB to a few MB.
Secondary memory refers to the storage medium for huge files such as program
source codes, compilers, operating systems, etc. These are not accessed directly or
very frequently by the microprocessor in a computer system. Secondary memory
consists of slow devices such as magnetic tapes and optical disks. Sometimes, they
are referred to as auxiliary or backup store. Stored information in a magnetic tape
or magnetic disk is not lost when power is turned off. Therefore, these storage
devices are called non-volatile memories.
Classification of primary memory Primary memory normally includes ROM
and RAM, which are further classified as shown in Fig. 1.3. Microprocessor-based
systems have at least one RAM and one ROM chip.
Primary memory
RAM devices allow both reading and writing to their memory cells. In static
RAM devices, bits are stored as the status of on/off switches. There are no charges
involved and hence, no charges to leak. However, static RAM devices have complex
construction and hence larger size per unit storage. So they are more expensive.
Static RAMs are comparatively faster and are used in cache memories.
In dynamic RAM devices, the data bits are stored as charge in capacitors. Since
capacitor charge has a tendency to leak, these devices need refreshing even when
they are powered. However, they have simpler construction and smaller size per
unit storage. These devices are less expensive and comparatively slower.
As the name implies, a ROM permits only read access. There are many kinds
of ROMs:
(i) Mask programmable ROMs (MPROMs) are custom-made for the customer;
their contents are programmed by the manufacturer. Since they are mass
produced, they are inexpensive. The customer cannot erase or program it
afterwards.
MICROPROCESSORS—INTRODUCTION AND EVOLUTION 13
POINTSTOREMEMBER’
• The microprocessor is an electronic circuit that functions as the central processing unit
(CPU) of a computer, providing computational control.
• The microprocessor is the controlling element in a computer system. The microprocessor
performs data transfers, does simple arithmetic and logical operations, and makes simple
decisions.
• The basic operation of the microprocessor is to fetch instructions stored in the memory
and execute them one by one in sequence.
• Microprocessors are used in almost all advanced electronic systems.
• Microcontrollers are advanced forms of microprocessors, with memory and ports
present within the chip.
• A microcomputer system is made by interfacing memory and I/O devices to a
microprocessor.
• Microprocessor evolution is classified into five generations. The processors that are
currently in use belong to the fifth generation.
REVIEW QUESTIONS/
INTEL 8085
ARCHITECTURE AND
PROGRAMMING
2.1 INTRODUCTION
The microprocessor is a semiconductor device consisting of electronic logic
circuits manufactured using either large-scale integration (LSI) or very large-scale
integration (VLSI) technique. It basically contains registers, an arithmetic and
logic unit, flip-flops, and timing and control circuits. All microprocessors work
using Von-Neumann architecture. In this architecture, the CPU or the processor
fetches instructions from the memory, decodes it (i.e., interprets the nature of
the instruction/command and develops clock-synchronized steps for execution),
generates appropriate control signals, and finally executes it. The program is
stored in consecutive memory locations. The execution steps are repeated for all
the instructions of the program until the execution is terminated by hardware or
software. The data required may be taken either from memory or from input ports;
the results of the program may be either stored in the memory or transferred out
through output ports.
A program is a list of instructions for the microprocessor to execute. Before
the start of execution, the complete program must be stored in the memory. Let us
assume that the starting address of the stored program is 88OOH. While running
the program, the microprocessor must be directed to ‘go’ from 88OOH. Once it has
executed the instruction in 88OOH, it goes to the next address 8801H (assuming
single-byte instructions) and so on until it reaches the end of the program.
Intel 8085 is an 8-bit microprocessor manufactured by Intel Corporation and
is usually called a general-purpose 8-bit processor. It is upward compatible with
microprocessor 8080, which was Intel’s earlier product. There are several faster
versions of the 8085 microprocessor such as 8085AH, 8085AH-1, and 8085AH-2.
A microprocessor system consists of three functional blocks—central
processing unit (CPU), input and output units, and memory units, as shown in
Fig. 2.1. The CPU contains several registers, an arithmetic and logic unit (ALU),
20 MICROPROCESSORS AND MICROCONTROLLERS
Memory
Processor
and a control unit. The function of ALU, as the name implies, is to perform
arithmetic and logical operations. The control unit translates the instructions and
executes the desired task.
and comparison (CMP) are the arithmetic operations possible in the 8085
microprocessor. The possible logical operations are AND (AND), OR (OR),
exclusive OR (EXOR), complement (CMA), etc.
The ALU of the 8085 processor is called accumulator-oriented ALU as one of
the data used in arithmetic and logic operations must be stored in the accumulator.
The other data is taken from a memory location or register. The results of the
arithmetic and logical operations are stored in the accumulator. If the operation
needs only one data, that data must be stored in the accumulator.
Any flag register bit is said to be ‘set’ when its value is 1 and ‘cleared’ when its
value is 0. The most commonly used flags are zero, carry, and sign. AC flag cannot
be accessed externally.
Sign flag (S) The sign flag is just a copy of the bit D7 (most significant bit—
MSB) of the accumulator. A negative number has a 1 in bit 7 and a positive
number has a 0 in 2’s complement representation. This flag indicates the sign of
the number. (It may be recalled that signed magnitude numbers use 1 to indicate
a negative number and 0 to indicate a positive number.) This flag can be used in
signed arithmetic operations.
Zero flag (Z) The zero flag is set if an arithmetic operation results in a zero. It
sets^Le-, it changes to binary 1 if the result in the accumulator is zero; if not, it
remains reset, i.e., at binary 0.
y.
Carry flag (C) The carry flag is set when a carry is generated in the process
of an arithmetic operation in the accumulator. When addition is carried out, it
sometimes results in a ninth bit being carried over to the next byte. The C flag
copies the value of the carry, which is an extra bit, from D7. It also reflects the
value of the borrow in subtractions.
Auxiliary carry flag (AC) The auxiliary carry flag is set when an auxiliary
carry is generated in the process of an arithmetic operation in the accumulator,
i.e., when a carry results from bit D3 and passes on to D4 (from the lower nibble ( >
to theJiigher.nibble). This carry is also called half-carry. It may also occur in the
process of a subtraction operation. In other words, this flag is set if the subtraction
operation results in borrow.
.
Parity flag (P) The parity flag is set if the content of the accumulator after an
arithmetic operation has an even number of Is. Otherwise, the parity flag is reset.
It is set for operation in the even parity mode.
2.2.3.3 Program Counter (PC)
PC is a register that always points to the address of the next instruction
to be executed. In other words, this register is used to sequence the execution of
the instructions. After execution of every instruction, the content of the memory
location indicated by the PC is moved to the instruction register and the PC is
loaded with the next address. It keeps track of a program by counting the memory
address from which the next byte is to be fetched, and hence the name program
counter.
2.2.3.4 Stack Pointer (SP)
Stack is an array of memory locations organized in last-in, first-out (LIFO) or first-
in, last-out (FILO) fashion. It is accessed using a 16-bit pointer register called stack
pointer, which holds the address of the memory location of the top of the stack.
The programmer can reserve and allocate a series of RAM locations to be used
as a stack and accordingly initialize the stack pointer. The range of stack memory
locations must be chosen carefully so that it does not affect the program space.
In all microprocessor-based systems, the stack is mainly used to store the return
24 MICROPROCESSORS AND MICROCONTROLLERS
address of the main program when a subroutine is called. While the programmer
uses the stack for storage and retrieval of data, the microprocessor uses the stack
during subroutine calls. Care must be taken by the programmer to ensure that the
data stored in the stack is retrieved properly, so that the data stored in the stack by
the processor is not affected.
later part. To separate the address from the data, a latch is used externally to save
the address before the function of the bits changes.
2.2.5.3 Control Bus
The control bus carries control signals_that are partly unidirectional and partly
bidirectional. For a microprocessor to function correctly, these control signals are
vital. The control bus typically consists of a number of single lines that coordinate
and control microprocessor operations. For example, a read/write control signal
will indicate whether memory is being written into or read from. Thus, they are
individual lines that provide a pulse to indicate the operation of the microprocessor.
In fact, the microprocessor generates specific control signals for every operation,
which in turn are used to identify the type of device the processor intends to
communicate with. The following points describe the control and status signals
of the 8085 processor:
(i) ALE (output): Address Latch Enable is a pulse that is provided when an
address appears on the AD0-AD7 lines, after which it becomes 0. This
signal can be used to enable a latch to save the address bits from the AD
lines, thereby de-multiplexing the address bus and data bus.
(ii) RD (active low output): The Read signal indicates that data are being read
from the selectedj/Q or memory device and that they are available on the
data bus.
(iii) WR (active low output): The Write signal indicates that the data on the data
bus are to be written into a selected memory or I/O location.
(iv) IO/M (output): It is a signal that distinguishes between a memory operation
and an I/O operation. An active low on this signal shows it is ajnemory
operation (IO/M = 0) and a high on this line indicates an I/O operation (IO/
M=l). Table 2.1 Status signals and
(v) SI and SO (output): These are status associatedoperations
signals used to specify the kind of S1 so States
operation being performed. The status
signals combine with I/O signals to 0 0 Halt
govern various operations; they are \ 0 1 Write
1 0 Read
listed in Table 2.1. If both SO and SI 11 11 retcn
are low, the operation of the processor _
tends to halt. If SO is low and S1 is high, the processor reads data. If SO is
high and SI is low, the processor writes data onto a memory or I/O device.
If both SO and SI are high, the fetch operation is performed.
The schematic representation of the 8085 bus structure, shown in Fig. 2.5, explains
how the movement of data within the computer is accomplished by a series of
buses. Address information, data, and control signals have to be carried around
inside the microprocessor as well as in the external system. Hence, the buses are
present both internally and externally.
(vi) Interrupts: These signals are used to make the microprocessor respond to high
priority externally initiated signals. When an interrupt signal is detected by the
processor, it suspends the execution of the current program and executes the
26 MICROPROCESSORS AND MICROCONTROLLERS
accept data. If this signal goes low, then the processor is allowed to wait
for an integral number of clock cycles until Ready becomes high. The
Ready signal must be synchronized with the processor clock.
X1 E1 40 ^Vcc
X2 E 2 39 2 HOLD
RESET OUT C 3 38 3 HLDA
SODE4 37 3CLK(OUT)
signals SID E5 36 3 RESET IN
+5V
TRAP E6 35 J READY
RST7.5 d7 34 3 io/m XTAL Higher-order
"X1 X2 address bus.
RST6.5 E8 33 3S1
A15
RST5.5 E9 32 3RD SID — A8
8085 Multiplexed'
INTR C 10 31 3 WR SOD <-
TRAP — lower-order
INTA E 11 30 3 ALE address/data bus
RST7.5 —
AD7
ADO E12 29 3 SO RST6.5 —
AD1 RST5.5 — ADO
E13 28 3A15 -►ALE
INTR — 8085
AD2 E14 27 3A14
AD3 Ej15 26 3A13 READY —
HOLD — ■> I0/M
AD4 C16 25 3A12 HLDA <■
AD5 C 17 24 3A11 ^ETin-
AD6 C 18 23 3A10
AD7 C 19 22 3 A9
C. 21 3A8 RESET OUT CLK(OUT)
(a) (b)
Fig. 2.7 (a) Pin diagram of the 8085 (b) Signal groups of the 8085
28 MICROPROCESSORS AND MICROCONTROLLERS
(ii) RESET OUT (output): This output signal indicates that the CPU is being
reset. It can be used as a reset signal for peripheral chips. The signal is
synchronized to the processor clock.
(iii) Ready (input): This signal is used to interface slow peripheral devices with
the fast microprocessor. If Ready is high during a read or write cycle, it
indicates that the memory or peripheral is ready to send or receive data. If
Ready is low, the CPU waits for it to go high before completing the read or
write cycle.
(iv) Hold (input): Hold is an active high signal used in the direct transfer of
data between a peripheral device and memory locations. This type of data
transfer is called as direct memory access (DMA). During this transfer,
the microprocessor loses control over the address and data buses and these
buses are tri-stated. Logic 1 on the Hold pin indicates that another controller,
generally the DMA controller, is requesting the use of the address and data
buses. The CPU, upon receiving the hold request, will relinquish the use of
the buses after completing the current instruction. An acknowledge signal
is sent out by the processor and the address, data, RD, WR, and IO/M lines
are tri-stated. The processor can regain control over the buses only after the
Hold signal is removed.
(v) HLDA (output): Hold acknowledge, an active high signal, indicates that
the CPU has received the hold request and that it will relinquish the buses
in the next clock cycle. HLDA goes low after the hold request is removed.
The CPU takes the buses half a clock cycle after HLDA goes low.
(vi) INTR (input): Interrupt request is a general-purpose interrupt. It is sampled
only during the last clock cycle of the instruction. If INTR is high, the
program counter (PC) will not be allowed to increment and an INTA will be
issued. Program execution can be shifted to the interrupt service routine by
inserting an RST or CALL instruction on the data lines during this cycle.
(vii) INTA (output): Interrupt acknowledge is an active low signal used instead
of RD, after an interrupt request has been accepted. This signal is used to
read the opcode from the data bus and execute it.
(viii) RST 7.5, RST 6.5, RST 5.5—restart interrupts (input): These three inputs
are hardware interrupt signals similar to INTR. They are used to make the
processor execute a subroutine at a predefined address. However, these
interrupts do not have an acknowledgement signal.
(ix) Trap (input): Trap interrupt is a non-maskable restart interrupt. It is
unaffected by any mask or interrupt enable signal. It is the highest priority
interrupt. Interrupts, and their masking and enabling, are discussed in detail
in Chapter 5.
POINTS TO REMEMBER
KEY TERMS
Accumulator It is an 8-bit register; it is a part of the ALU and is the most important
register. It is used to store 8-bit data and to perform arithmetic and logical operations. The
output of an operation is also stored in the accumulator. The accumulator is identified as
register A.
Address bus This bus carries the binary number (i.e., the address) used to access a
memory location. Binary data can then be written into or read from the addressed memory
location. The address bus consists of 16 wires and can, therefore, handle 16 bits.
Bus It is a group of conducting lines that carry data, address, and control signals
Clock speed This determines how many instructions per second the processor can
execute. It is specified in megahertz (MHz).
Control bus This bus has various lines for coordinating and controlling microprocessor
operations. For example, RD and WR lines.
Data bus This bus carries data in binary form between the microprocessor and external
units such as memory. Typical size is eight or 16 bits.
DMA controller It is used to take control of the system bus by placing a high signal on
the Hold pin.
32 MICROPROCESSORS AND MICROCONTROLLERS
Flag It is a flip-flop used to store information about the status of the processor and the
status of the instruction executed most recently.
Hold and HLDA These signals are used for direct memory access (DMA) type of data
transfer. The Hold request makes the 8085 drive all its tri-stated pins to high impedance
state. The HLDA signal goes high to acknowledge the receipt of the Hold signal.
IO/M signal This signal is used to differentiate memory access and I/O access. For
input/output instructions it is high; for memory reference instructions it is low.
Ready It is an input signal to the processor. It is used by the memory or I/O devices to get
extra time for data transfer or to introduce wait states in the bus cycles.
Trap It is a non-maskable interrupt of the 8085 and is not disabled by processor reset or
after reorganization of interrupt.
REVIEW QUESTIONS ]
Type Example
operation also uses the accumulator as reference, i.e., it subtracts the content of a
register or memory location from that of the accumulator and stores the result in
the accumulator.
Increment/Decrement These operations can be used to increment or decrement
the contents of any register, register pair, or memory location. Unlike the arithmetic
and logical operations, the increment and dccrcment operations need not be based
upon the accumulator.
3.2.1.3 Logical Operations
Logical instructions are also accunHilator-oriented, i.e., they require one of the
operands to be placed in the accumulator. The other operand can be any register or
memory location. The result is stored in the accumulator. The operations that use
twooperands are logical AND, OR, and EXOR. The operation that uses a single
operand (i.e., the accumulator) is the logical complement orNOT operation.
The instruction set of the 8085 supports rpjtajdon of the data stored in accumulator.
The data can.be rotated left or right, through the carry or without the carry.
The most important 8085 instruction is the compare instruction. This instruction
is used to compare register or memory content with the accumulator content. The
result of comparison such as equal to, greater than, or less than is reflected in the
flag register bits.
3.2.1.4 Branching Operations
Branching instructions are important for programming a microprocessor. These
instructions can transfer control of execution from one memory location to another,
either conditionally or unconditionally. Branching can take place in the following
two ways:
(i) Execution control cannot return to the point of branching. Example: Jump
instructions
(ii) Execution control can return to the point of branching, which is stored by the
8085. Example: Subroutine call instructions
3.2.1.5 Machine Control Operations
These instructions can be used to control the execution of other instructions.
They include halting the operation of the microprocessor, interrupting program
execution, etc. Detailed explanations for 8085 instructions are given in
Section 3.3.
Example:
MVI A, 32H (coded as 3E 32 in two contiguous bytes)
This is an example of immediate addressing.
The following two instructions are also examples of two-byte instructions:
(i) ADI data (A <—A + data)
(ii) OUT port (where port is an 8-bit device address. (Port) <<- A) Since the byte
INSTRUCTION SET AND EXECUTION IN 8085 37
is not the data itself, but points directly to where it is located, this is called
direct addressing. For a detailed account of addressing modes, see Section
3.2.3.
3.2.2.3 Three-byte Instructions
Instructions that require three bytes in machine code are called three-byte
instructions. In 8085 machine language, the first byte of the three-byte instructions
is the opcode which specifies the operation to be performed. The next two bytes
refer to the 16-bit operand, which is either a 16-bit number or the address of a
memory location. Some common examples of three-byte instructions are listed in
Table 3.4.
Example:
ADI 05H
(a) Add 05H to the contents of the accumulator.
(b) 05H is the operand.
Immediate addressing has no memory reference to fetch data. It executes faster,
but has limited data range.
3.2.3.2 Memory Direct Addressing
Memory direct addressing moves a byte or word between a memory location and
register. The memory location address is given in the instruction. The instruction
set does not support memory-to-memory transfer. Memory direct addressing is
illustrated in Fig. 3.2.
Example:
'' LDA 850FH
This instruction is used to load the contents of the memory location 850FH in the
accumulator.
Example:
-ZSTA 9001H
This instruction is used to store the contents of the accumulator in the memory
address 9001H.
In these instructions, the memory address of the operand is given in the instruction.
INSTRUCTION SET AND EXECUTION IN 8085 39
Direct addressing is also used for data transfer between the processor and
input/output devices. For example, the IN instruction is used to receive data from
the input port and store it in the accumulator; the OUT instruction is used to send
the data from the accumulator to the output port.
Example:
IN OOH and OUT 01H
Example:
MOV Rd, Rs
MOV B, C
It copies the contents of register C to register B.
Example:
s' ADD B
It adds the contents of register B to the accumulator and saves it in the accumulator.
3.2.3.4 Indirect Addressing
Indirect addressing transfers a byte or word between a register and a memory
location. The address of a memory location is stored in a register and that register
is specified in the instruction. This is illustrated in Fig. 3.4.
In indirect addressing, the effective address is calculated by the processor using
the contents of the register specified in the instruction. This type of addressing
employs several accesses—two accesses to retrieve the 16-bit address and a further
access (or accesses) to retrieve the data which is to be loaded in the register.
Example:
MOV A* M
Here, the data is in the memory location pointed to by the contents of the HL pair.
The data is moved to the accumulator.
40 MICROPROCESSORS AND MICROCONTROLLERS
In general, the assembly language mnemonics with their operands are written
first. The address where the instructions are stored is given a dummy name called
label. The purpose of labels is to give the correct branch addresses in instructions.
Labels are separated from mnemonics with a colon.
The comments column is essential for any program as it helps the programmer
understand the logic of the program at any point in time. Without comments, it is
difficult to understand an assembly language program. Comments are separated
from the mnemonics with a semicolon.
INSTRUCTION SET AND EXECUTION IN 8085 41
The first two columns correspond to the physical memory address and the
actual machine code. These two columns are filled in after completing the assembly
language programming. These columns must contain only binary numbers, but for
easy understanding, hexadecimal numbers are used. For manual assembling, these
two columns are filled in by the programmer. An assembler can generate these
columns automatically.
An example of the assembly language program format is given in Table 3.5.
Table 3.5 Sample assembly language program
The instruction in Table 3.5 moves the data 5FH to the accumulator.
MVI R, 8-bit Moves the 8-bit data Immediate Two bytes MVI B, 3FH
to the register
LXI Rp, 16-bit Loads the 16-bit data in Immediate Three bytes LXI B, 5AF3H
the register pair
MOV Rd, Rs Copies the data from Register One byte MOV A, B
the source register to direct
the destination register
LDA 16-bit Loads the accumulator Register Three bytes LDA 905FH
with the data from the direct
memory location
indicated by the
16-bit address
(Contd)
42 MICROPROCESSORS AND MICROCONTROLLERS
LHLD 16-bit Loads the H and L Memory Three bytes LHLD 900AH
registers directly from direct
the two consecutive
memory locations
indicated by the
16-bit address
STA 16-bit Stores the contents of Memory Three bytes STA 9050H
the accumulator in the direct
memory location
indicated by the
16-bit address
SHLD 16-bit Stores the contents of Memory Three bytes SHLD 809FH
the H and L registers direct
in two consecutive
memory locations
indicated by the
16-bit address
PUSH Rp Pushes the contents of Register One byte PUSHB
the register pair onto direct
a stack
POPRp Pops the top two memory Register One byte POPH
locations of the stack direct
Qjito a register pair
OUT 8-bit Outputs the data in the I/O Two bytes OUT 40H
accumulator to the port
indicated by the
8-bit address
IN 8-bit Inputs the data from the I/O Two bytes IN 3 OH
port indicated by the
8-bit address to the
accumulator
MOV Rd, M Copies the contents of Indirect One byte MOV B, M
the memory location
pointed to by the HL
register pair to
y a
the register
MOV M, Rs Copies the contents of the Indirect One byte MOVM7C
register to the memory
location pointed to by
the HL register pair
LDAX Rp Loads accumulator with Indirect One byte LDAX B
the contents of the
memory location pointed
to by the register pair
(Contd)
INSTRUCTION SET AND EXECUTION IN 8085 43
(iv) LDA and STA use memory direct addressing mode and a 16-bit memory
address as operand.
(v) LDAX and STAX use indirect addressing mode for data transfer. The
operand given in the instruction is one of the register pairs BC or DE.
Register pair HL is not used with LDAX due to the availability of the
alternative instruction MOV A, M.
(vi) LHLD and SHLD are the instructions used to transfer 16-bit data between
the HL register pair and two consecutive memory locations. For example,
executing SHLD 9000H instruction will store the contents of L register in
9000H and the contents of H register in 9001H.
(vii) PUSH and POP instructions are used for data transfer between a register
pair and a stack. The stack is a set of memory locations configured as a last
in, first-out (LIFO) or first-in, last-out (FILO) array. The top of the stack
locations is pointed to by a special register, the stack pointer, which is within
the microprocessor. PUSH instruction will store the register pair given in
the instruction to the top two memory locations of the stack. Similarly, POP
instruction will copy the last two bytes stored in the stack to the register pair
mentioned in the instruction. Care must be taken in using these instructions as
the stack is configured as a LIFO array. Another instruction to store data in the
stack is XTHL, which exchanges the top two memory locations of the stack
with the contents of the HL register pair.
(viii) Stack pointer can be initialized using LXI or SPHL instructions. SPHL
instruction will copy the contents of the HL register pair to the stack pointer.
(ix) IN and OUT instructions use 8-bit port addresses as operand. IN instruction
is used to get data from the input port and the data obtained is stored in the
accumulator. OUT instruction is used to issue data from the accumulator to
an output port.
44 MICROPROCESSORS AND MICROCONTROLLERS
ADI 8-bit Adds the 8-bit data to Immediate Two bytes ADI 30H
the contents of the
accumulator
ACI 8-bit Adds the 8-bit data and Immediate Two bytes ACI 4FH
the carry flag to the
contents of the
accumulator
SUI 8-bit Subtracts the 8-bit data Immediate Two bytes SUI 2AH
from the contents of the
accumulator
SBI 8-bit Subtracts the 8-bit data Immediate Two bytes SBI 5CH
and the borrow from the
contents of the
accumulator
ADDR Adds the contents of the Register One byte ADDC
register to the contents direct
of the accumulator
ADCR Adds the contents of the Register One byte ADCE
register and the carry to direct
the contents of the
accumulator
SUBR Subtracts the contents of Register One byte SUBB
the register from that of direct
the accumulator
SBB R Subtracts the contents Register One byte SBB C
of the register and the direct
borrow from that of the
accumulator
DADRp Adds the contents of the Register One byte DADB
register pair to that of the direct
H and L registers
INRR Increments the register Register One byte INRB
by 1 direct
INX Rp Increments the register Register One byte INXB
pair by 1 direct
(Contd)
INSTRUCTION SET AND EXECUTION IN 8085 45
(v) The contents of a register pair can be incremented or decremented using INX
and DCX instructions.
(vi) DAA is the 8085 instruction that supports BCD addition. The addition of
BCD data is done like binary addition, using the ADD instruction. DAA is
used to convert the result of the binary addition of BCD numbers into a BCD
number. This instruction cannot be used to directly convert binary numbers
into BCD numbers.
ANI 8-bit The 8-bit data is logically ANDed Immediate Two bytes ANI OFH
with the contents of the accumulator
XRI 8-bit The 8-bit data is logically EXORed Immediate Two bytes XRI01H
with the contents of the accumulator
ORI 8-bit The 8-bit data is logically ORed Immediate Two bytes ORI 80H
with the contents of the accumulator
ANAR The contents of the register are Register One byte ANAC
logically ANDed with the contents direct
of the accumulator
XRAR The contents of the register are Register One byte XRAD
logically EXORed with the contents direct
of the accumulator
ORAR The contents of the register are Register One byte ORAE
logically ORed with the contents direct
of the accumulator
ANAM The contents of the memory Indirect One byte ANAM
location pointed to by the HL
register pair is logically ANDed
with the contents of the accumulator
XRAM The contents of the memory Indirect One byte XRAM
location pointed to by the HL
register pair is logically EXORed
with the contents of the accumulator
ORAM The contents of the memory Indirect One byte ORAM
location pointed to by the HL
register pair is logically ORed
with the contents of the accumulator
RLC Rotates the bits of the accumulator Implicit One byte RLC
left by one position
(Contd)
INSTRUCTION SET AND EXECUTION IN 8085 47
RRC Rotates the bits of the accumulator Implicit One byte RRC
right by one position
RAL Rotates the bits of the accumulator Implicit One byte RAL
left by one position, through the carry
RAR Rotates the bits of the accumulator Implicit One byte RAR
right by one position, through
the carry
CPI 8-bit Compares the 8-bit data with the Immediate Two bytes CPI FFH
contents of the accumulator
CMPR Compares the contents of the register Register One byte CMPB
with that of the accumulator direct
CMPM Compares the contents of the memory Indirect One byte CMPM
location pointed to by the HL register
pair with that of the accumulator
CMA Complements the contents of the Implicit One byte CMA
accumulator
CMC Complements the carry Implicit One byte CMC
STC Sets the carry Implicit One byte STC
For logical operations, one of the data must be stored in the accumulator and the
other given or addressed in the instruction. Logical operations can be performed
with immediate data, data stored in a register, or indirectly addressed memory
location content.
Besides the instructions already mentioned, two types of rotate instructions
are available in the 8085. One set—RLC and RRC—rotates the accumulator
contents within itself. The RLC instruction shifts the accumulator content left by
one bit. In the process, the most significant bit of the accumulator becomes the
least significant bit. The RRC instruction shifts the accumulator content right by
one bit.
The other set of rotate instructions—RAL and RAR—rotates the accumulator
content along with the carry flag. The RAL instruction shifts the accumulator
content left by one bit and in the process, the most significant bit will be shifted to
the carry flag and the carry flag content will be shifted to the least significant bit of
the accumulator.
The instruction set of the 8085 supports a compare instruction for comparing
the magnitude of two binary numbers. The compare instructions are used to
compare the accumulator content with the operand specified in the instruction.
CPI instruction uses immediate addressing and CMP uses registers or indirectly
addressed memory location for comparing with the accumulator. The result ot the
compare instruction is indicated in the flag register, as follows:
If [(A) - operand J = 0, i.e., (A) - operand, the zero flag is set.
48 MICROPROCESSORS AND MICROCONTROLLERS
If [(A) - operand] < 0, i.e., (A) < operand, the carry flag is set.
If [(A) - operand] > 0, i.e., (A) > operand, the zero and carry flags arc reset.
3. Write an assembly language program to add the two numbers stored in the
memory locations 85OOH and 8501H and store the result in 8502H.
This program uses indirect addressing instructions to load the numbers to be
INSTRUCTION SET AND EXECUTION IN 8085 51
added in the processor registers. The carry, if generated, is ignored. The program
is shown in Table 3.13.
Z A Au
u Halt
(high impedance)
0 0 1 Memory write
1 0 1 I/O write
0 1 0 Memory read
1 1 0 I/O read
0 1 1 Opcode fetch
1 1 1 Interrupt acknowledge
The 8085 microprocessor takes four T-states to execute the opcode fetch machine
cycle and three T-states to execute the memory and I/O read/write cycles. The
interrupt acknowledge cycle is similar to the opcode fetch cycle and is explained
in Chapter 5. Every instruction of the 8085 requires a definite number of machine
cycles. Sections 3.5.1-3.5.5 explain the execution of some instructions, dividing
them into the corresponding machine cycles and further into many T-states.
Figure 3.6 shows the common waveform of the clock signal used in
microprocessor systems. The clock signal is a square waveform of high frequency
in the range of MHz. The rising and falling edge of the clock signal can be clearly
seen, as its frequency is very high and time period is correspondingly very low.
The timing diagram for an instruction is obtained by drawing the binary levels
on the various signals of the 8085. It is drawn with respect to the clock input of
the microprocessor. It explains the execution of the instruction using the basic
machine cycles of that instruction.
SIGNAL T1 T2 T3 T4
CLOCK
ALE
y
IO/M, S1, SO IO/M = 0, S1 = 1 SO = 1
RD
———
The following points explain the various operations that take place and the
signals that are changed during the execution of the opcode fetch machine cycle:
During T1 clock cycle
(i) The content of the program counter is placed in the address bus. The lower-
order address is placed in the AD0-AD7 lines and the higher-order address
is placed in the A8-A15 lines. The change of binary levels on these lines is
shown in the timing diagram by a cross.
(ii) IO/M signal goes low to indicate that a memory location is being accessed.
This signal is used by external devices to identify memory and I/O device
accesses for interfacing. The signals on the status lines SO and SI are also
changed to the levels indicated in Table 3.14.
(iii) ALE signal becomes active high to indicate that the multiplexed AD0-AD7
lines are acting as the lower-order address bus.
port addresses. So the port address is placed in the lower-order address bus. At the
same time, the port address is also placed in the higher-order address bus. This
facilitates easy design of hardware for address decoding. The processor takes three
T-states to execute this machine cycle. The IN instruction uses this machine cycle.
The timing diagram is given in Fig. 3.10.
(i) During the first machine cycle, the opcode DBH is fetched from the memory,
placed in the instruction register, and decoded.
(ii) During the second machine cycle, the port address 80H is read from the next
memory location.
(iii) During the third machine cycle, the address 80H is placed in the address bus
and the data read from that port address is placed in the accumulator.
placed in the address bus, the memory content is brought to the processor,
and its value is incremented.
(iv) During the third machine cycle, the incremented data is again written back
into the same address, as shown in the timing diagram given in Fig. 3.14.
the multiplexed address and data bus must be de-multiplexed. The de-multiplexing
can be done with the help of the signal ALE given by the processor for this purpose.
The hardware needed for de-multiplexing is given in Fig. 3.16.
Fig. 3.16 Hardware interfacing for de-multiplexing lower-order address bus and data bus
During the first T-state in all the machine cycles, the AD0-AD7 lines act
as address bus and the lower-order address is available on these lines. The
AD0-AD7 lines are converted into the data bus during the second T-state.
However, the memory devices require the lower-order address lines during the
entire machine cycle. So, during the first T-state, the lower-order address lines sent
out by the microprocessor is stored into a separate latch or register. The common
74LS373 latch, which has inputs and outputs of 8 bits each, can be used for this
purpose. The ALE control signal is used for latching the address lines available in
INSTRUCTION SET AND EXECUTION IN 8085 61
the AD0-AD7 lines to the latch in the first T-state. The latched address lines are
available for the entire machine cycle. Memory interfacing and address decoding
are then done using the lines A0-A15 (which are available for the entire machine
cycle). Memory interfacing is discussed in detail in Chapter 6.
POINTS TO REMEMBER^
T-state It is the basic unit used to calculate the time taken for execution of instructions
and programs in a processor. It is the time corresponding to one clock period.
REVIEW QUESTIONS j
1. List the four categories of 8085 instructions that are used for data manipulation.
2. Define opcode and operand. Identify the opcode and the operand in the instruction
MOV H, L.
3. Explain the instruction XCHG.
4. What is an instruction? List any four arithmetic instructions and their uses.
5. Define stack. Explain the instructions related to stack operations.
6. When is the instruction XRA A used?
7. How many operations are there in the instruction set of the 8085 microprocessor?
8. Explain with examples the different instruction formats, based on the length of the
instructions.
9. List the four instructions which control the interrupt structure of the 8085
microprocessor.
10. What is the last instruction executed in a program? Why?
11. What is the significance of XCHG and SPHL instructions?
12. Explain the operation carried out when the 8085 executes the instruction RST 0.
13. What is addressing? What are the various addressing modes available in the 8085?
14. Explain direct addressing with an example.
15. Explain implied addressing with an example.
16. What are the machine cycles in the 8085 microprocessor?
17. Define T-state. In which T-state is the ALE signal activated?
18. Explain the various timing parameters involved in read and write timings of a typical
RAM.
19. Draw the timing diagram of the instruction MOV C, A.
20. Explain, with the help of a timing diagram, the instruction IN 82H.
21. Explain the timing diagrams for (i) opcode fetch cycle and (ii) I/O write cycle.
22. Draw and explain in brief the timing diagrams for the instruction CALL 2000H, with
appropriate control and status signals.
23. Draw the timing diagram for the instruction STA 4500H and explain.
ASSEMBLY LANGUAGE
PROGRAMMING OF 8085
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Assemblers and their types
• Assembler directives
• Programming examples based on simple arithmetic and logical operations
♦ Programming examples based on looping and branching operations
• Code conversion, decimal arithmetic, and bit manipulation
• Programming examples based on subroutine concepts
• Programming examples based on counters and time delays
4.1 ASSEMBLER
An assembler is a program that is used to translate assembly language mnemonics
into equivalent binary code. The assembly language, and hence the assembler
program, varies from one processor to another. The 8085 assembler program
converts mnemonic code into binary object code or machine code, which can be
executed by the 8085 microprocessor. The assembler is a software tool or program
to be used with a computer. The functioning of an assembler program is shown in
Fig. 4.1. The object code or machine code file generated in the computer system is
then used in an 8085-based system.
The input to the assembler is a file with the extension .asm. The assembler
output consists of at least two possible files:
64 MICROPROCESSORS AND MICROCONTROLLERS
(i) The object or the hex fi ie that contains the binary machine code corresponding
to each mnemonic in the source assembly language program.
(ii) The list file containing the source assembly code along with the corresponding
machine code generated by the assembler and the list of symbols used in the
assembly program.
While different types of assemblers are commercially available, the most
common are the one-pass assembler, two-pass assembler, macro assembler, and
cross assembler. In one-pass assembler, the assembly language program is processed
only once from start to end. In two-pass assembler, the assembly language program
is processed twice—once for symbols and labels, to assign memory locations for
them, and then once again for the actual assembly process. Cross assemblers are
used to convert assembly language programs into machine codes that can be run on
other processors. This assembly process can be done on any PC.
The macro assembler is an assembler that contains a provision for the
programmer to define macro instructions. Macros are user-defined abbreviations
for particular sequences of program routine. When the program is assembled, each
occurrence of the macro is replaced by the instructions for which it stands.
(vi) The assembler checks for syntax errors such as errors in opcodes, labels,
and expressions and indicates the occurrence of these errors.
Example 4.1:
Store the data byte FFH in the memory location 9000H.
Program 1:
This uses direct addressing to access the memory location, as shown in Table 4.2 (a).
Table 4.2 (a) Program for storing data in memory using direct addressing
Mnemonics Comments
Program 2:
This uses indirect addressing to access the memory location, as shown in
Table 4.2 (b).
Table 4.2 (b) Program for storing data in memory using indirect addressing
Mnemonics Comments
Table 4.3 (a) Program for exchanging contents of memory locations using direct addressing
Mnemonics Comments
LDA 9000H ; Load the contents of memory location 9000H in the accumulator.
MOV C, A ; Move the contents of the accumulator to register C.
LDA 9050H ; Load the contents of memory location 9050H in the accumulator.
(Contd)
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 69
Table 4,3 (a) Program for exchanging contents of memory locations using direct addressing
(Contd)
Mnemonics Comments
STA 9000H ; Store the contents of the accumulator in memory address 9000H.
MOV A, C ; Move the saved contents in register C back to the accumulator.
STA 9050H ; Store the contents of the accumulator in 9050H.
HLT ; Terminate program execution.
Program 2:
Table 4.3 (b) Program for exchanging contents of memory locations using indirect addressing
Mnemonics Comments
LXI H, 9000H ; Load the first address 9000H in the HL register pair.
LXI D, 9050H ; Load the second address 9050H in the DE register pair.
MOV C, M ; Move the contents of memory location 9000H to register C.
LDAXD ; Move the contents of memory location 9050H to the
accumulator.
MOV M, A ; Store the contents of the accumulator in memory location
9000H.
MOV A, C ; Move the contents of register C to the accumulator.
STAXD ; Store the contents of the accumulator in the memory location
9050H.
HLT ; Terminate program execution.
Example 4.3:
Add the numbers present in memory locations 9000H and 9001H. Store the result
in memory location 9002H.
Sample data:
(9000H) = 32H
(9001H) = AAH
Result = 32H + AAH = DCH
Flowchart:
The algorithm for this problem is explained in the form of a flowchart in
Fig. 4.2.
Program:
It neglects the carry (if any) produced in the addition. So it works only for 8-bit
sum. The program is given in Table 4.4.
70 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.4 Program for adding two numbers and placing the result in a memory location
Mnemonics Comments
LXI H, 9000H ; Load the first address 9000H in the HL register pair.
MOV A, M ; Load the first operand in the accumulator.
INXH ; Load the second address 9001H by incrementing the HL pair by 1.
ADDM ; Add the content of 9001H to the accumulator.
INXH ; Load the third address 9002H by incrementing the HL pair by 1.
MOV M, A ; Store the result of addition in 9002H.
HLT ; Terminate program execution.
Example 4.4:
Subtract the contents of memory location 9001H from that of memory location
9000H and store the result in memory location 9002H.
Sample data:
(9000H) = CCH
(9001H) = AOH
Result = CCH - AOH = 2CH
Flowchart:
The algorithm for this problem is explained in the form of a flowchart in
Fig. 4.3.
Program:
The program given in Table 4.5 subtracts one 8-bit number from another and
stores the result in a memory location.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 71
Table 4.5 Program for subtracting one number from another and placing the result in
a memory location
Mnemonics Comments
LXI H, 9000H ; Load the first address 9000H in the HL register pair.
MOV A, M ; Load the first operand in the accumulator.
INXH ; Load the second address 9001H by incrementing the HL pair by 1.
SUB M ; Subtract the second operand from the first.
INXH ; Load the third address 9002H by incrementing the HL pair by 1.
MOV M, A ; Store the result of subtraction in 9002H.
HLT ; Terminate program execution.
Example 4.5:
Two 16-bit numbers are available in memory locations 9000H and 9001H, and
in 9002H and 9003H, where the most significant bits are in 9001H and 9003H.
Add the two numbers and save the result in 9004H and 9005H, with the most
significant byte in 9005H.
Sample data:
(9000H) = AAH
(9001H) = 55H
(9002H) = BBH
(9003H) = 21H
Result = 55 AAH + 21 BBH = 7765H
(9004H) = 65H
(9(X)5H) = 77H
72 MICROPROCESSORS AND MICROCONTROLLERS
Flowchart:
The step-by-step solution to this problem is illustrated in Fig- 4-
Tables 4.6 (a) and 4.6 (b) illustrate two programs that add two 16-bit numbers and
store the result in two consecutive memory locations.
Program I:
Table 4.6 (a) Program for adding two 16-bit numbers using ADD and ADC instructions
Mnemonics Comments
LHLD 9000H ; Load the first 16-bit number in the HL register pair.
XCHG ; Save the first 16-bit number in the DE register pair.
LHLD 9(X)2H ; Load the second 16-bit number in the HL register pair.
MOV A, E ; Move the lower-order byte of the first number to the accumulator.
ADD L ; Add the lower-order bytes of the two numbers.
(Contd)
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 73
Table 4.6 (a) Program for adding two 16-bit numbers using ADD and ADC instructions (Contd)
Mnemonics Comments
MOV L, A ; Store the result in register L.
MOV A, D ; Move the higher-order byte of the first number to the accumulator.
ADC H ; Add the higher-order bytes of the two numbers with carry.
MOV H, A ; Store the result in register H.
SHLD 9004H ; Store the 16-bit result in memory locations 9004H and 9005H.
HLT ; Terminate program execution.
Program 2:
Table 4.6 (b) Program for adding two 16-bit numbers using DAD instruction
Mnemonics Comments
LHLD 9000H ; Load the first 16-bit number in the HL register pair.
XCHG ; Save the first 16-bit number in the DE register pair.
LHLD 9002H ; Load the second 16-bit number in the HL register pair.
DADD ; Add the DE and HL register pairs.
SHLD 9004H ; Store the 16-bit result in memory locations 9004H and 9005H.
HLT ; Terminate program execution.
In Program 1, the 8-bit addition instructions ADD and ADC are used and addition
is performed in two steps. The lower-order bytes are added first, using ADD
instruction, and then the higher-order bytes using ADC instruction. In Program 2,
the 16-bit addition instruction DAD is used. Both programs assume that the result
is only 16-bits wide and neglect any carry generated.
Example 4.6:
Two numbers are stored in memory locations 9000H and 9001H. Add them and
store the result (including the carry) in memory locations 9002H and 9003H.
Sample data:
(9OOOH) = F1H
(9001H) = 5AH
Result = Fl H + 5AH = 014BH
(9002H) = 4BH
(9003H) = 01H
Flowchart:
Figure 4.5 illustrates the step-by-step method to solve this programming exercise.
Program:
This program includes the carry generated after 8-bit addition; the result is stored
in two registers, as shown in Table 4.7.
74 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.7:
Two 16-bit numbers are available in memory locations 9000H and 9001H, and
in 9002H and 9003H, where the most significant bits are in 9001H and 9003H.
Subtract the latter from the former and save the result in 9004H and 9005H, with
the most significant byte in 9005H.
Sample data:
(9000H) = FFH
(9001H) = EEH
(9002H) = 43H
(9003H) = 21H
Result = EEFFH - 2143H = CDBCH
(9004H) = BCH
(9005H) = CDH
Flowchart:
The algorithm for this programming exercise is given in Fig. 4.6.
Fig. 4.6 Algorithm for subtracting one 16-bit number from another
76 MICROPROCESSORS AND MICROCONTROLLERS
Program:
The program for subtracting one 16-bit number from another and saving the result
in two consecutive memory locations is given in Table 4.8.
Table 4.8 Program for subtracting one 16-bit number from another
Mnemonics Comments
LHLD 9000H ; Load the first 16-bit number in the HL register pair.
XCHG ; Save the first 16-bit number in the DE register pair.
LHLD 9002H ; Load the second 16-bit number in the HL register pair.
MOV A, E ; Move the lower-order byte of the first number to the
accumulator.
SUB L ; Subtract the lower-order byte of the second number from that of
the first.
MOV L, A ; Store the result in register L.
MOV A, D ; Move the higher-order byte of the first number to the
accumulator.
SBB H ; Subtract the higher-order byte of the second number and borrow
from the contents of the accumulator.
MOV H, A ; Store the result in register H.
SHLD 9004H ; Store the 16-bit result in memory locations 9004H and 9005H.
HLT ; Terminate program execution.
Example 4.8:
Find the l’s complement of the number stored in memory location 9000H. Store
the result in memory location 9001H.
Sample data:
(9000H) = 55H
Result = (9001H) = AAH
Flowchart:
The algorithm for finding the
l’s complement of a number is
given in Fig. 4.7.
Cl End 2-^
Fig. 4.7 Algorithm for finding 1 's complement of a number
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 77
Program:
Table 4.9 shows the program for finding the l’s complement of a number.
Table 4.9 Program for finding 1 's complement of a number
Mnemonics Comments
LDA 9000H ; Load the number to be complemented in the accumulator.
CMA ; Complement the number.
STA 9001H ; Store the result.
HLT ; Terminate program execution.
Example 4.9:
Find the 2’s complement of the number stored in memory location 9000H. Store
the result in memory location 9001H.
Sample data:
(9000H) = 55H
Result = (9001H) = AAH + 1 = ABH
Flowchart:
The procedure for finding the 2’s complement of a number is illustrated in
Fig. 4.8.
Program:
The 2’s complement of a number can be found using the program given in
Table 4.10.
Mnemonics Comments
Example 4.10:
Write a set of instructions to read and complement the contents of the flag
register.
Program:
In the 8085, the contents of the flag register and the accumulator together are
called program status word (PSW). The PSW can be accessed only using PUSH
and POP instructions. The PUSH instruction moves the flag register contents (as
the lower-order byte) and the accumulator contents (as the higher-order byte) to
the stack.
The contents of the flag register can be read and complemented using the program
given in Table 4.11.
Table 4.11 Program for reading and complementing the contents of the flag register
Mnemonics Comments
PUSH PSW ; Push the contents of the accumulator and the flag register onto the stack.
POPH ; Bring the PSW to the HL pair.
MOV A, L ; Move the contents of the flag register to the accumulator.
CMA ; Complement the accumulator.
MOV L, A ; Store the complemented flags in register L.
PUSHH ; Push the contents of the HL register pair onto the stack.
POP PSW ; Pop it back to the PSW.
HLT ; Terminate program execution.
Example 4.11:
Write a program to shift the 16-bit number in the HL register pair left by one bit.
Program:
A binary number can be shifted left by one bit by adding the number to itself.
Adding a number to itself also multiplies it by two.
DAD H: Adds the content of the HL register pair to itself.
Sample data:
(HL) = 57EAH = 0101 0111 1110 1010
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 79
Example 4.12:
Write a program to shift
the 8-bit number in
memory location 9000H
right by four bits.
Rotating a binary
number right once is
equivalent to dividing by
two. So rotating it right
four times is equivalent
to dividing by 16. Store the result in another memory location
Flowchart:
The algorithm for
shifting an 8-bit number
right by four bits is
shown in Fig. 4.9.
Fig. 4.9 Algorithm for shifting an 8-bit number right by four bits
Program:
The program given in Table 4.12 shifts an 8-bit number right by four bits.
Table 4.12 Program for shifting an 8-bit number right by four bits
Mnemonics Comments
LDA 9000H ; Load the number in the accumulator.
RRC ; Rotate right without carry.
RRC ; Rotate right without carry.
RRC ; Rotate right without carry.
RRC ; Rotate right without carry.
STA 9001H ; Store the result in memory location 9001H.
HLT ; Terminate program execution.
Example 4.13:
Write a program to shift a 16-bit number right by one bit. Assume that the data is
in memory locations 9000H and 9001H. Store the result in 9002H and 9003H.
Flowchart:
The algorithm for shifting a 16-bit number right by one bit is shown in Fig- 4.10.
80 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 4.10 Algorithm for shifting a 16-bit number right by one bit
Program:
The program for shifting a 16-bit number right by one bit is given in Table 4.13.
Table 4.13 Program for shifting a 16-bit number right by one bit
Mnemonics Comments
9000H and 9001H. The least significant digit is in 9000H. Store the result in
memory location 9002H.
Sample data:
(9000H) = 07H
(9001H) = 01H
Result = (9002H) = 17H
Flowchart:
The algorithm for packing two unpacked BCD numbers is given in Fig. 4.11.
Program:
The program for packing two unpacked BCD numbers is given in Table 4.14.
Table 4.14 Program for packing two unpacked BCD numbers
Mnemonics Comments
LDA 9000H ; Load the least significant BCD digit in the accumulator.
MOV B, A ; Move it to register B.
LDA 9001H ; Load the most significant BCD digit in the accumulator.
RLC ; Rotate the accumulator left without carry.
RLC ; Rotate the accumulator left without carry.
RLC ; Rotate the accumulator left without carry.
RLC ; Move the digit to the higher-order four bits by shifting left four times.
ORA B ; Add the lower-order BCD digit, keeping the higher-order unchanged.
(Contd)
82 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.14 Program for packing two unpacked BCD numbers (Contd)
Mnemonics Comments
Example 4.15:
Unpack the 2-digit BCD number in memory location 9000H and store the two
digits in memory locations 9001H and 9002H, with the units digit in 9001H.
Sample data:
(9000H) = 31H
Result = (9001H) = 01H and (9002H) = 03H
Flowchart:
The algorithm for unpacking a packed BCD number is given in Fig. 4.12.
Program:
The program given in Table 4.15 unpacks a packed BCD number.
Table 4.15 Program for unpacking a packed BCD number
Mnemonics Comments
Example 4.16:
A 4-digit BCD number is stored in memory locations 9000H and 9001H and
another in memory locations 9002H and 9003H. Add the two BCD numbers and
store the result in memory locations 9004H and 9005H. Ignore the carry after the
16th bit.
Sample data:
(HL) = 3629
(DE) = 4738
Step 1:29 + 38 = 61 and auxiliary carry flag = 0
Auxiliary carry flag is set. So add 06 (decimal adjust accumulator)
61+06 = 67
Step 2: 36 + 47 + 0 (carry of LSB) = 7D
Lower nibble is greater than 9. So add 06.
7D + 06 = 83 (decimal adjust accumulator)
Result = 8367
Flowchart:
The algorithm for adding two 4-digit BCD numbers is given in Fig. 4.13.
Program:
The program for adding two 4-digit BCD numbers is given in Table 4.16.
84 MICROPROCESSORS AND MICROCONTROLLERS
f-
S'-
'»■
Mnemonics Comments
LHLD 9000H ; Load the first 4-digit BCD number in the HL register pair.
XCHG ; Move it to the DE register pair.
LHLD 9002H ; Load the second 4-digit BCD number in the HL register pair.
MOV A, L ; Load the two lower-order digits of the first number in the
accumulator.
ADDE ; Add with the two lower-order digits of the second number.
DAA ; Adjust the result to make it a valid BCD number.
STA 9004H ; Store the partial result in 9004H.
MOV A, H ; Load the two higher-order digits of the first number in the
accumulator.
ADCD ; Add with the two higher-order digits of the second number and the
carry of the previous addition.
DAA ; Adjust the result to make it a valid BCD number.
STA 9005H ; Store the partial result in 9005H.
HLT ; Terminate program execution.
Example 4.18:
Transfer ten bytes of data from one memory block to another. The source memory
block begins at 9000H, while the destination memory block begins at 9100H.
Program:
The program for transferring a block of data from one series of memory locations
to another is given in Table 4.18.
88 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.18 Program for transferring a block of data between memory locations
Example 4.19:
Two 8-bit numbers are available in memory locations 9000H and 9001H. Multiply
them by repetitive addition and store the result in memory locations 9002H and
9003H.
Sample data:
(9000H) = A1H
(9001H) = 04H
Result = A1H + A1H + A1H + A1H = 284H
(9002H) = 84H
(9003H) = 02H
Flowchart:
The algorithm for multiplying two 8-bit numbers by repetitive addition is given
in Fig. 4.15.
Program:
The program given in Table 4.19 multiplies two 8-bit numbers by repetitive
addition.
Table 4.19 Program for multiplying two 8-bit numbers by repetitive addition
Table 4.19 Program for multiplying two 8-bit numbers by repetitive addition (Contd)
Fig. 4.15 Algorithm for multiplying two 8-bit numbers by repetitive addition
90 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.20:
A 16-bit number is stored in memory locations 9000H and 9001H and an 8-bit
number in 9002H. Divide the former by the latter; store the quotient in 91 OOH and
9101H and the remainder in 9102H and 9103H.
Sample data:
(9000H) = 9FH
(9001H) = 70H
(9002H) = OFH
Result = 709FH/0FH = 782H (quotient) and 01H (remainder)
(9100H) = 82H
(9101H) = 07H
(9102H) = 01H
(9103H) = OOH
Flowchart:
Figure 4.16 shows the algorithm for dividing a 16-bit number by an 8-bit
number.
Program:
The program for dividing a 16-bit number by an 8-bit number is given in
Table 4.20.
Table 4.20 Program for dividing a 16-bit number by an 8-bit number
Example 4.21:
Two 8-bit unsigned numbers are stored in memory locations 9000H and 9001H.
Multiply them and store the result in memory locations 9002H and 9003H, with
the most significant bits in 9003H.
92 MICROPROCESSORS AND MICROCONTROLLERS
Sample data:
(9000H) =0000 1100 (OCH)
(9001H) =0000 0101 (05H)
Multiplicand = 1100(12D)
Multiplier =0101 (5D)
Result =12x5 = (60D)
1100 —Multiplicand
0101 — Multiplier
0001100 — Least bit of multiplier multiplied with multiplicand
000000- — Second bit of multiplier multiplied with shifted multiplicand
01100— — Third bit of multiplier multiplied with shifted multiplicand
0000— — Fourth bit of multiplier multiplied with shifted multiplicand
0111100 — Added to get product
Flowchart:
The algorithm for multiplying an 8-bit number by another is given in Fig. 4.17.
Program:
The program for multiplying an 8-bit number by another is given in Table 4.21.
Table 4.21 Program for multiplying an 8-bit number by another
Example 4.22:
A 16-bit unsigned number is stored in memory locations 9000H and 9001H (most
significant bits in 9001H) and an 8-bit unsigned number in memory location
9002H. Divide the former by the latter and store the quotient in memory location
91 OOH and the remainder in 9101H.
94 MICROPROCESSORS AND MICROCONTROLLERS
Sample data: The numerical calculation shown explains the algorithm used
10101 in this exercise. The example here shows an 8-bit dividend
0100)01010 ’ divided by a 4-bit divisor. The calculation shown is 86/4. This
oiooj produces a quotient of 21, which is 10101 in binary form and
ooioj a remainder of two, which is 10 in binary form. The number of
00101 comparisons needed here is five. The first comparison is with
0100H the higher-order nibble. Then there are four comparisons for
000110 the next four bits, with one bit being rotated left each time.
0100 In the program, a 16-bit dividend and an 8-bit divisor are
0010 considered. The number of comparisons required is nine. So a
count is set up for nine.
Flowchart:
The algorithm for dividing a 16-bit number by an 8-bit number is given in
Fig- 4.18.
Program:
The program for dividing a 16-bit number by an 8-bit number is given in
Table 4.22.
Table 4.22 Program for dividing a 16-bit number by an 8-bit number
NEXT: MOV A, H ; Move the most significant byte of the dividend to the
accumulator.
JNZ NEXT ; If the value of count is not equal to zero, jump to NEXT.
Example 4.23:
Calculate the sum of an array of numbers. The length of the array is stored in
memory location 9000H; the array begins at memory location 9001H.
(a) Assume that the sum is an 8-bit number. So ignore the carry and store the sum
in memory location 9100H.
(b) Assume that the sum is a 16-bit number. Store the sum in memory locations
9100H and 9101H.
96 MICROPROCESSORS AND MICROCONTROLLERS
Flowchart:
The algorithm for adding an array of numbers is given in F’S-
(Contd)
98 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.24 Program for finding 16-bit sum of an array of numbers (Contd)
Example 4.24:
Given a series of numbers, calculate the sum of the even numbers only. The
length of the series is available in memory location 9000H. The series begins at
9001H. Ignore the carries; assume that the sum is only eight bits long and store it
in memory location 91 OOH.
Sample data:
(9000H) = 4H
(9001H) = 20H
(9002H) = 15H
(9003H) = 13H
(9004H) = 22H
Result = (91 OOH) = 20 + 22 = 42H
Flowchart:
The algorithm for calculating the 8-bit sum of the even numbers in a series is given
in Fig. 4.20.
Program:
The program for calculating the 8-bit sum of the even numbers in a series is given
in Table 4.25.
Table 4.25 Program for calculating 8-bit sum of even numbers in a series
Table 4.25 Program for calculating 8-bit sum of even numbers in a series (Contd)
.....................
Fig. 4.20 Algorithm for calculating 8-bit sum of even numbers in a series
100 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.25:
Given a list of numbers, calculate the sum of the odd numbers only. The length of
the series is available in 9000H; the series starts at 9001H. Assume that the sum is
16 bits long. Store it in memory locations 91 OOH and 9101H.
Sample data:
(9000H) = 04H
(9001H) = 9AH
(9002H) = 52H
(9003H) = 89H
(9004H) = 7FH
Result = 89H + 7FH = 108H
(9100H) = 08H (lower-order byte)
(9101H) = 01H (higher-order byte)
Flowchart:
The algorithm for calculating the 16-bit sum of the odd numbers in a series is
given in Fig. 4.21.
Fig. 4.21 Algorithm for calculating 16-bit sum of odd numbers in a series
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 101
Program:
The program for calculating the 16-bit sum of the odd numbers in a series is given
in Table 4.26.
Table 4.26 Program for calculating 16-bit sum of odd numbers in a series
Example 4.26:
Calculate the sum of an array of numbers. The array starts at memory location
9000H. The memory location after the last element of the array has the data FFH.
The length of the array is not known. Consider the sum to be a 16-bit number.
Store the sum in memory locations 9100H and 9101H.
102 MICROPROCESSORS AND MICROCONTROLLERS
This program cannot use a counter to terminate the addition loop. The loop is
terminated by checking each number in the array. If the number is not FFH, the
addition is carried out. If the number is FFH, the loop is terminated and the result
is stored. The last number FFH is not added to the sum. This type of looping is
similar to the do-while loop in high-level languages.
Sample data:
(9000H) = 13H
(9001H) = E4H
(9002H) = 6BH
(9OO3H) = 33H
(9004H) = FFH
Result = 13 + E4 + 6B + 33 = 195H
(9100H) = 95H
(9101H) = 01H
Flowchart:
The algorithm for calculating the sum of a series of numbers when the length of
the series is not given is shown in Fig. 4.22.
Pig. 4.22 Algorithm for calculating the sum of a series of numbers when the length of the series
is not given
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 103
Program:
Table 4.27 shows the program for calculating the sum of a series of numbers when
the length of the series is not given.
Table 4.27 Program for calculating the sum of a series of numbers when the length of the series
is not given
Example 4.27:
Find the number of negative numbers (indicated by an MSB of 1) in a given series
and store it in memory location 91 OOH. The length of the series is in memory
location 9000H. The series begins at 9001H.
Sample data:
(9000H) = 04H
(9001H) = 56H
(9002H) = A9H
(9003H) = 73H
(9004H) = 82H
Result = 02 since 9002H and 9004H contain numbers with an MSB of 1.
Flowchart:
The algorithm for finding the number of negative numbers in a series is given in
Fig. 4,23.
104 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 4.23 Algorithm for finding the number of negative numbers in a series
Program:
The program for finding the number of negative numbers in a series is given in
Table 4.28.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 105
Table 4.28 Program for finding the number of negative numbers in a series
Example 4.28:
Add the corresponding elements of two arrays having ten 8-bit numbers each and
store them in a third array. The arrays start at memory locations 9000H, 91 OOH,
and 9200H, respectively. Assume that all the sums obtained in the process are not
more than eight bits long.
Flowchart:
The algorithm for adding the corresponding elements of two arrays is given in
Fig. 4.24.
Program:
The program for adding the corresponding elements of two arrays is given in Table
4.29.
Table 4.29 Program for adding the corresponding elements of two arrays
LXI H, 9000H ; Initialize memory pointer 1 for the first data array.
LXI B, 91 OOH ; Initialize memory pointer 2 for the second data array.
LXI D, 9200H ; Initialize memory pointer 3 for the result array.
LOOP: LDAX B ; Load data from the second array in the accumulator.
(Contd)
106 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.29 Program for adding the corresponding elements of two arrays (Contd)
pig. 4.24 Algorithm for adding the corresponding elements of two arrays
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 107
Example 4.29:
Two decimal numbers of six digits in packed BCD format each, occupying a
sequence of memory bytes are stored. The starting address of the first number
is 9(X)0H and that of the second number is 9100H. Write an assembly language
program to add these two numbers and store the sum in the same format, starting
at memory location 9200H.
Flowchart:
The algorithm for adding two 6-digit packed BCD numbers is given in Fig- 4.25.
Program:
The program for adding two 6-digit BCD numbers is given in Table 4.30.
Table 4.30 Program for adding two 6-digit BCD numbers
Example 4.30:
Find the largest number in a given series of 8-bit unsigned binary numbers. The
length of the series is stored in memory location 9000H. The series begins at
9001H. Store the result in memory location 91 OOH.
Sample data:
(9000H) = 04H
(9001H) = 34H
(9002H)=A9H
(9003H) = 78H
(9004H) = 56H
Result = (91 OOH) = A9H
Flowchart:
The algorithm for finding the largest number in a series is given in Fig- 4.26.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 109
I
I
r-*
Program:
The program for finding the largest number in a series is given in Table 4.31.
Table 4.31 Program for finding the largest number in a series (Contd)
Example 4.31:
Sort a given list of 10 numbers starting at memory location 9000H in ascending
order.
Flowchart:
The algorithm for sorting a series of numbers in ascending order is given in
Fig. 4.27.
Program:
The program for sorting a series of numbers in ascending order is given in
Table 4.32. 7
\ /Table 4.32 Program for sorting a series of numbers in ascending order
Example 4.32:
Arrange an array of 8-bit unsigned numbers in descending order.
Program:
The program for arranging an array of 8-bit unsigned numbers in descending order
is given in Table 4.33.
4.33 Program for arranging an array of 8-bit unsigned numbers in descending order
Example 4.33:
Tind the square of the numbers stored as an array starting at memory location
9000H. Store the result as an array starting at 91(X)H. Assume that there are five
numbers in the array.
The following program uses the look-up table concept. Instead of finding the squares
by arithmetic operations, the calculated values of squares of 0 to 9 are stored in
memory locations. These memory locations are accessed using their addresses, the
last digits of which are from zero to nine. The program given in Table 4.34 uses
three register pairs and indirect addressing mode for data transfer.
Look-up table:
Table 4.34 lists the squares of numbers 0-9.
Flowchart:
The algorithm for finding the squares of an array of numbers is given in
Fig. 4.28.
Program:
The program for finding the squares of an array of numbers is given in
Table 4.35. Here the array size is 5.
Table 4.35 Program for finding the squares of an array of numbers (Contd)
Search for the first occurrence of the byte in register C in a list of 50 numbers
stored in the consecutive memory locations starting at 9000H. Store the memory
location containing the given byte in 9100H and 9101H. If the byte is not found
in the list, store OOH in 91 OOH and 9101H.
Flowchart:
The algorithm for finding a number from a list is given in Fig. 4.29.
Program:
The program for finding a number from a list is given in Table 4.36.
Example 4.35:
Identify the even numbers from a series of 50 numbers starting at 9000H and store
them as another series starting at 9100H.
Flowchart:
The algorithm for finding the even numbers from a list of 50 numbers is given in
Fig. 4.30.
Program:
The program fof finding the even numbers from a list of 50 numbers is given in
Table 4.37. /
x'~Table 4.37 Program for finding even numbers from a list of 50 numbers
(Contd)
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 117
r
f
Fig. 4.30 Algorithm for finding even numbers from a list of 50 numbers
118 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.37 Program for finding even numbers from a list of 50 numbers (Contd)
Example 4.36:
Move a block of data 256 bytes long from the series of memory locations starting
at 9000H to the series starting at 9050H. In the process of shifting, do not transfer
the data to any other memory location.
The two blocks of data (9000-90FF and 9050-914F) are overlapping.
Therefore, it is necessary to transfer the last byte first and the first byte last.
Program:
The program to transfer a block of data between two overlapping blocks of memory
locations is given in Table 4.38.
Table 4.38 Program to transfer a block of data between two overlapping blocks
of memory locations
Example 4.37:
Find the number of occurrences of negative numbers, zeros, and positive numbers
from a list of 50 numbers stored in a block of memory locations starting at 9000H.
Store the counts in memory locations 91 OOH, 9101H, and 9102H, respectively.
Flowchart:
The algorithm for finding the number of occurrences of negative numbers, zeros,
and positive numbers from a list of 50 numbers is given in Fig. 4.31.
Program:
The program for finding the number of occurrences of negative numbers, zeros,
and positive numbers from a list of 50 numbers is given in Table 4.39.
T^bie 4.39 Program for finding the number of occurrences of negative numbers, zeros, positive
numbers in a list
Fig. 4.31 Algorithm for finding the number of occurrences of negative numbers, zeros
and positive numbers in a list
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 121
Table 4.39 Program for finding the number of occurrences of negative numbers, zeros, and
positive numbers in a list (Contd)
Example 4.38:
Given an array 50 bytes long, insert four bytes in the array starting from the tenth
memory location. Assume that the array starts at 9000H and that the string of bytes
to be inserted start at 91 OOH.
Solution:
Step 1: Move all the bytes starting from the 10th memory location, four bytes
down.
Step 2: Insert four bytes at the 10th, 11th, 12th, and 13th memory locations.
Program:
The program for inserting four bytes in an array of 50 bytes is given in
Table 4.40.
Table 4.40 Program for inserting four bytes in an array of 50 bytes
(Contd)
122 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.40 Program for inserting four bytes in an array of 50 bytes (Contd)
JNZ LOOP1 ; If the value of the counter is not equal to zero, repeat
the process.
HLT ; If it is equal to zero, terminate program execution.
Example 4.39:
Given an array 50 bytes long, delete four bytes in the array starting from the 10th
memory location.
Solution:
Move all bytes starting at the 14th memory location up by four bytes.
Program:
The program for deleting four bytes from an array of 50 bytes is given in
Table 4.41.
Table 4.41 Program for deleting four bytes from an array of 50 bytes
(Contd)
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 123
Table 4.41 Program for deleting four bytes from an array of 50 bytes (Contd)
Sample data:
(9000H) = 01H
(9001H) = 02H
(9002H) = 09H
(9003H) = OAH
(9004H) = OBH
Result = (9100H) = 31H
(9101H) = 32H
(9102H) = 39H
(9103H) = 41H
(9104H) = 42H
Flowchart:
The algorithm for converting a hexadecimal number into ASCII code is given in
Fig. 4.32.
Program:
The program for converting a hexadecimal number into ASCII code is given in
Table 4.43.
Table 4.43 Program for converting a hexadecimal number into ASCII code
pig- 4-32 Algorithm for converting a hexadecimal number into ASCII code
126 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.41:
Convert the ASCII number in memory location 9000H into its equivalent decimal
number and store it in 9001H. If the ASCII code is not that of a decimal number,
store FFH in 9001H.
The algorithm for this conversion follows in reverse order, the steps explained in
the previous problem. If the ASCII code value is between 30H and 39H, subtract
30H to get its decimal equivalent.
Piogram:
The program for converting an ASCII number into its decimal equivalent is given
in Table 4.44.
Table 4.44 Program for converting ASCII number into decimal equivalent
LXI H, 9000H ; Load the memory location where the ASCII number is
available in the HL register pair.
MOV A, M ; Load the number in the accumulator.
SUI 30H ; Convert it to decimal form.
CPI OAH ; Check whether it is a valid decimal number.
JC NEXT ; If it is, jump to NEXT.
MVI A, FFH ; If it is not, load FFH in the accumulator.
Example 4.42:
Convert the hexadecimal number in memory location 9000H into its equivalent
decimal number and store it in the succeeding memory locations.
Solution:
This is done by converting the binary value of the hexadecimal number into its
equivalent BCD value. For example, FFH is converted to 255. So, an 8-bit binary
value is converted into 3-digit BCD format. The program checks the number of
100s (hundreds digit) in the binary number by repeated subtraction. From the
remainder, the number of 10s (tens digit) is calculated and then the number of
ones (units digit). These three results form the 3-digit BCD equivalent of the given
binary number (or the decimal equivalent of the given hexadecimal number).
Program:
The program for converting a hexadecimal number into its decimal equivalent is
given in Table 4.45,
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 127
Table 4.45 Program for converting hexadecimal number into decimal equivalent
Example 4.43:
Add even parity to a string of 7-bit ASCII characters and place it in the most
significant bit of each character. The length of the string is in memory location
9000H and the string begins in memory location 9001H. Store the result in the
array starting at memory location 9101H.
Flowchart:
The algorithm for adding even parity to a string of 7-bit ASCII characters is given
in Fig- 4.33.
_______ t______ _
Store ASCII character with parity in
memory location
f
Increment memory pointer
pig. 4.33 Algorithm for adding even parity to a string of 7-bit ASCII characters
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 129
Program:
The program for adding even parity to a string of 7-bit ASCII characters is given
in Table 4.46.
Table 4.46 Program for adding even parity to a string of 7-bit ASCII characters
Example 4.44:
Convert a 2-digit BCD number stored in memory address 9000H into its equivalent
binary form. Store the result in memory location 91 OOH.
Sample data:
(9000H) = 59H
(9100H) = 5 x OAH + 9 = 32H + 9 = 3BH
Flowchart:
The algorithm for converting a BCD number into its equivalent binary form is
given in Fig. 4.34.
Program:
The program for converting a BCD number into its equivalent binary form is given
in Table 4.47.
130 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 4.34 Algorithm for converting BCD number into equivalent binary form
Table 4.47 Program for converting BCD number into equivalent binary form
Table 4,47 Program for converting BCD number into equivalent binary form (Contd)
; Shift the tens digit of the BCD number to the ones place by
RRC
shifting right four times.
MOV B, A ; Move the unpacked tens digit (BCD2) to register B.
XRA A ; Clear- the accumulator, i.e., sum = 0.
MVI D, OAH ; Load the multiplier 10 (OAH) in register D.
SUM: ADD D ; Add register D to the accumulator.
DCRB ; Decrement BCD2.
JNZ SUM ; If the value of BCD2 is not equal to zero, repeat the
process.
ADDC ; If the value of BCD2 is equal to zero, add BCD1 to the
accumulator.
STA9100H ; Store the result.
HLT ; Terminate program execution.
that register in the subroutine. At the end of the subroutine, these register
contents can be replaced in the corresponding registers from the stack.
(iii) Proper use of stack: The processor register contents can be stored in the
stack. However, it must be remembered that stack is a Last In First Out
array of registers. So the data retrieved from the stack is in the reverse order
in which it was stored.
(iv) Retaining return address: The processor also uses the stack to store the
return address in the main program when a subroutine is called. So the stack
content at the start and end of the subroutine execution must be the same.
This can be ensured only if there are an equal number of PUSH and POP
instructions within the subroutine. If this is not ensured, the return address
will be modified and the program may not be executed as desired.
In the following example, the subroutine ASCII converts a hexadecimal digit
to ASCII. The digit is passed using the accumulator and the result is stored in the
accumulator and D and E registers. The HL and BC register pairs are stored in the
stack and retrieved in reverse order.
Example 4.45:
A binary number is stored in memory location 9000H. Write a main program and
a conversion subroutine to convert it into its equivalent BCD form and store the
result in a series of memory locations starting 91 OOH.
Sample data:
(9000H) = 8AH
1. 8AH/64H (Decimal 100) : Divide by 64H (Decimal 100)
8AH/64H Quotient = 1, Remainder = 26H
26H < 64H (Decimal 100) : Go to step 2; Digit 2=1
2. 26H/0AH (Decimal 10) : Divide by OAH (Decimal 10)
26H/0AH Quotient = 3, Remainder = 08H
08H < OAH (Decimal 10) : Go to step 3; Digit 1 = 3
3. Digit 0 = 08H
Flowchart:
The algorithm for converting a binary number into its equivalent BCD form is
given in Fig. 4.35.
Program:
The program for converting a binary number into its equivalent BCD form is
given in Tables 4.48 (a) and 4.48 (b). The subroutine given in Table 4.48 (a)
converts the binary number in the accumulator into three unpacked BCD digits
with the hundreds digit in register E, tens digit in register D, and units digit in the
accumulator.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 133
Fig. 4.35 Algorithm for converting a binary number into its equivalent BCD form
134 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.48 (a) Subroutine for converting a binary number into its equivalent BCD form
Table 4.48 (b) Program for converting a binary number into its equivalent BCD form
Example 4.46:
Write a program to test the RAM by writing ‘1’ in all bit positions and reading it
back and later writing ‘0’ and reading it back. The RAM addresses to be checked
are 9000H to 90FFH. In case of occurrence of an error, it is indicated by writing
FFH in port 40H.
Program:
The program for testing the RAM is given in Table 4.49.
Table 4.49 Program for testing the RAM
ERROR: MVI A, FFH ; If there is an error in the memory, load FFH in the
accumulator.
OUT 40H ; Output it to port 40H.
Example 4.47:
Write a program to generate the Fibonacci series. Store it in the series of memory
locations starting 9000H.
Program:
The program for generating the Fibonacci series is given in Table 4.50.
Table 4.50 Program for generating Fibonacci series
Example 4.48:
Write a subroutine to generate a delay of 1 ms in a processor with crystal frequency
3 MHz.
As we have just learnt, the time delay is produced by loading a register with a
count and then decrementing it until it becomes zero. Here, register C is used for
counting.
Program:
The subroutine for generating a delay of 1 ms is given in Table 4.51.
Table 4.51 Subroutine for generating delay of 1 ms
Now, it is necessary to calculate the count required for executing this program in
1 ms.
Time delay calculation:
In the 8085, the operating frequency is 3 MHz. The time required for executing
each instruction is given in Table 4.51 in T-states. One T-state corresponds to
one clock period. So the time required for execution of the entire program can be
calculated. The instructions DCR C and JNZ LOOP are executed inside the loop
‘count’ times. The other two instructions are executed only once.
Time for execution of the program (in terms of number of T-states) = (10 + 4) x
count + 14
Time for execution of program in seconds = [14 x count + 14] x (time period T)
For a frequency of 3 MHz, time for execution = [14 x count + 14] x (1/3 MHz)
The time delay required is 1 ms.
So, 1ms = [14 x count + 14] x (1/3 MHz). Simplifying this equation gives the
count required for the 1 ms delay. Here, the value of count is approximately 213
(decimal) or D5 (hexadecimal).
Note; In the program given in Table 4.51, the maximum delay is produced by
using a count of 255; the corresponding maximum delay is 1.19ms for a clock
138 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.49:
The delay routine given is in an infinite loop. Identify the error and correct the
program.
Delay routine with error:
DELAY : LXI H, count
LI :DCXH
JNZL1
Solution:
(i) The fault is in the instruction JNZ LI. In the given program this condition
is always true, hence the occurrence of an infinite loop.
(ii) Reason for infinite looping: In the 8085 the instruction DCX H decrements
the HL register pair by one, but does not affect the zero flag. So when
the count reaches 0000H in the HL pair, the zero flag is not affected. The
instruction JNZ LI is, therefore, always true and the loop continues to
execute infinitely. So the HL pair decrements below 0000H to FFFFH and
the execution continues.
(iii) The modification in the program is given in Table 4.52. It is written as a
subroutine.
Table 4.52 Subroutine for producing time delay
Example 4.50:
Write a program for displaying a binary up counter. It should count numbers from
OOH to FFH and increment every 0.5 s. Assume that the operating frequency of the
8085 is equal to 2 MHz and that the display routine is available.
Delay subroutine:
Example 4.51:
Write a program for displaying a BCD up counter. It should count numbers from
00 to 99 and increment every 1 s. Assume that the operating frequency of the 8085
is equal to 3 MHz and that the display routine is available.
Time delay calculation:
Operating frequency = 3 MHz
Time for one T-state = 1/3 MHz = 0.333 ps
Let the external loop multiplier count be 3.
So, time delay required for the inner loop = 1 s/3 = 0.333 s
Required count = [(0.333/T) - 20]/24
Required count = 41624 = A298H
Program:
The program for displaying a BCD up counter is given in Tables 4.54 (a) and
4.54 (b).
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 1 41
Table 4.54 (b) Subroutine for time delay using two loops
Example 4.52:
Write a program for displaying a BCD down counter. It should count numbers
from 99 to 00 and decrement every 1 s. Assume that the operating frequency of
the 8085 is 3 MHz and that the display routine is available.
Flowchart:
The algorithm for displaying a BCD down counter is given in Fig. 4.37.
Program:
The program for displaying a BCD down counter is given in Table 4.55.
142 MICROPROCESSORS AND MICROCONTROLLERS
Example 4.53:
Write a delay routine for producing a delay of 0.5 s. The operating frequency of
the microprocessor is 3.072 MHz.
Time delay calculation:
1 T-state = 1/operating frequency
= 1/(3.072 x 106)
Count = [(0.5/T) - 20J/24 = 62499 = F423H
Delay subroutine:
The subroutine for generating a delay is given in Table 4.56.
Table 4.56 Subroutine for generating delay
Example 4.54:
Write a program to calculate the factorial of a number between zero and eight.
The number is stored in the memory location 9000H. Store the result in 9100H
and9101H.
Flowchart:
The algorithm for finding the factorial of a number between zero and eight is given
in Fig. 4.38.
Program:
The program for finding the factorial of a number between zero and eight is given
in Tables 4.57 (a) and 4.57 (b).
Table 4.57 (a) Program for finding factorial of a number between zero and eight
Table 4.57 (a) Program for finding factorial of a number between zero and eight (Contd)
Subroutine:
This subroutine finds the factorial of the number stored in register E and stores the
result in the DE register pair.
pig. 4.38 Algorithm for finding the factorial of a number between zero and eight
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 1 45
Table 4.57 (b) Subroutine for finding factorial of a number between zero and eight
Example 4.55:
Write a program to find the square root of an 8-bit binary number stored in memory
location 9000H. Store the square root in 9100H.
Flowchart:
Figure 4.39 shows the algorithm followed in the division subroutine used in this
program.
This program assumes that X, whose initial value is taken as 2, is the square root
of Y. For every assumed value of X, ((Y/X) + X)/2 is calculated and this is taken
as the new value of X. If the new value of X equals the earlier value of X, then that
value is taken as the square root of Y.
Program:
The program for finding the square root of an 8-bit number is given in
Tables 4.58 (a) and 4.58 (b).
Table 4.58 (a) Program for finding the square root of an 8-bit number
(Contd)
146 MICROPROCESSORS AND MICROCONTROLLERS
Table 4.58 (a) Program for finding the square root of an 8-bit number (Contd)
CALL DIV ; Call the subroutine DIV to get the initial value of X in
register D.
REP: MOV E, D ; Move the initial value of X to register E.
MOV A, B ; Move the dividend Y to the accumulator.
MOV C, D ; Move the initial value of X to register D.
CALL DIV ; Call the subroutine DIV to get the initial value (Y/X) in
register D.
MOV A, D ; Move Y/X to the accumulator.
ADDE ; Add X to the accumulator content to make it (Y/X) + X.
MVI C, 02H ; Load the divisor (02H) in register C.
CALL DIV ; Call the subroutine DIV to get ((Y/X) + X)/2 in register D.
This is XNEW.
MOV A, E ; Load the initial value of X in the accumulator.
CMPD ; Compare X and XNEW.
Subroutine:
This subroutine loads the dividend in the accumulator, the divisor in register C,
and the quotient in register D.
Table 4.58 (b) Subroutine for finding the square root of an 8-bit number
DIV: MVI D, OOH ; Load OOH in register D as initial value of the quotient.
NEXT: SUB C ; Subtract the divisor from the dividend.
INRD ; Increment the quotient.
CMPC ; Compare the dividend with the divisor.
JNC NEXT ; Repeat the subtraction process until the divisor is lesser
than the dividend.
RET ; Return to the main program.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 1 47
Fig. 4.39 Division subroutine used in finding the square root of an 8-bit number
Example 4.56:
Subtract the BCD number stored in register E from the BCD number stored in the
register D.
Program:
The program for subtracting one BCD number from another is given in
Table 4.59.
Table 4.59 Program for subtracting two BCD numbers
Mnemonics Comments
Note: When two BCD numbers are added, the DAA instruction is used to convert
the result to BCD. Therefore, the subtraction of two BCD numbers is carried out
by 100’s complement method. The 100’s complement of a decimal number is
equal to the 99’s complement plus 1. To subtract two BCD numbers using 100’s
complement method:
(i) Find the 100's complement of the subtrahend (99 - subtrahend +1).
(ii) Add the two numbers using BCD addition method and neglect the carry, if
any.
Example 4.57:
Write a program to multiply two BCD numbers.
Program:
The program for multiplying two BCD numbers is given in Table 4.60.
Table 4.60 Program for multiplying two BCD numbers
ADI OOH ; Add OOH so that the DAA instruction can be used to
convert the result to BCD form.
(Contd)
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 1 49
Example 4.58:
In the program given here, what would be the contents of the registers after
execution of all instructions?
Main program:
8000H: LXI SP, 27FFH
8OO3H: LXI H, 2000H
8OO6H: LXI B, 1020H
8009H: CALL SUB
800CH: HLT
Subroutine:
8I00H: SUB: PUSH B
8101H: PUSHH
8102H: LXI B, 4080H
8105H: LXIH, 4090H
81O8H: SHLD2200H
8109H: DADB
810CH: POPH
810DH: POPB
810EH: RET
Table 4.61 gives the instruction sequence and the contents of all registers and the
stack after execution of each instruction.
Table 4.61 Register and stack contents during program execution (Contd)
POINTS TO REMEMBER
KEY TERMS
Access time The time required to translate the assembly code to object code is called
access time.
Arithmetic operations The 8085 microprocessor performs various arithmetic operations
such as addition, subtraction, increment, and decrement.
Assembler It translates the assembly language program text, which is given as input to
the assembler, to its binary equivalent known as object code. The assembler checks for
syntax errors and displays them before giving the object code.
ASSEMBLY LANGUAGE PROGRAMMING OF 8085 151
REVIEW QUESTIONS
Part 2
--- ----------------- -------------------------------------
HARDWARE
.RFACING WITH
INTEL 8085
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Different approaches to data transfer
• Different data transfer mechanisms
• Serial data transfer protocols
• Data transfer for slow peripheral devices
• Intel 8085 interrupt types and sources
• Interrupt vectors and priorities
• Interrupt masking
• Timing of interrupts
.. .11111101)1 .1 II.I UH. ....I V. 1.1.UH. . m. , . . J,,,,,,. t w
A is shown being transmitted on the line with the parity bit as 1. The format uses
two stop bits of logic 1 consecutively.
Another parameter specified by the RS-232 communication standard is the
baud rate. It is the rate at which data is transmitted and received. The baud rate
and the timing for each bit is related by the following formula:
Time period for each bit in seconds = 1/baud rate
Table 5.1 lists the standard baud rate Table 5.1 Bit timings for standard baud rates
used by the RS-232 communication
Baud rate Time for each bit in
standard and the corresponding bit microseconds
duration.
RS-232 communication connection 1200 833
is done through standard connectors. 2400 417
Two types of RS-232 connectors are 9600 104
available. One has 25 pins and the other 19,200 52
has nine. Details of the DB25S and
DB9S connectors are shown in Fig. 5.3. A cable with any one of these connectors
is used to connect the DTE (computer) with the DCE (modem).
Pin Signal
13 12 11 10 9 8 7 6 5 4 3 2 1 2 TXData
ooooooooooooo 3 RXData
oooooooooooo 4 RTS
25 24 23 22 21 20 19 18 17 16 15 14 , 5 CTS
6 DSR
(a) 7 GND
20 DTR
Pin Signal
2 RXData
5 4 3 2 1 3 TXData
o o o o o 4 DTR
o o o o 5 GND 1
9 8 7 6 6 DSR
7 RTS
(b)
8 CTS
Fig. 5.3 Basic details of RS-232 (a) DB25S and (b) DB9S connectors
The basic signals used in these connectors are given in Table 5.2.
Table 5.2 Signals of RS-232 connection
Figure 5.4 shows the standard connection for RS-232 communication between
two DTEs through two modems.
The system uses all handshake signals such as RTS, CTS, DTR, and DSR
mentioned in Table 5.2. Although the figure does not indicate the handshake
signals between the two modems, some amount of signal transfer takes place
between the modems also.
If there is no modem, and two processors or computer systems are directly
connected using the serial communication line, the connection shown in Fig. 5.5 is
used. Here, the handshake signals are not used, since they are connected within the
DTE system itself. The communication assumes that the receiver is always ready
to receive data.
(ii) RS-485 can be used for communication over longer distances than RS-232,
(iii) RS-485 can communicate with higher baud rates, i.e., faster than RS-232,
(iv) One RS-485 transmitter can drive up to 32 receivers in a network.
(v) RS-485 transmitter uses two signal lines—Fsig and -sig. The RS-485
receiver senses the voltage difference between these lines. So, any voltage
difference on the ground line between the transmitter and the receiver does
not affect the reception. However, RS-232 receiver senses the voltage level
of the signal with respect to the ground and so, the noise voltage level may
affect the data sensed.
By default, all the senders on the RS-485 bus are tri-stated, i.e., in high
impedance state. In higher-level protocols, one of the nodes is defined as a master
that sends queries or commands over the RS-485 bus. All other nodes receive these
data. Depending of the information contained in the data sent, zero or more nodes
on the line respond to the master. In this situation, bandwidth of almost 100% can
be used. There are other ways of implementing the RS-485 network, where every
node can start a data session on its own. This is comparable to the way Ethernet
networks function. Since there is a possibility of data collision in this type of
implementation, theoretically only 37% of the bandwidth will be effectively used.
With such an implementation of an RS-485 network, it is necessary to implement
error detection in the higher-level protocol so as to detect data corruption and
resend the information later.
for this signal and once the NDAC is high, the DAV signal is removed by the
talkers.
The GPIB uses active low logic with standard TTL levels. For example, when
DAV is active, the devices send a TTL low level (<0.8V), and when DAV is
made inactive, the line has a TTL high level (>2.0V).
stack pointer is called push operation. Reading a data from the stack is called pop
operation.
Stack is used by the interrupt system of the microprocessor for implementing
the subroutine call and return mechanism, passing parameters to subroutines, etc.
When the transfer of control takes place from the interrupted program to the ISR,
the program counter content is stored in the stack, because after the execution of
the ISR, the control must return to the program counter content. To facilitate this
control transfer, the stack pointer must be properly initialized to a physically available
memory with sufficient memory range. In the 8085, the stack memory grows towards
lower addresses and so, the stack pointer must be initialized with the highest memory
address allotted for the stack operation.
The stack can be accessed by the instructions PUSH and POP. The ISRs should
not disturb the return address stored by the processor in the stack. So, the ISRs
should have equal number of PUSH and POP instructions. This condition ensures
that the return address stored in the stack is retrieved properly by the processor.
RST0 C7 0000H
RST 1 CF 0008H
RST 2 D7 001 OH
RST 3 DF 0018H
RST 4 E7 0020H
RST 5 EF 0028H
RST 6 F7 0030H
RST 7 FF 0038H
The software instructions can be treated as CALL instructions with default call
locations. The concept of priority does not apply to software interrupts as they are
inserted into the program as instructions by the programmer and executed by the
processor when the respective program lines are read.
168 MICROPROCESSORS AND MICROCONTROLLERS
The interrupt vector addresses for the hardware interrupts are given in Table
5.4. It can be seen that the four interrupts Trap, RST 7.5, RST 6.5, and RST 5.5
have fixed interrupt vector addresses. However, INTR does not have a fixed
interrupt vector address, since Intel has designed the INTR interrupt in a different
manner. When the INTR interrupt sent by an external device is recognized by the
processor, it gives an active low interrupt acknowledgement (INTA) signal. Upon
receiving this active low signal, the peripheral device that issued the INTR signal
should now place the machine code of the RST instruction to be executed by the
processor on the data bus of the processor. So the processor after issuing low
INTA signal, reads the instruction available on the data bus and places it in the
instruction register for decoding and executing.
Interrupt priority decides which interrupt must be serviced when more than one
interrupt is sensed by the processor. As there are five interrupts, it is possible that
more than one interrupt signal will be applied to the processor by the peripheral
devices. The interrupt priority indicates the order in which the interrupts must be
serviced. From Table 5.4, it can be seen that Trap is the highest priority interrupt,
followed by RST 7.5, RST 6.5, and RST 5.5. INTR has the least priority.
In addition to priorities, Intel has provided a feature using which a programmer
can suppress the hardware interrupts. This can be done by masking the interrupts.
TRAP is the only NMI; the other four interrupts can be masked.
METHODS OF DATA TRANSFER AND INTERRUPT STRUCTURE IN 8085 1 69
I
I
Bit position D7 D6 D5 D4 D3 D2 D1 DO
Explanation Serial Serial Not Reset Mask set Set to Set to Set to
data data used RST 7.5 enable— 1 to 1 to 1 to
to be enable— flip-flop Set to 1 mask mask mask
sent set to to mask RST RST RST
1 for interrupts 7.5 6.5 5.5
sending
The least significant three bits D2-D0 are used to individually mask the
three RST interrupts, as shown in Table 5.5. These bits are made 0 to unmask
the interrupts and 1 to mask the interrupts. In addition, a master control is also
provided in the D3 bit. This bit must also be set to 1 to make the least significant
three bits meaningful. Otherwise, the data in the least significant three bits are
ignored by the processor.
As already discussed, the RST 7.5 is an edge-triggered interrupt and a separate
flip-flop is used to recognize it. This flip-flop can be reset, thereby ignoring the
RST 7.5 interrupt. This is done by making the D4 bit 1.
In addition to masking interrupts, the SIM instruction has another function. It
can send serial data on the SOD line of the processor. The data to be sent is placed
in the MSB of the accumulator and the serial data output is enabled by making the
D6 bit 1.
Bit D7 D6 D5 D4 D3 D2 D1 DO
position
Any other interrupt issued to the microprocessor will be recognized once the
EI instruction is executed. If the programmer has written the EI instruction at
the start of the ISR, the microprocessor can be interrupted once again before the
completion of the ISR.
+5V
POINTS TO REMEMBER
• Different data transfer schemes are available for data transfer between two processors
or between a processor and an I/O device.
• The different data transfer schemes are programmed data transfer and DMA, polled and
interrupt-driven, and serial and parallel data transfer.
• Various serial port standards such as RS-232, RS-485, IEEE488, and GPIB are used for
data transfer in different applications.
• Interrupts are an important mechanism available in the processors to temporarily stop
current program execution and execute a program of higher priority.
• Interrupt vector addresses and the source, priorities, and timing of interrupts are very
important to program and understand the operation of interrupts in a processor.
• Interrupts can be either hardware generated and random, or software generated and
programmed.
• The processor can be interrupted before the completion of an interrupt service
routine (ISR) if the program has executed the EI instruction. This enables nested ISR
execution.
KEYTERMS :
DMA It is a special method of data transfer between I/O devices and memory without the
need of processor for data transfer.
I/O-mapped I/O scheme This scheme uses special control lines and different address
space for accessing I/O devices. The processor needs separate instructions for I/O-mapped
I/O access.
Interrupt priorities The sequence or order in which the interrupts are sensed by the
microprocessor. This order decides which ISR will be executed first, when more than one
interrupt is applied simultaneously to the processor.
Interrupt service routine The routine executed by the processor upon sensing an
interrupt signal is called interrupt service routine.
Interrupt vector address It is the location to which program control is transferred, upon
receipt of an interrupt.
Interrupt-driven I/O scheme This scheme uses a special signal from the I/O devices to
initiate a data transfer by the processor.
Memory-mapped I/O scheme This scheme uses the same instructions and hardware
used for memory accesses, for accessing I/O devices.
Parallel data transfer It is the method in which all the bits of a word are transmitted
simultaneously.
Polled I/O transfer This method uses a software routine to access and transfer data
between processor and I/O devices.
RIM It is an 8085 instruction to read the status of the interrupt masks and pending
interrupts.
174 MICROPROCESSORS AND MICROCONTROLLERS
Serial data transfer It is the method of transferring a single bit at a time over a
transmission line.
SIM It is an 8085 instruction to mask or unmask the hardware interrupts.
REVIEW QUESTIONS"
1. What can be done to make the 8085 microprocessor start executing the instructions at
the address 003CH?
2. What are the ways to identify the device that has interrupted the processor in a
microprocessor-based system?
CHAPTER 6 |
6.1 INTRODUCTION
The programs and data that are executed by the microprocessor have to be stored
in ROM/EPROM and RAM, which are basically semiconductor memory chips.
The programs and data that are stored in ROM/EPROM are not erased even
when the power supply to the chip is removed. Hence, ROM/EPROM is called
non-volatile memory. It can be used to store permanent programs (e.g., monitor
program, also known as system start-up program) and data (e.g., look-up table),
which are necessary in microprocessor-based systems. The difference between
ROM and EPROM is that a ROM chip can be programmed only once, whereas an
EPROM chip can be programmed many times after erasing the previously stored
contents. The contents of an EPROM chip can be erased by passing UV rays for a
few minutes through the quartz window situated at the top of the chip.
In a RAM, stored programs and data are erased when the power supply to the
chip is removed. Hence, RAM is called volatile memory. Programs and data that
are modified often are stored in the RAM. Examples of such programs and data
include programs written during software development for a microprocessor-based
system, programs written when one is learning assembly language programming,
and data entered while testing these programs. RAM is also used to store data
that are variable in nature. For example, data to be used by the programmer for
arithmetic and logic operations can be stored in the RAM. Data can only be read
from a ROM or EPROM, whereas it can be written into and read from a RAM.
Input and output devices, which are interfaced to the 8085, are essential in any
microprocessor-based system. They can be interfaced using two schemes—I/O-
mapped I/O and memory-mapped I/O. In the I/O-mapped I/O scheme, the I/O
176 MICROPROCESSORS AND MICROCONTROLLERS
devices are treated differently from memory. In the memory-mapped I/O scheme,
each I/O device is assumed to be a memory location.
This chapter discusses the interfacing of the following:
(i) EPROM and RAM chips with the 8085—using address decoders made of
either logic gates or decoder ICs (e.g., 74LS138)
(ii) I/O devices with the 8085—using I/O-mapped I/O and memory-mapped
I/O schemes
IO/M RD WR Operation
Using the IO/M, RD, and WR signals, two control signals MEMR (memory
read) and MEMW (memory write) are generated, such that only MEMR is in logic
0 when data is being read from the RAM or EPROM and only MEMW is in logic
0 when data is being written into the RAM. Figure 6.1 shows the circuit used for
generation of MEMR and MEMW signals.
Table 6.2 shows the relation between the 8085’s control signals and the memory
chip’s control signals.
Table 6.2 Relation between 8085's control signals and memory chip’s control signals
When the IO/M signal is high, both memory control signals are deactivated
(i.e., in logic high state) irrespective of the status of the RD and WR signals. This
is shown in the third row of Table 6.2.
Table 6.3 Memory locations selected for various values of address inputs in IC 2764
1 1 1 1 1 1 1 1 1 1 1 1 0 0 8191s' location
1 1 1 1 1 1 1 1 1 1 1 1 1 0 8192nd location
XXXXXXXXXXXXX1 (Chip is not
selected)
Let us now discuss the interfacing of EPROM chip with the 8085 using logic
gates and 74LS138 decoder IC.
6.2.2.11nterfacing EPROM Chip with 8085 using Logic Gates
While the 8085 has 16 address lines, the number of address lines in an EPROM chip
depends on the storage capacity of the chip. For example, an 8K x 8 EPROM chip
has only 13 address lines. The lower-order address lines (A0-A12) of the 8085 are
connected to the address lines (A0-A12) of the 8K x 8 EPROM chip. The remaining
address lines (A13-A15) of the 8085 are connected to the address decoder, the
output of which is given to the chip enable pin (CE) of the EPROM. The memory
read signal (MEMR) from the 8085 is given to the OE pin of the EPROM. The data
lines (D0-D7) of both the 8085 and the EPROM are connected together.
The following examples illustrate this concept.
Example 6.1:
Interface an IC 2764 with the 8085 using NAND gate address decoder such that
the address range allocated to the chip is 0000H-1FFFH.
Solution:
The 13 address lines (A0-A12) of IC 2764 are connected to the corresponding 13
address lines (A0-A12) of the 8085. The remaining address lines (A13-A15) of
the 8085 are connected to the address decoder formed using logic gates, the output
of which is connected to the CE pin of IC 2764. To design the address decoder,
the address range allocated to the chip (0000H-1FFFH) is first written in binary
form as shown in the Table 6.4. Since IC 2764 has 13 address lines, a partition
is made between A0-A12 and the remaining address lines A 13-Al5, which are
shown in bold.
INTERFACING MEMORY AND I/O DEVICES WITH 8085 179 •
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000H
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0001H
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1FFEH
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1FFFH
From Table 6.4, it is observed that for the addresses 0000H to 1FFFH, the value
in the address lines A15, A14, and A13 is 0. Therefore, the address decoder can
be designed using a NAND gate and three inverters to produce a NAND output
of 0 whenever the value in the address lines A15, A14, and A13 is 0. (Note: In a
NAND gate, the output is 0 only when all inputs are 1.) The output of the NAND
gate is connected to the CE (chip enable) pin of IC 2764 so that the chip is enabled
whenever the 8085 places an address allocated to the EPROM chip in the address
bus. This is shown in Fig. 6.3. It is assumed that the circuit for de-multiplexing
lower order address bus and data bus and the circuit for generating MEMR are
available separately and these are not shown in Fig. 6.3.
Example 6.2:
Interface a 27128 EPROM (16K x 8 bits or 16KB) IC with the 8085 using a
NAND gate address decoder such that the starting address assigned to the chip is
C000H.
180 MICROPROCESSORS AND MICROCONTROLLERS
Solution:
Since 16KB = 214 bytes, the number of address lines in IC 27128 is 14 (A0-A13).
The 14 address lines of IC 27128 are connected to the corresponding 14 address
lines (A0-A13) of the 8085. The remaining address lines (A 14 and A15) of the
8085 are connected to the address decoder formed using logic gates. The output of
the address decoder is connected to the CE pin of IC 27128.
The starting address assigned to the chip is C000H. To find the ending address,
add the hexadecimal value of a 14-bit binary number with all bits equal to 1 to the
starting address of the chip. (Note: The binary number is assumed to be 14 bits
long as the number of address lines in the IC is 14.)
14-bit binary number with all bits equal to 1----------► 11 1111 1111 1111
V I I I
Corresponding hexadecimal number-------------------- > 3 F F F
The ending address of the chip is C000H + 3FFFH = FFFFH
The addresses C000H to FFFFH are written in binary form in Table 6.5. Since IC
27128 has 14 address lines a partition is made between A0-A13 and the remaining
address lines A14 and A15, which are shown in bold.
Table 6.5 Interfacing IC 27128 with 8085 (Example 6.2)
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C000H
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 C001H
• •
• •
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 FFFEH
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 FFFFH
From Table 6.5, it is observed that for the addresses C000H to FFFFH, the value in
the address lines A15 and A14 is 1. Therefore, an address decoder can be designed
using a NAND gate to produce a NAND output of 0 only when A15 = A14=1.
The designed circuit is shown in Fig. 6.4.
inputs Outputs
Enable inputs Select inputs
G1 G2A G2B c B A Y7 Y6 Y5 Y4 Y3 Y2 Y1 YO
X 1 X X X X 1 1 1 1 1 1 1 1
X X 1 X X X 1 1 1 1 1 1 1 1
0 X X X X X 1 1 1 1 1 1 1 1
1 0 0 0 0 0 1 1 1 1 1 1 1 0
1 0 0 0 0 1 1 1 1 1 1 1 0 1
1 0 0 0 1 0 1 1 1 1 1 0 1 1
1 0 0 0 1 1 1 1 1 1 0 1 1 1
1 0 0 1 0 0 1 1 1 0 1 1 1 1
1 0 0 1 0 1 1 1 0 1 1 1 1 1
1 0 0 1 1 0 1 0 1 1 1 1 1 1
1 0 0 1 1 1 0 1 1 1 1 1 1 1
Solution:
Since 8K x 8 EPROM has 13 address lines (A0-A12), the ending address of the
chips is obtained by adding 1FFFH (i.e., the binary number with thirteen Is) with
the starting address assigned to each chip. Therefore, the ending addresses are
1FFFH and 9FFFH, respectively. The addresses assigned to the two chips are
shown in binary form in Table 6.7. The set of addresses assigned to different
memory chips in a system is called a memory address map.
Table 6.7 Addresses assigned to EPROM chips 1 and 2 (Example 6.3)—Address Map
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1FFFH
(for chip
1)
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8000H
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 8001H
1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 9FFFH
(for chip
2)
For EPROM chip 1 (with address range 0000H-1FFFH), the address lines
A15, A14, and Al3 carry 0. For EPROM chip 2 (with address range 8000H-
9FFFH), the address line A15 carries 1 and A14 and A13 carry 0. The address
lines A15, A14, and A13 of the 8085 are connected to the IC inputs C, B, and A,
respectively. The decoder is enabled by connecting G1 to Vcc and G2A and G2B
to ground.
Figure 6.6 shows the interfacing of the two EPROM chips with the 8085,
using 74LS138 IC. The Y0 output of the decoder is connected to the chip enable
(CE) pin of the EPROM chip 1 whose address range is 0000H-1FFFH. When
these addresses are placed in the address bus by the 8085, the select inputs of
the decoder take the value C = B = A = 0. Hence Y0 output of the decoder goes
low, selecting EPROM chip 1. The output Y4 of the decoder is connected to the
chip enable pin of EPROM chip 2. When the 8085 places any of the addresses
from 8000H to 9FFFH on the address bus, the select inputs of the decoder take
the values C = 1 and B = A = 0. The Y4 output of the decoder goes low, thereby
selecting EPROM chip 2.
INTERFACING MEMORY AND I/O DEVICES WITH 8085 183
Figure 6.7 shows the diagram of IC 6264 (8K x 8) RAM chip. Here Vcc and
Vss represent the power supply and ground terminals, respectively.
6.2.3.1 Interfacing RAM Chip with 8085 using Logic Gates
The interfacing of a RAM chip with the 8085 is the same as that of an EPROM
chip with the 8085, except that one more signal MEMW is used. This signal is
connected to the WE pin of the RAM chip. The following example illustrates this
concept.
Example 6.4:
Interface a 6264 IC (8K x 8 RAM) with the 8085 using a NAND gate decoder
such that the starting address assigned to the chip is 4000H,
184 MICROPROCESSORS AND MICROCONTROLLERS
Solution:
NC
The 6264 IC has 13 address
A12 WE
lines (A0-A12), since 8 KB =
213 bytes. The ending address A7 CE2
Example 6.5:
Interface two 6116 (2K x 8 RAM) ICs with the 8085 using 74LS138 decoder such
that the starting addresses assigned to them are 8000H and 9000H, respectively.
Solution:
The 6116 IC has 11 address lines (A0-A10) since 2KB = 211 bytes. The ending
addresses of 6116 chip 1 and 6116 chip 2 are 87FFH and 97FFH, respectively.
Table 6.9 shows the binary form of the addresses 8000H to 87FFH and 9000H to
97FFH assigned to the chips.
Figure 6.9 shows the interfacing of these RAM chips with the 8085. The
address lines A0-A10 of the 8085 are connected to the address lines A0-A10 of
the RAM chip. Since the 74LS138 decoder has three select inputs C, B, and A,
three address lines of the 8085 that have a specific value for a particular RAM chip
must be connected to them. From Table 6.9 we can see that A13 = A12 = A11 =
0 for the addresses assigned to RAM chip 1 and that A13 = 0, A12 = 1, and Al 1
= 0 for the addresses assigned to RAM chip 2. The remaining address lines of the
8085, whose values are constant for the address ranges assigned to the two RAM
chips, are connected to the enable inputs of the decoder. Since A15 = l and A14
= 0 they are connected to the decoder’s active high and active low enable inputs,
respectively.
Fig. 6.9 Interfacing two 6116 RAM chips using IC 74LS138 (Example 6.5)
When the 8085 places any of the addresses between 8000H and 87FFH in the
address bus, the select inputs C, B, and A of the decoder are all 0. The Y0 output
of the decoder also becomes 0, selecting RAM chip 1. When the 8085 places any
of the addresses between 9000H and 97FFH in the address bus, the select inputs C,
B, and A of the decoder are 0, 1, and 0, respectively. The Y2 output of the decoder
becomes 0, selecting RAM chip 2.
186 MICROPROCESSORS AND MICROCONTROLLERS
Table 6.9 Addresses assigned to the two 6116 RAM chips (Example 6.5)
1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 87FFH
(RAM
chip 1)
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 9000H
1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 97FFH
(RAM
chip 2)
Example 6.6:
While interfacing a 16K x 8 EPROM chip and a 16K x 8 RAM chip with the 8085,
both having only one active low chip enable signal (CE), the address line A15 of
the 8085 is directly connected to the CE of the EPROM chip. The same line is
connected to the CE of the RAM chip through an inverter. Find the address ranges
that are assigned to each chip.
Solution:
Since 16K = 214, the number of address lines in a 16K x 8 EPROM chip and a
16K x 8 RAM chip is 14 (A0-A13). The 14 address lines A0-A13 of the 8085
are connected to the corresponding 14 address lines (A0-A13) of the 16K x 8
EPROM chip and the 16K x 8 RAM chip. Among the remaining address lines
INTERFACING MEMORY AND I/O DEVICES WITH 8085 187
(A14 and A15) in the 8085, A14 is left unconnected and only A15 is used to select
the memory chips as given in the problem.
Since the address line A15 of the 8085 is directly connected to the CE pin
of the EPROM chip and to the CE pin of the RAM chip through an inverter,
the EPROM chip is selected when A15 = 0 and the RAM chip is selected when
A15 = 1. This is irrespective of the value in the address line A14 (either 0 or 1), as
it is left unconnected. The addresses assigned to the two chips can be found using
this concept.
Address range assigned to 16K x 8 EPROM chip
(i) When A 14 = 0
A15 A14 A13 A12 All A10 A9 A8 A7 A6 A5 A4 A3 A2 Al AO
0000000000000000
= 0000H (lowest address)
0 0 11111111111111
= 3FFFH (highest address)
(ii) When A14 = 1
A15 A14A13 A12A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 Al A0
0100000000000000
= 4000H (lowest address)
0 111111111111111
= 7FFFH (highest address)
So address ranges 0000H-3FFFH and 4000H-7FFFH can be used to access
the EPROM chip.
Address range assigned to 16K x 8 RAM chip
(i) When A14 = 0
A15 A14A13 A12A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 Al A0
1000000000000000
= 8000H (lowest address)
10 11111111111111
= BFFFH (highest address)
(ii)When A14 = 1
A15 A14A13 A12 All A10 A9 A8 A7 A6 A5 A4 A3 A2 Al A0
1 100000000000000
= C000H (lowest address)
1111111111111111
= FFFFH (highest address)
So the address ranges 8000H-BFFFH and C000H-FFFFH can be used to
access the RAM chip.
Table 6.10 shows the values in the IO/M, RD, and WR Lines of the 8085, and
the corresponding values in the signals IOR and IOW, during the I/O read and I/O
write operations.
Table 6.10 Status of IOR and IOW signals in 8085
Example 6.7:
Interface an 8-bit DIP switch with the 8085 such that the address assigned to the
DIP switch is FOH.
Solution:
The instruction IN FOH is used to get the data from the DIP switch and store it in
the accumulator. The steps involved in the execution of the instruction IN FOH are
as follows.
(i) The address in the IN instruction, which is FOH in this case, is placed in the
address lines A0-A7 and a copy of it in the address lines A8-A15.
INTERFACING MEMORY AND I/O DEVICES WITH 8085 189
(ii) The IOR signal is activated (i.e., IOR = 0), which makes the selected input
device to place its data in the data bus.
(iii) The data in the data bus is read and stored in the accumulator.
Figure 6.11 shows the interfacing of a DIP switch so as to assign the address
FOH to it.
When the address FOH is placed in the address bus (A0-A7) during the execution
of IN FOH by the 8085, the value in the address lines A0-A7 is as follows:
A7 A6 A5 A4 A3 A2 Al AO
1 1 1 1 0 0 0 0 = FOH
These address lines are connected to a NAND gate address decoder such
that the output of the NAND gate is 0 under this condition. The output of the
NAND gate decoder is ORed with the IOR signal and the output of the OR gate
is connected to the enable inputs 1G and 2G of the 74LS244. When the NAND
gate decoder’s output and IOR are both 0, the 74LS244 is enabled and data from
the DIP switch is placed in the data bus of the 8085. The 8085 reads that data and
places it in the accumulator. Thus, the data from the DIP switch is transferred to
the accumulator.
Example 6.8:
Interface a set of eight LEDs in common cathode configuration with the 8085 such
that the address assigned to them is F8H.
Solution:
The instruction OUT F8H is used to send the data in the accumulator to the LEDs.
The steps involved in the execution of the instruction OUT F8H are as follows:
(i) The address F8H is placed in the address lines A0-A7 and a copy of it in
the lines A8-A15.
190 MICROPROCESSORS AND MICROCONTROLLERS
(ii) The data in the accumulator is placed in the data bus (D0-D7).
(iii) The IOW signal (i.e., IOW = 0) is activated for some duration, during which
the data gets stored in the external latch.
Figure 6.12 shows the interfacing of a set of eight LEDs with the 8085.
Fig. 6.12 Interfacing set of eight LEDs with the 8085 using 74LS373 (Example 6.8)
When the address F8H is placed in the address bus (A0-A7) during the
execution of the instruction OUT F8H by the 8085, the value in the address lines
A0-A7 is as follows:
A7 A6 A5 A4 A3 A2 Al A0
1 1 1 1 1 0 0 0 = F8H
When the instruction OUT F8H is executed by the 8085, the address F8H is
placed in the address bus (A0-A7), the data in the accumulator is placed in the
data bus (D0-D7), and the IOW signal is activated. This causes the activation of
the clock input (CLK) of the 74LS373 (i.e., it is made 1) since both AND gates
produce logic 1 output. This latches the value in the data lines.
Example 6.9:
Interface an 8-bit DIP switch with the 8085 using logic gates such that the address
assigned to it is FOFOH.
Solution:
Since a 16-bit address has to be assigned to a DIP switch, the memory-mapped
VO technique must be used. Using LDA FOFOH instruction, the data from the
8-bit DIP switch can be transferred to the accumulator. The steps involved in the
execution of the instruction LDA FOFOH are as follows:
(i) The address FOFOH is placed in the address lines A0-A15.
(ii) The MEMR signal is made low (i.e. logic 0) for some time.
(iii) The data in the data bus is read and stored in the accumulator.
Figure 6.13 shows the interfacing of an 8-bit DIP switch with the 8085 so as to
assign the address FOFOH to it.
When the 8085 executes the instruction LDA FOFOH, it places the address
FOFOH in the address lines A0-A15 as follows:
A15 A14A13 A12 All A10 A9 A8 A7 A6 A5 A4 A3 A2 Al A0
1111000011110000
= FOFOH
Since 8-input AND gate ICs such as CD4068B are available, these address
lines are connected to two 8-input AND gates so that the outputs of both the AND
192 MICROPROCESSORS AND MICROCONTROLLERS
gates are 1 when the address FOFOH is placed in the address bus (A0-A15) by
the 8085. These two outputs along with the inverted MEMR signal are given to a
NAND gate so that its output becomes 0 when MEMR = 0, thereby enabling the
buffer (74LS244). The data from the DIP switch is placed in the 8085’s data bus.
The 8085 reads the data from the data bus and stores it in the accumulator.
Example 6.10:
Interface a seven-segment LED display with common cathode connection to the
8085 so as to assign the address FFF3H to it.
Solution:
Since a 16-bit address has to be assigned to the seven-segment LED display,
memory-mapped I/O technique must be used. Using instructions such as STA
FFF3H, the data in the accumulator can be sent to the LED to display a particular
digit. The steps involved in the execution of the instruction STA FFF3H are as
follows:
(i) The address FFF3H is placed in the address lines A0-A15.
(ii) The contents of the accumulator are placed in the data bus (D0-D7).
(iii) The MEMW signal is activated (i.e., made 0) for some duration.
Figure 6.14 shows the interfacing of a seven-segment LED with the 8085 using
memory-mapped I/O scheme.
When the 8085 executes the instruction STA FFF3H, the address FFF3H is
placed in the address lines A0-A15 as follows:
A15 A14A13 A12 All A10 A9 A8 A7 A6 A5 A4 A3 A2 Al AO
1111111111110 0 11
=FFF3H
The address lines are connected to the inputs of two AND gates such that
both the AND gates produce 1 at their output for the address FFF3H. These two
outputs along with the inverted MEMW signal from the 8085 are given as inputs
to another AND gate whose output is connected to the CLK input of the latch.
Therefore, when the 8085 executes the instruction STA FFF3H, the clock input
of the 74LS374 becomes l.The data in the data bus, which is the content of the
accumulator, is stored in the latch.
Example 6.11:
While interfacing an 8-bit DIP switch with the 8085 using I/O-mapped I/O scheme
as given in Example 6.7, if the address lines A7 and A6 of 8085 are not connected
to the NAND gate shown in Fig. 6.11, find the addresses assigned to the DIP
switch.
Solution:
Since A7 and A6 of the 8085 are not connected to the address decoder, they can
take any value. There are four combinations of values that A7 and A6 can take,
based on which four different addresses are obtained for the same DIP switch.
(i) When A7 = A6 = 0
A7 A6 A5 A4 A3 A2 Al A0
0 0 1 1 0 0 0 0 = 30H
(ii) When A7 = 0 and A6 = 1
A7 A6 A5 A4 A3 A2 Al A0
0 1 1 1 0 0 0 0 = 70H
(iii) When A7 = 1 and A6 = 0
A7 A6 A5 A4 A3 A2 Al A0
1 0 1 1 0 0 0 0 = BOH
194 MICROPROCESSORS AND MICROCONTROLLERS
(iv) When A7 = A6 = 1
A7 A6 A5 A4 A3 A2 Al AO
1 1 1 1 0 0 0 0 = FOH
Therefore, the DIP switch can be accessed using any of these four addresses.
POINTS TO REMEMBER1
• Any microprocessor-based system needs memory and VO ports to be interfaced with it.
Systems based on the 8085 need at least two memory chips—one RAM and one ROM.
Memory address maps are important and should differentiate the addresses for different
memory chips in a system.
• The memory can be interfaced with the processor using address lines, data lines, and
control signals from the decoder and the processor. The control signals IO/M, RD, and
WR play an important role in interfacing.
• Address decoding is necessary to select a particular memory chip in a system. Chip
selection uses higher-order address lines and can be done in two ways—absolute
address decoding and partial address decoding.
• Input/output devices also follow the same interfacing concepts as memory chips.
• Input/output devices such as switches and LEDs can be interfaced to the 8085 using
either memory-mapped or peripheral-mapped interfacing techniques.
KEY TERMS
Absolute address decoding It is a decoding technique in which all the bits of the address
bus are used for accessing and decoding the addresses of memory chips.
EPROM It is a semiconductor memory in which the data can be programmed many
times. Data erasure can be done by the user. There are two types of erasures—one by
electrical means and the other using ultraviolet rays.
Memory-mapped I/O It is the technique by which input/output devices are addressed
using 16-bit memory addresses and accessed like memory locations, using memory access
instructions such as LDA, STA, etc.
MEMR and MEMW These are control signals for reading from and writing into
memory. Generally, these two control signals are generated using the IO/M, RD, and WR
signals of the 8085.
Partial address decoding It is a decoding technique in which all address bits are not used
for decoding. In particular, some higher-order bits are not used in decoding and selection
of memory locations.
Peripheral- or I/O-mapped I/O It is the technique by which input/output devices are
addressed using 8-bit I/O addresses and accessed using IN and OUT instructions.
RAM It is a semiconductor memory in which both read and write operations are possible.
IC 6264 is an 8K RAM chip with 8-bit words.
REVIEW QUESTIONS
3. How much memory, in terms of bytes, can be interfaced with the 8085? Why?
4. What are the logic levels in the IO/M, RD, and WR lines of the 8085 during the
different machine cycles?
5. Draw the circuit used to generate the MEMR and MEMW signals in the 8085.
6. What is the function of CE and OE signals in an EPROM chip?
7. What is the function of CE, OE, and WE signals in a RAM chip?
8. What are the criteria to be considered before interfacing memory to a processor?
9. Differentiate between partial decoding and absolute decoding for device (memory and
I/O) selection. Give an example.
10. What are the differences between memory-mapped I/O and I/O-mapped I/O
schemes?
11. Calculate the maximum number of input/output devices that can be interfaced with an
8085-based system that needs 16 KB of memory while using
(i) I/O-mapped I/O scheme
(ii) Memory-mapped I/O scheme.
12. Why is a tri-state buffer required to interface an input device with the 8085?
13. Why is a latch required to interface an output device with the 8085?
14. What is the function of 1G and 2G signals in IC 74LS244?
15. What is the function of CLK and OC signals in IC 74LS373?
NUMERICAL/DESIGN-BASED EXERCISE
1. What is the output of a three-input NAND gate for the following inputs?
(i) 0, 0, 1
(ii) 1, 1,0
(iii) 1, 1, 1
2. In a 74LS138 decoder that is in enabled condition, which output of the decoder goes
low under the following conditions?
(i) C= l,B = 0, A = 1
(ii) C = 0, B = l,A = 0
3. Interface an 8K x 8 EPROM chip and a 32K x 8 RAM chip with the 8085, using
logic gates, such that the starting addresses assigned to them are 4000H and 8000H,
respectively.
4. Interface two 8K x 8 RAM chips and a 8K x 8 EPROM chip with the 8085, using
a 74LS138 decoder, such that the starting addresses assigned to them are 6000H,
8000H, and 0000H, respectively.
5. In an 8085-based system, only an 8K x 8 EPROM chip and an 8K x 8 RAM chip
are needed. Interface these chips with the 8085 using only one inverter, such that the
starting addresses assigned to the EPROM and RAM chips are 0000H and 8000H,
respectively.
6. In the previous question, find the address ranges that are assigned to the 8K x 8
EPROM chip and the 8K x 8 RAM chip.
7. Interface an 8-bit DIP switch and a seven-segment LED display with common anode
connection with the 8085, such that the addresses assigned to them are FEH and FAH,
respectively. Write a program to read the data from the DIP switch and send it to the
seven-segment display repeatedly.
196 MICROPROCESSORS AND MICROCONTROLLERS
8. Interface an 8-bit DIP switch and a set of eight LEDs in common anode configuration
with the 8085 such that the address assigned to both of them is FFH. Write a program
to read the data from the DIP switch and send it to the LEDs repeatedly. (Hint: Use the
same address decoder for both the DIP switch and the LEDs.)
9. Interface an 8-bit DIP switch and a seven-segment LED display with common cathode
connection with the 8085, such that the address assigned to both of them is FFH. Write
a program to read the unpacked BCD number from the DIP switch and display the
number in the seven-segment display repeatedly. (Hint: Use look-up table technique
to find the corresponding seven-segment code of the number that is read from the DIP
switch.)
10. Interface an 8-bit DIP switch and a set of eight LEDs in common anode connection
with the 8085, such that the address assigned to both is FF00H. Write a program to
read the data from the DIP switch and send it to the LEDs repeatedly.
11. Interface two input ports with addresses FFFOH and FFF1H and two output ports
with addresses 9000H and 9001H using memory-mapped I/O technique. Indicate the
assumptions made (if any).
1. In the following ICs, calculate the number of address and data lines.
(i) 16K x 8 EPROM IC
(ii) 32K x 8 RAM IC
(iii) 16K x 4 RAM IC
2. How many 8K x 4 RAM ICs are needed to construct a 32K x 8 RAM IC?
3. The memory address of the last location of a 1 KB memory chip is FBFFH. What is the
address of the first location?
4. What is the maximum number of I/O devices that can be interfaced with the 8085 in the
I/O-mapped I/O technique?
5. While interfacing an 8-bit DIP switch with the 8085 using 74LS244, the following
circuit is used. What are the addresses that can be used to access the DIP switch?
(ii) Port C is divided into two groups, port C upper (PCU) and port C lower
(PCL), of 4 bits each. Each of them can be programmed independently or
as 4-bit ports, for input and output operations.
(iii) All the ports can be programmed for simple I/O or handshake I/O in the
input/output mode.
(iv) Each port C bit can be set/reset individually in bit set/reset mode.
(v) The bits of port A and PCU are grouped as group A (GA).
(vi) The bits of port B and PCL are grouped as group B (GB).
PA2 □2 39 □ PA5
lines are connected to the data
bus of the processor. Eight lines PA1 E3 38 □ PA6
operating mode using commands and control words. The three ports of the 8255
are grouped as groups A and B. Groups A and B accept commands from the read/
write control logic, receive control words from the internal data bus, and issue
commands to the associated ports.
The chip has to be programmed to configure its operation, before using it. The
configuration is done by the control word, which tells the 8255 whether the ports
are input, output, bidirectional, or strobed.
D7 D6 D5 D4 D3 D2 D1 DO
1 Group A Port A Port C upper Group B Port B Port C
(1 = Mode select Direction Direction Mode Direction lower
I/O) 00—mode 0 select select select select Direction
01—mode 1 1—input 1—input 0—mode 0 1—input select
IX—mode 2 0—output 0—output 1—mode 1 0—output 1—input
0—output
The MSB D7 is set to 1 to indicate that the chip is configured in I/O mode. Bits
D6 and D5 are used to select the operating mode of group A. There are three basic
modes of operation for group A:
(i) Mode 0—Basic I/O (bits D6 and D5 are both 0)—Ports A and B and the
higher-order four bits of port C can be operated as inputs or outputs. This
mode uses simple I/O operation; no interrupts are used. The outputs written
into the ports are latched and available at any time. Inputs available at the
port pins are buffered through port latches.
(ii) Mode 1—Strobed or handshake input/output (bits D6 and D5 are 0 and
1, respectively)—Port A is configured in mode 1, while port C is used for
handshaking and control of data transfer in Port A. Input and output data are
latched.
(iii) Mode 2—Bidirectional I/O mode (bits D6 and D5 are 1 and X, respectively)—
Port A is bidirectional (i.e., both input and output), while port C is used for
handshaking. Port B cannot be programmed to this mode.
Bit D4 is used to select the direction of data flow in the port A bits, i.e., it
decides whether the port A pins are input pins (D4 = 1) or output pins (D4 = 0).
Bit D3 is used to decide whether the four higher-order bits of port C are used for
input (D3 = 1) or output (D3 = 0).
Bit D2 of the control word is used to select the mode for group B. As discussed
earlier, only two operating modes—mode 0 and mode 1—are possible for group B.
(i) Mode 0—Basic I/O is selected if bit D2 is 0. This mode uses simple I/O
operation and no interrupts are used.
(ii ) Mode 1—Strobed or handshake I/O is selected if bit D2 is 1. Port B is
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 201
configured in mode 1, while port C lower bits are used for handshaking and
control of data transfer.
Bit DI is used to select the data direction for port B pins. If it is 0, port B pins
are configured as output pins; if it is 1, they are configured as input pins.
Bit DO is used to select the data direction for the lower-order pins of port C. DO
= 0 for output; DO = 1 for input.
7.1.3.2 BSR Mode Control Word Format
The control word format for BSR configuration is given in Table 7.3.
Table 7.3 BSR control word format of the 8255
D7 D6 D5 D4 D3 D2 D1 DO
0 X X X B2 Bl BO Bit set/reset
(0 = BSR (Don’t (Don’t (Don’t 1 = set
Bit Select bits—select one of 8 bits
mode) care) care) care) 0 = reset
of port C
In BSR mode, any of the eight bits of port C can be set or reset using a single
control word written into the control register. This feature helps the programmer
to control the port C pin outputs individually. It is also used in mode 1 and mode
2 I/O operations, wherein the individual ports of port C can be controlled by the
programmer to indicate the status and control.
7.1.3.3 I/O Mode 1 Operation
Mode 1 configuration of the 8255 provides a means for transferring I/O data to or
from a specified port, in conjunction with strobes or handshaking signals. In mode
1, ports A and B use the lines on port C to generate or accept these handshaking
signals. In mode 1, the ports are divided into two groups—A and B. Each group
contains one 8-bit port and one 4-bit control/data port. The 8-bit data port is either
port A or port B; it can be either input or output. Both inputs and outputs are
latched. The 4-bit control port, either PCU or PCL, can be used to control and
decide the status of the 8-bit ports A and B. Figure 7.4 shows the operation of
handshake signals for input operation using the 8255.
The sequence of operations for inputting data from an input device to the
microprocessor through the 8255 is as follows:
Step 1 The input device places data in the data lines, i.e., port A or port B. This
is communicated to the 8255 by making the strobe input pin (STB) low. STB is an
active low signal applied through PC4 or PC2.
Step 2 The 8255 acknowledges the receipt of the data to the input device by
making the input buffer full pin (IBF) high. This also indicates that the data has
been latched into the input port.
Step 3 The 8255 then makes the interrupt request line (INTR) high and applies
an interrupt to the processor. This signal is applied only when the interrupt enable
signal (INTE) is high. INTE for port A is controlled by the set/reset bit PC4
202 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 7.4 Control and handshake signal for input operation in mode 1
and INTE for port B is controlled by the set/reset bit PC2. PC2 and PC4 can be
controlled using BSR mode.
Step 4 In the interrupt service routine, the processor reads the data from the
corresponding input port. Reading from the port is done by selecting the 8255 port
and applying the active low RD signal.
Step 5 During read operation, the RD signal is low. When the RD signal goes
low, INTR signal is reset. IBF is reset by the rising edge of the RD input.
Thus, mode 1 allows an input device to request service from the CPU by simply
sending its data to the port and activating the active low STB signal.
The handshake signals used for output operation in mode 1 are OBF, ACK, and
INTR. The sequence of operations that take place for outputting data from the
processor to an output device is as follows:
Step 1 The processor initiates the data transmission by writing the data to be
transmitted to the corresponding port of the 8255. This is done by sending the port
address to the 8255, placing the data on the data lines, and activating the active
low WR signal.
Step 2 To transfer the data to the output device, the 8255 makes the active low
output buffer full signal (OBF) low to indicate that the CPU has written data to
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 203
be given to the specified device. The OBF flip-flop is set by the rising edge of the
WR input.
Step 3 The data available on the output port pins are then read by the output
device. After receiving data from the port pins, the output device acknowledges
the receipt by making ACK low. ACK is an active low input signal to the 8255
from the peripheral device, indicating that it has accepted a data. The OBF output
signal of the 8255 is reset by the ACK input going low.
Step 4 The 8255 now informs the processor that data has been transferred to the
output device, by making the interrupt request line (INTR) high. A high on this
output can be used to interrupt the CPU when an output device has accepted data
transmitted by the CPU. INTR is set when ACK, OBF, and INTE are all 1.
Step 5 In the interrupt service routine, the processor writes the next data to be
transmitted to the output device into the output port of the 8255. INTR signal is
reset by the falling edge of WR.
Figure 7.5 shows the operation of handshake signals for output operation in
mode 1.
Fig. 7.5 Control and handshake signals for output operation in mode 1
Interrupt generation and enable/disable functions are also available through port C
pins. Port B can be configured to be in mode 0 or 1, but not in mode 2. Both inputs
and outputs are latched. The 5-bit control port (port C) is used for controlling and
deciding the status of the 8-bit, bidirectional port (port A). The basic control signal
transmission and data transfer operation in mode 2 is shown in Fig. 7.6.
The input and output operation of the 8255 in mode 2 is similar to its operation
in mode 1, except that port A is a bidirectional port. For output operation, as
in mode 1, data transfer is initiated by the processor by making the active low
signal OBF low. This indicates that the processor has written data into the output
port. The output device, after reading the data, will give an acknowledgement
by making the active low acknowledge signal (ACK) low. The processor is then
interrupted by the 8255 to indicate that the output data port is ready for the next
data output. Here, the interrupt can be applied to the processor only if INTE 1
flip-flop associated with OBF and controlled by PC4 has already been set by the
processor.
The input operation is also similar to mode 1 operation. Here, the data transfer
is initiated by the input device by placing the data on the port pins. Then an active
low control signal STB is given to the 8255 by the input device. The 8255 now
latches up the data to its port and then gives an active high signal IBF to the
input device. The 8255 then issues an interrupt signal to the processor to indicate
that data is available for read operation. Here, the interrupt can be applied to the
processor only if INTE 2 flip-flop associated with IBF and controlled by PC4 has
already been set by the processor.
D7 D6 D5 D4 D3 D2 D1 DO
Port C lower
1 Group A Port A Port C upper Group B Port B ' t 1
(1 = I/O) mode—00 input--1 output—0 mode—0 output—0
The control word from this figure is 10010001B, i.e., 91H. The following
program instructions will configure the control word of the 8255.
MVI A, 91H ; Load the control word in the accumulator.
OUT 43H ; Transfer it to the control register of the 8255.
Example 7.2:
Find the data direction and modes of operation of the 8255 ports if the control
word written into it is AOH.
The control word bit pattern is given in Table 7.5.
Table 7.5 Control word bit pattern (Example 7.2)
D7 D6 D5 D4 D3 D2 D1 DO
1 0 1 0 0 0 0 0
Port A Port C upper Port B Port C lower
1 Group A Group B
direction direction direction direction
(1 = 1/0) mode—1 mode—0
0—output 0—output 0—output 0—output
These connections ensure that the port is not damaged and also that it does not
source over-current. This ensures safe operation of the ports and switches.
Figure 7.7 shows the interfacing of LEDs to the ports through an inverter driver.
When logic 1 is given out on the port pin, it is inverted by the inverter and ground
(logic 0) is connected to the cathode of the LED. This forward biases the LED
and it glows. This connection ensures that the port pin does not source enormous
amounts of current and also that the current required for LED illumination is from
the supply and driver IC.
Fig. 7.7 Interfacing switches and LEDs with the 8085 through the 8255
The software part consists of initializing the 8255 for port A input and port B
output operation. All the ports are initialized to mode 0. So the control word 90H
shown in Table 7.6 is used.
Table 7.6 Control word bit pattern for interfacing LEDs and switches
D7 D6 D5 D4 D3 D2 D1 DO
The program for initializing the 8255 and outputting data available in port A to
port B is given in Table 7.7.
Table 7.7 Program for initializing the 8255 and performing I/O operation
Mnemonics Comments
Example 7.3:
Design a system (both software and hardware) that will cause four LEDs to flash
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 207
alternately when a push button is pressed. Use the 8255 PPI. The interfacing
scheme for a switch and the four LEDs is shown in Fig. 7.8. The LSB of port A is
used for the switch interface and the least significant four bits of port C are used
for the LED interface.
Fig. 7.8 Interfacing LEDs with the 8085 through the 8255 (Example 7.3)
Program:
The program for interfacing LEDs with the 8085 using the 8255 is given in Table
7.8. The program checks the switch status and accordingly, makes the LEDs blink
alternately. The data to be given to port C for making alternate LEDs blink is
00001010 (OAH) and 00000101 (05H).
Table 7.8 Program for interfacing blinking LEDs with the 8085 using the 8255
Table 7.8 Program for interfacing blinking LEDs with the 8085 using the 8255 (Contd)
executed and the value of the voltage on the switch line is checked again to make
sure the line has stopped bouncing. The delay is normally 10 ms, as the oscillations
settle down within that period in most switches.
In common anode display the anodes of all segments are connected together.
So to illuminate a segment, the common anode is connected to the supply and the
corresponding segment input is connected to a low-level voltage or logic 0.
In common cathode display the cathodes of all the LEDs are connected together.
So to illuminate a segment, the corresponding segment input is connected to the
210 MICROPROCESSORS AND MICROCONTROLLERS
high-level voltage or logic 1 and the common cathode is connected to the ground.
This forward biases the LEDs and illuminates them.
Figure 7.12 shows the circuit required to drive a single seven-segment LED
display from 4-bit BCD output. The BCD to seven-segment display decoder
IC 7447 converts
the 4-bit BCD
code applied at
its input into the t
patterns required
to display the BCD inputs
Table 7.9 shows the instructions that can be used to display data in a seven-segment
display.
Two seven-segment displays can be connected to a single 8-bit port. One
7447 IC can be connected to the four lower-order bits and another 7447 can be
connected to the four higher-order bits of port A. So six seven-segment displays
can be connected to a single 8255 that has three parallel I/O ports. This results in
a more complicated circuit. The complexity of the circuit can be reduced by using
a technique called multiplexed display. By using multiplexed display, as many as
eight displays can be connected to the two ports. The multiplexed display concept
is discussed in Section 7.9.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 211
Mnemonics Comments
MVI A. control word ; Load the accumulator with the 8255 control word.
OUT control_register ; Output it to the control register of the 8255.
MVI A, data ; Load the accumulator with the data to be displayed.
OUT PORTA ; Output it to port A, where the display is connected.
Source program:
The program given in Table 7.11 assumes that the 8255 IC is interfaced to the
microprocessor at the addresses 80H, 81H, 82H, and 83H. The data for the port
is available at the address 9000H. The port data for ports A and B are stored
consecutively in the memory locations starting at 9000H.
212
sides (yellow)
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 213
(Contd)
214 MICROPROCESSORS AND MICROCONTROLLERS
Table 7.11 Program for traffic light control at a four-road junction (Contd)
Note: The sequence shown in the table can be changed by the programmer to suit his/her
own design and traffic flow.
will have a resolution of 5/256, i.e., approximately 19.5 mV. The conversion time
of ADCs is decided by the type of the ADC and the clock frequency used in the
converter circuits.
Some ADC chips come with the option of having more than one analog input.
One of the analog input channels is selected using select lines and an analog
multiplexer circuit. The ADC chips also have a sample and hold circuit. The
sample and hold circuit is used to maintain the analog input voltage constant when
the conversion is in progress.
The single-chip analog-to-digital converters available in the markets have
many options. The commonly available ADC chip family is ADC080X from
National Semiconductor. ADC 0800, ADC 0804, ADC 0808, and ADC 0816
are the common chips available in this family. ADC 0804 has one analog input
channel with 8-bit output. ADC 0808/0809 has eight analog inputs with three-bit
channel select lines and an 8-bit output. ADC 0816 has 16 analog input channels
with four select lines and 8-bit outputs.
This section discusses the operation and interfacing of ADC 0816 with the
8085 microprocessor through the 8255 PPI. ADC 0816 is an 8-bit successive
approximation type ADC chip with a built-in analog multiplexer, which can select
one of the 16 analog inputs for conversion into digital format. Select lines A, B,
C, and D are used to select one of the 16 analog inputs IN0-IN15. The analog to
digital conversion is started using the active high control signal Start Conversion
(SC). The conversion of the analog voltage on the input channel selected then takes
place based on the clock signal applied to the ADC chip. After the conversion is
over, the ADC chip issues an active high End of Conversion signal on the EOC
line. The digital output is then read from the data lines after issuing the output
enable signal to the ADC chip. The interfacing of ADC 0816 with the 8255 is
given in Fig. 7.16.
The 8255 PPI is in turn interfaced with the 8085 as shown in Fig. 7.7. In the
interfacing diagram shown in Fig. 7.7, it can be noted that port A of the 8255 is
used to send data to the channel select lines and the related control signals. Port B
lines are used to get the resultant digital data from the ADC chip. The LSB of port
C is used to check the end of conversion signal. With this hardware arrangement,
the ADC chip can only be interfaced with the software polling method. For an
interrupt driven interface, the EOC signal can be connected to any interrupt input.
The analog inputs can be applied to the analog input pins of ADC 0816.
The software interfacing procedure follows the steps shown in the flowchart in
Fig. 7.17.
The ADC conversion process is started after applying the analog input to
the channels. The conversion process is started by initializing the 8255 with the
control word. The control word is given in Table 7.12. Then the channel selection
and start of conversion are done simultaneously, as these two control bits are tied
together in the hardware. The start conversion command must be issued as a pulse
for a very short duration. Then the conversion takes place in the ADC chip, if it
is properly powered and clock pulses are given. After the conversion, the logic
high end-of-conversion signal is issued by the ADC chip. This is sensed by the
software. The data is then read from the data lines, after issuing the logic high
output enable signal.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 217
D7 D6 D5 D4 D3 D2 D1 DO
The program for the ADC conversion process is given in Table 7.13 as a
software routine. This routine assumes that the channel number to be converted is
available in the memory location named CH_NUM.
Table 7.13 Program for ADC conversion
only to a port, there is a need for an output port to be connected between the
processor and the DAC. The 8255 can act as an output port to give data from the
processor to the DAC chip. One port is enough to interface an 8-bit DAC with the
8255. The interface diagram in Fig. 7.18 uses port A of the 8255 for connecting
the data to DAC 0800. The other control signals are directly connected to either
logic 0 or logic 1. The DAC chip gives a proportional current output. This current
output is difficult to measure in most cases and so a current-to-voltage (I-to-V)
converter is used at the output. DAC chips have a built-in latch. This latch stores
the digital input given by port A and outputs a proportional voltage.
Sections 7.6.1-7.6.4 explain the software for common application examples
involving interfacing of the DAC with the 8255. Four common applications—
square wave generation, ramp wave generation, staircase wave generation, and
sine wave generation—are discussed.
Three levels V , V , and V3 are assumed in the output voltage waveform. The
hexadecimal output to be given to the port is calculated using the formula given
in Example 7.5. The program given in Table 7.15 uses the labels DATA1, DATA2,
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 221
and DATA3 for the three output voltage levels. A fixed time delay is used in all
the three levels.
The delay calculation is slightly different from the previous examples. Here,
the voltage levels are increased from 0 to FFH, i.e., 255 in decimal form. So within
T seconds, there are 255 levels. Therefore, the delay for each level will be T/255
seconds. The program given in Table 7.16 increments the value in port A from
OOH to FFH and calls a time delay routine at each level. The delay time should be
as small as possible for ramp generation. Otherwise, the waveform will look like
a staircase waveform with 255 levels.
Table 7.16 Program for ramp waveform generation
A, B, C, and D connected to the 8255 port pins through the transistor drivers. The
transistor drivers or buffers are essential as the port pins cannot directly source
the current required for the motor drive. As explained earlier, the motor terminals
have to be excited in the proper sequence so that rotor rotates continuously in one
direction. Two types of excitation are possible with a four-phase motor—one-
phase excitation and two-phase excitation. In one-phase excitation, only one phase
of the stepper motor is excited at a time. In two-phase excitation, two phases
are excited at a time. The excitation sequence is fixed for rotation in a particular
direction. The excitation sequence for one-phase excitation (for the interface
diagram in Fig. 7.22) is given in Table 7.18. The single phase excitation results in
low current through the motor windings. This is also called wave mode.
0 0 0 1 1 4 01
0 0 1 0 2 3 02
0 1 0 0 3 2 04
1 0 0 0 4 1 08
The excitation sequence for two-phase excitation is given in Table 7.19. In two-
phase excitation, the excitation current through the motor winding is high. So it is
also called high torque excitation. Tables 7.18 and 7.19 also list the corresponding
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 225
hexadecimal byte values to be given to port A, assuming the higher-order four bits
to be zero.
Table 7.19 Switching sequence: two-phase excitation (hi-torque excitation)
The program for driving a stepper motor mainly consists of providing excitation
signals in the proper sequence to the port A terminals of the 8255. An appropriate
delay can be inserted between excitations to control the speed of rotation of the
motor. A minimum delay must be maintained so that the motor coils are properly
excited. This minimum delay also sets the maximum speed of operation of the
stepper motor.
Examples 7.9 and 7.10 explain the procedure for running the stepper motor
under various conditions.
Example 7.9:
Write a program to drive the stepper motor continuously at 60 rpm using the
interface diagram in Fig. 7.22.
It is assumed that a stepper motor with 1.8° step angle is used in Fig. 7.22.
If the time delay for each step is controlled, the speed of the motor can be
controlled. Let us assume that the required speed is N rpm. Then the speed
in rps (revolutions per second) is N/60. If N/60 is the number of rotations in
one second, the time taken for one rotation is 60/N. So the time taken for 1.8°
rotation is 60 x 1.8/(N x 360), which is equivalent to 0.3/Ns. If the time delay
introduced for each 1.8° rotation is 0.3/Ns, the speed of continuous rotation will
be N rpm. Here, the required speed N is 60 rpm. So the necessary time delay is
0.3/60, i.e., 5 ms.
The program given in Table 7.20 is used for continuous rotation of the stepper
motor. The count in the delay routine must be calculated to produce the required
time delay. The program first initializes the control word for the IC 8255. Then
a counter with an initial value of 04H is set up to indicate that the switching
sequence needs four steps, which are to be repeated continuously. A memory
pointer is then initialized to load the switching or excitation data to be given to
port A of the 8255. The switching data are stored initially in the memory locations.
The program shows four sets of switching tables stored in different locations. For
example, to excite the stepper motor coils in one-phase excitation method and
in clockwise direction, the memory pointer must be initialized with the memory
location 9008H. Then the program gives these switching data to the port A pins ot
the 8255, one after another, with a particular delay.
226 MICROPROCESSORS AND MICROCONTROLLERS
Example 7.10:
Write a program to rotate a stepper motor by 180° using the interface diagram in
Fig. 7.22.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 227
In most stepper motor applications, the stepper motor is not rotated continuously.
The angle by which the stepper motor rotates needs to be controlled. In this
example, the stepper motor shaft has to be rotated by exactly 180°. This can be
done by stopping the motor excitation when the motor shaft has rotated by 180°.
This is possible because each switching state of the motor coils rotates the shaft
by exactly the step angle, i.e., 1.8°. For 180° rotation, the number of switching
steps required is 180/1.8 = 100. These 100 steps are produced by repeating the
sequence of four excitations. So a count of 100/4 = 25 is needed. This count of
25 is used for exciting the motor coils with four complete step sequences. Then
the excitation is stopped and the motor rotation is halted. The program given in
Table 7.21 implements this by adding another loop with register D as a counter.
The program uses the same logic and the instructions used in Example 7.9.
9000: DB 03, 06, 0C, 09 ; Store excitation values for biphase clockwise
rotation.
9004: DB 09, 0C, 06, 03 ; Store excitation values for biphase
anti-clockwise rotation.
9008: DB 01, 02, 04, 08 ; Store excitation values for one-phase
clockwise rotation.
900C: DB 08, 04, 02, 01 ; Store excitation values for one-phase
anti-clockwise rotation.
START: MVI A, 80H ; Load the 8255 control word in the
accumulator.
OUT CONTROL_REG ; Move it to the control register.
MVI D, 19H ; Initialize a counter with 19H or 25D for 180°
rotation.
LOOP: MVI C, 04H ; Initialize a counter with 04H for excitation
sequences.
LXI H, 9000H ; Initialize the memory pointer (9000H,
9004H, 9008H, or 900CH).
MOV A, M ; Get the excitation value from the memory.
RPT: OUT PORTA ; Send it to port A.
CALL DELAY ; Call the delay subroutine for proper
excitation of the motor coils.
INXH ; Increment the pointer to the next memory
location.
DCRC ; Decrement the counter.
JNZ RPT ; If it is not zero, get the next data.
(Contd)
228 MICROPROCESSORS AND MICROCONTROLLERS
The common commands that are used in LCD units are given in Table 7.23.
The display can be cleared by issuing a command with LSB alone as 1. Similarly,
cursor control, display position control, and display method control can be done
using appropriate control words.
Table 7.23 LCD display command words
Code
Instruction Description
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Clear 0000000 00 1 Clears display and returns
display cursor to the home position
(address 0)
Cursor 0000000 0 1 x Returns cursor to home position
home (address 0). Display RAM
contents remain unchanged
Entry 0000000 1 I/D S Sets cursor move direction
mode set (I/D) and specifies whether
to shift the display (S). These
operations are performed
during data read/write.
For I/D, 1 = increment;
0 = decrement (Contd)
230 MICROPROCESSORS AND MICROCONTROLLERS
Code
Instruction Description
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Display 0 0 0 0 0 0 1 D C B Sets on/off position of all
on/off displays (D) and cursors (C)
control and blink of character at cursor
position (B)
Cursor/ 0 0 0 0 0 1 S/C R/L x X Sets move cursor—0 or
display shift display—1(S/C), shifts
shift direction (R/L) left—0 or
right—1
DDRAM contents remain
unchanged.
Function 0 0 0 0 1 DL N F x X Sets interface data length
set (DL)—1 for 8-bit data and 0 for
4-bit data, number of display
lines (N)—0 for one-line and
1 for two-line display, and
character font (F)—1 for 5 x 10
dot and 0 for 5 x 7 dot font
Set 0 0 0 1 CGRAM address Sets the CGRAM address;
CGRAM CGRAM data is sent and
address received after this setting.
Set 0 0 1 DDRAM address Sets the DDRAM address;
DDRAM DDRAM data is sent and
address received after this setting.
Read busy 0 1 BF CGRAM/DDRAM address Reads busy flag (BF), which
flag and indicates that internal operation
address is being performed and reads
counter CGRAM or DDRAM address
counter contents (depending on
previous instruction).
Write to 1 0 Write data Writes data into CGRAM or
CGRAM DDRAM
or
DDRAM
Read from 1 1 Read data Reads data from CGRAM or
CGRAM DDRAM
or
DDRAM
The interfacing of an LCD using the 8255 is shown in Fig. 7.23. The 8255 is
in turn interfaced to the 8085, as shown in Fig- 7.7. Port A is connected to the data
lines of the LCD and port C is connected to the control lines. PC2 is connected to
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 231
the enable (EN) line of the LCD. RS signal is connected to the LSB of port C. PCI
is connected to the R/W control signal.
The characters displayed for various ASCII data are given in Fig. 7.24.
Higher-order
LoweX^rbte 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
order four bits\^
CGRAM • :•••. ..... ...
■ •""" ■ ‘s!
XXXXOOOO (0) “J :...:
XXXX0001 (1) •
•
.....
aaaaa
a
:...s I
■ ■ a -a ::: a a
■
XXXX0010 (2)
■ ■ aaaaa a"’
XXXX0011 (3) • -...’
’...- ....! !..." .*"■
■■■•S ■
BM
8.'.*
•■a a.
1.4
■ • J ’..J
XXXX0100 (4) ■ ■ ■ • ... .a a a aa aa
iL
;—! ; Zu:. ■aa.a aa I."
XXXX0101 (5) :: s...! ... M ■ a aaa aa a
XXXX0110 (6) « ■ a ■ a ■ a ,5 J
a.a
:...’ S
aaaaa
(7)
• ■ ■
! hI
s"'i :. j
a^a
aaaaa .■•■s
XXXX0111
XXXX1010 (2) » :■ ■
I ..... K t t:::i 5J
aaaaa aaaaa
a” 1 : aaaaa
a*""l
J. ! aaaa .•5:. ’j”!
(4) J8 "a
XXXX1100 a.a
t"‘s ..........
a....
f»’I 1
! ! ! jHi
’x*
■■■a
a
a*.
a J
t.
T: ’’j’
•T* • !•.! ’ s.". ..h. j”j
S':::!
■
XXXX1110 (6) :: : ■! ■ • ’ ■ 1
.... SU tit:
u
s'"i Hi
XXXX1111 (7) ■
u....
....4
| 18 i • B
lili
in;
1£
The LCD works with its own internal clock pulses. So any command or data
written to the LCD must be enabled with the EN signal. This EN signal must be
applied for a predefined duration. Each command and data requires between 40 ps
and 1.6 ps, depending on the type of LCD and its clock frequency. So a separate
subroutine is written to give proper control signals for the predefined delay time.
Here, two subroutines—COMMAND and DISP—are used to write a command
word and a data for display, respectively. These two subroutines use a common
delay routine.
The first step in the program is to clear the display, set the cursor to home
position, and start display from there. The simple program given in Table 7.24 is
written to display an array of characters stored in memory locations starting from
9000H. The number of characters displayed is NUM_CHAR; it is initialized as a
count in register C. All the characters are displayed continuously.
Table 7.24 Program for displaying data in an LCD
checked for occurrence of Os. If there is none, the next row is made logic 0 and the
procedure is repeated until the key that was pressed is identified.
Once a key is pressed, steps must be taken to remove the contact bounce
problem. Mechanical switches have a problem called contact bounce because of
their construction. Pressing a mechanical switch must produce a single pulse output.
Practically, instead of producing a single clean pulse output, the switches generate
a series of pulses because the switch contacts do not come to rest immediately.
As the microprocessor is faster than manual key pressing, the single key pressed
will be registered as multiple key presses. This is the main disadvantage of key
bouncing. The signal from keys falls and rises a few times within a period of about
5 ms as the contact bounces. So the signal from the key must be made free from
key bouncing transients. This technique is called de-bouncing of key.
Keyboard de-bouncing can be accomplished using hardware or software.
The bouncing of the key occurs within 5 ms. Since a human cannot press and
release a switch in less than 20 ms, the logic employed for de-bouncing checks the
signal after 20 ms and identifies whether a key is pressed or not. This logic can
be implemented both in hardware or software. The hardware techniques employ
set-reset flip-flops, non-inverting CMOS gates, or integrating de-bouncer. The
software techniques use the wait-and-see method. When a signal is sensed from a
switch, the program waits for 10 ms and checks the key again. If the signal from
the switch still indicates a key press, the program decides that the user has pressed
the key. Otherwise, the signal received is rejected as noise.
The software for identifying a key pressed in a keyboard matrix requires the
scanning of the rows and columns. Even after identifying the key, the key bouncing
problem must be overcome. If these tasks are accomplished using software, the
time taken is high and the processor may be held up for a long time in the process
of scanning the keyboard. To overcome this problem, Intel has produced IC 8279.
This IC can scan the keyboard, identify the key that has been pressed, and also
take care of the key bouncing problem.
Example 7.11:
Interface a 64-key matrix keyboard to the 8085 microprocessor using the 8255.
Write an 8085 assembly language program to initialize the 8255 and to read the
key code.
Figure 7.25 shows a matrix keyboard with 16 keys, connected to the 8085
microprocessor using the 8255. A matrix keyboard with 64 keys uses similar
hardware. However, eight bits instead of four are used in each port. In this example,
port A is assumed as the output port; port B lines are scanned for the occurrence
of 0, to check for a key press.
Figures 7.26 (a) and 7.26 (b) show the algorithm for the program and the delay
subroutine. This algorithm uses a routine called KEY. It keeps track of the keys
pressed in the same sequence in which they are scanned, and stores them in the
L register. When any key press is detected, the code corresponding to that key is
loaded in the accumulator and processed further. If necessary, a look-up table may
be used to convert the key detected into its corresponding ASCII code.
236 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 7.26 (a) Algorithm for interfacing a matrix keyboard with the 8085
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 237
Source program:
Table 7.25 gives the program for interfacing the matrix keyboard with the 8085.
Table 7.25 Program for interfacing a matrix keyboard with the 8085
MVI A, 1000001OB ; Set port A as the output port and port B as the
input port.
OUTCR ; Initialize the control register of the 8255.
START: MVI A, OOH ; Make all the scan data zero.
OUT PA ; Output to port A.
BACK: INPB ; Get data from port B.
CPI FF ; Check for key press.
JZ BACK ; If no key has been pressed, jump to BACK
and check for key press again.
CALL DELAY ; If a key has been pressed, wait for key de
bounce.
MVI L, OOH ; Initialize a key counter.
MVI C, 04H ; Initialize a column counter for four columns.
MVI B, 11111 HOB ; Make one column low.
NEXTCOL: MOV A, B ; Move the content of register B to the
accumulator.
OUT PA ; Output it to port A.
MVI D, 04H ; Initialize a row counter for four rows.
IN PB ; Read the return line status.
(Contd)
238 MICROPROCESSORS AND MICROCONTROLLERS
Table 7.25 Program for interfacing a matrix keyboard with the 8085 (Contd)
5V
is done by displaying the data in quick succession in all the display units. Due to
the persistence of vision, the human eye holds the display image in it and the user
sees all the display units illuminated simultaneously. As long as the displays are
turned on and off fast enough, the eye will perceive them as being illuminated all
at the same time.
The program involves outputting data to one display unit and repeating the
same procedure for all the other display units in quick succession. The timer can
be used to control the rate at which data is displayed and refreshed. This consumes
a considerable amount of processor time. To overcome this problem, IC 8279 can
be used.
Example 7.12:
Write a program routine to scan and display the data available in the series of
memory locations starting at 9000H in four seven-segment displays connected to
the 8085 through the 8255. Assume that the data for display is connected to port A
of the 8255 and digit selection is done through the port B lines.
The port A lines are connected to the eight LEDs of the common anode seven
segment display. So the data to be sent is active low, meaning that logic 0 has to
be sent to light an LED segment. The common anode is driven by a PNP transistor.
The logic 0 connected to the base of the PNP transistor will switch it on and make
the corresponding segment display a digit. Here, the least significant four bits
of port B are assumed to be connected to the four transistor drivers. The 8255 is
assumed to have addresses as follows: port A—80H, port B—81H, port C—82H,
and control register—83H. The program given in Table 7.26 assumes that the
display data for the four digits is available in the memory locations 9000H-9003H.
A software lime delay is used to display the devices in a multiplexed manner.
240 MICROPROCESSORS AND MICROCONTROLLERS
The delay in this routine must be such that within the time period of persistence
of vision, the first digit is displayed again after displaying other digits. Note that
this routine will be in a continuous loop. For multiplexed display, the display must
be continuously refreshed. The delay used here is a software delay routine. So
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 241
processor time is wasted, since no other processing of data is done by the processor
during this time. This disadvantage can be overcome by using a dedicated 8279 IC
(keyboard and display interface IC), which will update the displays.
] Keyboard section
' I
The control section consists of a data bus buffer for interfacing with the
processor. This VO section uses control signals such as AO, CS, RD, and WR.
The active low control signal CS is used to select the IC. Similarly, the active low
control signals RD and WR are used to indicate the direction of data transfer on
the data bus (DB0-DB7). The signal AO is used to select a data or control register.
242 MICROPROCESSORS AND MICROCONTROLLERS
A logic 1 on the AO line means that the content of the data bus is a command or
status. A logic 0 on the line means that the content of the data bus is data for the IC.
The control and timing registers store the keyboard and display modes and other
operating conditions.
Although there are many control and data registers, the 8279 uses only two
addresses—one with AO = 0 and the other with AO = 1. This is done using a
unique control word for each operation. For example, two different control words
are available for accessing the display RAM and the keyboard FIFO. For every
operation, the corresponding control word is written, the necessary register is
accessed, and then the operation is carried out.
SL0-SL3 are the four scan lines of the 8279. There are two programmable
options for the scan lines—encoded mode and decoded mode. In encoded mode,
the SL0-SL3 lines are binary counter outputs and need to be decoded externally
for scanning keyboards and displays. In decoded mode, the SL3-SL0 outputs are
decoded; one of the four lines has an active low output. The scan lines SL0-SL3
are common to both keyboards and displays. RL0-RL7 are the eight return lines
and are used as inputs to sense a key press in the keyboard matrix.
The other signals available in the 8279 are as follows:
(i) BD: Active low output signal, used to blank all displays
(ii) CLK: Clock input to be given to the 8279, for proper operation of internal
circuits
(iii) CNTL/STB: Control or strobe signal, given as input from the control key in
the keyboard
(iv) Shift: Input to the 8279 RL2 E 1 40 Vcc □
from the shift key of the RL3 E 2 39 RL1 □
keyboard CLK E 3 38 RLO □
(v) IRQ: Interrupt request IRQ E 4 37 □
CNTL/STB
sent to the processor from RL4 E 5 36 SHIFT □
the 8279 to indicate a key RL5 E 6 35 SL3 □
press RL6 E 34 SL2 □
(vi) OUT A0-A3 and OUT RL7 E 8 33 SL1 □
B0-B3: Data output lines RESET E 9 8279 32 ^3 SLO
for the display units RD E 10 31 OUT BO□
(vii) Reset: Input to the 8279 WR E 11 30 OUTB1 □
and connected to the DBO E 12 29 El OUTB2
processor RESET OUT DB1 E 13 28 OUT B3□
DB2 E 14 27 El OUT AO
The pin diagram of the 8279 is
DB3 E 15 26 El OUTA1
given in Fig. 7.29.
DB4 □ 16 25□ OUTA2
DB5 C 17 24 El OUT A3
7.9.4 Programming of 8279 DB6 E 18 23 El BD
IC 8279 can be programmed to DB7 C 19 22 El CS
select the number of displays, V8S E 21 E] AO
the type of key scan, the memory
to write the display data into, a Fig. 7.29 Pin diagram of IC 8279
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 243
blank display, and the key code read option, and to control the interrupt request
signal. All these operations or commands are written into the 8279 through the
data bus, with logic 1 on the AO line. The most significant three bits of the control
word differentiate the operations. The first three bits of the byte sent to the control
port select one of eight control words, which are listed in Table 7.27.
Table 7.27 Control word selection using the most significant three bits
D7 D6 D5 Function Purpose
D7 D6 D5 D4 D3 D2 D1 DO
GOOD D K K K
The display control word bits DD produce four options—8 bits or 16 bits, with
calculatorlike right entry or typewriterlike left entry.
The lines SL0-SL3 provide encoded and decoded output options for the
keyboard interface. In the case of encoded output option, SL0-SL3 outputs will
have active high outputs in binary form. To select a single row in the keyboard
and to select a single display digit, the encoded binary outputs must be decoded
externally with a decoder IC. In the case of decoded output option, the lines SLO-
SL3 will have only one active low output at a time. An external decoder in not
needed now. However, only four rows of the keyboard can be scanned with these
four lines, i.e., only one of four display digits can be selected.
In the keyboard matrix scan mode, there are two options—2-key lockout and
n-key rollover. In 2-key lockout mode, if two keys are pressed simultaneously, the
key that is released last is considered as the key pressed; the other key is neglected.
In the case of n-key rollover, if two or more keys are pressed simultaneously, all
the keys are sensed and stored in the FIFO in the sequence in which the keys are
recognized by the logic. In sensor matrix mode, the de-bounce logic is suppressed;
any key press sensed in the matrix is directly stored in the sensor RAM.
7.9.4.2 Clock Signal Programming Command Word
The clock command word programs the internal clock driver. The code PPPPP
shown in Fig. 7.30 corresponds to the binary code by which the input clock signal
must be divided to achieve the desired operating frequency. With the five bits
D0-D4, division is possible by any number from two to 31. For example, for an
operating frequency of 100 kHz and a clock input of 1 MHz, the count should be
0101 OB (i.e., 10D). This control word decides the time taken for scanning and the
de-bouncing.
D7 D6 D5 D4 D3 D2 D1 DO
0 0 1 P P P P P
D7 D6 D5 D4 D3 D2 D1 DO
0 1 0 AI 0 A A A
D7 D6 D5 D4 D3 D2 D1 DO
1 0 0 AI A A A A
Set to 1 for auto Address of the 16-byte display 6
increment option RAM in four bits
Fig. 7.32 Write display RAM command word format
D7 D6 D5 D4 D3 D2 D1 DO
0 1 1 AI A A A A
Display inhibit/mask command word The display write inhibit control word
(Fig. 7.34) is used in applications where separate 4-bit display ports are used.
Examples include displays that use a BCD decoder. This control word is used to
inhibit either the leftmost four bits of the display or the rightmost 4 bits, using the
I-I bits. With this command word, it is possible to write a nibble into the display
RAM without affecting the other digits being displayed. The masking of bits MM
is similar in operation to inhibit, but these bits will selectively blank either the
leftmost or rightmost display.
D7 D6 D5 D4 D3 D2 D1 DO
1 0 1 0 I I M M
Clear display command word The clear display control word (Fig. 7.35) can
clear the display RAM using the CD bits. This command word has the option of
246 MICROPROCESSORS AND MICROCONTROLLERS
making the display RAM all Os (D3 and D2 = 0) or all Is (D3 and D2 = 1). Setting
CF bit is used to clear the keyboard FIFO RAM. Setting CA bit is used to clear
both the display RAM and the FIFO RAM.
End interrupt command word The end interrupt command (Fig. 7.36) is issued
to clear the IRQ pin in sensor matrix mode.
D7 D6 D5 D4 D3 D2 D1 DO
1 1 1 E 0 0 0 0
D7 D6 D5 D4 D3 D2 D1 DO
D S/E 0 U F N N N
D7 D6 D5 D4 D3 D2 D1 DO
CTRL SHIFT s2 s. So *2 R. Ro
The number of display devices used in this scheme is six. The seven-segment
displays are all common anode type and a transistor driver is used with each display
device. A PNP transistor drive, similar to those used in Fig. 7.27 is used to switch
between the common anode and +5V supply. A logic low is required to turn on
the transistor driver; it is generated using the decoder IC. Common decoder ICs
such as IC 74138 can be used as these ICs can give an active low signal on any of
their outputs. The segments of the display devices are all connected together on a
common bus and connected to the A0-A3 and B0-B3 outputs of the 8279. As the
displays are all of common anode type, the data output for illuminating the LEDs
must be logic low. This means that a logic 1 in the data lines A0-A3 and B0-B3
blanks the display and a logic 0 displays all the segments.
The program given in Table 7.29 initializes the 8279 with the control word for
encoded output and eight-digit display. The writing of data in the display RAM
of the 8279 is enough to display data. The 8279 automatically scans and refreshes
the display. As shown in Fig. 7.39, the program assumes that a six-digit display is
248 MICROPROCESSORS AND MICROCONTROLLERS
interfaced to the 8085 using the 8279. The program given uses programmed and
polled method of data transfer.
Table 7.29 Program for interfacing seven-segment displays using the 8279
START: MVI A, OOH ; Load the mode set command word in the
accumulator.
OUT COMMAND PORT ; Output it to the command port.
MVI A, 11010000B ; Load the clear display command word in
the accumulator.
OUT COMMAND PORT ; Output it to the command port.
MVI A, 90H ; Load the write display RAM command
word in the accumulator.
OUT COMMAND PORT ; Output it to the command port.
MVI A, DATA1 ; Load the first data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
MVI A, DATA2 ; Load the second data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
MVI A, DATA3 ; Load the third data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
MVI A, DATA4 ; Load the fourth data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
MVI A, DATA5 ; Load the fifth data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
MVI A, DATA6 ; Load the sixth data in the accumulator.
OUT DATAPORT ; Output it to the seven-segment display.
HLT ; Terminate program execution.
of the eight return lines, RL0-RL7. The 8279 does this scanning automatically and
stores the key code into the FIFO RAM. In this figure, the CNTL and SHIFT lines
are not used and are connected to logic low.
The program for interfacing a matrix keyboard with the 8085 using the 8279
is given in Table 7.30. Initially, the mode set command word is written into the
command port. Here, the encoded scan keyboard mode with 2-key lock out is
used. After writing the command word, IC 8279 starts scanning the key presses.
Any key press can be sensed by reading the status word from 8279 and checking
the least significant three bits. The following program checks for a single key press
and reads the key code from the FIFO.
START: MVI A, OOH ; Load the mode set command word in the
accumulator.
OUT COMMAND PORT ; Output it to the command port.
LOOP: IN COMMAND PORT ; Read the status word from the 8279.
ANI 07H ; Mask the most significant five bits.
JZ LOOP ; If no key is pressed, loop again to read
the status word.
MVI A, 50H ; If a key has been pressed, load the read
FIFO RAM command word in the
accumulator.
OUT COMMAND PORT ; Output it to the command port.
IN DATA_PORT ; Read the FIFO RAM data from the data port.
STA KEY_CODE ; Store the key code in a memory location.
HLT ; Terminate program execution.
250 MICROPROCESSORS AND MICROCONTROLLERS
The program in Table 7.30 uses the polling method of data transfer to transfr
data from the 8279 FIFO to the processor, by reading the status word. To save
processor time and to avoid the reading and checking of the status register, the
IRQ line of the 8279 can be used to interrupt the processor. The IRQ signal is
activated by the IC 8279 whenever a key press is sensed and its code is loaded
into the FIFO RAM. This interrupt request line can be tied to any of the interrupt
signals of the processor and the corresponding interrupt service routine can be
used to read the key code from FIFO RAM.
CLKO
GATEO
OUTO
CLK1
GATE1
OUT1
CLK2
GATE 2
OUT 2
DO 8 17 OUT 2
the decoder. In addition, the 8253
requires two address lines AO and Al
CLKO 9 □ 16 □
GATE 2
I
OUTO C 10 15 CLK1
to be issued from the 8085 hardware.
These address lines are used to select
GATEO C 11 14 □
GATE1 I
cs RD WR A1 AO Operation
read it. Similarly, initial setup can define whether an 8-bit value or a 16-bit value
is loaded into the counter. The most significant two bits SCO and SCI are used to
select the counter.
Bit position D7 D6 D5 D4 D3 D2 D1 DO
The six operating modes of the timer IC 8253 along with the function of the
gate pin are listed in Table 7.33.
Table 7.33 Operating modes of the 8253
Clock
WRn
Output (interrupt)
WRm
Gate
Output (interrupt)
A+B=m
Then the count value is reloaded and decremented every clock pulse. As only
one low pulse is generated during the entire count cycle, this mode is called rate
generator or frequency divider. The divided output frequency is given by the
following formula:
Output frequency = (Input clock frequency)/(Count value loaded)
This mode is commonly used to generate a real-time clock interrupt. The output
can be used as an interrupt signal to interrupt the processor during every output
period of the counter.
The Gate input in this mode acts as Reset input. If it becomes 0, counting is
disabled. When it becomes 1, the count value is reloaded and counting starts again.
The waveforms for counter operation in mode 2 are given in Fig. 7.45.
square waveform will be given by the input clock frequency divided by the count
value. The waveforms for counter operation in mode 3 are given in Fig. 7.46.
4 3 2 1 0
Output | |
Load n WR | n=4 |
Gate | |
________________ 4______________ 4 3 2 1 0
Output
/ ...... .......
Fig. 7.47 Waveforms for counter operation in mode 4
0 is selected and a 16-bit count value has to be loaded for the binary counter. The
mode 2 control word for the above configuration is given in Fig. 7.50.
Bit position D7 D6 D5 D4 D3 D2 D1 DO
Value (34H) 0 0 1 1 0 1 0 0
The next step is to form the count value. The count value should be such that
the counter becomes 0 after counting the predetermined count value in 1 second.
So if the clock frequency for the counter operation is selected as 1 kHz, the counter
is decremented after every clock, i.e., after every 1 ms. So the count value of 1000
results in a delay of 1 second, when the counter becomes 0 after 1000 counts.
If the counter is designed to count in binary, then the count value 1000 must be
converted to binary and loaded into the counter as 3E8 (in hexadecimal form). If
the counter is designed to count in BCD, then the count value can be loaded in
BCD format as 1000.
The program consists of three parts. The first part is initializing the counter and
the count value. The clock signal must be applied to the selected counter’s clock
input pin. Here, the counter is operated in mode 2. So the count value need not be
loaded repeatedly after the count is over. The count value is reloaded automatically
after it becomes zero. The second part is checking whether the counter value has
become 0 using the software polling technique. In the software polling method,
the counter is first latched with a latch counter command control word. Then the
count value is read from the counter. After the 16-bit count value is loaded in the
processor registers, it is checked for occurrence of zero. This is done by performing
an OR operation on the two bytes. The third part is incrementing the display on the
seven-segment displays at port A of 8255. The program is given in Table 7.34.
Table 7.34 Program for interfacing the 8253 with the 8085 using polling method
START: MVI A, 80H ; Load the 8255 control word in the accumulator.
OUT 43H ; Send it to the 8255 control register.
MVI A, OOH ; Load the initial data for the seven-segment
displays in the accumulator.
OUT 40H ; Send it to port A of the 8255.
STA 88OOH ; Store the displayed data in the memory.
MVI A, 34H ; Load the control word for the timer IC 8253 in the
accumulator.
OUT 33H ; Send it to the 8253 control register.
MVI A, 0E8H ; Load the lower-order count value in the
accumulator.
(Contd)
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 259
Table 7.34 Program for interfacing the 8253 with the 8085 using polling method (Contd)
The program for interfacing the 8253 with the 8085 using the interrupt method
is given in Table 7.35.
Table 7.35 Program for interfacing the 8253 with the 8085 using interrupt method
RST5.5 ISR LDA 8800H ; Load the display data in the accumulator.
ADI01H ; Increment it by 1.
(Contd)
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 261
Table 7.35 Program for interfacing the 8253 with the 8085 using interrupt method (Contd)
Figure 7.52 shows the bit format used for transmitting the asynchronous serial
data.
This format is also called frame. When no data is being sent, the signal line is
in a constant high level. The first data character is indicated by the line going low
for one bit duration and is usually called start bit. The data bits are then sent out
on the line one after another. Here, the least significant bit is sent out first. The
data bit is followed by an optional parity bit, which is used to check for errors in
received data. After the data bits and the parity bit, the signal bit is made high for
at least one bit duration to identify the end of character. This is referred to as stop
bit. Some systems may use two stop bits also.
Baud rate is the rate at which serial data is being transferred and in general
measured in bits/second. Baud rate = l/(Time between signal transitions). If
the signal is changing every 6.3 ms, then baud rate is 1/(6.3 x 10~3) or 600 Bd.
Common baud rates are 300, 600, 1200, 2400, 4800, 9600, and 19,200.
RS-232C is a standard that describes the function of the signal and handshake
pins for serial data transfer. A major problem with RS-232C is that it can transmit
data reliably for only about 50 ft (16.4 m) at its maximum rate of 20,000 Bd. If
longer lines are used, the transmission rate has to be drastically reduced. This
limitation is caused by the open signal lines with a single common ground that are
used in RS-232C.
The Electronics Industries Association (EIA) has a standard named RS-423A,
which is an improvement over RS-232C. This standard specifies a low-impedance
single-ended signal that can be sent over a 50 Q coaxial cable. Logic high in this
standard is represented by the signal line being between 4 V and 6 V negative
with respect to the ground and logic low is represented by the signal line being
from 4 V to 6 V positive with respect to the ground. The RS-423 standard allows a
maximum data range of 100,000 Bd on a 40-foot line or a maximum baud rate of
1000 Bd on a 4000-foot line.
RS-422A, another new standard for serial data transfer, specifies that each
signal will be sent differentially over two adjacent wires in a ribbon cable or a
twisted pair of wires. The term differential used in this standard means that the
signal voltage is developed between the two signal lines rather than between the
signal line and ground as in RS-232C and RS-423. In RS-422A, logic high is
transmitted by making the ‘b’ line more positive than the ‘a’ line. A logic low
is transmitted by making the ‘a’ line more positive than the ‘b’ line. The voltage
difference between the two lines must be greater than 0.4 V but less than 12 V.
Modem is a modulator and demodulator that sends digital Is and 0s as
modulated tones over standard phone lines. A modem is essential for transmitting
a signal over long distances. In the United States, modem standards are handled
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 263
and a serial-to-parallel converter for data received on the RXD line. A separate
control unit is available to determine the operation of the IC according to the control
word written into it. A modem control unit is present for interfacing a modem with
the 8251. In addition to these units, IC 8251 has an I/O port that can be used for
interfacing with any processor along with its read and write control logic. The
8251 requires clock and reset signals for working in a synchronized manner with
the processor. It has a 16-bit control register with which it can be programmed.
The status of operation of the 8251 can be read from the status register. These two
registers can be accessed by the processor by making C/D pin of the 8251 logic 1.
The data register can be accessed by making the C/D pin logic 0. Read operation
is used to read the serial data received and write operation is used to write the data
to be transmitted. The address line AO can be used as the C/D signal. So the 8251
uses two addresses—one for control and status and the other for data.
The basic operations of the 8251 are shown in Table 7.36.
Table 7.36 Basic operations of the 8251 and related control signals
CS C/D RD WR Function
1 X X X Chip not selected; data bus in high impedance state
0 X 1 1 Data bus in high impedance state
0 1 0 1 Status word read by CPU
0 1 1 0 Control word written into 8251 by CPU
0 0 0 1 Data read by CPU from 8251
0 0 1 0 Data written into 8251 by CPU
The 8251 has 28 pins. The details and functions of these pins are listed here.
(i) Data bus (D0-D7): A group of bidirectional lines that are used for data and
control word transfer between the CPU and the 8251
(ii) Reset: An active high signal applied to reset IC 8251. After resetting, the IC
has to be initialized again starting from the mode word.
(iii) CLK: The input signal used to apply a clock frequency to IC 8251. This
signal is used for the internal timing of all operations. This CLK frequency
must be higher than the transmit and receive clock frequency.
(iv) WR: Active low input signal, used to write data or command into IC 8251
(v) RD: Active low input signal, used to read data or status from IC 8251
(vi) C/D: Input signal used to select command/status or data. Input of 0 indicates
command/status; input of 1 indicates data.
(vii) CS: Active low input signal, used to select IC 8251. Any operation with the
IC can be done only when the CS signal is active low.
(viii) TXD: Transmit data line, used to send data out from the 8251
(ix) TXRDY (Transmit ready): Active high signal sent by the 8251 to the
processor, indicating that it is ready to accept a byte of data for transfer.
(x) TXEMPTY (Transmit buffer empty): Active high output signal, used to
indicate that the output register for transmitting data is empty.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 265
(xi) TXC (Transmitter clock): Input clock signal used for transmitting or shifting
data to TXD line. The frequency of this signal decides the transmit baud rate.
(xii) RXD: Receive data line, used to receive data from another USART
(xiii) RXRDY (Receiver ready): Active high output signal to the processor,
indicating that it is ready with the received data.
(xiv) RXC (Receiver clock): Input clock signal, used for receiving and shifting
data on the RXD to the buffer. The frequency of this signal decides the
receive baud rate.
(xv) SYNDET/BD: Active high output. In asynchronous mode, it is used to
indicate a data break. In synchronous mode, it is used to indicate the correct
receipt of synchronous characters and the next data to be received.
The following signals are used with a modem for handshaking and establishing
connection:
(i) DTR: Active low output signal sent out by the 8251 to the modem, to
indicate that it is ready for communication
(ii) DSR: Active low input signal sent by the modem to indicate that it is ready
to transmit or receive
(iii) RTS: Active low output signal to the modem by the 8251, indicating that it
is ready to send data
(iv) CTS: Active low input signal sent by the modem, indicating that it can
accept data for transmission
D7 D6 D5 D4 D3 D2 D1 DO
S1 SO EP PEN L1 LO B1 BO
Frame control Parity check Character Baud rate select bits
stop bit length X0—Disable length 00—SYN mode
00—Inhibit 01—Odd parity 00—5 bits 01—IX clock
01—1 stop bit 10—Even 01—6 bits 10—16X clock
10—1.5 stop bits parity 10—7 bits 11—64X clock
11—2 bits 11—8 bits
266 MICROPROCESSORS AND MICROCONTROLLERS
D7 D6 D5 - D4 D3 D2 D1 DO
D7 D6 D5 D4 D3 D2 D1 DO
DSR SYNDET/BD FE OE PE TXEMPTY RXRDY TXRDY
Data set Same as in 1— 1—Overrun 1— Parity Same as Same Same
ready I/O pin Framing error error in I/O as in as in
0—DSR = 1 error pin I/O I/O pin
1—DSR = 0 pin
The data bus lines D0-D7 are connected to the data lines of the 8251. The
higher-order address lines are used for address decoding and selection. The chip
selection signal CS is generated using an address decoder. The AO line is connected
to the C/D line of the 8251 to select either a control word or a data word. The
read and write control signals are connected to the corresponding signals of the
8251. The reset and clock output signals from the 8085 are connected to reset and
clock inputs of the 8251. In addition, the 8251 needs separate clock signals for
transmission and reception. These RXC and TXC clock signals can be obtained by
dividing the clock output from the 8085. This is not shown in Fig. 7.55.
Normally, two systems are interconnected using the serial port. For serial
communication, the TXD signal of one system is connected to RXD line of the
other system and vice versa. Care must be taken to ensure that the frequency of the
268 MICROPROCESSORS AND MICROCONTROLLERS
transmit clock of the transmitting computer is the same as that of the receive clock
of the receiving computer.
Programming the 8251 involves initializing the 8251 and then using it for data
transmission and reception. Initialization of the 8251 implies writing the mode
command word immediately after reset. The mode control word for synchronous
operation must be followed by the corresponding sync characters. Then the
command word for setting the parameters of the serial port is written into the
control register. Once the initialization is over, the 8251 is ready for transmission
and reception of data if proper clock signals are applied to it.
The serial data received is stored in the serial data buffer and the processor is
informed about the reception of data using the RXRDY signal. This signal may be
connected to an interrupt request in the 8085 and the corresponding interrupt service
routine can read the received data from the 8251. Programmers can also use the
status word read from the 8251 for checking whether data has been received or not.
Serial transmission is started by writing the data to be transmitted into the
data register of the 8251. The serial shifting of data into the TXD line starts
immediately. Once the transmission of data is over, the 8251 asserts the TXRDY
signal informing the processor that the 8251 is ready for transmission of the next
data. Programmers can also read the status word for checking whether data can be
written or not.
(ii) It supports cascading of eight 8259 ICs and multiplexes 64 interrupt sources
into one.
(iii) It can set priorities for the interrupts, mask the interrupt sources, and provide
different interrupt vector addresses.
IC 8259 receives interrupts from different sources, resolves their priorities and
masking, and then passes the interrupt to the processor along with the interrupt
vector address.
In 8085-based systems, the interrupt vector address is provided by a three-byte
CALL instruction. In 8086-based systems, it is provided by an 8-bit vector number.
It can be operated in polled and vectored mode. The starting address of the ISR or
vector number is programmable. No clock is required for the IC.
Using the read/write logic, the 8259 is interfaced with the processor. The data
bus lines D0-D7 are connected to the data lines of the processor. The 8259 chip
is selected using the CS line. The address line AO is used to select the control
word or the data word. If AO is low, the controller selects the writing a command
word/reading a status option. If AO is high, the controller selects another register
for writing the initialization words.
The control logic has the signals INT and INTA. The INT output pin is used
to interrupt the CPU. The 8259 receives the interrupt acknowledge pulse from the
CPU through its INTA input. The 8259 can receive interrupt signals from eight
different sources on the lines IR0-IR7. When these lines go high, the requests are
stored in the interrupt request register (IRR). The interrupt service register (ISR)
stores all the levels that are currently being serviced. The interrupt mask register
(IMR) stores the masking bits of the interrupt lines to be masked. The priority
resolver examines the interrupt registers and determines whether the INT signal
should be sent to the microprocessor or not. The internal block diagram of the
interrupt controller is shown in Fig. 7.56.
The 8259 can be used in cascaded mode. The cascade buffer or comparator is
used to expand the number of interrupt levels by cascading two or more 8259s. It
can cascade a maximum of eight ICs.
The following three registers are used to program and control the operation of
IC 8259:
(i) Interrupt mask register (IMR)
(ii) Interrupt request register (IRR)
(iii) In-service register (ISR)
The interrupt mask register is used to program the masking of external interrupt
sources. This register is written into by the programmer. The interrupt request
register is used to store the interrupts that have been sensed by the 8259 at its
inputs. The in-service register maintains the list of interrupts that are currently
being serviced and the corresponding service routine that is being executed.
or a master wr 2□ 27 AO □
3 26 INTA
Figure 7.57 shows the pin RD
diagram of the 8259. D7 □ 4 25 IR7
D5 6 23 IR5
To service the interrupt
D4 7 22 IR4
requests, the interrupt controller 8259
8 21 IR3
should be initialized by writing D3 i
20 IR2 t
control words in the control D2 9
control words—initialization
command words (ICWs) and
D0 □ 11 18 IRO
12 17 INT
operational command words 16 SP/EN
13
(OCWs). The ICWs are used
15 CAS2
to set up the appropriate initial GND
conditions and specify the
restart vector location. The Fig 7.57 Pin diagram of the 8259
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 271
OCWs are used for masking interrupts, setting up status read operations, etc. The
8259 can be initialized with four ICWs, of which the first two are essential and other
two are optional, based on the modes being used. These words must be issued in a
given sequence. Once initialized, the interrupt controller can be set up to operate
in various modes using three different operational command words. Figure 7.58
shows the sequence in which the IC 8259 must be initialized. Operation command
words can be written into the 8259 at any time to perform specific functions.
D7 D6 D5 D4 D3 D2 D1 DO
A7 A6 A5 1 LTIM ADI SNGL ICW4
Address lines Requirement
Level-triggered Address interval 1—Single
A5-A7 of for ICW4
interrupt mode 1—4 bytes apart PIC
interrupt vector 0—No ICW4
1 —Level-triggered 0—8 bytes apart 0—Cascaded
address 1—ICW4
0—Edge-triggered (For 8085 only) PIC
(For 8085 only) required
D7 D6 D5 D4 D3 D2 D1 DO
Master S7 S6 S5 S4 S3 S2 SI SO
Slave 0 0 0 0 0 ID2 ID1 IDO
There are two different ICW3 formats—one for the master and the other for the
slave.
(i) For the master mode, ICW3 is used to indicate whether a slave 8259 is
connected in the interrupt request line IRQ or not. If a bit is 1, it indicates
that slave is present on that interrupt request line. A 0 in a bit position
indicates it is a direct interrupt request from an external device.
(ii) For the slave mode, ICW3 assigns the slave a specific ID, using the three
least significant bits. ID0-ID2 is the slave ID number. For example, slave 4
has ICW3 = 04H (00000100).
D7 D6 D5 D4 D3 D2
D1 DO
SFNM AEOI
1—Special fully BUF M/S 1—Auto end Mode
0 0 0 nested mode 0—Non-buffered 1—Master of interrupt 0—8085
0—Not special 1—Buffered 0—Slave 0—Normal 1—8086
fully nested mode mode
D7 D6 D5 D4 D3 D2 D1 DO
M7 M6 M5 M4 M3 M2 Ml M0
D7 D6 D5 D4 D3 D2 D1 DO
R SL EOI 0 0 L3 L2 LI
D7 D6 D5 D4 D3 D2 D1 DO
0 ESMM SMM 0 1 P RR RIS
OX—No effect
OX—No effect 10—Read IR register
0—Polling
10—Reset special mask on next read
1—No Polling
11—Set special mask 11—Read IS register
on next read
The 8259 requires two addresses, with AO being 0 and 1. The AO line from the
address bus is connected to the AO line in the 8259. The higher-order address bus
is used to select a particular chip using an address decoder. The read and write
control signals of the 8085 are connected to the corresponding signals of the 8259.
The data lines of the 8259 are connected to the multiplexed lower-order address
and data buses of the 8085. The multi-purpose SP/EN pin is tied to logic high
because only one 8259 is used in the system. The interrupt request line INT of the
8259 is connected to the 8085’s interrupt line INTR. The INTA line of the 8085 is
connected to the INTA line of the 8259. When only one 8259 is used in a system,
the cascade lines (CASO, CAS1, CAS2) can be left open. The eight IR inputs of
the 8259 can be connected to the interrupt sources from various external devices
such as A/D converter, keyboard, and printer. Unused IR inputs must be tied to the
ground in order to avoid noise being recognized as an interrupt signal.
Initializing the 8259 involves writing the initialization command words in
proper sequence, as shown in Fig. 7.58. After initialization, the operation command
words can be written as and when required.
be written to transfer data from a device to the memory. Thus, programmed data
transfer is a slow process. This causes a problem while transferring large amounts
of data.
DMA stands for direct memory access. It is one of the ways to accomplish
high-speed data transfer directly between the memory and peripheral devices,
without the intervention of the microprocessor. This method is often used when a
large block of data is to be transferred.
DMA data transfer is controlled using a separate DMA controller. The
microprocessor must be disabled during the DMA data transfer process. To start
the DMA process, the microprocessor loads an external register in the DMA
controller with the data file’s starting address and the terminal count register
with the total number of bytes to be transferred. The microprocessor disables the
address and data buses and gives memory system control to the DMA controller.
The DMA controller places sequential addresses on the microprocessor’s memory
bus and issues the read-write pulses. As each byte is transferred, the terminal
count register is decremented. When the register is decremented to 0, it tells the
external device that the data transfer is complete.
As this is a limited application, a special-purpose hardware controller can do
it very quickly. DMA transfers take place with speeds close to the memory cycle
time. Once the DMA controller has finished transferring data into or out of memory,
the DMA controller gives control back to the microprocessor. The microprocessor
cannot accomplish any other function when a DMA transfer is taking place. This
is due to two reasons. First, the microprocessor’s memory is being used for a data
transfer. It is not available to supply program instructions or receive the results of
computations. Second, the typical DMA process requires that the microprocessor
place its memory address bus and data bus in a high impedance condition. This
high impedance condition allows the DMA controller and the memory system to
control the bus, but prevents the microprocessor from providing any bus control.
Thus, the DMA controller temporarily borrows the address bus, data bus, and
control bus from the microprocessor and transfers the data bytes directly from the
external peripheral devices to a series of memory locations. Since the data transfer
is done by hardware means, it is much faster than it would be if done by program
instructions. In this section, we shall discuss Intel’s DMA controller chip 8237 in
detail.
IOR □ ~40 □ A7
low □ 2 39 □ A6
V<x ^ss
MEMR □ 3 38 □ A5
MEMW □ 4 37 □ A4 A0-A3
NC □ 5 36 L EOP A4-A7
DB0-DB7
READY □ 6 35 E A3
DMA
34 E A2 handshake
HLDA e ADSTB signals
33 E A1
ADSTB □ 8 AEN <^DRQ(P-DRQ3
Control
32 E AO
AEN E 9 8237 signals MEMR
DMA requests
I Vcc
from
31 for the four
HRQ □ 10 and to MEMW 8237
DBO memory channels
CS □ 11 30 E
Control IOW
29 E DB1
CLK □ 12 signals
EOP DACK0-DACK3
DB2 from
RESET c 13 28 E and to DMA
DB3 peripherals READY acknowledge
DACK2 □ 14 27 E
-------- >HR0
26 E DB4 RESET
DACK3 □ 15 •HLDA
25 E DACKO CLK
DREQ3 □ 16
DREQ2 17 24 E DACK1
23 E DB5
DREQ1 □ 18 ▼
22 DB6 CS
DREQO 19
21 E DB7
(GND)VU □ 20
These pins are connected to the system data bus. The programming of the 8237
is done through this data bus. A0-A3 pins are used to select one of the internal
registers when the 8237 is in the slave mode under the control of the processor.
A4-A7 lines, along with the A0-A3 lines, are used to send the higher-order 8-bit
addresses when the 8237 is acting as master and doing DMA data transfer. The
timing and control block derives internal timing from clock input and generates
external control signals. The 8237 has four separate DMA channels and each
channel includes two 16-bit registers, a DMA register, and a count register.
DRQ0-DRQ3 are the four DMA request signals input to the 8237 by external
peripheral devices. These four requests can be prioritized. The priority encoder
block resolves priority contention between DMA channels requesting service
simultaneously. The details of the 8237 pins are as follows:
(i) DB0-DB7 (I/O data bus): The data bus lines are bidirectional three-state
signals connected to the system data bus, which carries data.
(ii) CLK (clock input): The clock input is used to generate the timing signals
that control 82C37A operations. This input may be driven from DC to 12.5
MHz for the 82C37A-12, from DC to 8 MHz for the 82C37A, and from DC
to 5 MHz for the 82C37A-5. The clock may be stopped either in 1 state or
in 0 state for standby operation.
(iii) CS (Chip Select): Chip Select is an active low input used to enable the
controller.
(iv) Reset: This is an active high input which clears the command, status,
request, and temporary registers, the first/last flip-flop, and the mode register
counter. The mask register is set to ignore requests. Following a reset, the
controller is in an idle cycle.
(v) Ready: This signal can be used to extend the memory read and write pulses
from the 8237 to accommodate slow memories or I/O devices.
(vi) HLDA (Hold Acknowledge): The active high Hold Acknowledge from the
CPU indicates that it has handed over control of the system buses.
(vii) DREQ0-DREQ3 (DMA request): The DMA request (DREQ) lines are
individual asynchronous channel request inputs used by peripheral circuits
to obtain DMA service. In fixed priority mode, DREQO has the highest
priority and DREQ3 has the lowest priority. A request can be generated
by activating the DREQ line of a channel. The polarity of the DREQ is
programmable. Reset signal initializes these lines to active high. The DREQ
must be maintained until the corresponding DACK goes active. It will not
be recognized while the clock is stopped.
(viii) IOR (I/O read): I/O read is a bidirectional active low three-state line. In the idle
or slave mode, it is an input control signal used by the CPU to read the control
registers. In the active or master mode, it is an output control signal used by the
8237 to access data from the peripheral during a DMA write transfer.
(ix) IOW (I/O write): I/O write is a bidirectional active low three-state line. In the
idle cycle, it is an input control signal used by the CPU to load information
into the 8237. In the active cycle, it is an output control signal used by the
8237 to load data in the peripheral during a DMA read transfer.
(x) EOP (endof process): EOP is an active low bidirectional signal. Information
concerning the completion of DMA services is available at the bidirectional
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 279
EOP pin. A pulse is generated by the 8237 when terminal count (TC) for
any channel is reached, except for channel 0 in memory-to-memory mode.
(xi) A0-A3 (I/O address): The four least significant address lines are bidirectional
three-state signals. In the idle cycle, they are inputs and are used by the
8237 to address the control register to be loaded or read. In the active cycle,
they are outputs and provide the lower four bits of the output address.
(xii) A4-A7 (address): The four most significant address lines are three-state
outputs and provide four bits of address. These lines are enabled only during
the DMA service.
(xiii) HRQ (Hold Request): The Hold Request (HRQ) output is used to request
control of the system bus. When a DREQ occurs and the corresponding
mask bit is clear, or a software DMA request is made, the 82C37A issues
HRQ. The HLDA signal then informs the controller when access to the
system buses is permitted.
(xiv) DACK0-DACK3 (DMA Acknowledge): DMA Acknowledge is used to
notify the individual peripherals when one has been granted a DMA cycle.
DACK acknowledges the recognition of a DREQ signal.
(xv) AEN (Address Enable): The address enable signal is an active high signal
used to indicate the availability of the higher-order 8-bit address. It can be
used by the latch to store the address. AEN can also be used to disable other
system bus drivers during DMA transfers. AEN is an active high signal.
(xvi) ADSTB (Address Strobe): This is an active high signal used to control
latching of the upper address byte.
(xvii) MEMR (Memory Read): The Memory Read signal is an active low three-
state output used to access data from the selected memory location during a
DMA read or a memory-to-memory transfer.
(xviii) MEMW (Memory Write): The Memory Write signal is an active low three-
state output used to write data to the selected memory location during a
DMA write or a memory-to-memory transfer.
Table 7.44 lists the names and sizes of the internal registers of the 8237.
Name Size
Base address registers 16 bits
Base word count registers 16 bits
Current address registers 16 bits
Current word count registers 16 bits
Temporary address register 16 bits
Temporary word count register 16 bits
Status register 8 bits
Command register 8 bits
Temporary register 8 bits
Mode registers 6 bits
Mask register 4 bits
Request register 4 bits
280 MICROPROCESSORS AND MICROCONTROLLERS
Mode register Each channel has a mode register associated with it. When the
register is being written into by the microprocessor in the program condition, the
least significant bits 0 and 1 determine which channel is chosen. Figure 7.66 lists
the details of the mode register bits.
Bit number
00—Select channel 0
01—Select channel 1
10—Select channel 2
11—Select channel 3
XX—Readback
00—Verify transfer
01—Write transfer
10—Read transfer
11—Illegal
XX—If bits 6 and 7 = 11
Request register The 8237 can respond to requests for DMA service that are
initiated by software or by DREQ input. Each channel has a request bit associated
with it in the 4-bit request register. These are non-maskable and subject to
prioritization by the priority encoder network. Each register bit is set or reset
separately under software control. The entire register is cleared by a reset or
master clear instruction. Figure 7.67 shows the format of the request register and
its address coding. A software request for DMA operation can be made in block
mode or single mode. While reading the request register, bits 4-7 will always
read as Is, and bits 0-3 will display the request bits of channels 0-3, respectively.
Figure 7.67 shows the request register bits.
Mask register Each channel has a mask bit associated with it that can be set
to disable an incoming DREQ. Each mask bit is set when its associated channel
282 MICROPROCESSORS AND MICROCONTROLLERS
Bit number
00—Select channel 0 mask bit
01—Select channel 1 mask bit
10—Select channel 2 mask bit
11—Select channel 3 mask bit
(b)
Fig. 7.68 (a) Mask register (format 1) (b) Mask register (format 2)
Status register The status register contains information about the status of the
devices, to be read by the processor at any time. The format of status register is
shown in Fig. 7.69. This information includes which channels have reached a
terminal count and which channels have pending DMA requests. Bits 0-3 are set
every time a TC is reached by that channel or an external EOP is applied. These
bits are cleared upon reset, master clear, and each status read operation. Bits 4-7
are set when their corresponding channels request service, irrespective of the mask
bit state. If the mask bits are set software can poll the status register to determine
which channels have DREQs, and selectively clear a mask bit, thus allowing user
defined service priority. Status bits 4-7 are updated while the clock is high. They
are cleared upon reset or master clear.
Temporary register The temporary register is used to hold data during
memory-to-memory transfers. The temporary register always contains the last
byte transferred in the previous memory-to-memory operation, if not cleared by a
reset or master clear.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 283
(Contd)
284 MICROPROCESSORS AND MICROCONTROLLERS
The DMA controller operates in two major cycles, active and idle. After being
programmed, the controller is normally idle until a DMA request occurs on an
unmasked channel or a software request is given. The 8237 will then request control
of the system buses and enter the active cycle. The active cycle is composed of
several internal states, depending on what options have been selected and what
type of operation has been requested.
7.13.2.1 Idle Cycle
When no channel is requesting service, the 8237 enters the idle cycle. In this
cycle, the 8237 samples the DREQ lines on the falling edge of every clock cycle
to determine if any channel is requesting a DMA service.
7.13.2.2 Active Cycle
When the 8237 is in the idle cycle, and a software request or an unmasked
channel requests a DMA service, the device issues a hold request (HRQ) to the
microprocessor and enters the active cycle. It is in this cycle that the DMA service
takes place, in one of the following four modes:
Single transfer mode In single transfer mode, the device is programmed to make
one transfer only. The word count is decremented and the address decremented or
incremented following each transfer. When the word count rolls over from 0000H
to FFFFH, a terminal count bit in the status register is set and an EOP pulse is
generated. DREQ must be held active until DACK becomes active. If DREQ is
held active throughout the single transfer, HRQ becomes inactive and releases
the bus to the system. It becomes active again and upon receipt of a new HLDA,
another single transfer is performed. An exception to this occurs when a higher
priority channel takes over.
Block transfer mode In block transfer mode, the device is activated by DREQ
or a software request and continues making transfers until a TC, caused by the
word count going to FFFFH, or an external end of process (EOP) is encountered.
DREQ need only be held active until DACK becomes active.
Demand transfer mode In demand transfer mode the device continues making
transfers until aTC or an external EOP is encountered, or until DREQ goes inactive.
The data transfer continues until the I/O device has exhausted its data capacity.
Higher priority channels may intervene in the demand process, once DREQ has
gone inactive. EOP is generated either by a TC or by an external signal.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 285
Cascade mode This mode is used to cascade more than one 8237 for simple
system expansion. The HRQ and HLDA signals from the additional 8237 are
connected to the DREQ and DACK signals, respectively, of a channel for the initial
8237. This allows the DMA requests of the additional device to propagate through
the priority network circuitry of the preceding device. Figure 7.70 shows two
additional devices cascaded with an initial device using two of the initial device’s
channels. This forms a two-level DMA system. More 8237s could be added at the
second level using the remaining channels of the first level. Additional devices can
also be added by cascading into the channels of the second level devices, forming
a third level.
Second level
Additional devices
The following sequence explains the DMA method of data transfer in detail.
j) The DMA controller is initialized by writing the proper control words, the
data count (i.e., amount of data to be transferred), and the starting address
for data transfer in the address registers.
ii) The mode register is programmed for proper data transfer.
ii) The appropriate bits are selected to enable the DMA operation.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 287
(iv) When the peripheral device has the first byte of data ready, it sends a DMA
request, i.e., DREQ signal, to the DMA controller.
(v) If the input (channel) of the DMA controller is unmasked, the DMA
controller sends a hold request, i.e., HRQ signal to the microprocessor
HOLD input.
(vi) The microprocessor responds to this input by floating its buses and sends a
hold acknowledge signal to the DMA controller.
(vii) When the DMA controller receives the HLDA signal, it sends out a control
signal, disconnects the processor from the buses, and connects the DMA
controller to the buses.
(viii) When the DMA controller gets control of the buses, it sends out the
memory address where the first byte of data from the peripheral device is to
be written.
(ix) Then the DMA controller sends a DMA acknowledge, i.e., DACKO signal
to the peripheral device to tell it to get ready for the byte.
(x) Finally, the DMA controller asserts both the MEMW and the IOR lines on
the control bus.
(xi) Asserting the MEMW signal enables the addressed memory to accept data
written into it.
(xii) Asserting the IOR signal enables the disk controller to output the byte of
data from the disk to the data bus.
(xiii) The byte of data is then transferred directly from the peripheral device to the
memory location without passing through the CPU or the DMA controller.
(xiv) When the data transfer is complete, the DMA controller resets its hold
request signal to the processor and releases the buses. This lets the processor
take over the buses again until another DMA transfer is needed.
(xv) The processor continues executing from where it left off in the program.
POINTS TO REMEMBER
• The Intel processor IC 8085 needs additional slave chips like programmable peripheral
interfaces, keyboard/display interfaces, serial ports, timers, interrupt controllers, and
DMA controllers.
• Intel IC 8255 is a general-purpose programmable peripheral interface and can be used
to interface other devices like seven-segment displays, switches, ADCs, DACs, etc.
• Multiplexed displays and matrix keyboards reduce hardware complexity and can be
easily implemented using the slave IC Intel 8279.
• Serial data transmission can be easily done by the processor by interfacing the USART
8251 IC.
• The timing of various events can be controlled by the programmer by interfacing timer
ICs such as 8253 with the processor and connecting a clock to it.
• There is a need for a programmable interrupt controller if the number of peripherals
interfaced using the interrupt driven I/O method is higher than the interrupt capability
288 MICROPROCESSORS AND MICROCONTROLLERS
of the processor. Intel provides the programmable interrupt controller IC 8259 for such
applications.
• High speed data transfer between I/O devices and the processor can be achieved using a
technique called direct memory access. The DMA controller IC 8237 can be interfaced
with the processor to achieve direct memory access of memory by I/O devices.
Active cycle This is the cycle during which DMA service takes place.
Analog-to-digital converter The ADC converts the input analog voltage levels to
corresponding discrete digital signals.
Asynchronous transmission It is the method of serial data transfer done without a
common clock but at a common baud rate; it is character-oriented.
Baud rate The rate at which serial data is being transferred is called baud rate.
Bit set-reset mode The BSR mode is applied to port C of the 8255 for setting and
resetting individual port C bits.
Block transfer mode This is the mode in which the device that is activated by DREQ or
software request continues making transfers during the service until a TC caused by word
count going to FFFFH, or an external end of process (EOP) is encountered.
Cascade mode This is the mode in which the system is constructed using more than one
8237 cascaded for simple system expansion.
Cascading It is a method of connecting more than one 8259 in a microcomputer system,
to increase the number of interrupt sources.
Command instruction It is used for setting the operation features of the 8251.
Control word It contains information such as mode, bit set, and bit reset, that initializes
the functional configuration of the 8255.
Control words These are used to initialize each counter of the 8253. They program the
mode, loading sequence, and mode of counting (binary or BCD).
Counting This is the process of counting pulses applied at a random period and time.
Demand transfer mode This is the mode in which the device continues making transfers
until a TC or external EOP is encountered, or until DREQ goes inactive.
Digital-to-analog converter The DAC is used to get a proportional analog voltage or
current for the digital data given out by the microprocessor.
Display RAM It is a sequence of RAM locations in the 8279 to store the character data
to be used for display.
DMA Acknowledge This signal is used to notify the individual peripherals when one
has been granted a DMA cycle.
DMA request (DREQ) lines These are individual asynchronous channel request inputs
used by peripheral circuits to obtain DMA service.
DMA It is a method of data transfer between the memory and I/O devices, done without
the intervention of the microprocessor.
FIFO RAM It is a sequence of RAM locations in the 8279 to store the key code pressed
in a matrix keyboard interface.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 289
I/O mode This mode applied to ports A, B, and C of the 8255 for programming the data
transfer and direction of data transfer.
Idle cycle This is the state of the system when no channel is requesting service.
Initialization command words These are used to set up appropriate initial conditions
and specify the restart vector location
Interrupt mask register This register stores the masking bits of the interrupt lines to be
masked.
Interrupt service register This register stores all the levels that are currently being
serviced.
Key board de-bouncing This is the process of removing switch transient voltages and
detecting an actual key press.
Matrix keyboard It is an arrangement of switches in matrix wiring so that they can be
interfaced with the processor with minimum hardware and scanning requirements.
Mode instruction It is used for setting the function of the 8251.
Modem It is a modulator and demodulator that sends digital Is and Os as modulated
tones over standard phone lines.
Multiplexed display This is a method of interfacing many display devices with a
processor and using scanning method to display digits, with one digit being displayed at
a time.
Operational command words (OCWs) These are used for masking interrupts, setting
up status read operations, etc.
Priority resolver It examines the interrupt registers to determine whether the INT request
should be sent to the microprocessor or not.
Programmable timer A device in which the initial count value can be loaded using the
data from the data bus, and counting can be started and stopped using software instructions
written to the control register is called programmable timer.
Rate generator The frequency output of this mode is equal to the input frequency
divided by n.
Serial communication This term refers to the process of sending and receiving
information bit by bit.
Single transfer mode This is the mode in which the device is programmed to make one
transfer only.
Synchronous transfer It is the method of serial transfer in which the transmission and
reception of data is done simultaneously with a common clock.
Timing This is the prrocess of counting using a precise clock pulse at fixed frequency.
REVIEW GUESTIONS
6. Find the data direction and modes of operation of the ports of the 8255, if the control
word written into it is 80H.
7. Describe the function of EOC in the ADC interface with the 8085.
8. How can the frequency of a waveform generated using DAC be changed?
9. Why is a driver circuit needed for interfacing an LED to a port pin?
10. With encoded scan keyboard mode, the total number of keys that can be connected to
the 8279 is 128. Justify this statement.
11. Describe the block diagram of the 8279 keyboard/display interface.
12. What are the functions performed by the 8279?
13. Describe the different modes of operation of the keyboard interface with the 8279.
14. What are the different formats of display possible with the 8279?
15. What are the different control words of the 8279? Explain the function of each
command.
16. Name some applications of the 8253.
17. What is the difference between the 8253 and the 8254?
18. Explain how to configure a timer/counter using software.
19. List the various operating modes of the 8253.
20. How is the output frequency of the 8253 determined in rate generator mode?
21. What is the difference between hardware triggered strobe and software triggered
strobe?
22. What are the basic programming requirements to interface the 8253 with the 8085?
23. Compare serial and parallel communication.
24. Compare simplex and duplex transmission.
25. What is the difference between synchronous and asynchronous serial data transfer?
26. What is a modem?
27. Compare RS 232, RS 422, and RS 432 standards.
28. Draw the block diagram and explain the operations of the 8251 serial communication
interface.
29. Write and explain the mode word, command word, and status word formats of the
8251.
30. List the features modified by the mode instruction of the 8251.
31. Name the features modified by the command instruction of the 8251.
32. The synchronous mode of the 8251 is used for very high rate of data transfer. Is this
statement true or false? Justify your answer.
33. The order of instructions used to initialize the 8279 is important. Is this statement true
or false? Justify your answer.
34. Explain how data can be transferred using 8251 US ART at different baud rates.
35. What is the need for interrupt controller?
36. Compare maskable and non-maskable interrupts.
37. What is priority resolver?
38. List the internal registers of the 8259.
39. Write a note on cascaded mode of operation in the 8259.
40. What is EOI?
41. Explain the initialization process of the 8259.
42. Explain how the 8259 communicates with the 8085. Explain the different functions
available in the priority interrupt controller.
43. Draw the block diagram of the 8259 and explain how it can be used for increasing the
interrupt capabilities of the 8085.
FEATURES AND INTERFACING OF PROGRAMMABLE DEVICES FOR 8085-BASED SYSTEMS 291
NUMERICAL/DESIGN-BASED EXERCISES
1. Form the control word for setting port C’s fourth pin.
2. Configure the ports of the 8255 (PPI) as follows: port A = output, port B = input, port
C higher = output, port C lower = input. (Assume that the 8255 PPI is located at 20H-
23H.)
3. Write the handshaking signals and their functions if port A of the 8255 is set up as an
input port in mode 1.
4. Design an interface using the 8279 for interfacing six seven-segment displays and a
matrix hexadecimal keypad to work with the 8085 processor. Explain the software
needed for the interfacing.
5. Write a program to set up the 8253 as a square wave generator with a period of 1 sec.
(Assume that the input frequency to the 8253 is 1 MHz.)
6. Write a program using the 8253 to generate a PWM signal whose frequency and pulse
width can be changed. (Hint: You can use two timers—one in programmable one-shot
mode to generate variable pulse width and the other in rate generator mode to trigger the
one-shot mode counter at desired frequency.)
7. Write an ALP to initialize the 8251 USART and receive data on polled basis, given the
following parameters: Baud rate factor = 64, character length = 8 bits, no parity check,
and 1 stop bit. Assume port address 50H for data and 51H for control/status.
PROGRAMMING EXERCISES
1. Draw and explain a typical stepper motor interface. Further, write an ALP to rotate the
shaft of a four-phase stepper motor five times in the clockwise direction.
2. Write an ALP to generate a square wave using the 8255.
3. Show how you would interface a keyboard with the 8085 processor using the 8255.
Write an ALP to generate a key code for the key pressed.
4. Interface a set of eight simple switches and eight simple LEDS with the 8085 using a
8255 PPI chip. The 8255 should be selected for the following memory addresses: port
A—0740 H, port B—0742H, port C—0744H, and CWR: 0746H. Write a program to
indicate the status of the switches on LEDs.
5. Assume that a key matrix with the keys 0-9, *, -, /, and + are interfaced with the 8085
through the 8279. Eight single-digit display units are interfaced with the same 8279.
Develop a software for using the display and the keyboard as a calculator.
CHAPTERO j
8.1 INTRODUCTION
In the previous chapters, we have dealt with the basic architecture, instruction
set, programming, and interfacing of peripherals with the Intel 8085 processor.
Interfacing of memory chips and peripherals (such as LEDs, seven-segment
displays, ADCs, DACs, stepper motors, LCD displays, and timers) were considered
separately. However, when we want to build a complete system, the required
amount and type of memory and peripherals have to be simultaneously interfaced
with the processor. All these devices must be able to work in synchronism with
the processor without affecting one another. This chapter discusses the nuances of
interfacing all these devices to the 8085 to make a complete system and to execute
programs in the desired manner.
the data bus at a time. The devices that are not using the data bus must float
their individual data buses. This means that these devices must have their data
buses in the high impedance state. In this state, the devices can neither source nor
sink current. So the data bus is not affected, even though the device is physically
connected to it.
The control bus carries various control signals from the processor to the
peripherals and the memory. These control signals are responsible for selecting the
direction of data transfer. The memory and I/O device selection is also done using
control signals. The peripherals obey the control signals given by the processor.
Some control signals are given by the peripherals to the CPU. An example of such
a signal is the Ready signal. This is a signal given by a slow peripheral to the CPU.
If this signal is low, the processor waits until it is made high by the peripheral.
The sequence of instructions or programs for execution is stored as binary
numbers or codes in successive memory locations. The memory in a system
must be of at least two types. Read only memory (ROM) must be present in a
system because the start-up code, which is executed when the power is turned on,
must be resident in the memory permanently. Every system has some programs
stored permanently in the ROM. These programs are called firmware. To store the
temporary data during program execution, random access memory or read/write
memory (RAM) is necessary. Hence, a system must have at least one ROM and
one RAM chip. The addressing of memory locations is very important; so is the
selection of addresses for the ROM and RAM chips.
The Address Latch Enable (ALE) is the signal given by the processor to tell the
latch that the address has been sent out by the processor and has to be stored onto
the latch.
The Reset signal should be applied to the processor when it is switched on.
Hence, a power-on reset circuitry, as shown in Fig. 8.3, must be connected to the
active low RESET input pin of the processor. The capacitor voltage during power-
on is zero. Therefore, an active low Reset signal is applied to the processor. The
Reset signal should be applied long enough for the reset action to be completed
by the processor. The typical values of R and C are 75 kQ and 1 pF, respectively.
During normal operation, the capacitor will be charged to the Vcc supply voltage
and so the Reset signal will not be applied to the processor. A push button connected
to the RC network is used to apply the Reset signal manually. The value selected
for RI is comparatively low so that when the RESET push button is pressed, the
capacitor is discharged and an active low signal is applied to RESET IN.
The control signals IO/M, RD, and WR are used for data transfer between the
peripherals and the processor. The peripheral addressed by the processor is selected
using an address decoder. The higher-order address Unes are used by the address
decoder to select the
desired chip of memory
or an I/O port.
The address
decoding is done
using the higher-
order address lines
A14 and A15 and the
control signal IO/M.
The decoder IC 74139
can be used for this
purpose. IC 74139 is a
dual 2-4 decoder. The Fig. 8.4 Address decoding using decoder for the address map in
Table 8.1
inputs to the IC are the
higher-order address lines A14 and A15. The four outputs are active low outputs. The
enable input E is called gate input G1 and is an active low signal. So, this chip is
enabled for memory access when the IO/M signal is low. For the address map shown
in Table 8.1, the YO output is used as the active low Chip Select for the ROM chip.
The outputs Y2 and Y3 must be combined using an AND gate to form the active low
Chip Select for the RAM chip. The circuit diagram for this is given in Fig. 8.4.
A general 8085-based system needs at least two I/O ports—one port for an
input device and the other for an output device. The commonly used input device
is the keyboard matrix discussed in Chapter 7; the commonly used output device is
the multiplexed seven-segment display. These two devices can be interfaced with
the processor using a single support chip, the keyboard and display Controller
IC 8279, provided by Intel. So the system needs one 8279 chip. In addition, the
system needs to interface with digital input and output ports. This is supported
by Intel’s programmable peripheral interface chip IC 8255. The timing of events
A COMPLETE 8085-BASED SYSTEM 297
is an important issue in any microcomputer system and that necessitates the use
of a timer IC such as IC 8253. As an example, three slave ICs—8279, 8255, and
8253—are considered for interfacing with the 8085.
To start with the interfacing design, Tab|e g 2 Address map for |;Q devices
it is necessary to fix the address map for
the devices to be interfaced. Table 8.2 Chip Address range MSBs
gives the address map assumed for the A7 A6
three devices. Remember that the 8085 $279 OOH, 01H 0 0
processor uses only eight bits for I/O §255 40H, 41H, 42H, 43H 0 1
device accesses. Here, 8-bit addresses are §253 80H. 81H. 82H, 83H 1 0
assumed for the peripheral devices. The
most significant two bits vary from one chip to another. The 8279 uses only two
addresses, whereas the 8255 and 8253 use four addresses each. The interfacing of
these devices has been discussed in detail in Chapter 7.
The interfacing diagram for these three chips with the addresses assumed
is shown in Fig. 8.5. Chip selection is done using IC 74LS139, which is a
2-4 decoder. The two higher-order address lines A7 and A6 are used for address
decoding, i.e., device selection. This chip has to be selected only for I/O device
addresses. So the active low Chip Enable of the 74139 is connected to the inverted
IO/M control signal given by the processor. The VO chips under consideration are
selected only for VO accesses. The chips require a binary low signal as they have
an active low Chip Select. For memory accesses, the CS inputs will be high and so
these chips will not be selected. The lines have been connected such that the chip
8279 is selected when the higher-order address lines are 00. Similarly, the 8255
chip is selected when the higher-order address lines are 01 and the 8253 when
74LS139
Data bus
Fig. 8.5 Address decoding and interfacing I/O devices for the address map in Table 8.2
298 MICROPROCESSORS AND MICROCONTROLLERS
the lines are 10. If necessary, another peripheral IC chip can be connected to the
system with the Y3 output of 74139 as Chip Select.
Note that the data bus is common to all the devices since the individual data
buses of all devices are connected together. The data bus of only the device that is
selected is made active. Other devices that are not selected by a proper Chip Select
signal assign their data buses to the high impedance state. In this high impedance
state, the device data bus does not source or sink current.
In general, Figs 8.3-8.5 when combined form a complete 8085-based system.
To this system, additional devices such as ADCs, DACs, LEDs, switches, and
LCD displays can be connected, as explained in Chapter 7. Additional memory
chips can be interfaced by adjusting the address map, as explained in Chapter 6.
POINTS TO REMEMBER
• The 8085 processor requires at least one ROM and one RAM chip to make it a complete
microcomputer system.
• The system requires at least one input device and one output device.
• The address map for the memory and I/O devices used must be finalized before designing
the address decoder.
• Decoder chips such as 74138 and 74139 can be used for address decoding in a micro
computer system.
• The decoder for the system must consider whether the Chip Select signals are active low
or active high.
• The data bus is common and connected to all the devices in the system.
REVIEW QUESTIONS
NUMERICAL/DESIGN-BASED EXERCISE
Design an 8085-based system to read data from a temperature sensor and display the
temperature in a seven-segment display. Assume the relevant data.
Part 3
o--------------------- AAAA--------------------
INTEL 8051
MICROCONTROLLERS
INTRODUCTION TO 8051
MICROCONTROLLERS
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Differences between microprocessors and microcontrollers
• Description and features of Intel MCS-51 series microcontrollers
• Intel 8051 microcontroller architecture and features
• Power control modes of the 8051
• Stack operation in the 8051
9.1 INTRODUCTION
The microprocessor is a programmable chip that forms the CPU of a computer.
Nowadays, many microprocessor chips are available in the market for users to select
from depending on the application. In general, processor chips can be classified as
general-purpose microprocessors, microcontrollers, and DSP processors.
A general-purpose microprocessor is the CPU of a digital computer and
needs external components such as memory, input devices, output devices, and
decoders to function as a microcomputer system. These chips can be used to suit
any general-purpose application and can be configured by the user. Examples of
8-bit processors are Intel’s 8085, Zilog 80, and Motorola 6800. Examples of 16-
bit processors are Intel’s 8086 and 8088 and Motorola’s 68000 and examples of
32-bit processors are Intel’s 80186, 80286, and 80386, and Motorola’s 68030. In
general, these microprocessor-based systems get data from mass storage devices,
perform calculations, and store the results in storage devices. General-purpose
microprocessors use external memory and a lot of processor time is involved in
data transfer between the external memory and the processor.
Microcontrollers are processor chips that generally have memory, input ports,
and output ports within the chip itself. Therefore, they can also be called single
chip computers, computer-on-a-chip, or system-on-a-chip. Microcontrollers are
used in machine control applications, where there is no need to change the program.
Equipments that use microcontrollers include computer printers, plotters, fax
machines, Xerox machines, telephones, automotive engine control mechanisms,
and electronic instruments such as oscilloscopes, multimeters, planimeters, IC
testers, etc. The major difference between microprocessors and microcontrollers is
that microcontrollers are comparatively faster because of reduced external memory
accessing. Intel’s 8031, 8051, and 8096 and Motorola’s 68HC11 are examples of
microcontrollers.
304 MICROPROCESSORS AND MICROCONTROLLERS
DSP processors are processing chips that have flexibility in hardware and
software, to implement signal-processing algorithms. These processors have an
extended arithmetic and logic unit for operation on floating or fixed point number
formats. Like microcontrollers, DSP processors also have on-chip memory, I/O
ports, A/D converters, and serial ports. They are used in mobile phones, digital
cameras, PBX systems, and smart card readers.
Chapters 9-12 give a complete idea about the Intel 8-bit microcontroller’s
architecture, instruction set, programming, and hardware interfacing. Readers of
these chapters are expected to know the fundamentals of microprocessors, memory
structures, and assembly language programming.
64 KB
32 KB
8 KB 16 KB ROM
ROM
ROM ROM
4 KB ROM
Device number Data bus width (bits) RAM capacity (bytes) ROM capacity
All the microcontroller chips listed in Table 9.1 have the same basic ar chitecture.
The Intel 8051 is considered for further discussion in the topics that deal with
instruction set, and software and hardware interfacing in Chapters 9-12.
(iii) 128 bytes of on-chip data RAM (iv) four ports of eight bits each
(v) two 16-bit timers (vi) full-duplex serial port
(vii) on-chip clock oscillator
Figures 9.2 (a) and 9.2 (b) show the architecture and block diagram of the
8051, respectively.
In addition to these features, the 8051 provides Boolean processing, six
interrupt capabilities, and an 8-bit CPU for control applications.
The 8051 is an 8-bit microcontroller, i.e., the data bus within and outside the
chip is eight bits wide. The address bus of the 8051 is 16 bits wide. So it can
address 64 KB of memory. The lower-order address bus is multiplexed with the
data bus, as in the 8085 processor. The port 0 and port 2 pins of the 8051 form the
multiplexed address and data bus.
The 8051 is a 40-pin chip. The power supply +VCC and Vss takes two pins
and the built-in clock oscillator requires two pins (-XTAL1 and XTAL2) for
connecting the crystal.
The four control signal pins of the 8051 are PSEN, ALE, EA, and RST as
shown in Fig. 9.3 on page 307. RST is an active-high reset signal used to restart
the controller chip. The 8051 responds to an RST high input only if the RST is held
high for at least two machine cycles. A machine cycle is the period taken by any
processor to fetch and execute one instruction. In the 8051, the maximum number of
clock cycles taken for a machine cycle is 12. So the RST pin must be high for at least
PO 0-P0 7 P2 0-P2 7
AAAAAAAA AAAAAAAA
PSEN Timing
and
control
RST
Port 1 latch Port 3 latch
Port 1 drivers
-XtAL1 XTAL2
P1 0-P1 7 P3 Q-P3 7
24 clock periods. PSEN, ALE, and EA are the signals used in conjunction with
the external memory access of the 8051. They are discussed in detail in Chapters
11 and 12.
T2/P1.0 e1 40 □ Vcc
T2EX/P1.1 □2 39 □ P0.0/AD0
ECI/P1.2 e3 38 □ P0.1/AD1
CEX0/P1.3 e4 37 □ P0.2/AD2
CEX1/P1.4 □5 36 □ P0.3/AD3
CEX2/P1.5 □6 35 □ P0.4/AD4
CEX3/P1.6 □7 34 □ P0.5/AD5
CEX4/P1.7 □ 8' 33 □ P0.6/AD6
RST □ 9' 32 □ P0.7/AD7
8061
RXD/P3.0 □ 10 31 3 EA/VPP
Dual in-line
TXD/P3.1 □ 11 package 30 □ ALE
INT0/P3.2 E 12 29 □ PSEN
iNT1/P3.3 E 13 28 □ P2.7/A15
T0/P3.4 E 14 27 □ P2.6/A14
T1/P3.5 E 15 26 □ P2.5/A13
WR/P3.6 E 16 25 □ P2.4/A12
RD/P3.7 E 17 24 □ P2.3/A11
XTAL2 E 18 23 □ P2.2/A10
XTAL1 E 19 22 □ P2.1/A9
Vss E 20 21 □ P2.0/A8
Memory
I-------------- J I •
Program memory Data memory
I
Internal (4 KB)
I ■
External (64 KB)
I
Internal (128 bytes)
1
External (64 KB)
. t ... • ’ ' . , . - ' t
register banks are identified with two bits in the processor status word (PSW). The
PSW has two bits for identifying the register bank, i.e., 00 represents bank 0, 01
represents bank 1,10 represents bank 2, and 11 represents bank 3.
In the 8051, bitwise operations are also possible with special instructions using
the bit addresses. The bit-addressable memory is both bit-addressable (from OOH
to 7FH) and byte-addressable (from 20H to 2FH). Bit operations are helpful in
many control algorithms.
Using general-purpose scratch pad memory, programmers can read and write
data at any time for any purpose. This memory ranges from the byte address 30H
to the address 7FH.
memory, are the registers that control the entire processor. They can be accessed
only by direct addressing. The common SFRs are listed in Table 9.2.
The registers available in the 8051 are as follows:
(i) Accumulators—A and B
(ii) Processor status word—PSW
(iii) I/O port registers—PO, Pl, P2, and P3
(iv) Data pointers—DPH and DPL
(v) Serial data buffer register—SBUF
(vi) Stack pointer—SP
(vii) Timer registers—THO, TH1 and TLO, TL1
(viii) Timer control registers—TCON and TMOD
(ix) Power and port control—PCON and SCON
(x) Interrupt control registers—IP and IE
Programmers should not use the addresses in the range 80H-FFH (other than
the SFRs), as they are used by Intel Corporation for expanding the functions of the
8051. The 8051 has two accumulators—registers A and B. Register B forms the
accumulator for multiplication and division instructions; for other instructions it
can be accessed as a general-purpose register.
The stack in the 8051 is organized within the internal RAM area. The stack
pointer is eight bits wide and has to be initialized with an address in the RAM area.
When the 8051 is reset, the stack pointer is by default set to 07H. The stack pointer
is incremented before storing a data in the stack. Similarly, while reading data
from the stack, the data is read first and then the stack pointer is decremented.
80 P0 90 Pl
81 SP 98 SCON
82 DPL 99 SBUF
83 DPH A0 P2
88 TCON A8 IE
89 TMOD B0 P3
8A TLO B8 IP
8B TL1 DO PSW
8C THO E0 ACC
8D TH1 F0 B
accessed with the bit addresses given in Table 9.3. The contents of the PSW upon
reset are given in the third row.
Table 9.3 PSW format of 8051
Bit address D7H D6H D5H D4H D3H D2H D1H DOH
Parity bit (P) It is set to 1 if the accumulator contains an odd number of is, after
an arithmetic or logical operation.
Overflow flag (OV) This flag is set during ALU operations, to indicate overflow
in the result. It is set to 1 if there is a carry out of either the D7 bit or the D6 bit of
the accumulator. Overflow flag is set when arithmetic operations such as add and
subtract result in sign conflict.
The conditions under which the OV flag is set are as follows:
Positive + Positive = Negative
Negative + Negative = Positive
Positive - Negative = Negative
Negative - Positive = Positive
Register bank select bits (RSO and RSI) These bits are user-programmable.
They can be set by the programmer to point to the correct register banks. The
register bank selection in the programs can be changed using these two bits.
Table 9.4 explains how these bits can be changed to select the appropriate
register bank.
Table 9.4 Register bank selection using PSW
0 0 BankO 00H-07H
0 1 Bank 1 08H-OFH
1 0 Bank 2 10H-17H
1 1 Bank 3 18H-1FH
the accumulator and register B are also bit-addressable. The bit addresses of all the
bits of the accumulator and register B are given in Tables 9.5 (a) and 9.5 (b).
Table 9.5 (a) Addresses and contents of accumulator bits
Accumulator bits ACC.7 ACC.6 ACC.5 ACC.4 ACC.3 ACC.2 ACC.l ACC.O
Bit address E7 E6 E5 E4 E3 E2 El E0
Register B bits B.7 B.6 B.5 B.4 B.3 B.2 B.l B.O
Bit address F7 F6 F5 F4 F3 F2 Fl F0
actions will reset PCON.O and processor execution will resume at the instruction
following the instruction that set the idle mode.
POINTS TO REMEMBER
KEY TERMS
Data memory It is a read and write memory, which is used to store data.
Flags These are the bits used to indicate the status of the results after arithmetic and
logical operations.
Processor Status Word (PSW) It is the register containing the various flags that are
used to indicate the status of the results after arithmetic and logical operations.
Program memory It is a read only memory, which is used to store programs.
Special Function Registers (SFRs) These are part of the internal memory and are used
for specific functions and control of various functional blocks in the 8051.
REVIEW QUESTIONS
1. Find the contents of the flags in the 8051 after adding the binary numbers 01011100B
and 11000101B.
2. Write the binary word to be written into the PSW of the 8051, to select the register
bank 2 in the internal RAM.
3. Give at least two examples of situations in which the overflow flag is set.
4. What does a ‘0’ in the zero flag after an arithmetic operation mean?
CHAPTER 10
10.1 INTRODUCTION
With a basic idea about the architecture and the memory organization of the 8051,
it is easy to study the instruction set and its flexibility for control applications.
Unlike the 8085 instruction set, the 8051 instruction set has instructions for bit
manipulations. In addition, the 8051 instruction set supports addressing modes
such as indexed addressing and relative addressing.
Example:
ADD A, #80H
This instruction adds the data 80H to the contents of the accumulator and the result
is stored in the accumulator itself. This addressing mode is used when the data for
the arithmetic and logical operation is needed only once and is a constant.
This instruction adds the contents stored in register RO to the accumulator contents
and stores the result in the accumulator. The registers A, DPTR (data pointer
register), and R0-R7 are used in register direct addressing. This addressing mode
uses temporary registers, which hold the data for the operation.
This instruction adds the data in the accumulator to that stored in the memory
address 74H. All internal RAM addresses, including that of the special function
registers, can be used in memory direct addressing instructions. This addressing
mode is used when the data stored in memory is to be used in arithmetic and logical
instructions. The data in the memory, which is used in memory direct addressing,
can be changed at any point in the program.
The value stored in the register RO is now the address of the memory location
of the data to be fetched. From this memory location, the data is fetched and
the instruction is executed. The DPTR is used to access the data in the external
memory with 16-bit addresses. The indirect addressing mode is very useful for
accessing data that are stored in consecutive memory locations and accessed
serially in the program.
This instruction adds the contents of the accumulator to the contents of the data
pointer and the result forms the actual address from where data is to be fetched.
This data is moved to the accumulator.
316 MICROPROCESSORS AND MICROCONTROLLERS
The ADD instruction is used to add 8-bit data with the accumulator and the
result is stored in the accumulator (A) register. The carry generated (if any) is
stored in the carry flag of the processor status word PSW. The ADDC instruction
is used to add 8-bit data with the accumulator, along with the carry bit. The SUBB
instruction is used to subtract the contents of a register and the carry bit from the
contents of the accumulator. For the ADD and SUBB instructions, one of the data
must be in accumulator; the other data can be immediate or in any direct addressed
or indirect addressed internal memory location.
In addition to the ADD, ADDC, and SUBB instructions, there are two
more instructions—MUL and DIV. The register B is used exclusively for these
two instructions. The operands should be stored in the registers A and B. The
MUL instruction multiplies the contents of registers A and B and stores the 16-bit
result in both the registers. The lower-order byte of the result is stored in register
A and the higher-order byte in register B. The DIV instruction upon execution
divides the contents of register A by the contents of register B. The quotient of the
result is stored in register A and the remainder in register B. Division by 0, i.e., a
‘0’ in register B before the execution of the instruction DIV AB sets the overflow
flag (OV) to 1.
The DAA instruction is used to convert the binary sum obtained after adding
two BCD numbers into a BCD number.
Mnemonics Operation
The syntax for short jump instruction is ‘SJMP 8-bit address’ and can be explained
as follows:
(i) This 8-bit address is a relative address, relative to the program counter.
(ii) The branching address is calculated by adding the address given in the
instruction with the program counter content.
(iii) The 8-bit address is a 2’s complement number, i.e., the most significant bit
stands for the + or - sign. The remaining seven bits are used to specify the
address. Thus, using SJMP the user can branch to anywhere between 127
bytes after the program counter content and 128 bytes before it, i.e., from
(PC - 128) bytes to (PC + 127) bytes.
Example:
8800: SJMP 06H
This instruction shifts the execution to the location 8808H. The program counter
content after fetching the two-byte SJMP instruction is 8802H. So, adding 06H to
8802H results in 8808H.
The syntax for LJMP instruction is ‘LJMP 16-bit address’.
After execution of this instruction, the program counter is loaded with the
16-bit address in the instruction and execution shifts to that location.
The syntax for AJMP instruction is ‘AJMP 11-bit jump address’.
The destination branching address for this absolute jumping instruction is
calculated by keeping the most significant five bits of the program counter and
changing the least significant 11 bits as given in the instruction.
Example:
8800: AJMP 7F0H
This instruction branches the execution to the address 8FF0H. After fetching
this instruction, the program counter content will be 8802H. Keeping the most
significant five bits of the PC (10001) and changing the least significant 11 bits as
given in the instruction (111 1111 0000) results in the branching address 8FF0H.
The 8051 has a single instruction (DJNZ) for counter operation, i.e., to
decrement the destination data and branch according to the result of decrement
operation. This instruction is useful in looping using a counter, similar to the ‘for’
loop in high-level languages.
Similarly, jumping after checking the result of a comparison can be done by
the instruction CJNE. This instruction is useful in looping of instruction execution
based on a condition. This instruction can be used in programming constructs,
similar to the ‘do-while’ condition in high-level languages.
Mnemonics Operation
The logical instructions include ANL and ORL. Conditional branching includes
JC, JNC, JB, JNB, and JBC.
The other instructions available in this category include CLR, SETB, CPL, and
MOV.
Note that there are no instructions for halting machine execution.
Table 10.7 shows the flag bits affected by the various instructions. Note that
the increment and decrement instructions do not affect the flag register.
ADD yf yf
ADDC yf yf yf
SUB B yf yf yf
MUL 0 yf
DIV 0 yf
DA yf
RRC yf
RLC yf
SETB C 1
CLR C 0
CPLC yj
(Contd)
322 MICROPROCESSORS AND MICROCONTROLLERS
ANL C, /bit
ORL C, bit
ORL C, /bit
MOV C, bit
CJNE
Program:
START: MOV RI.tfCOUNT ; Load number count.
MOV RO, #30 ; Load the starting address of the memory.
LOOP: MOV @R0, #DATA : Load data in the memory location pointed to
by R0.
INC R0 ; Point to the next memory location.
DJNZ RI, LOOP ; Check for count after decrementing and loop
if not 0.
The following program uses direct addressing for achieving the same programming
objective, using the memory locations 30H-34H.
8051 INSTRUCTION SET AND PROGRAMMING 323
Example 10.2:
Add three 8-bit numbers.
The following program is developed assuming that the numbers are in memory
locations 3OH, 31H, and 32H of the internal data RAM. The result is stored in
memory locations 50H and 51H of the internal RAM.
Algorithm:
(i) The first byte is moved to the accumulator and the second byte is added
to it.
(ii) If the carry flag is set, register RI is incremented.
(iii) The third byte is added to the intermediate result.
(iv) If the carry flag is set, register RI is again incremented.
(v) The accumulator forms the least significant byte of the result and register
RI forms the most significant byte of the result.
Program:
START: MOV RI, #00H ; Set a register for storing the MSB of the
result.
MOV RO, #30H ; Set the starting address for the memory
locations with stored data.
MOV A, @R0 Get the first data.
INC RO Point to the next memory location.
ADD A, @R0 Add the data.
JNC LI Check for carry.
INC RI If a carry is present, increment the MSB of
the result.
LI: INC RO Point to the next memory location.
ADD A, @R0 Add the third data.
JNC L2 Check for carry.
INC RI If a carry is present, increment the MSB of
the result.
L2: MOV 50H, A Save the result.
MOV 51H, RI
Example 10.3:
Add two BCD numbers.
The following program is developed assuming that the BCD numbers are in
memory locations 30H and 31H of the internal data RAM. The result is stored in
memory locations 50H and 51H of the internal RAM, with the lower-order sum in
50H and the carry (if any) in 51H.
324 MICROPROCESSORS AND MICROCONTROLLERS
Algorithm:
(i) The first byte is moved to the accumulator and added to the second byte.
(ii) The accumulator is now decimal adjusted.
(iii) The value OOH is moved to the accumulator and added to the carry.
(iv) The result is stored in memory locations 50H and 51H.
Program:
MOV A, 30H ; Get the first data.
ADD A, 31H ; Add the. second data.
DAA ; Convert the surirto a BCD number.
MOV SOH, A ; Save the sum.
MOV A, #00H ; Move OOH to the accumulator.
ADDO A, #00H ; Load the carry of the sum in register A using ADDC
MOV 51H, A ; Save the result.
Example 10.4:
Add two 16-bit data.
In this example, the data are assumed to be initially stored in external memory
locations. The first data is stored in locations 4000H and 4001H, while the second
is stored in locations 4002H and 4003H.
Program:
MOV DPTR, #4000H ; Point to the first data.
MOVX A, @DPTR ; Bring the data pointed to by DPTR to the
accumulator.
MOV RO, A ; Move the LSB of the first data to RO.
INC DPTR ; Point to the MSB of the first data.
MOVX A, @DPTR ; Bring it to the accumulator.
MOV RI, A ; Move the MSB of the first data to RI.
INC DPTR ; Point to the next data.
MOVX A,@DPTR ; Move the LSB of the second data to the
accumulator.
ADD A, RO ; Add the LSBs of the two data.
MOV RO, A ; Store the sum in register RO.
INC DPTR ; Point to the MSB of the second data.
MOVX A, @DPTR ; Move the MSB of the second data to the
accumulator.
ADDC A, RI ; Add the MSBs of the data along with carry
from the previous addition.
MOV RI, A ; Store the MSB of the sum in register RI.
The sum is stored in registers RO and Rl, at the end of the execution of this
program.
Example 10.5:
Shift a 4-digit BCD number left by one digit. Assume that the data is stored in
30H and 31H.
8051 INSTRUCTION SET AND PROGRAMMING 325
Algorithm:
(i) The value OOH is stored in memory location 35H.
(ii) The least significant two digits (byte) are moved to the accumulator.
(iii) The nibbles of the accumulator are reversed and the least significant nibble
is exchanged with the value stored in memory location 35H.
(iv) The result is stored in memory location 50H.
(v) The same process is repeated for the next byte and the result is stored in
memory locations 51H and 52H.
Program:
MOV 35H, #00H ; Initialize the intermediate storage register.
MOV RO, #35H ; Initialize the memory pointer.
MOV A, 30H ; Move the least significant two digits of the data
to the accumulator.
SWAP A ; Exchange the two digits (nibbles) within the data.
XCHD A, @R0 ; Move the tens digit to the memory.
MOV 50H, A ; Register A has units digit of BCD data in the
upper nibble and 0 in the lower nibble and this is
stored in memory.
MOV A, 31H ; Move the higher-order data to register A.
SWAP A ; Exchange the lower-order and higher-order nibbles.
XCHD A, @R0 ; Move the thousands digit to the memory and
the tens digit to register A.
MOV 51H, A ; Save the shifted data in the memory.
MOV 52H, @R0 ; Save the thousands digit.
Example 10.6:
Read a byte from port 0 and depending on which bit is set, jump to one of eight
different locations.
Algorithm:
(i) First, the byte is moved from port 0 to one of the bit-addressable bytes (i.e.,
within 20H-27H of the SFR).
(ii) Then, depending on which bit is set, control is transferred to one of eight
different locations.
(iii) Control is returned to the start of the program to read the next data in the
port.
Program:
LOOP: MOV 20H, PORT 0 ; Get the data from port 0 to internal RAM 20H.
JB 00, LI ; Check the LSB using the bit address and
if set, jump to the relative address LI
JB 01, L2
JB 02, L3
JB 03, L4
JB 04, L5
JB 05, L6
326 MICROPROCESSORS AND MICROCONTROLLERS
JB 06, L7
JB 07, L8
LJMP LOOP
In this program, L1-L8 are the 8-bit relative addresses to which the branching has
to take place. The 8-bit relative address is assumed to be a 2’s complement number
and branching takes place above or below the main program.
Example 10.7:
Reverse the bits in a byte.
Algorithm:
(i) Assume that the byte to be reversed is stored in register RO.
(ii) Register RI is initialized with OOH and register R2 with 08H for use as
counter.
(iii) The byte from register RO is loaded in the accumulator.
(iv) The accumulator is shifted left through the carry and exchanged with
register RI. Now, the LSB of the data is moved to the carry and the shifted
data is moved to RI.
(v) The accumulator is shifted right through the carry and exchanged again
with register RI. Now, the LSB in the carry is shifted into register RI.
(After subsequent shifts, it is moved into the MSB of RI.)
(vi) The value stored in register R2 is decremented and if it is not zero, execution
is transferred to step 4.
Program:
MOV R0,#data ; Initialize RO with data.
MOV RI, #00H : Initialize RI with zero, to store the result
MOV R2, #08H ; Initialize the counter.
MOV A, RO : Get the data.
LOOP: RLC A : Bring one bit to the carry.
XCH A, RI : Bring RI to register A.
RRC A ; Move the bit in the carry flag in reverse
order into RI.
XCH A, RI ; Bring the shifted data to the accumulator,
for shifting the next bit.
DJNZ R2, LOOP : Check if all eight bits have been reversed.
If not, repeat the process for the next bit
MOV 30H, RI ; Store the result from register RI in the
internal RAM location 30H.
WAIT: SJMP WAIT ; Loop indefinitely here.
Example 10.8:
Find the biggest number in a block of data stored in the memory locations 70H-
7FH.
Algorithm:
(i) A memory pointer is initialized to point to the starting address of the series
of memory locations.
8051 INSTRUCTION SET AND PROGRAMMING 327
Program:
MOV RI, #00H ; Initialize RI to store the biggest
number.
MOV RO, #70H : Initialize the memory pointer.
MOV 30H, @R0 : Store the first data as the biggest,
in register 30H.
MOV RI, 30H ; Store it in register RI also.
LOOP: INC RO ; Point to the next location.
MOV A, RI : Bring the biggest data to the
accumulator.
SUBB A, @R0 : Compare by subtracting the next data
from the biggest.
JNC NEXT : If there is no carry, the data is
smaller. So, jump to NEXT.
MOV 30H, @R0 ; If the compared data is bigger, bring
it to register 30H as the biggest
data.
MOV RI, 30H ; Store it in register RI also.
NEXT: CJNE RO, #80H, LOOP ; Repeat these steps for all the data
in the memory up to the address 7FH.
The programs in this section illustrate only the basic instructions of the 8051.
In general, the instruction set of the 8051 is very flexible. Therefore, readers are
recommended to go through all the 8051 instructions.
POINTS TO REMEMBER
• Intel 8051 supports data types such as integer, signed integer, and bits. There are
instructions to support these data types.
• The Intel 8051 instructions are classified based on their operations as data transfer,
arithmetic, logical, and branching instructions.
• Intel 8051 supports immediate, direct, indirect, indexed, and relative addressing
modes.
• Some instructions support only specific addressing modes.
• The flag register plays an important role in conditional jumping instructions.
• Relative jumping instructions use an 8-bit signed number as the offset to be added to the
program counter contents, to get the branching address.
328 MICROPROCESSORS AND MICROCONTROLLERS
KEY TERMS
Absolute jump This refers to jumping using the 11-bit address mentioned in the
instruction, keeping the most significant five bits the same as in the PC.
Long jump This refers to jumping using the 16-bit address in the instruction.
Short jump This refers to jumping using the 8-bit relative address added to the contents
of the PC.
REVIEW QUESTIONS
PROGRAMMING EXERCISESl
1. Write a program to multiply two 8-bit numbers in the internal RAM and store the result
in the external RAM. [Hint: Use MOVX instruction to access external memory.]
2. Write a program to divide a number in the internal RAM location 40H by the number
in the location 41H. Store the quotient in 50H and the remainder in 51H.
3. Write a program to search for a particular data in a block of internal RAM. Identify and
store the memory location of that data.
4. Write a program to arrange a block of binary numbers in ascending order.
1. Find the address to which program execution is transferred after the execution of the
instruction SJMP FOH, if it is stored in the address 8811H.
2. Find the relative address to be used in the SJMP instruction, so that the same SJMP
instruction is executed after jumping.
3. Explain the difference between the following instructions:
JB 30H, 4EH
JBC 30H, 4EH
CHAPTER 11
11.1 INTRODUCTION
The major benefit of microcontrollers lies in their built-in parallel ports. The
parallel ports can be used to interface all data converters (ADCs, DACs, etc.) and
display devices (LEDs, LCDs, etc.).
Any microcontroller-based system needs to transfer data between the external
peripherals and the microcontroller. The microcontroller needs to read data fed
by the user from the external interface, process it, and give the output to the
peripherals or to the user again. To communicate data with the external world, the
microcontroller needs ports. The ports may support either parallel or serial data
transfer.
ports by their bit addresses. Using these bit addresses, individual bits can be read
or changed.
Port 0 80 Port 2 A0
Port 1 90 Port 3 B0
Figure 11.1 shows the parallel ports in the 8051 and their pins.
+5V
40
30 39
ALE P0.0/AD0
31 38
EA P0.1/AD1
29 37
PSEN P0.2/AD2
9 36
C2 RST P0.3/AD3
35
P0.4/AD4
h n33PF 34
P0.5/AD5
18 33
XTAL2 P0.6/AD6
19 32
XTAL1 P0.7/AD7
X1
1 21
P1.0 P2.0/AD8
Ld U33pf 2 P1 1 P2.1/AD9
22
C1 3 23
P1.2 8051 P2.2/AD10
4 24
P1.3 P2.3/AD11
5 25
P1.4 P2.4/AD12
6 26
P1.5 P2.5/AD13
7 27
P1.6 P2.6/AD14
8 28
P1.7 P2.7/AD15
10
P3.0/RXD
11
P3.irrxD
12
P3.2/INT0
13
P3.3/INT1
U
P3.4/T0
15
P3.5/T1
j6
P3.6/WR
17
P3.7/RD
Read latch
Internal bus
Write to latch
Read pin
Besides input and output, ports 0 and 2 have an alternative function—they can
be used as address/data bus when external memory or I/O devices are accessed.
Port 0 acts as the lower-order address bus and port 2 acts as the higher-order
address bus. The drivers of ports 0 and 2 have an internal multiplexer for this
purpose. The internal structure of port 2 is shown in Fig. 11.4.
Read latch
Internal bus
Write to latch
Read pin
The internal structure of the port 3 pins is shown in Fig. 11.5. From the figure
it can be understood that the alternative functions can be activated only if the
data ‘1’ is written in the port 3 bits.
With respect to port access, there are two possibilities for the read operation.
The read instruction for a port can read either the port latch or the port pins. This
difference is made in the internal hardware of the 8051, to avoid misinterpretation
of the voltage level at the pins. Some of the instructions read a port, modify the
data, and write the data back into the port. These instructions read the data from the
latches and not the pins. All other instructions accessing the ports read the data from
the port pins only. Examples of instructions that read the port latch are as follows:
ANL Pl, A
ORL P2, A
XRL P3, A
INC P0
DEC Pl
JBC Pl.l, DELAY
CPL P3.0
334 MICROPROCESSORS AND MICROCONTROLLERS
Connecting the EA pin of the 8051 to logic 1 or +5V will program the
microcontroller to use the internal program memory for the addresses starting
at 0000H. After the available internal memory is used completely, the external
memory is accessed.
If EA = 0, the internal program memory is not accessed.
If EA = 1, the internal program memory is accessed for the address range 0000H-
OFFFH (or the available range) and the external program memory is accessed for
addresses greater than OFFFH.
Example 11.1:
Design an interface circuit to connect a 16 KB EPROM IC 27128 to the 8051.
Solution:
As the 27128 has 16 KB of memory registers, 14 address lines are required to
select one memory location. The first step in interfacing is to select or fix the
address range for the chips to be used. So, the address map of the 27128 is fixed
as 0000H-3FFFH. The most significant bits A14 and A15 are used to decode and
select the chip. For decoding purposes, a 2-to-4 decoder chip, the 74139, is used.
The 74139 has a dual 2-to-4 decoder and one of them is used for selecting the
EPROM chip. The connection diagram is shown in Fig. 11.7. The 8-bit latch or
register IC 74373 is used to de-multiplex the lower-order address and data bus.
OE is the data read enable line of the 27128 and is connected to the PSEN signal
output of the 8051.
Solution:
IC 6264 has 8 KB of static RAM. So, 13 address lines are required to select a
memory location. The first step in interfacing is to select the address range for
the chip to be used. The address range for the RAM chip is selected as C000H-
DFFFH. The most significant bits A14 and Al5 are used to decode and select the
chip. Bits Al4 and Al5 are at logic 1 for the address range selected. So a 2-to-4
decoder chip, the 74139, is used and the Y3 output is made active low when the
A14 and Al5 lines are at logic high. This Y3 signal is used as the active low chip
select input of the IC 6264. The connection diagram is shown in Fig. 11.9. The
data read enable line of the 6264 is OE, which is connected to the P3.7 port output
'of the 8051. Similarly, the write enable input 6264 is connected to the port line
P3.6 of the 8051. The 8-bit latch or register IC 74373 is used to de-multiplex the
lower-order address and data bus.
HARDWARE FEATURES OF 8051 337
11.3.3 Timing Diagram for External Program and Data Memory Access
The timing diagram for the 8051 program memory access is given in
Fig. 11.10 (a). As discussed earlier, port 0 is used for the lower-order address as
well as for data. Port 2 is used for the higher-order address. Program memory
selection is done by the active low PSEN signal.
During the first state, the lower-order address from the program counter is
placed on the port 0 lines. To de-multiplex the lower-order address bus and the
data bus, an active high signal is sent on the ALE line. This signal is used by
the external latch to hold the lower-order address. The PSEN signal is used as a
Read signal for the memory. During the second state, the microcontroller sends the
higher-order contents of the program counter on the port 2 lines. During the fourth
state of the machine cycle, the data (or the opcode in this case) is placed on the
port 0 lines by the program memory, which is read by the microcontroller. The last
two states or four clock cycles in a machine cycle can be used for the next memory
access. So, in 8051 microcontrollers, during one machine cycle, two memory read
operations are possible, using the ALE signal twice. If the instruction read is a
one-byte instruction, the second byte read is discarded.
The timing diagram for signals related to external data memory write operation
is given in Fig. 11.10 (b). The external data memory, with 16-bit addresses, is
accessed using the MOVX instruction; the data pointer DPTR is used to hold
the address of the data memory. Port 0 sends the lower-order byte of DPTR and
338 MICROPROCESSORS AND MICROCONTROLLERS
Clock
signal
ALE
WR
Port 2
PortO
Fig. 11.10 (a) Timing diagram for external program memory access (b) Timing diagram for
external data memory access—write operation
port 2 sends the higher-order byte. The active low Write signal is sent by the
microcontroller on the line P3.6. As seen in the program memory access timing
HARDWARE FEATURES OF 8051 339
diagram in Fig. 11.10 (a), the memory access for the data memory can be started
in the fifth state, S5, of a machine cycle and completed in the fourth state, S4, of
the next machine cycle.
The external memory read access also follows the timing diagram given in
Fig. 11.10 (b) with the control signal WR replaced with RD. The data is transferred
from the memory devices to the microcontroller. The active low RD signal is sent
out through the P3.7 line.
TMOD SFR
The TMOD SFR in the 8051 controls the timer operation. The TMOD register bits
are used to select the timer operating modes, counting or timing operation, and
gate control. The higher-order four bits are used for Timer 1 and the lower-order
four bits are used for Timer 0. The individual bits of TMOD have the functions
shown in Table 11.4.
Table 11.4 Bit patterns for TMOD (89H) SFR
D7 D6 D5 D4 D3 D2 D1 DO
T1 T1 TO TO
GATE1 C/Tl GATE0 C/T0
Ml MO Ml M0
1—count 1— count Mode set 1—count 1— Mode set
only if pulses on 00— 13-bit only if INTO count 00—13-bit
INTI pin the pin T1 timer pin (P3.2) pulses timer
(P3.3) (P3.5) input is high on the
input is 01—16-bit pin 01—16-bit
high 0—count timer 0—count T0(P3.4) timer
on every regardless of
0—count machine 10—8-bit INTO pin 0— 10—8-bit
regardless cycle auto reload count auto reload
of INTI mode on every mode
pin machine
11—Split cycle 11—Split
timer mode timer mode
For each timer, two bits are used to specify the mode of operation. So each timer
can be operated in any one of four modes.
TCON SFR
The SFR that controls the two timers and provides valuable information about
them is TCON. The TCON SFR bit pattern is given in Table 11.5.
HARDWARE FEATURES OF 8051 341
The higher-order four bits of the TCON SFR are described in Table 11.5. The
lower-order four bits are related to interrupts and are explained in Section 11.5.
Note that the individual bits of TCON register can be addressed separately by their
bit addresses. This allows the programmer to run the timers using bit-addressable
instructions and check the overflow independently.
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto reload
1 1 3 Split timer mode
The clock pulses selected by D2 and D6 bits of TMOD are then controlled by
programmer setting and connected to the timer registers. The control is done by
three different means. The first is the Timer Run control bits D4 and D6 of the
TCON register. The timer will run only when the Timer Run control bits are set
to 1. Other controls for the timer are through the GATE control bits D4 and D7 of
TMOD and the external inputs for the timer. Setting GATE to 1 allows the timer
to count only if the external control input INTO or INTI is set to 1. Setting GATE
to 0 will disable the corresponding external timer control inputs INTO and INTI.
Setting the timer to mode 0 will make it overflow back to zero, after 8192
counts. This will set the TF1 and TFO bits for Timer 1 and Timer 0, respectively.
Mode 1—16-bit Timer Mode
In timer mode 1 of the 8051, each timer is operated as a 16-bit timer. This is a
very commonly used mode. It functions just like mode 0, except that all 16 bits are
used. As mode 1 uses 16 bits, the counter will count from 0 to 65,535. The clock
pulses are applied to the lower-order eight bits and the overflow from this lower-
order byte will be counted by the higher-order byte.
Timer operation in mode 1 and control of its gating are similar to mode 0, as
shown in Fig. 11.12.
For example, let us say THO holds the value FDH and TLO holds the value
FEH. At the next counting pulse, TLO will be incremented to FFFI. Then for the
next pulse, the TLO will overflow and will become OOH. As it is in reload mode, the
TLO will be loaded with the value in THO, i.e., FDH. The value of THO will never
change. THO/1 is set to a known value and TL0/1 is the SFR that is constantly
incremented. The auto reload mode is very commonly used for establishing a baud
rate for serial communications.
Timer operation in mode 1 and control of its gating are similar to mode 0, and
are shown in Fig. 11.13.
In mode 3, all the bits that are related to the real Timer 1 simply hold their
count and do not run. The situation is similar to maintaining TRI at 0. In split
timer mode of Timer 0, the real Timer 1 (i.e., TH1 and TL1) cannot be started or
stopped, since the related control bits are now linked to THO. The real Timer 1, in
this case, will be incremented every machine cycle.
When two separate timers, in addition to a baud rate generator, are required in
344 MICROPROCESSORS AND MICROCONTROLLERS
an application, the real Timer 1 can be used as a baud rate generator and THO/TLO
can be used as two separate timers in mode 3.
Example 11.3:
Write a program to generate a square wave of frequency 2 kHz on any one of the
port pins, using Timer 0, assuming that the clock frequency of the 8051 system is
12 MHz.
Solution:
As a continuous square wave has to be generated, the 8-bit timer auto reload mode
is selected for this operation. Since the clock frequency is 12 MHz, after dividing
by 12, the counter will get the clock pulses at the rate of 1 MHz. So, the counter
will be incremented after every 1 ps.
To generate a square wave of 2 kHz, the period should be set to 500 ps
(1/2kHz). This means that the program must give an output of logic 1 for
250ps and logic 0 for another 250 ps, so that a square wave of 500 ps period can
be generated. Here, as the counter is incremented after every microsecond, the
counter has to count up to 250. The counter will overflow after every 250 counts.
During an overflow, the logic output on the pin will be inverted. By doing this
continuously, a square wave can be generated.
This program uses the polled method to check whether the timer overflow has
occurred. The LSB of port 1 is used to generate the square wave.
MOV TMOD, #000000106 ; Set Timer 0 in mode 2.
CLR TFO ; Clear Timer 0 overflow flag.
346 MICROPROCESSORS AND MICROCONTROLLERS
Example 11.4:
Write a program to generate a square wave of frequency 1 kHz on any one of the
port pins, using Timer 0, assuming that the clock frequency of the 8051 system is
12 MHz.
Solution:
Example 11.4 is similar to Example 11.3. However, to generate a square wave of
1kHz, the period should be set to lOOOps (1/1 kHz). So, the 8-bit counter mode
cannot be used, as the count value will exceed 255.
The clock frequency is 12 MHz after dividing by 12; the counter will get the
clock pulses at the 1 MHz rate. So, the counter will be incremented every 1 ps. The
program must give an output of logic 1 for 500 ps and logic 0 for another 500 ps,
so that a square wave period of 1000 ps can be generated.
The program uses 16-bit counter mode. As there is no auto reload option with
the 16-bit timer mode, the user or the programmer has to reload the count value
every time in the program. The timer should produce a delay of 500 ps. So, the
16-bit counter must be initialized to 65,035 (i.e., 65,535 - 500). The hexadecimal
equivalent of this decimal value is FE0B. Therefore, the lower-order eight bits of
the timer are initialized to 0BH and the higher-order eight bits to FEH.
The following program uses the polled method to check whether the timer
overflow has occurred. The LSB of port 1 is used to generate the square wave.
MOV TMOD, #00000001B ; Set Timer 0 in mode 1 (16-bit operation).
CLR TFO ; Clear the Timer 0 overflow flag.
CLR P1.0 ; Output logic 0 on the LSB of port 0.
LOOP: MOV THO, #OFEH ; Initialize the 16 bits of Timer 0 with the
appropriate value.
MOV TLO, #OBH
SETB TRO ; Start or run Timer 0 by setting the TRO bit.
WAIT: JNB TFO, WAIT ; Read, check, and loop until the overflow bit
TFO in TCON register is set.
CLR TRO
CLR TFO ; Clear the overflow flag.
CPL P1.0 ; Complement Pl bit 0.
LJMP LOOP ; Jump again for loading and starting the
counter.
HARDWARE FEATURES OF 8051 347
Example 11.5:
Write a program to count 10,000 objects on a conveyor belt, assuming that a sensor
gives a pulse for every object moving on the conveyor belt. Generate an interrupt
after that. Use the ISR to give a logic 1 output on any port pin.
Solution:
To count the external pulses, Timer 1 is initialized as a counter by setting the D6
bit of TMOD. Mode 1 of Timer 1 is used in this example, as 10,000 objects are to
be counted and this can be accomplished by the 16-bit timer mode only. Timer 1
is loaded with the value 55,535 (i.e., 65,535 - 10,000), which in hexadecimal form
is D8EF. This gives an interrupt after 10,000 counts.
The program is written in two parts. The main program initializes the timer and
runs it. After that, the main program does nothing. Detecting the count value of
10,000 is done automatically by running the timer and generating an interrupt at
every overflow. Then the interrupt service routine is used to give a logic 1 output
on the port pin.
Main program:
MAIN: MOV TMOD. #01010000B Set Timer 1 in mode 1 (counter
operati on).
MOV TH1, #0D8H Load the Timer 1 highe r-order byte
MOV TL1, #OEFH Load the Timer 1 lower -order byte.
MOV IE, #10001000B Enable Timer/Counter 1 i nterrupt.
SETB TRI Start Timer/Counter 1.
LOOP: LJMP LOOP Loop and do nothing.
The different interrupt sources have to be distinguished and the 8051 must
execute different subroutines depending on which interrupt was triggered. This
is accomplished by jumping to or calling a fixed address, when a given interrupt
occurs. These addresses are called interrupt vector addresses or interrupt handler
addresses. Table 11.7 lists the interrupt vector addresses for the five interrupts.
Table 11.7 Interrupts and their vector addresses
Whenever Timer 0 overflows (i.e., the TFO bit is set), the main program is
suspended temporarily and control is transferred to 000BH. It is assumed that the
service routine at address 000BH handles the Timer 0 overflow.
Bit position D7 D6 D5 D4 D3 D2 DI DO
Bit address AF AC AB AA A9 A8
Name EA - - ES ET1 EXI ETO EXO
Each 8051 interrupt has its own bit in the IE SFR. A particular interrupt can
be enabled by setting the corresponding bit. For example, to enable the Timer 1
interrupt, one of the following instructions can be executed: MOV IE, #08H or
SETB ET1.
However, before the Timer 1 interrupt (or any other interrupt) is truly enabled,
bit 7 of the IE SFR must also be set. Bit 7, which is the global interrupt enable/
disable, enables or disables all interrupts at the same time, i.e., if bit 7 is cleared,
HARDWARE FEATURES OF 8051 349
no interrupts occur, even if all the other bits of the IE SFR are set. Setting bit 7
enables all the interrupts that have been selected, by setting other bits in the IE
SFR.
This list also gives the priority of the interrupts. So, whenever the External 0
interrupt and Timer 1 interrupt occur at the same instant, the 8051 microcontroller
executes the interrupt service routine corresponding to External 0 interrupt first.
Then the 8051 returns to the main program, executes one instruction, and then
executes the interrupt service routine corresponding to the Timer 1 interrupt.
This also means that if a serial interrupt occurs at the same instant that an
External 0 interrupt occurs, the External 0 interrupt routine is executed first and
then the serial intermpt routine is executed.
There are two levels of intermpt priority in the 8051—high and low. Using
intermpt priorities, the above intermpts can be divided into two separate interrupt
priority categories.
Interrupt priorities are controlled by the IP SFR (B8H), which has the format
shown in Table 11.9. For example, if the serial intermpt is more important than
the Timer 0 interrupt, the Intermpt Priority register IP SFR at the address B8H
can be programmed to set the priority appropriately. This can be accomplished by
assigning a high priority to the serial intermpt and a low priority to the Timer 0
intermpt. By setting the D4 bit to 1, the serial intermpt will be set to higher priority
and by making the DI bit 0, the Timer 0 intermpt will be set to lower priority. Note
that the priority can be set individually by using the bit addresses of the IP register.
For example, the Timer 0 intermpt priority can be made high by setting the DI bit
of the IP SFR. So, the following instructions can be used: SETB PT0, SETB B9H,
and MOV IP, #82H.
Table 11.9 Bit patterns for IP (B8H) SFR
Bit position D7 D6 D5 D4 D3 D2 DI DO
Bit address BC BB BA B9 B8
Name EA — — PS PT1 PX1 PT0 PX0
Explanation Enable Undefined Undefined Serial Timer 1 External Timer 0 External
Interrupts— interrupt interrupt 1 interrupt 0
Made 0 to priority priority interrupt priority interrupt
disable all priority priority
• interrupts
350 MICROPROCESSORS AND MICROCONTROLLERS
Based on the two-level priority set by the IP register, interrupt subroutines are
executed as follows under various conditions:
If there is more than one high-priority interrupt and if any two of them appear
at the same time, the priority among these two will be decided by the order in
which the interrupt conditions are checked by the hardware.
The complete structure ofthe 8051 interrupts can be understood well by referring
to Fig. 11.16. The five interrupt sources are first passed through the IE register,
which decides the enabling and disabling of interrupts. The global interrupt enable
signal is also shown in the figure. The IP register sets two priority levels among
the available interrupts. This is shown in the figure as the high priority and low
priority blocks. The bits ITO and IT1 can be set by the TCON SFR; they are used
to decide whether the hardware interrupt is level-triggered or edge-triggered.
enable
one machine cycle, the interrupt signal applied at the pins of the 8051 must be
available for at least 12 clock periods.
External interrupts are applied at the pins INTO and INTI. The sensing of the
voltage level applied to this pin can also be programmed in the 8051. The interrupts
can be either level-triggered or edge-triggered, as set by the ITO and IT1 bits of
the SFR TCON, as shown in Table 11.10. A ‘0’ in these bit positions will make
both the hardware interrupts level-triggered. When an interrupt is level-triggered,
a low-level voltage on the interrupt pin will activate the interrupt.
Table 11.10 Bit patterns for the TCON (88H) SFR
Bit position D7 D6 D5 D4 D3 D2 DI DO
Bit address 8F 8E 8D 8C 8B 8A 89 88
Name TF1 TRI TF0 TRO LEI IT1 IE0 ITO
Interrupt Interrupt
1 type 0 type
control. control.
Timer Timer
Set to Set to
1 run 0 run
External 1 by External 1 by
Timer 1 control Timer 0 control
interrupt software interrupt software
Explanation overflow bit. Set overflow bit. Set
1 edge for edge- 0 edge for edge-
flag to 1 by flag to 1 by
detect bit triggering detect bit triggering
software software
and and
to run. to run.
cleared cleared
for level- for level-
triggering triggering
A ‘1’ on the ITO and IT1 bits of the SFR TCON will program the hardware
interrupts as edge-triggered. When an interrupt is edge-triggered, a change of
voltage from a high state to a low state will activate the interrupt.
When an interrupt is triggered, the microcontroller performs the following actions
automatically:
(i) The lower-order byte of the program counter is stored in the location
pointed to by the stack pointer and the higher-order byte is stored in the
next consecutive location.
(ii) The priority among all the interrupts received is resolved, the lower-order
priorities are blocked, and only one interrupt is considered for execution.
(iii) If the interrupt selected is a timer or an external interrupt, the corresponding
interrupt flags are cleared.
(iv) The corresponding interrupt vector address is loaded into the program
counter. This results in the execution of the interrupt service routine.
(v) The RETI instruction at the end of the interrupt service routine transfers the
execution to the main program by popping the return address from the stack
to the program counter.
The internal architecture of the 8051 is such that the external hardware
interrupts will be cleared automatically when the interrupt service routine is
352 MICROPROCESSORS AND MICROCONTROLLERS
Solution:
In this example, the timer is initialized in mode 2. This is the 8-bit auto reload
mode. The clock frequency applied to the counter is assumed to be 1 MHz. So, the
counter is incremented every 1 ps. To generate a square wave of 10kHz, the port
pin is toggled after every 1/ (2x10,000) s. This time is calculated to be 50 ps. So,
the counter is loaded with the count value of 50 to generate a delay of 50 ps.
The LSB of port 1 is toggled in the interrupt service routine. The timer will
give an interrupt after every 5Ops.
ORG OOOBH ; Timer 0 interrupt vector
OOOBH: CPL P1.0 jToggle the port bit.
RETI
Main program:
MOV TMOD, #02H ; Initialize Timer 0 in mode 2.
MOV THO. #-50 ; Initialize the reload value with 50, to
produce 50ps delay.
SETB TRO : Start Timer 0.
MOV IE, #82H ; Enable Timer 0 interrupt.
Loop: SJMP Loop ; Loop and do nothing.
Example 11.7:
Construct a digital clock, using an 8051 system employing a 12 MHz clock. Use
timer and interrupt.
Solution:
To implement the digital clock, Timer 0 is used in this example. The timer is
initialized in mode 1 and is programmed in timer mode (C/T0 = 0) in the TMOD
register. The priority for the timer 0 interrupt is made high in the IP register and
all other interrupts are set to low priority. To enable the Timer 0 interrupts, the
appropriate control word is written into the IE register.
The timer will be incremented every 1 ps. To produce clock operation, an
interrupt is needed every 1 s. So, it has been designed to give one interrupt every
(1 /60)01 of a second, i.e., every 16.67 ms. So, the Timer 0 must be made to count up
to 16,667, to give an interrupt every (l/60)lh of a second. So, the timer is initialized
to 48869 (i.e., 65536 - 16667) in decimal form or BEE5 in hexadecimal form.
HARDWARE FEATURES OF 8051 353
Four registers—RO, RI, R2, and R3—are used to store time values, the values
of (l/60)lh of a second, seconds, minutes, and hours, respectively. These registers
are incremented in the interrupt service routine. If these register contents exceed
their maximum value, the registers are reset to their initial values. Care must be
taken to avoid using the registers RO to R3 in the main program.
Algorithm:
(i) Program the timer to produce an interrupt every (l/60)Ih of a second.
(ii) In the ISR, increment the seconds register.
(iii) If it is greater than 59, increment the minutes register and reset the seconds
register.
(iv) If the minutes register is greater than 59, increment the hours register and
reset the minutes register.
Bit position D7 D6 D5 D4 D3 D2 DI DO
Name GATE1 C/Tl T1M1 TIMO GATEO C/TO TOMI TOMO
Data 0 0 0 0 0 0 0 1
= 01H
Table 11.11(b) TCON register bit pattern
Bit position D7 D6 D5 D4 D3 D2 DI DO
Name TF1 TRI TFO TRO IE1 IT1 IE0 ITO
Data 0 0 0 •1 0 0 0 0
= 10H
Table 11.11(c) IP register bit pattern
Bit position D7 D6 D5 D4 D3 D2 DI DO
Name EA — — PS PT1 PX1 PTO PXO
Data 0 0 0 0 0 0 1 0
— 02H
Table 11.11 (d) IE register bit pattern
Bit position D7 D6 D5 D4 D3 D2 DI DO
Data 1 0 0 0 0 0 1 0
= 82H
354 MICROPROCESSORS AND MICROCONTROLLERS
Main program:
MOV TMOD, #01H Initialize the TMOD register.
MOV THO, #BEH
MOV TLO, #E5H Load the count for (l/60)th of a second in the THO
and TLO registers.
MOV RO, #00H Initialize the (l/60)th of a second register.
MOV RI, #00H Initialize the seconds register.
MOV R2, #00H Initialize the minutes register.
MOV R3, #00H Initialize the hours register.
MOV IP, #02H Initialize the Interrupt Priority register.
MOV IE, #82H Initialize the Interrupt Enable register.
SETB TRO Start Timer 0 and run it.
In addition to these two registers, the MSB of the PCON register (the SMOD
bit) is used to double the baud rate of serial transmission and reception. If the
SMOD bit is set to 1, the baud rate is doubled.
The individual bits of SCON have the functions shown in Table 11.13. As the
356 MICROPROCESSORS AND MICROCONTROLLERS
SCON register has many individual status bits, the individual bits of this register
are bit-addressable. The bit address is also given in Table 11.13. The programmer
can use these bit addresses to check the status of the serial port and set the mode
individually.
D7 SMO 9FH
Serial port mode select bits
D6 SMI 9EH
D5 SM2 9DH Multiprocessor communications enable bit
Receiver enable—this bit must be set, to receive
D4 REN 9CH
characters
Transmit bit 8—the 9th bit to be transmitted in modes
D3 TB8 9BH
2 and 3
D2 RB8 9AH Receive bit 8—the 9th bit received in modes 2 and 3
Transmit Interrupt flag—set when a byte has been
DI TI 99H
completely transmitted
Receive Interrupt flag—set when a byte has been
DO PT
I\1 98H
completely received
The D7 and D6 bits of the SCON register define the operating modes of the
serial port; the basic operating modes are given in Table 11.14. The SMO and SMI
bits can select any one of the four operating modes described in Section 11.6.2.
The next bit, SM2, is a flag used for enabling ‘multiprocessor communication’
in modes 2 and 3. When SM2 is set to 1 in modes 2 and 3, the ‘Receive Interrupt’
RI flag will not be activated if the 9th data bit received is 0. When SM2 is set in
mode 1, the RI flag will not be activated if a valid stop bit is not received. This
is useful in certain advanced serial applications. For now, it can assumed that the
SM2 bit has to be cleared so that the RI flag will be set when any character is
received.
The next bit, REN, is ‘Receiver Enable’. This bit is set in order to receive the
characters from the receive data line of the serial port.
The TB8 and RB8 bits are used in modes 2 and 3. In these modes, nine data
HARDWARE FEATURES OF 8051 357
bits are transmitted and received. The programmer must store the ninth bit to be
transmitted in TB8; RB8 holds the ninth bit received.
TI stands for ‘Transmit Interrupt’. When a program writes a data to the serial
port buffer SBUF, the serial port will start shifting this data in the serial transmit
line bit by bit at the predefined clock speed or baud rate. The 8051 will give the TI
signal to the programmer, after sending the data completely. Upon sensing that the
TI bit is set to 1, the programmer can write the next data for transmission. When
the TI bit is set, the programmer may assume that the serial port is free and ready
to transmit the next byte.
The RI bit stands for ‘Receive Interrupt’. Whenever a data is received on the
receive data line of the serial port, it is shifted to a buffer and then stored in the
SBUF register. Setting of the RI bit indicates that a byte has been received. Upon
sensing that the RI bit is set to 1, the programmer may read the data from the
SBUF.
TXD RXD
As mentioned earlier, SBUF is physically two registers with the same address.
When data to be transmitted is written into the SBUF register, it will be shifted bit
by bit into the TXD line of the 8051. The port 3 pin 3.1 acts as the TXD line. The
shifting is done by the transmit clock, which determines the baud rate.
Similarly, when data bits are received on the RXD line (pin 3.0 of port 3), the
bits are shifted serially into the shift register in synchronization with the receiver
clock. After the reception is complete, the data received will be placed on SBUF,
from where it can be read by the programmer through the internal bus.
358 MICROPROCESSORS AND MICROCONTROLLERS
Mode 0: In this mode, serial data enters and exits through the RXD pin. So, in
mode 0, full-duplex is not possible, i.e., both transmission and reception cannot
take place simultaneously. The TXD pin is used to output the shift clock in this
mode. Eight bits are transmitted/received (LSB first). The baud rate is fixed at
(l/12)th the oscillator frequency. Transmission is started by writing a data byte
into the SBUF register and once the transmission is complete, the TI flag is set.
Figure 11.18 shows the transmission of data on the RXD line.
TI_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Similarly, reception is started by enabling REN in the SCON register. Once the
data reception is complete, the RI flag is set as shown in Fig. 11.19. The baud rate
in mode 0 is fixed at (l/12)th the clock frequency.
Mode 1: In this mode, 10 bits are transmitted through TXD and simultaneously,
10 bits can be received through RXD. The 10 bits include a start bit (0), eight data
bits (LSB first), and a stop bit (1). On completion of reception, the stop bit goes
into RB8 in the SFR SCON. The baud rate is variable and is set by the Timer 1
overflow rate. The baud rates for mode 1 are fixed as follows:
Baud rate = (Timer 1 overflow rate/16) if SMOD bit in PCON SFR is set to 1.
Baud rate = (Timer 1 overflow rate/32) if SMOD bit in PCON SFR is set to 0.
Note that PCON is a SFR (described in Section 9.6). The MSB of the PCON
register can be set or reset by the programmer. The baud rate can be doubled by
setting the MSB of PCON. To generate the baud rate clock from Timer 1, Timer 1
can be configured in auto reload mode with the Timer 1 interrupt disabled.
As in mode 0, transmission is initiated by writing a data into the SBUF register.
Reception is enabled when REN of the SCON SFR is 1; data reception is initiated
by a 1 -to-0 transition on the RXD line.
Mode 2: In this mode, 11 bits are transmitted through TXD or received through
RXD. The 11 bits include one start bit (always 0), eight data bits (LSB first), a
programmable 9th data bit, and a stop bit (always 1). The ninth data bit transmitted
is the same as the TB8 bit in the SCON SFR. It can be assigned the values 0 or
1 by the programmer. For example, the parity bit (P in the PSW) could be moved
into TB8 for transmission as the ninth bit. On reception, the ninth data bit will go
HARDWARE FEATURES OF 8051 359
into RB8 in the SCON SFR, while the stop bit will be ignored. The baud rate is
programmable to either 1/32 or 1/64 of the oscillator clock frequency.
Baud rate = (Clock frequency/32) if SMOD bit in PCON SFR is set to 1.
Baud rate = (Clock frequency/64) if SMOD bit in PCON SFR is set to 0.
Mode 3: In this mode, 11 bits are transmitted through TXD and simultaneously,
11 bits are received through RXD. The 11 bits include a start bit (0), eight 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.
The baud rates for mode 3 are fixed as follows, similar to mode 1:
Baud rate = (Timer 1 overflow rate/16) if SMOD bit in PCON SFR is set to 1.
Baud rate = (Timer 1 overflow rate/32) if SMOD bit in PCON SFR is set to 0.
In general, serial transmission in the 8051 starts immediately after a data is
written into the SBUF register, after initializing the serial port. Similarly, serial
data reception is possible if the REN bit of the SCON register is set. In addition,
for mode 0, the RI bit must be 0 for data reception.
of 11.059 MHz. Similarly, the baud rate for mode 2 is fixed at (clock frequency)/64.
This results in a baud rate of 1,72,797 for a system frequency of 11.059 MHz.
The baud rate for the modes 1 and 3 is fixed by the Timer 1 overflow frequency.
Timer 1 can be programmed to overflow at regular intervals in the 8-bit auto reload
mode. The reload value loaded into the TH1 register will decide the baud rate.
The relation between the baud rate and the TH1 timer reload value is given in the
following equations.
TH1 = 256 - ((Clock frequency/384)/Baud) if SMOD in PCON SFR is 0.
TH1 = 256 - ((Clock frequency/192)/Baud) if SMOD in PCON SFR is 1.
Table 11.15 gives the commonly used baud rates and the corresponding reload
value for the timer in mode 2, assuming that the clock frequency is 11.059 MHz
and that SMOD is reset.
The following set of instructions will set the timer for a baud rate of 9600.
MOV TMOD, #001000006 ; Set Timer/Counter 1 set in mode 2
(8-bit timer operation)
M0V.TH1, #OFDH ; Time the Timer/Counter 1 for 9600 baud.
SETB TRI ; Enable the Timer/Counter 1 for free run.
For initializing the serial port for mode 3 operation, the following instruction can
be used:
MOV SCON, #110100006
11.6.3. 2 Transmitting and Receiving Data using Serial Port
Once the serial port is properly configured, it is ready to send and receive data.
To write a byte to the serial port, one must write the value to be transmitted in
the SBUF (99H) SFR. For example, to send the letter ‘A’ to the serial port, the
following instruction can be written:
MOV SBUF, #‘A’
Upon execution of this instruction, the 8051 will begin transmitting the
character through the serial port. Once the transmission is complete, the serial port
transmit interrupt flag TI is set. Since the 8051 does not have a serial output buffer,
a character cannot be written to SBUF before the previously written character is
completely transmitted. The completion of data transmission can be detected by
checking the TI flag. This flag is set after data is written into the SBUF register.
Reading data received by the serial port is also easy. To read a byte from the
serial port one must read the value stored in the SBUF (99H) SFR, after the 8051
has automatically set the RI flag in the SCON register.
HARDWARE FEATURES OF 8051 361
Example 11.8:
Write a program to transmit the ASCII character ‘A’ continuously using the 8051
serial port. Use 9-bit data at 9600 baud. Use polled operation.
Solution:
MAIN: ; Set up Timer/Counter 1 to drive baud rate
of 9600.
MOV TMOD. #001000006 : Set Timer/Counter 1 in mode 2 (8-bit timer).
MOV TH1, #OFDH : Time the Timer/Counter 1 for 9600 baud.
SETB TRI : Enable Timer/Counter 1 for free run.
MOV SCON, #110100006 ; Initialize serial port for mode 3 operation.
SEND: MOV SBUF, #41H ; Move the ASCII character ‘A’ to SBUF.
LOOP: JNB TI, LOOP : Test TI flag to check whether data has
been sent.
CLR TI : Clear TI.
LJMP SEND ; Loop back and send ‘A’ again.
END
Example 11.9:
Write a program to receive a character from the serial port and save this character
in R7. Use 9-bit data at 9600 baud. The parity bit can be ignored. Use polled
operation.
I
Solution:
MAIN: : Set up Timer/Counter 1 to drive baud rate
of 9600.
MOV TMOD, #001000006 ; Set Timer/Counter 1 in mode 2 (8-bit timer).
MOV TH1, #OFDH ; Time Timer/Counter 1 for 9600 baud.
SETB TRI : Enable Timer/Counter 1 for free run.
MOV SCON, #110100006 ; Initialize serial port for mode 3 operation.
LOOP: JNB RI, LOOP ; Test RI to check whether data has been
recei ved.
CLR RI ; Clear RI.
MOV R7, SBUF ; Move data from SBUF to R7.
END
Example 11.10:
Write a program to receive a character from the serial port using interrupt driven
method and save this character in R7. Use 9-bit data at 9600 baud. The parity bit
can be ignored.
Solution:
When a character is received from the serial port, the ISR saves the received
362 MICROPROCESSORS AND MICROCONTROLLERS
character in R7. The initialization of the ISR vector address is done in the following
manner:
ORG 23H ; Vect r address of serial port interrupt
LJMP ISR_SERIAL
The main program and tl e ISR follows:
MAIN: ; Set up Timer/Counter 1 to drive baud rate
of 9600 baud rate.
MOV TMOD, #00100000B : Set Timer/Counter 1 in mode 2 (8-bit timer).
MOV TH1, #OFDH ; Time Timer/Counter 1 for 9600 baud.
SETB TRI : Enable Timer/Counter 1 for free run.
MOV SCON, #110100006 ; Initialize serial port for mode 3 operation.
MOV IE, #10010000B ; Enable the serial port interrupt.
LOOP: LJMP LOOP ; Loop and do nothing.
; ISR—SERIAL
ISR_SERI AL: ; TI or RI will cause a serial port interrupt.
This routine, upon setting RI, reads the
received character and saves it in R7.
JNB RI, RETURN ; Return if RI is not set (TI caused the
i nterrupt).
MOV R7, SBUF ; Move data from SBUF to R7.
CLR RI : Clear RI.
RETURN:
RETI ; Return from interrupt.
END
Example 11.11:
Write a program to transmit the block of data stored from the internal memory
address 40H onwards continuously, using the 8051 serial port. Use 8-bit data at
2400 baud. Use polled operation.
Solution:
MAIN: Set up Timer 1 to drive baud rate of 2400.
MOV TMOD, #001000006 Set Timer 1 in mode 2 (8-bit timer).
MOV TH1, #OFAH Time Timer 1 for 2400 baud.
SETB TRI Enable Timer 1 for free run.
MOV SCON, #010000006 Initialize serial port for mode 1 operation.
MOV RO, #40H Initialize memory pointer.
MOV RI, //COUNT Initialize a counter for the number of data
in the block.
SEND: MOV SBUF,@RO Get the data from memory and send it to SBUF
for transmission.
LOOP: JNB TI, LOOP Test TI flag to check whether data has been
sent.
CLR TI Clear TI.
INC RO Point to the next data.
DJNZ RI, SENO Loop again to send data, if not completed.
END
HARDWARE FEATURES OF 8051 363
POINTS TO REMEMBER
• The 8051 microcontroller has four parallel ports of eight bits each. These ports have
alternative functions, in addition to parallel I/O.
• The ports read differently for reading from pin and for reading from port registers for
certain specific instructions.
• The ports are used for external memory interfacing.
• The 8051 has two timers of 16 bits each, both of which can be operated in four different
modes.
• The timers can be accessed using polled method or interrupt driven method.
• The 8051 has five different sources of interrupts and can be masked, prioritized, and
vectored using the related SFRs.
• The serial port of the 8051 is duplex and can be programmed using the internal SFRs
to operate in different modes. The baud rate clock can be either supplied externally or
can be generated using timers. The serial data transfer can be either polled or interrupt
driven.
KEY TERMS
Baud rate It is the rate or frequency at which the serial data is shifted onto the transmission
lines
EA It is external memory access signal, an input signal to 8051.
IE SFR This interrupt enable register in the 8051 SFR area, used to enable or disable a
particular interrupt
Interrupt It is a signal to a microcontroller to execute a high priority program.
Interrupt priority It is the sequence in which the interrupts will be serviced when all the
interrupt signals appear at the same time.
Interrupt vector address It is the address of the memory location from where the
program will be executed upon sensing a particular interrupt.
IP SFR This interrupt priority register is in the 8051, used to set two levels of priority,
high and low, among the available interrupt sources.
Operating modes of serial port These are modes that decide the number of bits
transmitted or received and the baud rate used.
PSEN It is the program memory Read strobe signal given by 8051.
SBUF It is the serial port register from where the data is transmitted; register in which
received data is stored.
SCON It is the serial port control register, used to select the modes and control the serial
port.
Structure of port This refers to internal circuit organization and connection of various
signals to the ports
TCON This SFR is used by programmers to control the running of the timer and to read
the timer overflow status.
364 MICROPROCESSORS AND MICROCONTROLLERS
Timer It is a register which acts as a counter and is incremented for every clock pulse
Timer operating modes These are the different methods available for programming and
using the timers.
Timer overflow It is the condition in which the timer content becomes all 1 s and becomes
all Os upon next count. This overflow will set the timer overflow flag in the TCON SFR.
TMOD This SFR is used to set the modes of operation of the 8051 timers.
UART It is the universal asynchronous receiver transmitter, the serial data reception and
transmission mechanism in microprocessor technology.
REVIEW QUESTIONS
NUMERICAL/DESIGN-BASED EXERCISE
Show the circuit connections for interfacing 16 K of EPROM IC 27158 and 8 K of RAM
IC 6264 with the 8051.
1. What are the conditions for external memory access in the 8051?
2. What is the purpose of multiplexing the lower-order address bus with the data bus for
external memory access?
3. What are the advantages of separate data and program memory (Harvard architecture)?
4. Write a delay routine for 1ms using Timer 0 of the 8051, for 12 MHz crystal
frequency.
5. Write a routine using a timer of the 8051 to count the cars moving on a road and to
give a signal when the count value reaches 100.
6. Write the interrupt priority word so that the following settings will be implemented in
the 8051: The serial port interrupts and external interrupt 1 are high-priority interrupts;
the other interrupts are low-priority ones.
7. Write the control word for masking external interrupts in an 8051-based system.
8. Write the control word format for setting the serial port in mode 1.
9. Calculate the reload value of Timer 1 for achieving a baud rate of 4800 in the 8051,
for a crystal frequency of 11.0592 MHz.
10. Write an 8051 ALP to transmit ‘Hello World’ serially at 9600 baud for a crystal
frequency of 11.0592 MHz.
CHAPTER 12
Example 12.1:
Assume that the 8255 is interfaced to the 8051 at the addresses 8000H-8003H.
Write a program to read the content of port A and write it in other ports.
Solution:
The first step in programming is to finalize the control word to be written in
the control register of the 8255. Here, port A is initialized as the input port in
8051 INTERFACE EXAMPLES 367
Port A
PortC
mode 0. Ports B and C are initialized as output ports in mode 0. For this initialization,
the control word is 10010000B or 90H (refer to Chapter 7). The addresses for the
port access are stored in the data pointer register of the 8051, in the following
program.
MOV A, #90H ; Initialize the control word.
MOV DPTR, #8003H ; Initialize the data pointer to the control
regi ster.
MOVX @DPTR, A ; Write the control word in the control
regi ster.
MOV DPTR, #8000H ; Load the address of port A in the data
pointer.
MOVX A, @DPTR ; Move data from port A to the accumulator.
INC DPTR ; Increment the data pointer so that it
points to port B.
MOVX @DPTR, A ; Write the data in port B.
INC DPTR ; Increment the data pointer so that it points
to port C.
MOVX @DPTR, A ; Write the data in port C.
Fig.12.4 Interfacing four switches and four LEDs with the 8051
8051 INTERFACE EXAMPLES 369
and sent as output to the port 1 lines, the LEDs are not illuminated since the circuit
is reverse biased.
Fig. 12.5 Arrangement of LEDs and digit display format in seven-segment displays
display the decimal point. In addition to these eight pins, seven-segment displays
have one more pin, for power supply. Seven-segment displays come in two types,
common anode and common cathode.
In common anode display, the anodes of all the LEDs are connected together
to form a pin. To illuminate a segment, the common anode pin is connected to the
supply and the corresponding segment input is connected to a low-level voltage,
i.e., logic 0.
On the other hand, in common cathode display, the cathodes of all the LEDs
are connected together. So to illuminate a segment, the corresponding segment
input is connected to a high-level voltage, i.e., logic 1, and the common cathode is
connected to the ground. This forward biases the LEDs and illuminates them.
The interfacing of seven-segment displays with the 8051 microcontroller needs
a driver, as shown in Fig. 12.3. Here, the 74240 TTL inverter driver IC is used for
driving the seven-segment displays. The seven-segment display is assumed to be
of common anode type. The common anode is connected to the +5 V supply. The
interfacing diagram for displaying the BCD code read from the key connected
to P3 and displaying it in seven-segment display connected to port 1 is shown in
Fig. 12.6.
A segment can be illuminated when its pin is connected to a low-level voltage
or to ground. This is done by connecting the segment input to logic 0, through
the 74240. The 74240 IC inverts the inputs applied to it. This implies that if a
particular segment has to be illuminated, the corresponding output for that segment
has to be at logic 1. The data output for displaying decimal digits 0-9 is shown in
Table 12.1.
370 MICROPROCESSORS AND MICROCONTROLLERS
Table 12.1 Data format for displaying decimal numbers in seven-segment displays
0 0 0 1 1 1 1 1 1 3F
1 0 0 1 1 0 0 0 0 30
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
9 0 1 1 0 1 1 1 1 6F
The program for getting data from port 3 and displaying it in port 1 is as
follows. In this program, the look-up table concept is used to select the display
code corresponding to each digit. The data in Table 12.1 is stored in a series of
memory locations starting at ‘TABLE’. Indexed addressing mode is used to access
this table.
MOV A, P3 Get data from port 3.
ANL A, OFH Mask the higher-order four bits.
GETCOOE: MOV DPTR. //TABLE Initialize the pointer to the table.
MOVC A, @A + DPTR Get the display code from the table.
8051 INTERFACE EXAMPLES 371
The software part follows the flowchart shown in Fig- 12.8. The channel
selection signal is issued to the ADC chip, followed by the ALE/SC signal. Then,
the conversion starts within the chip and the EOC signal is received at the end of
conversion. When the EOC signal is received, the program issues the OE signal
and then reads the data from the data lines connected to port 1.
The program for analog-to-digital conversion for the interface shown in
Fig. 12.7 is as follows:
ADCONVERT: MOV P1,#OFFH ; Make port 1 as the input port by writing
Is
MOV P0,#CH_NUM ; Select the channel number on port 0 LSB
3 1i nes.
SETB P0.3 Issue ALE/SC signal on P0.3 line.
NOP Wai t.
NOP Wait.
CLR P0.3 Remove the ALE/SC signal by making it 0.
CHECK: JNB P0.5, CHECK Read P0.5 to check EOC until it becomes 1
SETB P0.4 Make 0E signal high on P0.4.
MOV A,Pl Read the result data from port 1.
MOV ADC_RES,A Store it in a memory location ADC_res.
RET End of subroutine program.
lines should now be made output lines, to send digital output to the DAC. The
output from the DAC chip is a variable current and this is converted into a variable
voltage using a current-to-voltage converter.
The following section explains the programming for applications involving the
DAC. Four common applications—square wave generation, ramp wave generation,
staircase wave generation, and sine wave generation—are discussed.
The following program generates the ramp waveform shown in Fig. 12.11, with
V = 5 V. The delay calculation is slightly different from the previous examples.
Here, the voltage levels are increased from OOH to FFH, i.e., 0 to 255 in decimal
form. So within T seconds, there are 255 levels. Therefore, the delay for each
level will be T/255. The delay routine is then written to produce this delay of
T/255 seconds. The delay time should be as small as possible for ramp generation.
Otherwise, the waveform will look like a staircase waveform with 255 levels.
376 MICROPROCESSORS AND MICROCONTROLLERS
The following program involves incrementing the value given to port A from
OOH to FFH, with a time delay routine called at each level.
START: MOV R1,#OOH : Move the hexadecimal equivalent of
OV to register 1.
LOOP: MOV Pl, RI : Move the data in the register RI to
port 1, where the DAC is connected.
LCALL DELAY ; Call the subroutine DELAY.
INC RI : Increment register RI.
SJMP LOOP : Loop again to send the data to
the DAC.
DELAY: MOV RO, #COUNT ; Load a register with a count value.
RPT: DJNZ RO, RPT : Decrement it and loop.
RET ; If the value of count becomes zero,
return from subroutine.
For sine wave generation, the sine wave is first converted into the corresponding
digital values at the sample intervals. To generate a sine wave, it is assumed that
the DAC gives an output voltage of 0V-10V. This voltage range can be easily
adjusted by changing the gain of the I-to-V converter at the output of the DAC
network. So the equation (5 + 5 x sin 0) is used to calculate the voltage required
at the output of the DAC network. This voltage waveform is shown in Fig. 12.12.
To find the digital values to be given at the input of the DAC network, we should
multiply the voltage value by 25.6. This is because for an 8-bit DAC, there are
256 levels with a full-scale output voltage of 10 V and so the per step value is
256/10. For reducing the number of levels, the sine wave is generated with values
for every 10° step. The decimal values to be given to the DAC network are given
in the program as the look-up table starting at 9000H. To get a more accurate
waveform, the table can be constructed with 1° steps.
In the program given, the delay corresponds to 10° steps in the waveform,
as the sine values are stored in the look-up table for every 10°. For example, to
generate a 50 Hz sine wave, the delay for 10° should be (1/(50 x 36)), i.e., 0.55 ms.
If the count in the delay routine corresponds to 0.55 ms, the waveform generated
will be at 50 Hz.
instead of producing a single clean pulse output, the switches generate a series
of pulses because the switch contacts do not come to rest immediately. As the
microprocessor is faster than a manual key press, the single key press will be
registered as multiple key presses. This is the main consequence of key bouncing.
The signal from the key falls and rises a few times within a period of about 5 ms,
as the contact bounces. So the signal from the key must be made free from key
bouncing transients. This technique is called key de-bouncing.
Key de-bouncing can be accomplished using hardware or software. The
bouncing of key signals occurs within 5 ms. A human cannot press and release a
switch in less than 20 ms. A de-bouncing logic will check the signal after 20 ms
and then recognize whether a key is pressed or not. This logic can be implemented
both in hardware and in software. The hardware techniques employ set-reset
flip-flops, non-inverting CMOS gates, or integrating de-bouncers. The software
technique uses the wait-and-see method. When a signal is sensed from a switch,
the program waits for 10 ms and checks the same key again. If the signal from the
switch still indicates the key press, the program decides that the user has pressed
the key. Otherwise, the signal received is rejected as noise. Figure 12.13 shows the
matrix keyboard interfaced with the 8051.
This software can also be modified and written using the ‘SETB bit’ and the
‘CLR bit’ instructions. A set of 16 flag bits in the bit-addressable internal RAM
region can be used to store bit information about which key was pressed, and this
can be used by the main software routine.
0 0 0 1 1 4 01
0 0 1 0 2 3 02
(Contd)
8051 INTERFACE EXAMPLES 381
0 1 0 0 3 2 04
1 0 0 0 4 1 08
of the motor is connected to the supply. The excitation sequence for the stepper
motor is given in Tables 12.2 and 12.3. The single-phase excitation results in low
current through the motor windings and is also called wave drive mode. In two-
phase excitation, the excitation current through the motor windings is high and so
it is called high-torque excitation mode.
The following program is used for continuous rotation of the stepper motor.
The value of count in the delay routine must be calculated to match the required
speed. A counter of 4 is set up to indicate that the switching sequence needs four
steps, which are to be repeated continuously. A memory pointer is then initialized
to load the switching or excitation data to be given to the port. The switching data
are stored initially in the memory locations.
START: MOV 30. #03H : Store the excitation values for bi-phase
clockwise rotation from the internal memory
location 30H.
MOV 31, #06H
MOV 32, #OCH
MOV 33, #09H
RPT: MOV RO, #30H : Initialize the memory pointer with the address
for excitation.
MOV RI, #04H : Initialize the counter for four steps of
excitation data.
LOOP: MOV PO,@RO : Get the excitation data and send it to
port 0.
LCALL DELAY : Call the subroutine DELAY for proper
excitation of the motor coils.
INC RO ; Point to the next memory location.
DJNZ RI, LOOP : Decrement the counter. If it is not zero, get
the next data.
SJMP RPT : Loop again for continuous rotation.
DELAY: MOV R3, #count : Load register R3 with the count value.
HERE: DJNZ R3, HERE : Decrement and loop until it becomes zero.
RET : Return from subroutine.
To rotate the stepper motor in the reverse direction, the register RO can be
initialized with 33H and decremented. The stepper motor connections are such
that either A or B is excited and then, either C or D is excited. The connections are
also such that the port output can be rotated bit by bit for the rotation of the motor.
The following program rotates data, so as to excite the motor in sequence. Here,
the accumulator is initialized with 00010001B. The left-shifting of this data will
excite the required motor phases one by one. The most significant four bits are set
as 0001, so that the continuous rotation of this data will result in the continuous
rotation of the motor.
START: MOV A, #00010001B ; Initialize the accumulator with 1 in
the bit PO.O.
8051 INTERFACE EXAMPLES 383
Example 12.2:
Write a program to rotate the stepper motor in Fig. 12.14—90° clockwise and 180°
anti-clockwise and then continuously repeat the same sequence.
Solution:
The stepper motor used is assumed to have a step angle of 1.8°. So for 90°
rotation, the number of excitation pulses required is 90/1.8, i.e., 50 in decimal
form and 32 in hexadecimal form. For 180° rotation, the number of excitation steps
required is 100 or 64H. If the single phase excitation data 0001000IB is rotated
left for clockwise motor rotation, the data is rotated right for anti-clockwise motor
rotation. The program is as follows:
START: MOV RO, #32H : Initialize the counter for 90s rotation.
MOV A, #00010016 : Initialize the accumulator with 1 in the
bit P0.0.
L00P1: MOV PO, A ; Get the excitation data and send it to port 0.
RL A ; Rotate the accumulator content left, to rotate
the motor clockwise.
LCALL DELAY ; Call DELAY for proper excitation of the
motor coils.
DJNZ RO, L00P1 ; Check for 90s rotation. If not, loop.
MOV RO, #64H ; Initialize the counter for 180Q rotation.
MOV A, #00010016 ; Initialize the accumulator with 1 in the
bit P0.0.
L00P2: MOV PO, A ; Get the excitation data and send it to port 0.
RR A ; Rotate for excitation in anti-clockwise
di rection.
LCALL DELAY : Call DELAY for proper excitation of the
motor coils.
DJNZ RO, L00P2 ; Check for 180e rotation. If not, loop.
SJMP START ; Loop for continuous operation.
DELAY: MOV R3, #count ; Load the value of count in R3.
HERE: DJNZ R3, HERE ; Decrement until the value becomes 0.
RET : Return from subroutine.
384 MICROPROCESSORS AND MICROCONTROLLERS
Eight data lines are used for interfacing the LCD with the processor. The three
control signals are RS, R/W, and E.
(i) RS is used to select a control register or a data register.
(ii) R/W indicates the direction of data flow between the processor and the
display.
(iii) The E signal is used to enable data transfer.
The three control signals and eight data lines are interfaced with the two ports
of the 8051. In the interface diagram shown in Fig. 12.15, the port 1 lines are used
to transfer data and the port 2 lines are used to issue the control signals from the
microcontroller to the LCD.
The LCD interface accepts a number of commands/instructions. A select list of
these commands is given in Table 12.5.
8051 INTERFACE EXAMPLES 385
Vcc
Code
Instruction Description
RS R/WDB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Code
Instruction Description
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Entry 0 0 0 0 0 0 0 1 1/ S Sets the direction of
mode set D movement of the cursor (1/
D) and specifies whether to
shift the display (S). These
operations are performed
during data read/write.
I/D: 1—increment;
0—decrement.
Display 0 0 0 0 0 0 1 D C B Sets displays on/off (D),
on/off cursor on/off (C), and
control blinking of character at
cursor position (B).
Cursor/ 0 0 0 0 0 1 s/ R/ * * Sets move cursor—0 or shift
display c L display—1(S/C),
shift shift direction left—0;
right—1 (R/L).
DDRAM contents remain
unchanged.
Function 0 0 0 0 1 DL N F * * Sets interface data length (DL
set = 1 for 8-bit data and 0 for
4-bit data),
number of display lines
(N = 0 for one-line and 1 for
two-line display), and
character font (F = 1 for 5 x
10 dots and 0 for 5 x 7 dots
font).
Set 0 0 0 1 CGRAM address Sets the CGRAM address.
CGRAM CGRAM data is sent and
address received after this setting.
Set 0 0 1 DDRAM address Sets the DDRAM address.
DDRAM DDRAM data is sent and
address received after this setting.
Read busy 0 1 BF CGRAM/DDRAM address Reads busy flag (BF), which
flag and indicates whether an internal
address operation is being performed
counter and reads CGRAM or
DDRAM address counter
contents (depending on
previous instruction).
(Contd)
8051 INTERFACE EXAMPLES 387
Code
Instruction Description
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Write to 1 0 Write data Writes data to CGRAM or
CGRAM DDRAM.
or
DDRAM
Read from 1 1 Read data Reads data from CGRAM or
CGRAM DDRAM.
or
DDRAM
The programming of the LCD is done with the appropriate initialization word
and command words. Every time a control or command word is written into the
LCD, the RS signal input must be made 0; R/W must be made 0 for the write
operation. After setting these two signals, the enable signal E must be applied as a
pulse for a duration of not less than 450 ns. Similarly, when data is written into the
LCD device, the RS signal must be made 1 and R/W must be made 0. To display
characters and numbers, the corresponding ASCII code is sent to the data registers
of the LCD display. The following program is used to display the 15-character
data available in the series of external memory locations starting at 9000H in the
LCD.
START: MOV A, #01H ; Load the control word to clear the display in
the LCD.
CALL COMMAND ; Call the subroutine to issue this command to
the LCD.
MOV A, #OEH ; Load the control word to initialize the cursor
to home position, and switch on the display and
the cursor.
CALL COMMAND ; Call the subroutine to issue this command to
the LCD.
MOV DPTR, #9000H ; Initialize the memory pointer.
MOV RO, #OFH ; Initialize the counter for the number of
characters to display.
NEXT: MOVX A, @DPTR ; Load the display data in the accumulator.
CALL DISP ; Call the subroutine to issue this data to
the LCD.
INC DPTR ; Point to the next data for display.
DJNZ RO, NEXT ; Decrement the count for the number of data to
be displayed. If it is not zero, loop again.
; Main program continues after this line.
Subroutines
COMMAND: MOV Pl,A ; Give the control word to the data lines of the LCD.
388 MICROPROCESSORS AND MICROCONTROLLERS
DISP: MOV Pl,A ; Give the data to the data lines of the LCD.
SETB P2.2 ; RS = 1 (data)
CLR P2.1 : R/W = 0 (write)
SETB P2.0 ; E = 1 (apply a high pulse)
CLR P2.0 ; E = 0 (apply a low to E, so that there will
be a H-to-L pulse)
CALL DELAY ; Wait for predefined time delay
RET ; Return from subroutine.
DELAY: MOV RI, tfCOUNTl ; Load a register with a count value, COUNT1.
LOOP1: MOV R2, #COUNT2 ; Load another register with a count value, COUNT2,
for more delay.
LOOP2: DJNZ R2, L00P2 ; Decrement it.
DJNZ RI, LOOP1 ; Decrement until the required time
delay is obtained.
RET ; When the required time delay has been produced,
return from subroutine.
This program calls a delay subroutine to allow the LCD to take the command
or data into its registers. The delay is written with two registers. Instead of the
delay routine, the busy flag of the LCD can be checked to see whether the data or
command has been written into the LCD registers. The busy flag is available in the
D7 data line of the LCD. The following subroutine, READY, can be used instead
of the DELAY subroutine. This subroutine checks the busy flag from the LCD and
returns from the subroutine when the busy signal is removed by the LCD.
READY: SETB Pl.7 ; Pl.7 is made an input port by writing a 1 to it.
CLR P2.2 ; RS = 0 (command)
SETB P2.1 ; R/W = 1 (read command register)
RPT: SETB P2.0 ; E = 1
CLR P2.0 ; E = 0 (H-to-L pulse applied on the E signal)
JB Pl.7, RPT ; Check the busy flag and loop until it is 1.
RET ; Return from subroutine.
5V
This circuit will work only for a 5 V DC motor. When the output on the port pin
is logic 1, i.e., 5 V, the PNP transistor is off. This means that the current through
the motor is made zero and hence it is off. If the port pin output is at logic 0, the
PNP transistor is on. So the transistor allows the current through the motor and the
motor generates torque.
If the motor supply voltage is higher, the circuit shown in Fig. 12.17 is used.
This circuit uses two transistors—one PNP and the other, NPN. In the circuit, a
12 V DC motor is interfaced with a microcontroller pin.
5V 19V
When the port pin is set at 5 V, the PNP transistor is off. This means that the NPN
transistor is also off and there is no path for current through the motor. Therefore,
the motor is off. When the port pin is cleared, the PNP transistor is on. This turns
on the NPN transistor, which allows current to flow through the motor to the
ground; thus the motor is on. The value of R2 needs to be chosen such that it is
neither too high nor too low. A high value of R2 will make the current in the base
of the NPN transistor so low that it will not be enough to turn on the transistor.
A very low value of R2 will result in too much current through the motor.
390 MICROPROCESSORS AND MICROCONTROLLERS
Table 12.7 Bidirectional motor control modes using two microcontroller pins
7448 BCD to
Seven-segment
decoder
a be d ef g a b c d e f g a b c d e f g a b c d e f g
f
7448 7448 7448 7448
D C B A D C B A D C B A D C B A
+5V
LP1.0 Vcc
L- P1.1 PO.O
P1.2 P0.1
+5V P1.3 P0.2
P1.4 P0.3
P1.5 P0.4 I
10nF=±= P1.6 P0.5
P1.7 P0.6
RST _P0.7
,2K (RXD) P3.0 EAA/pp
(TXD) P3.1 ALE/PROG Vcc
(INTO) P3.2 +5V
PSEN
(INT1) P3.3 P2.7
(TO) P3.4 P2.6 Switch
(T1JP3.5 P2.5
30 pF (WR) P3.6 P2.4
(RD) P3.7 P2.3
XTAL2 P2.2
XTAL1 P2.1
I
30 pF GND P2.0 i
in the four displays, we have to connect the pins of two ports to the display code
converter. Here, the port pins of PO and Pl are used. The BCD data of the display
is given to the port pins.
For stopwatch operation, an additional switch is needed to switch on and off
counting in the stopwatch. A separate push-to-on switch is connected to the LSB
of port 2. The voltage on this pin can be sensed to control the counting and display
in the seven-segment displays.
The program for stopwatch operation involves a timer register, which is
incremented at regular intervals. The interval can be programmed to be either
(l/10)th of a second or 1 second. With the interval of (l/10),h of a second and a
4-digit display, the hardware arrangement can be used to count up to 999.9
seconds. With an interval of 1 second, the hardware arrangement can count up
to 9,999 seconds. The timer register can be programmed to generate an interrupt
after every interval.
The flowchart for the stopwatch example is given in Fig. 12.20. The program
involves first initializing the timer to the required time interval. The display is also
8051 INTERFACE EXAMPLES 393
initialized to display all zeros. Then the status of the switch is sensed from the
LSB of port 2. If the switch is pressed, the program moves to the next step, i.e.,
starting the timer and giving data output to the displays. Otherwise, the program
keeps looping in the status check on P2.0. After every timer overflow, the data for
display is incremented and converted into BCD form. New data is given to the
port pins by writing them to the port registers. Data in the display is incremented
as long as the switch on P2.0 is pressed.
The hardware, flowchart, and program can be improved further to include
additional features such as resetting the display, using separate switches to start and
stop the stopwatch, and using a press switch to switch on and off the stopwatch.
Figures 12.22 (a)-12.22 (d) shows the traffic signals that are on. For example, in
Fig. 12.22 (a), the green light (WG) in the west direction is on and the red light is
on in all the other three directions. All other lights are off and they are not shown in
Fig. 12.22 (a).
Traffic light control using a microcontroller can be done easily with parallel ports.
The port pins can be connected to each light, LED, or group of LEDs through
a proper driver circuit. The data in the parallel ports can be changed using the
program, for turning on and off the lights. Figure 12.23 shows the connection
diagram for all the lights, which are assumed to be LEDs in this example. The port
pins of port 0 and port 1 are used. The least significant three bits of port 0 are used
for the west direction. The port 0 pins 3, 4, and 5 are used for the north direction.
Similarly, the least significant three bits of port 1 are used for the lights in the east
direction and pins 3, 4, and 5 are used for the south direction.
Table 12.8 shows the data to be given to the port pins for the sequences shown
in Fig. 12.22. The other pins such as XTAL1, XTAL2, and RST in Fig. 12.23 can
be connected as shown in Fig. 12.19. The reset pins and clock inputs are connected
to the corresponding circuit.
Table 12.8 Data for port pins for traffic light control
SG SY SR EG EY ER NG NY NR WG WY
PortO Port 1 WR
Sequence
P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 P0.5 P0.4 P0.3 P0.2 P0.1 PO.O Data Data
0 0 1 0 0 1 0 0 1 1 0 0 OCH 09H
Sequence 1
0 0 1 0 0 1 0 1 0 1 0 0 14H 09H
0 0 1 0 0 1 1 0 0 0 0 1 21H 09H
Sequence 2
0 0 1 0 1 0 1 0 0 0 0 1 21H OAH
0 0 1 1 0 0 0 0 1 0 0 1 09H OCH
Sequence 3
0 1 0 1 0 0 0 0 1 0 0 1 09H 14H
1 0 0 0 0 1 0 0 1 0 0 1 09H 21H
Sequence 4
1 0 0 0 0 1 0 0 1 0 1 0 OAH 21H
in different directions. This program is written using the look-up table concept. So
the same program can be used if LEDs are connected through the 8255 PPI port
pins.
This program can be rewritten using the SETB and CLRB instructions, as all
the port pins are bit-addressable. However, the program becomes lengthy as each
LED has to be individually controlled.
where a, b, and c are physical constants depending on the system. Within the
small temperature range of 30 °C-100 °C, further linear approximation can be done
as follows:
In Rt = In Ro + Z"
or
= Ro exp —
T)
Figure 12.25 shows a simple circuit that could be used to allow a microprocessor
to measure the temperature using a thermistor. A resistor (Rj) pulls the thermistor
up to a reference voltage. This is typically the same as the ADC reference. So Vref
would be 5 V, if the ADC reference were 5 V. This thermistor used in the circuit
has a nominal resistance value of lOkQ at 25 °C, and varies from 330 kfi at -40 °C,
down to 200 Q at 150 °C, a range of 1650:1. Such a huge dynamic range in the
output resistance can make measurement difficult.
It is possible to perform a ‘good enough’ linearization by adding some very
inexpensive circuitry. One way is to incorporate the thermistor into a Wheatstone
bridge, as shown in Fig. 12.26. However, this arrangement provides an essentially
8051 INTERFACE EXAMPLES 399
fast mode and 1 O-bit addressing, thereby satisfying the demand for higher speeds
and more address space. More recently, the high speed mode has been added in the
I2C bus, with speeds of up to 3.4 Mbits/s, which ensures that the I2C bus can support
existing and future high speed serial transfer rates for different applications.
Term Description
Master The device that initiates a data transfer, generates clock signals, and
terminates a data transfer
Slave The device addressed by a master
Transmitter The device that sends data to the bus
Receiver The device that receives data from the bus
It is the responsibility of the master device to generate the clock signals on the
I2C bus. Each master generates its own clock signals when transferring data on the
I2C bus. Bus clock signals from a master can only be altered when they are stretched
by a slave that is operating slowly and holding down the clock line (i.e., keeping
the clock line in the low state) or by another master when arbitration occurs. The
timing diagram of the various conditions in the I2C bus, and the reading and writing
of data from and to the bus are explained in Sections 12.13.1.1-12.13.1.6.
12.13.1.1 Start and Stop Conditions
Any data transfer in the I2C bus must be initiated with a start (S) condition. After
the data transfer is completed, it must be terminated by a stop (P) condition.
402 MICROPROCESSORS AND MICROCONTROLLERS
Figure 12.28 shows the timing diagram for the start and stop conditions. A high-
to-low transition on the SDA line when SCL is high defines a start condition. A
low-to-high transition on the SDA line when SCL is high defines a stop condition.
The start and stop conditions are always generated by the master. The bus is
considered to be busy after the start condition is generated. The bus is considered
to be free again, a certain time after the stop condition is generated.
Fig. 12.28 Start (S) and stop (P) conditions in l2C bus
The bus stays busy if a ‘repeated start’ (Sr) is generated instead of a stop
condition. In this respect, the start (S) and repeated start (Sr) conditions are
functionally identical. Detection of start and stop conditions by the devices
connected to the I2C bus is simple if they incorporate the necessary interfacing
hardware. However, microcontrollers with no such interface have to sample the
SDA line at least twice per clock period to sense the transition.
12.13.1.2 Data Validity Condition
The data bit on the SDA line (high or low) must be stable during the high period
of the clock. The state of the data line can change only when the clock signal on
the SCL line is low, as shown in Fig. 12.29.
SDA
SCL S or
Sr UU\ ACK ACK
START or STOP or
repeated START repeated START
condition condition
(SDA)
Not-acknowledge
(SDA) Data“*ull»'
' ' receiver
Acknowledge
each byte has been received. The master can then generate either a stop condition to
abort the data transfer or a repeated start condition to start a new data transfer. If a
master that is acting as a receiver is involved in a data transfer, it must signal the end
of data transfer to the slave transmitter by generating a Not-acknowledge signal (A)
on the last byte that was clocked out of the slave. The slave transmitter must release
the SDA line to allow the master to generate a stop or repeated start condition.
12.13.1.5 Writing Data to Slave Receiver by Master Transmitter
Figure 12.32 shows the format of the FC frame when a master transmitter writes
data to a slave receiver. The shaded portion indicates the transfer of data from
404 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 12.33 Master receiver reading data from slave transmitter having sub-addresses
8051 INTERFACE EXAMPLES 405
If the master wants to read data bytes from a specific location or internal
register in the slave successively, the sequence of operations done by the master is
as follows:
(i) The master sends the start (S) condition.
(ii) The master sends the 7-bit slave address, with the read/write (R/W) bit
(which is 0) appended as the LSB. The slave sends the Acknowledge (A)
signal.
(iii) The master sends the sub-address of a particular location or register in the
slave from where it wants to read data. This is called dummy write. The
slave sends the Acknowledge (A) signal.
(iv) The master sends a repeated start condition (Sr).
(v) The master again sends the 7-bit slave address, with the read/write (R/W)
bit, which is set to 1 now, to indicate read operation.
(vi) The slave sends data bytes one by one to the master and the master sends an
Acknowledge signal (A) after each byte read. If it is the last byte to be read
from the slave then the master sends a Not-acknowledge signal (A).
(vii) The master sends a stop (P) condition to end the data transfer.
When data is being read from a slave transmitter with no sub-addresses (such
as an analog-to-digital converter) by a master receiver in the I2C bus, the following
sequence of operations has to be performed:
(i) The master sends the start (S) condition.
(ii) The master sends the 7-bit slave address, with the read/write (R/W) bit
(which is 1) appended as the LSB. The slave sends the Acknowledge (A)
signal.
(iii) The slave sends data bytes one by one to the master and the master sends
an Acknowledge signal (A) after each byte read. If it is the last byte to be
read from the slave,
the master sends a Data
Not-acknowledge
— Data transferred
1 (Read)
signal (A). bytes + Acknowledge)
(iv) The master sends a [J From master to slave □ From slave to master
RET : Return
; Sending the Not-acknowledge signal
I2C_NACK:
SETB SDA ; Set SDA to 1.
SETB SCL ; Set SCL to 1.
CLR SCL ; Set SCL to 0.
RET ; Return
; Receiving data from a slave to register A of the 8051 (master
I2C_RECEIVE:
MOV R6, #08 ; Load the number of bits in A (i.e., 8) in R6.
REP2:
CLR SCL ; Set SCL to 0.
SETB SCL ; Set SCL to 1.
MOV C, SDA ; Move the bit SDA to the carry flag.
RLC A ; Rotate A left through the carry, to receive one bit
from the carry, starting from the MSB.
DJNZ R6, REP2 ; Decrement R6 and go to REP2, if R6 is not 0 (to
receive the next bit).
CLR SCL ; Set SCL to 0.
SETB SDA ; Set SDA to 1.
RET ; Return
Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 BitO Function Range
OOH CH 10 seconds Seconds Seconds 00-59
01H 0 10 minutes Minutes Minutes 00-59
12 10 hour 1-12
02H 0 24 PM/ 10 hour Hours Hours +AM/PM
AM 00-23
03H 0 0 0 0 0 Day Day 01-07
04H 0 0 10 Date Date Date 01-31
05H 0 0 0 10 month Month Month 01-12
06H 10 year Year Year 00-99
07H OUT 0 0 SQWE 0 0 RSI RS0 Control
08H- RAM 00H-FFH
3FH 56 x 8
Note: 'O' in the table implies that the bit will be read as 0.
is written. Write transfers occur on the FC acknowledge from the DS 1307. Once
the divider chain is reset, to avoid rollover issues, the remaining time and date
registers must be written within one second.
Control Register
The DS 1307 control register is used to control the operation of the SQW/OUT
pin. The bits in the control register and the function of different bits in the control
register are shown in Fig. 12.37.
The different bits in the DS 1307 control register and their functions are as follows:
OUT—The output control bit controls the output level of the SQW/OUT pin
when the square wave output is disabled by clearing the SQWE bit. If SQWE is
0, the logic level on the SQW/OUT pin is 0 if OUT is 0, and it is 1 if OUT is 1,
as shown in Table 12.11. On initial application of power to the DS 1307, this bit is
typically set to 0.
Square wave enable (SQWE)—This bit, when set to logic 1, enables the
oscillator output to be available at the SQW/OUT pin. The frequency of the
square wave output depends upon the value of the RSO and RS 1 bits in the control
register, as shown in Table 12.11. With the square wave output set to 1 Hz, the
clock registers update on the falling edge of the square wave. On initial application
of power to the DS 1307, this bit is typically set to 0.
Table 12.11 Function of different bits in the control register
Rate select (RSl:RS0)—These bits control the frequency of the square wave
output when the square wave output has been enabled. Table 12.11 lists the square
wave frequencies that can be selected with the RS bits. On initial application of
power to the DS 1307, these bits are typically set to 1.
12.13.3.3 Data Transfer between Master and DS1307
The DS 1307 can operate in two modes—slave receiver mode (during which
data is written into the DS 1307 by a master such as a microcontroller) and slave
transmitter mode (during which data can be read from the DS 1307 by a master
such as a microcontroller). The two modes are discussed in detail in this section.
412 MICROPROCESSORS AND MICROCONTROLLERS
Slave address R/W Word address (n) Data(n) Data (n+1) Data (n+X)
A
s 1101000
2______ ts 0 A XXXXXXXX A XXXXXXXX A XXXXXXXX A XXXXXXXX P
slave address, the DS13O7 outputs an Acknowledge signal (A) on the SDA pin.
The DS 1307 then begins to transmit the data, starting from the register whose
address is given by the register pointer. If the register pointer is not written into
before the initiation of a read mode, the first address that is read is the last one
stored in the register pointer. The register pointer automatically increments after
each byte is read. The DS 1307 must receive a Not-acknowledge signal (A) to end
a read.
When the DS 1307 is operating in the slave transmitter mode (read mode), if
the master receiver wants to access the data bytes starting at a specific internal
register or RAM location in the slave, after sending the slave address and (R/W)
bit (which is set to 0), the master receives an Acknowledge signal (A) from the
slave, as shown in Fig. 12.40. The master then sends the word address (i.e., sub
address or pointer) of the specific internal register or RAM location from where
reading has to be done in the slave, and receives the Acknowledge signal (A) from
the slave. Then, after a repeated start (Sr) condition, the master again sends the
slave address and the (R/W) bit (which is set to 1 now to indicate read operation)
and receives the Acknowledge signal (A) from the slave. Then the master reads
data bytes one by one from the desired internal registers or RAM locations of the
slave and generates an Acknowledge signal for each byte received. After the last
byte is received, the master sends a Not-acknowledge signal to the slave and the
slave terminates the transfer. The master sends the stop condition. Figure 12.40
shows the complete details of this data transfer.
Figure 12.41 shows the interfacing of the 8051 with the DS 1307. Pins P0.6 and P0.7
of the 8051 act as the SDA and SCL, respectively, of the FC bus. Rpu represents
the pull-up resistance that is to be connected to the SDA and SCL pins. To get a
square wave (if needed), another pull-up resistance is connected to the SQW/OUT
pin. Using the 8051 subroutines written in an assembly language program, along
with the sequence mentioned for data read and write in the DS 1307, data can be
transferred between the 8051 and DS 1307.
414 MICROPROCESSORS AND MICROCONTROLLERS
POINTS TO REMEMBEfS
REVIEW QUESTIONS
1. Discuss in detail the interfacing of the stepper motor with the 8051.
2. What is the need for the 8255 in a microcontroller-based system?
3. Explain the interfacing of push button switches and LEDs with the 8051
microcontroller.
8051 INTERFACE EXAMPLES 415
4. Assume that an ADC and a DAC chip are interfaced with the 8051. Write a program
to read the data from the ADC and output it on the DAC line with a 1 ms delay.
5. Write a program to read the analog voltage through the ADC chip and display the
result in a two-digit seven-segment display. Draw the related interface diagram with
the 8051.
6. Describe with a schematic, the scanning of the matrix keyboard in an 8051-based
system and identifying the key pressed.
7. Assume that the speed of a stepper motor has to be controlled using an 8051
microcontroller. Design the required hardware and explain the required software.
8. Show how a low-voltage DC motor can be controlled using the 8051.
9. Explain with a neat diagram, the interfacing of an LCD with the 8051.
10. Explain traffic light control in a three-road junction using the 8051.
1. Explain the interfacing of four seven-segment LED displays with the 8051, using the
multiplexed display concept.
2. Interface an ADC chip and an intelligent LCD with the 8051. Explain the algorithm
needed to read data from the ADC and display it in the LCD.
3. Interface a DC motor and two switches with the 8051 and explain the software needed
for controlling the direction of the DC motor using the switches.
13.1 INTRODUCTION
In 1978, Intel released its first 16-bit microprocessor, the 8086, which executes the
instructions at 2.5 MIPS (million instructions per second). The execution time for
one instruction is 400 ns (= 1/MIPS = 1/(2.5 x 106)). The 8086 can address 1MB
(1 MB = 220 bytes) of memory, as it has a 20-bit address bus. The width of the data
bus in the 8086 is 16 bits. This higher execution speed and larger memory size
have enabled the 8086 to replace the smaller minicomputers in many applications.
Another feature in the 8086 is the presence of a small six-byte instruction queue
in which the instructions fetched from the memory are placed before they are
executed.
divided into two 8-bit registers—AH and AL, BH and BL, CH and CL, and DH
and DL, respectively, as shown in Fig. 13.1. The general-purpose registers can
be used to store 8-bit or 16-bit data during program execution. In addition, each
register has the following special functions:
(i) AX/AL: AX or AL is used as the accumulator. It is used in the multiply,
divide, and input/output (I/O) operations, and in some decimal and ASCII
adjustment instructions.
(ii) BX: The BX register holds the offset address of a location in the memory.
It is also used to refer to the data in the memory using the look-up table
technique, with the help of the XLAT instruction.
(iii) CX/CL: CX is used to hold the count value while executing the repeated
string instructions (REP/REPE/REPNE) and the LOOP instruction. CL is
used to hold the count value while executing the shift/rotate instructions.
The count value indicates the number of times the same code has to be
executed when the LOOP instruction is used and the number of times the
data item has to be shifted/rotated when the shift/rotate instruction is used.
(iv) DX: DX is used to hold a part of the result during a multiplication operation
and a part of the dividend before a division operation. It is also used to hold
the I/O device address while executing the IN and OUT instructions.
(v) SP: The SP register or the stack pointer is used to hold the offset address of
INTEL 8086 MICROPROCESSOR ARCHITECTURE, FEATURES, AND SIGNALS 421
the data stored at the top of the stack segment. SP is used along with the SS
register to decide the address at which the data is to be pushed or popped,
during the execution of the PUSH or POP instruction, respectively.
(vi) BP: The BP register is called base pointer. It is also used to hold the offset
address of the data to be read from or written into the stack segment.
(vii) SI: The SI register is called source index register. It is used to hold the
offset address of the source data in the data segment, while executing string
instructions.
(viii) DI: The DI register is called destination index register. It is used to hold the
offset address of the destination data in the extra segment, while executing
string instructions.
Here, the term segment refers to a portion of the memory where the data, code,
or stack for a program is stored. In the 8086, the maximum size of a segment can
be 64 KB and the minimum size can be even 1 byte. A segment always begins at a
memory address divisible by 16. This means that the starting address of a segment
in the memory in hexadecimal form is XXXXOH. The reason for this is explained
in Section 13.2.2.
The flag register of the 8086 is shown in Fig. 13.2.
The flags in the flag register can be classified into status flags and control flags.
The flags CF, PF, AF, ZF, SF, and OF are called status flags, as they indicate the
status of the result that is obtained after the execution of an arithmetic or logic
instruction. The flags DF, IF, and TF are called control flags, as they control the
operation of the CPU. The functions of the different flags are as follows:
(i) CF (carry flag): CF holds the carry after an 8-bit or 16-bit addition or the
borrow after an 8-bit or 16-bit subtraction operation.
(ii ) PF (parity flag): If the lower eight bits of the result have an odd parity (i.e.,
odd number of Is), PF is set to 0. Otherwise, it is set to 1.
(iii ) AF (auxiliary carry flag): AF holds the carry after addition or the borrow
after subtraction of the bits in the bit position 3 (the LSB is treated as bit
position 0). This flag is used by the DAA or the DAS instruction to adjust
the value in AL after a BCD addition or subtraction, respectively.
(iv) ZF (zero flag): ZF indicates that the result of an arithmetic or logic operation
is zero. If Z = 1, the result is zero and if Z = 0, the result is not zero.
(v) SF (sign flag): SF holds the arithmetic sign of the result after an arithmetic
or logic instruction is executed. If S - 0, the sign bit is 0 and the result is
positive.
(vi) TF (trap flag): TF is used to debug a program using the single-step
422 MICROPROCESSORS AND MICROCONTROLLERS
technique. If it is set (i.e., TF = 1), the 8086 gets interrupted (trap or single-
step interrupt) after the execution of each instruction in the program. If TF
is cleared (i.e., TF = 0), the trapping or debugging feature is disabled.
(vii ) DF (direction flag): DF selects either the increment or decrement mode for
the DI and/or SI register, during the execution of string instructions. If D
= 0, the registers are automatically incremented; if D = 1, the registers are
automatically decremented. This flag can be set and cleared using the STD
and CLD instructions, respectively.
(viii ) IF (interrupt flag): IF controls the operation of the INTR interrupt pin of
the 8086. If IF = 0, the INTR pin is disabled and if IF = 1, the INTR pin is
enabled. This flag can be set and cleared using the STI and CLI instructions,
respectively.
(ix) OF (overflow flag): Signed negative numbers are represented in the 2’s
complement form in the microprocessor. When signed numbers are added
or subtracted, an overflow may occur. An overflow indicates that the result
has exceeded the capacity of the machine. For example, if the 8-bit signed
data 7EH (= +126) is added with the 8-bit signed data 02H (= +2), the
result is 80H (= -128 in the 2’s complement form). This result indicates
an overflow condition and the overflow flag is set during the given signed
addition operation. In an 8-bit register, the minimum and maximum value
of the signed number that can be stored is -128 (= 80H) and +127 (= 7FH),
respectively. In a 16-bit register, the minimum and maximum value of
the signed number that can be stored is -32,768 (= 8000H) and +32,767
(= 7FFFH), respectively. For operations on unsigned data, OF is ignored.
Example 13.1:
Let us assume that the segment registers have following values stored in them:
CS DS SS ES
2000H 4000H 6000H 8000H
INTEL 8086 MICROPROCESSOR ARCHITECTURE, FEATURES, AND SIGNALS 423
Example 13.2:
The fetching of an instruction from the memory in the 8086 is explained in this
example.
Let us assume that the CS register has the value 3000H and the IP register has
the value 2000H. To fetch an instruction from the memory, the CPU calculates the
memory address from which the next instruction is to be fetched, as follows:
CS x 10H = 30000H—► Base address of the code segment
+ IP = 2000H —> Offset address
32000H—> Memory address from where the next instruction is taken
Example 13.3:
Let us see the fetching of data from the memory using the DS and BX registers,
with an example. Consider the execution of the instruction MOV AX, [BX].
The square bracket around BX in this instruction indicates that the data
specified by the BX register is in the memory; the BX register holds the offset
address of the data in the data segment. The data obtained from the memory is
moved to the AX register. Let us assume that DS and BX have the values 10000H
and 3000H, respectively. To calculate the memory address from where the data
has to be taken, the CPU does the following operation:
DS x 10H =10000H —> Base address of the data segment
+ BX = 3000H —> Offset address
13000H —> Memory address from where the data is taken
This is also explained in Fig. 13.4.
Memory Address
10000H
13000H
13001H
Example 13.4:
Let us see the pushing of data into the stack segment using the PUSH instruction,
with an example.
Assume that the SS and SP registers have the values 3000H and 0105H,
respectively. Consider the execution of the instruction PUSH AX by the 8086. The
steps carried out by the 8086 to execute the PUSH AX instruction are as follows:
(i) SP is decremented by 1 (i.e., SP = 0104H) and the content of the AH register
INTEL 8086 MICROPROCESSOR ARCHITECTURE, FEATURES, AND SIGNALS 425
(upper byte of AX) is pushed into the offset address specified by SP in the
stack segment, as shown in Fig. 13.5 (a).
(ii) SP is again decremented by 1 (i.e., SP = 0103H) and the content of the AL
register (lower byte of AX) is pushed into the offset address specified by SP
in the stack segment, as shown in Fig. 13.5 (b).
Memory Address
30000H—Base address of
AH AL A stack segment (=SS X 10H)
3CH SP = 0104H
2BH
(a)
Memory Address
30000H—(SS X 10H)
30103H—(SSX10H + SP)
30104H
(b)
Fig. 13.5 PUSH AX: (a) pushing the first byte of AX onto the stack segment (b) pushing the
second byte of AX onto the stack segment
The instruction queue is six bytes long and stores the pre-fetched instructions
from the code segment. From there, the instruction is taken to the instruction
decoder, where it is decoded. The decoder passes the decoded information to the
timing and control circuit, which in turn generates the various control signals to
execute the instruction. Whenever this decoded instruction requires branching
(which arises when conditional or unconditional jump instructions are decoded),
the instruction queue is flushed and the instruction bytes from the branch address
are fetched into the queue. The BIU fetches the instruction bytes from the memory
whenever the EU is not using the address/data bus and puts them in the instruction
queue. Fetching and execution of instructions can take place simultaneously. Thus
the instruction queue reduces the execution time of a program.
The segment and offset mechanism for accessing the memory in the 8086 allows
the programmer to write relocatable programs or data structures. A relocatable
program or data structure is one that can be placed anywhere in the memory map
426 MICROPROCESSORS AND MICROCONTROLLERS
of the 8086 and executed without any modification. This is not possible in the 8085
microprocessor. In a relocatable program, the jump instructions use only relative
values (positive or negative) with respect to the program counter, using which the
jump address is calculated. In addition, in a relocatable data structure, the data is
referred to using the offset address in the data segment or the extra segment.
GND 40 □
ADU E2 39 □ AD15
AD13 38 □ A16/S3
AD12 4 37 □ A17/S4
AD11 E 5 36 □ A18/S5
AD10 6 35 J A19/S6
AD9 7 34 □ BHE/S7
AD8 E 8 33 □ MN/MX
AD7 9 32 □ RD
8086
AD6 E 10 31 □ RQ/GTO (HOLD)
$
AD5 E 11 30 □ RQ/GT1 (HLDA)
AD4 12 29 □ LOCK (WR)
AD3 E 13 28 □ S2 (M/iO)
AD2 14 27 □ SI (DT/R)
AD1 E 15 26 □ SO (DEN)
ADO 16 25 J QSO (ALE)
NMI 17 24 □ QS1 (INTA)
INTR E 18 23 □ TEST
CLK 19 22 □ READY
GND
4 E 20 21 □ RESET
the address signals A 19-Al6 and the status bits S6-S3. When ALE = 1,
these pins carry the address and when ALE = 0, they carry the status lines.
Using one external octal latch (74373) along with the ALE signal, these
pins can be de-multiplexed into the address bus (A 19-Al6) and the status
bus (S6-S3). S3 and S4 indicate the segment accessed by the 8086 during
the current bus cycle. This is shown in Table 13.2.
S4 S3 Segment accessed
0 0 Extra segment
0 1 Stack segment
1 0 Code segment or no segment
1 1 Data segment
The status bit S5 indicates the condition of the IF bit; S6 always remains at logic
0.
(iii) NMI: The non-maskable interrupt (NMI) input is a hardware interrupt. It
cannot be disabled by software. It is a positive edge-triggered interrupt and
when it occurs, the type 2 interrupt occurs in the 8086.
(iv) INTR: The interrupt request (INTR) is a level-triggered hardware interrupt,
which depends on the status of IF. When IF = 1, if INTR is held high (i.e.,
logic 1), the 8086 gets interrupted. When IF = 0, INTR is disabled.
(v) CLK: The clock signal must have a duty cycle of 33% to provide proper
internal timing for the 8086. Its maximum frequency can be 5, 8, and
10 MHz for different versions of the 8086—the 8086, 8086-2, and 8086-1,
respectively.
(vi) Vcc: This power supply pin provides a +5 V signal to the 8086. The variation
allowed in the power supply input is ±10%.
(vii) BHE/S7: The bus high enable (BHE) pin is used in the 8086 to enable the
most significant data bus (D15-D8) during a read/write operation. The
state of the status line S7 is always logic 1.
(viii) MN/MX: The MN/MX pin is used to select either the minimum mode or
the maximum mode operation for the 8086. This is achieved by connecting
this pin to either +5 V directly (for minimum mode) or to the ground (for
maximum mode).
(ix) RD: Whenever the Read signal (RD) is at logic 0, the 8086 reads the data
from the memory or EO device through the data bus.
(x) TEST: The TEST pin is an input that is tested by the WAIT instruction. If
the TEST pin is at logic 0, the WAIT instruction functions as a NOP (no
operation) instruction. If the TEST pin is at logic 1, the WAIT instruction
waits for the TEST pin to become logic 0. This pin is often connected to
the BUSY pin of the 8087 (numeric coprocessor) to perform floating-point
operations.
(xi) READY: This input is used to insert wait states into the timing cycle of the
428 MICROPROCESSORS AND MICROCONTROLLERS
8086. If the READY pin is at logic 1, it has no effect on the operation of the
microprocessor. If it is at logic 0, the 8086 enters the wait state and remains
idle. This pin is used to interface the slowly operating peripherals with the
8086.
(xii) RESET: This input causes the 8086 to reset, if it is held at logic 1 for a
minimum of four clocking periods. Whenever the 8086 is reset, CS and IP
are initialized to FFFFH and 0000H, respectively, and all other registers are
initialized to 0000H. This causes the 8086 to begin executing instructions
from the memory address FFFF0H.
(xiii) GND: The GND connection is the return for the power supply (Vcc). The
8086 has two GND pins and both must be connected to ground for proper
operation.
S2 S1 so Function
0 0 0 Interrupt acknowledge
0 0 1 I/O read
0 1 0 I/O write
0 1 1 Halt
1 0 0 Opcode fetch
1 0 1 Memory read
1 1 0 Memory write
1 1 1 Passive (inactive)
(ii) LOCK: The Lock output is used to lock peripherals off the system. This pin
is activated by using the LOCK prefix on any instruction.
(iii) RQ/GTO and RQ/GT1: The request/grant pins request DMA during the
maximum mode operation of the 8086. These lines are bidirectional and
are used to request and grant a DMA operation.
(iv) QS1 and QSO: The queue status bits show the status of the internal
instruction queue in the 8086. These pins are provided for access by the
numeric coprocessor (8087). Table 13.4 shows the function of the QS1 and
QSO bits.
Table 13.4 Function of QS1 and QSO pins
POINTS TO REMEMBER
• The internal architecture of the 8086 mainly contains two units—the bus interface unit
(BIU) and the execution unit (EU).
• The BIU fetches instructions and data from the memory to the processor, using the
content of a segment register and an offset.
• There exists a six-byte instruction queue in the 8086, which is used to store the recently
fetched instructions in the CPU. This is used to speed up the execution of a program.
• There are four memory segments—code, data, stack, and extra segments in the 8086 and
their base address is indicated by adding four binary 0s to the right of the corresponding
segment register’s content. The maximum size of a memory segment is 64 KB.
• For fetching either an instruction byte or a data, the 8086 adds the base address of the
particular segment with an offset address present in a register, available as an 8- or 16-
bit displacement in the instruction, or obtained by a combination of both.
• The designers of the 8086 have fixed the default offset register(s) for every segment
430 MICROPROCESSORS AND MICROCONTROLLERS
register. However, this can be changed using the segment override prefix in the
instruction.
• The EU contains the ALU, general-purpose registers, and the flag register, which are
used during the execution of an instruction.
• The flag register contain different flags, which can be classified as status flags and
control flags. The status flags reflect the result of arithmetic and logical operations, and
the control flags control the operation during execution of instructions.
• The 8086 can be operated in minimum mode or maximum mode.
• In the 8086, the size of the address bus and data bus is 20 bits and 16 bits, respectively.
The 8086 can access a maximum memory size of 1 MB (= 220), as it has a 20-bit address
bus.
Bus interface unit This unit BIU includes an adder for address calculations, four 16-
bit segment registers (CS, DS, SS, and ES), a 16-bit instruction pointer (IP), a six-byte
instruction queue, and bus control logic. This unit is responsible for fetching the instructions
and data into the 8086 from the memory or I/O device.
Code segment This segment contains the instructions of a program.
Data segment This segment contains the data for a program.
Execution unit This unit includes the ALU, eight 16-bit general-purpose registers, a
16-bit flag register, and the control unit. This unit is responsible for executing instructions
in the 8086.
Extra segment This is an additional data segment used by some string instructions.
Flags These show information related to the result of the arithmetic or logic operation
performed in the ALU. Flags in the flag register can be classified as status flags and control
flags.
Instruction queue It is six bytes long in the 8086 and stores the pre-fetched instructions
from the memory. It is used to speed up the execution of a program.
Maximum mode operation In this mode, some control signals must be externally
generated, using a bus controller such as the 8288.
Minimum mode operation In this mode, all control signals for the memory and I/O are
generated by the microprocessor itself.
Offset This is a 16-bit number that is added to the base address of a segment, to select a
byte of instruction or data from the memory.
Relocatable program It is the one that can be placed anywhere in the memory map of
the 8086 and executed without any modification.
Segment register This register indicates the starting or base address of a segment in the
memory.
Stack segment This segment holds the stack of a program.
REVIEW QUESTIONS
1. What is the size of the address bus and data bus in the 8086?
2. What is meant by multiplexed address and data bus?
3. Draw the register organization of the 8086 and explain typical applications of each
register.
4. How is the 20-bit physical memory address calculated in the 8086 processor?
INTEL 8086 MICROPROCESSOR ARCHITECTURE, FEATURES, AND SIGNALS 431
5. Write the different memory segments used in the 8086 and their functions.
6. List the segment registers and their default offset registers in the 8086.
7. What are the steps involved when PUSH BX is executed by the 8086?
8. Write the function of the DF, IF, and TF bits in the 8086.
9. The content of the different registers in the 8086 is CS = F000H, DS = 1000H,
SS = 2000H, and ES = 3000H. Find the base address of the different segments in the
memory.
10. If the current content of the CS and IP registers is FFFFH and 0000H, respectively,
from which memory location will the 8086 fetch the next instruction?
11. If the content of the DS and BX registers is 2500H and 1000H, respectively, from
which memory location will the 8086 fetch the data, while executing the instruction
MOV CX, [BX]?
12. If the content of the SS and SP registers is 5000H and 1000H, respectively, in which
memory location is the content of DX saved, when the 8086 executes the instruction
PUSH DX?
13. What is the difference between the minimum and maximum mode operation of the
8086?
14. What is the supply to be given to the Vcc input of the 8086?
15. What is the maximum frequency and duty cycle of the clock signal givento the 8086?
16. What is the function of the BHE and ALE signals in the 8086?
17. Which pins of the 8086 are used to enable and control the external data busbuffers?
18. What is the minimum time for which the Reset input must be activated for proper reset
of the 8086?
19. What are the contents of the CS and IP registers immediately after the reset of the
8086?
20. What is meant by DMA operation? Which pins of the 8086 are used to perform the
DMA operation in the minimum and maximum modes of the 8086?
21. What is the role of the status lines S4 and S3 in the 8086?
22. What is the function of the S2, SI, and SO signals in the maximum mode operation of
the 8086? _ ___
23. What is the role of the TEST pin in the 8086?
24. Explain the architecture of the 8086 with a neat functional block diagram.
25. Explain the function of the different flags in the 8086.
1. How much memory, in terms of bytes, can be interfaced with the 8086? Why?
2. What is the minimum and maximum size of a segment in terms of bytes? Why?
3. Why is memory divided into segments in the 8086? What are its advantages?
4. How many 8K x 8 memory chips are required to construct a 1 MB memory?
5. Which pin of the 8086 determines the mode of operation? How?
6. What are the differences between NMI and INTR interrupts in the 8086?
7. Which pin of the 8086 is used to synchronize the slowly operating peripherals with the
8086? How?
8. Is it possible for a segment to begin at a memory address that is not divisible by 16
(i.e., the address that does not end with the digit OH) in the 8086? Why?
9. Is it possible for two segments to overlap in the 8086? Why?
10. Why is the stack segment said to be growing downwards in the 8086?
11. Mention the differences between 8085 and 8086 microprocessors.
CHAPTER 14
ADDRESSING MODES,
INSTRUCTION SET, AND
PROGRAMMING OF 8086
Example:
(a) MOV AL, BL ; Move the content of BL to AL.
(b) MOV CX, BX ; Move the content of BX to CX.
(c) ADD CL, BL ; Add the contents of CL and BL and store the
result in CL.
(d) ADC BX, DX ; Add the contents of BX , the carry flag, and
DX, and store the result in BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 4 33
In the last example, [SI] represents the memory location in the data segment at
the offset address specified by the SI register.
Register CS DS SS ES BX BP SI DI
Stored value 1000H 3000H 4000H 6000H 2000H 1000H 1000H 3OOOH
Example:
(a)MOV CL, [BX]
EA = (BX) = 2000H
Memory address = DS x 10 4- (BX) = 32OOOH. The byte from the memory address
32OOOH is read and stored in CL.
434 MICROPROCESSORS AND MICROCONTROLLERS
Example:
(a)MOV BL, [SI]
EA = (SI) = 1000H
Memory address = DS x 10H + SI
= 30000H + 1000H = 31000H
A byte from the memory address 31000H is read and stored in BL.
(b)MOV CX, [DI]
EA = (DI) = 3000H
Memory address = DS x 10H + (DI)
= 30000H + 3000H = 33000H
A word from the memory address 33OOOH is read and stored in CX.
(v) Index relative addressing: This mode is the same as the base relative addressing
mode, except that instead of the BP or BX register, the SI or DI register is used.
Example:
(a)MOV BX, [SI - 100H]
EA = (SI) - 100H
Memory address = DS x 10H + (SI) - 100H
= 30000H + 1000H - 100H = 30F00H
A word from the memory address 30F00H is read and stored in BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 435
Base relative plus index addressing is used to access a byte or a word in a particular
record of a specific file in the memory. An application program may process many
files stored in the data segment. Each file contains many records and a record
contains a few bytes or words of data. In base relative plus index addressing, the
base register may be used to hold the offset address of a particular file in the data
segment; the index register may be used to hold the offset address of a particular
record within that file; the relative value is used to indicate the offset address of
particular byte or word within that record.
where it has to fetch an instruction using the relation CS x 10H + IP, the address
32000H is obtained using the given CS and IP values.
This type of jump is known as intersegment jump, using which the
microprocessor can jump to any memory location within the memory system (i.e.,
within 1 MB). It is also known as far jump. The inter-segment or FAR CALL
instruction also uses direct program memory addressing. While using the assembler
to develop the 8086 program, the assembler directive FAR PTR is sometimes used
to indicate the inter-segment jump instruction.
Example:
(a) JMP FAR PTR COMPUTE
(b) JMP FAR PTR SIMULATE
In these examples, COMPUTE and SIMULATE are the labels of memory
locations that are present in code segments other than the ones in which these
instructions are present.
(ii) Relative addressing: The term relative here means relative to the instruction
pointer (IP). Relative JMP and CALL instructions contain either an 8-bit or a
16-bit signed displacement, which is added to the current instruction pointer.
Based on the new value of IP thus obtained, the address of the next instruction to
be executed is calculated using the relation CS x 10H + IP.
The 8-bit or 16-bit signed displacement allows a forward or a reverse memory
reference, depending on the sign of the displacement. If the displacement is positive,
PC is incremented by the displacement value and if it is negative, PC is decremented
by the magnitude of the displacement value. A one-byte displacement is used in the
short jump and call instructions, and a two-byte displacement is used in the near
jump and call instructions. Both types are considered intrasegmentjumps, since the
program control is transferred anywhere within the cunent code segment.
An 8-bit displacement has a jump range between +127 and -128 bytes from the
next instruction, while a 16-bit displacement has a jump range between -32,768
and +32,767 bytes from the instruction following the jump instruction in the
program. The opcode of the relative short jump and near jump instructions are
EBH and E9H, respectively.
While using an assembler to develop the 8086 program, the assembler
directives SHORT and NEAR PTR are used to indicate the short jump and near
jump instructions, respectively.
Example:
(a) JMP SHORT OVER
(b) JMP NEAR PTR FIND
In these examples, OVER and FIND are the labels of memory locations that
are present in the same code segment in which these instructions are present.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 437
(iii) Indirect addressing: The indirect jump or CALL instructions use a 16-bit
register (AX, BX, CX, DX, SP, BP, SI, or DI), a relative register ([BP], [BX],
[DI], or [SI]), or a relative register with displacement. The opcode of the indirect
jump instruction is FFH. It can be either an inter-segment indirect jump or an
intra-segment indirect jump.
If a 16-bit register holds the jump address in an indirect JMP instruction, the
operation is a near jump. If the CX register contains 2000H and the JMP CX
instruction present in a code segment is executed, the microprocessor jumps to the
offset address 2000H in the current code segment to take the next instruction for
execution (this is done by loading the IP with the content of CX, without changing
the content of CS).
When the instruction JMP [DI] is executed, the microprocessor first reads a
word in the current data segment from the offset address specified by DI and
places that word in the IP register. Now, with this new value of IP, the 8086
calculates the address of the memory location to which it has to jump, using the
relation CS x 10H + IP.
Example:
Let us assume that the registers DS, DI, and CS have the values 1000H, 2000H,
and 3000H, respectively. When JMP [DI], present at the offset address 1500H
in the code segment 3000H is executed, the microprocessor reads a word from
the address given by DS x 10H + DI (= 12000H) in the memory, and loads
it in the instruction pointer (IP). Let us assume that the word that is stored
in the address 12000H is 4000H. Hence, the program counter will be loaded
with the value 4000H. Now, the microprocessor fetches the next instruction for
execution from the address given by CS x 10H + IP (= 3000H x 10H + 4000H
= 34000H).
Since SP gets decremented for every push operation, the stack segment is said
to be growing downwards, as for successive push operations, data are stored in the
lower memory addresses in the stack segment. Due to this, SP is initialized with
the highest offset address, according to the user’s requirement, at the beginning of
the program.
Example:
(a) PUSH AX ; Push the content of AX into the stack.
(b)PUSH DS ; Push the content of DS into the stack.
(c) PUSH [BX] ; Push the content of the memory location at
the offset address specified by BX in the
current data segment, into the stack.
The PUSHF instruction is used to push the flag register’s content into the
stack.
Whenever a word is popped from the stack, the lower-order eight bits of the
word are removed from the memory location specified by SP and the higher-order
eight bits of the word are removed from the memory location specified by SP + 1
in the current stack segment. SP is then incremented by two.
Example:
(a) POP BX : Pop the content of BX from the stack.
(b) POP ES ; Pop the content of ES from the stack.
(c) POP [BP] ; Pop the content of the memory 1 ocation at
the offset address specified by BP in the
current stack segment, from the stack.
The POPF instruction is used to pop a word stored in the stack and move it to
the flag register.
(ii) PUSH: The PUSH instruction is used to store the word in a register or a
memory location into the stack, as explained in the stack addressing mode. SP is
decremented by two after the execution of PUSH.
Example:
(a)PUSH CX : PUSH the content of CX into the stack.
(b)PUSH DS ; PUSH the content of DS into the stack.
(c)PUSH [BX] ; PUSH the word in the memory at [BX] into
the stack.
(iii) POP: The POP instruction copies the top word from the stack to a destination
specified in the instruction. The destination can be a general-purpose register, a
segment register, or a memory location. After the word is copied to the specified
destination, SP is incremented by two.
440 MICROPROCESSORS AND MICROCONTROLLERS
Example:
(a) POP BX ; Pop the content of BX from the stack.
(b)POP DS : Pop the content of DS from the stack.
(c) POP [SI] : Pop a word from the stack and store it in
the memory at [SI],
Note: [SI] indicates the memory location in the data segment at the offset address
specified by SL
(iv) XCHG: The XCHG instruction exchanges the contents of a register with the
contents of a memory location. It cannot exchange the contents of two memory
locations directly. The source and destination must both be either words or bytes.
The segment registers cannot be used in this instruction.
Example:
(a) XCHG AL, BL : Exchanges the content of AL and BL.
(b)XCHG CX, BX : Exchanges the content of CX and BX.
(c) XCHG AX, [BX] ; Exchanges the content of AXwith the content
of the memory at [BX].
(v) XLAT: The XLAT instruction is used to translate a byte in AL from one code
to another code. The instruction replaces a byte in the AL register with a byte in
the memory at [BX], which is one of the data items present in a look-up table.
Before XLAT is executed, the look-up table containing the desired codes must
be put in the data segment and the offset address of the starting location of the
look-up table is stored in BX. The code byte to be translated is put in AL. When
XLAT is executed now, it adds the content of the AL with BX to find the offset
address of the data in the look-up table. Further, the byte in that offset address will
get copied to AL.
(vi) IN: The IN instruction copies data from a port to the AL or AX register. If an
8-bit port is read, the data is stored in AL and if a 16-bit port is read, the data is
stored in AX. The IN instruction has two formats—fixed port and variable port.
In the fixed port type IN instruction, the 8-bit address of a port is specified
directly in the instruction. With this form, any one of 256 possible ports can be
addressed.
Example:
IN AL, 80H : Input a byte from the port with address 80H to AL.
IN AX, 40H ; Input a word from the port with address 40H to AX.
For the variable port type IN instruction, the port address is loaded into the DX
register before the IN instruction. Since DX is a 16-bit register, the port address
can be any number between 0000H and FFFFH. Hence, we will be able to address
up to 65,536 ports in this mode. The following example shows a part of a program
having the IN instruction. The operations done when the instructions are executed
are given in the corresponding comment fields.
Example:
MOV DX, 0FE50H ; Initialize DX with the port address of FE50H.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 441
(viii) LEA (load effective address): The general format of the LEA instruction is
LEA register, source. This instruction determines the offset address of the variable
or memory location called the source and puts this offset address in the indicated
16-bit register.
Example:
(a) LEA BX, COST ; Load BX with the offset address of COST in the
data segment, where COST is the name assigned
to a memory location in the data segment.
(b)LEA CX, [BX] [SI] ; Load CX with the value equal to (BX) + (SI),
where (BX) and (SI) represent the content of
BX and SI, respectively.
(ix) LDS: This instruction loads the register and DS with words from the memory.
The general form of this instruction is LDS register, memory address of first
word.
The LDS instruction copies a word from the memory location specified in the
instruction into the register, and then copies a word from the next memory location
into the DS register. LDS is useful in initializing the SI and DS registers at the start
of a string before using one of the string instructions.
442 MICROPROCESSORS AND MICROCONTROLLERS
Example:
LDS SI, E2000HJ ; Copy the content of the memory at the
offset address 2000H in the data segment to
the lower-order byte of SI, and the content
of 2001H to the higher-order byte of SI. Copy
the content at the offset address 2002H in
the data segment to the lower-order byte of
DS and the content of 2003H to the higher-
order byte of DS.
(x) LES and LSS: The LES and LSS instructions are similar to the LDS instruction,
except that instead of the DS register, the ES and SS registers, respectively, are
loaded, along with the register specified in the instruction.
(xi) LAHF: This instruction copies the lower-order byte of the flag register into
AH.
(xii) SAHF: This instruction stores the content of AH in the lower-order byte of
the flag register.
Except the SAHF and POPF instructions, no other data transfer instruction
affects the flag register.
Example:
(a) ADD BL, 80H : Add the immediate data 80H to BL.
(b)ADD CX, 12B0H ; Add the immediate data 12B0H to CX.
(c) ADD AX, CX ; Add the content of AX and CX and store the
result in AX.
(d)ADD AL, [BX] ; Add the content of AL and the byte from the
memory at [BX] and store the result in AL.
(e) ADD CX, [SI] ; Add the content of CX and the word from the
memory at [SI] and store the result in CX.
(f) ADD [BX] , DL ; Add the content of DL with the byte from the
memory at [BX] and store the result in the
memory at [BX].
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 443
The flags AF, CF, OF, PF, SF, and ZF are affected by the execution of the
ADD instruction.
(ii) ADC: This instruction adds the data in the source and destination with the
content of the carry flag and stores the result in the destination. The general format
of this instruction is ADC destination, source.
All the rules specified for ADD are applicable to the ADC instruction.
(iii) SUB: The general form of the subtract (SUB) instruction is SUB destination,
source. It subtracts the number in the source from the number in the destination
and stores the result in the destination. Like the ADD instruction, the source may
be an immediate number, a register, or a memory location. The destination can be
a register or a memory location. However, the source and destination cannot both
be memory locations. The data from the source and destination must be of the
same type (either bytes or words).
For subtraction, the carry flag (CF) functions as the borrow flag. If the result is
negative after subtraction, CF is set. Otherwise, it is reset. The flags AF, CF, OF,
PF, SF, and ZF are affected by the SUB instruction.
Example:
(a) SUB AL, BL Subtract BL from AL and store the result i n
AL.
(b)SUB CX, BX ; Subtract BX from CX and store the result i n
CX.
(c) SUB BX, [DI] ; Subtract thei word in the memory at [DI]
from BX and store the result in BX.
(d)SUB [BP] , DL ; Subtract DL from the byte in the memory at
[BP] and store the result in the memory at
[BP],
(iv) SBB: Subtract with borrow—The general form of this instruction is SBB
destination, source. The SBB instruction subtracts the content of the source
and the carry flag from the content of the destination and stores the result in the
destination. The rules for the source and the destination are same as that for the
SUB instruction. AF, CF, OF, PF, SF, and ZF are affected by this instruction.
(v) INC: The increment (INC) instruction adds 1 to the content of a specified
register or a memory location. The data incremented may be a byte or word. While
the carry flag is not affected by this instruction, the flags AF, OF, PF, SF, and ZF
are affected.
Example:
(a)INC CL : Increment the content of CL by 1.
(b)INC AX : Increment the content of AX by 1.
(c) INC BYTE PTR [BX] ; Increment the byte in the memory at [BX] by 1.
(d)INC WORD PTR [SI] ; Increment the word in the memory at [SI] by 1.
In these examples, the terms BYTE PTR and WORD PTR are assembler
directives, which are used to specify the type of data (byte or word) to be
incremented in the memory.
444 MICROPROCESSORS AND MICROCONTROLLERS
(vi) DEC: The decrement (DEC) instruction subtracts 1 from the content of a specified
register or memory location. The data decremented may be a byte or a word. CF is
not affected, but AF, OF, PF, SF, and ZF flags are affected by this instruction.
(vii) NEG: The negate (NEG) instruction replaces the byte or word in the specified
register or memory location by its 2’s complement (i.e., changes the sign of the
data). The CF, AF, SF, PF, ZF, and OF flags are affected by this instruction.
Example:
(a)NEG AL ; Take 2’s complement of the data in AL and
store it in AL.
(b)NEG CX ; Take 2’s complement of the data in CX and
store it in CX.
(c) NEG BYTE PTR [BX] ; Take 2’s complement of the byte in the memory
at [BX] and store the result in the same
pl ace.
(d)NEG WORD PTR [SI] ; Take 2’s complement of the word in the memory
at [SI] and store the result in the same place.
(viii) CMP: The general form of the compare (CMP) instruction is CMP
destination, source. This instruction compares a byte or word in the source with a
byte or word in the destination and affects only the flags, according to the result.
The content of the source and destination are not affected by the execution of this
instruction. The comparison is done by subtracting the content of the source from
that of the destination. The AF, OF, SF, ZF, PF, and CF flags are affected by the
instruction. The rules for the source and destination are the same as those for the
SUB instruction.
Example:
After the instruction CMP AX, DX is executed, the status of CF, ZF, and SF will
be as follows:
CF ZF SF
IfAX = DX 0 1 0
IfAX>DX 0 0 0
IfAX<DX 1 0 1
(ix) MUL: The multiply (MUL) instruction is used for multiplying two unsigned
bytes or words. The general form of the MUL instruction is MUL source. The
source can be a byte or a word from a register or memory location, which is
considered as the multiplier. The multiplicand is taken by default from AL and AX
for byte and word type data, respectively. The result of multiplication is stored in
AX and DX-AX (i.e., the most significant word of the result in DX and the least
significant word of the result in AX) for byte and word type data, respectively.
(Note: Multiplying two 8-bit data gives a 16-bit result and multiplying two 16-bit
data gives a 32-bit result.)
Example:
(a)MUL CH : Multiply AL and CH and store the result in
AX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 445
(xi) DIV: The divide (DIV) instruction is used for dividing unsigned data. The
general form of the DIV instruction is DIV source, where ‘source’ is the divisor.
It can be a byte or word in a register or memory location. The dividend is taken by
default from AX and DX-AX for byte and word type data division, respectively.
Table 14.3 shows the complete details of the DIV instruction.
represented as an 8-bit number in which the upper four bits are always zero. For
example, the decimal digit 3 is represented as 03H in unpacked BCD form.
(xv) AAA: The AAA (ASCII adjust after addition) instruction must always follow
the addition of two unpacked BCD operands in AL. When AAA is executed, the
content of AL is changed to a valid unpacked BCD number; the upper four bits of
AL are cleared. CF is set and AH is incremented if a decimal carry-out from AL
is generated.
Example:
Let AL = 05 (decimal) = 00000101
BH = 06 (decimal) = 00000110
AH = OOH
Consider the execution of the following instructions:
ADD AL, BH ; AL = 11 (decimal) and CF = 0
AAA : AL = 01 and AH = 01 and CF = 1
Addition of 5 and 6 gives a decimal result of 11, which is equal to 0101H in
unpacked BCD form. It is stored in AX. When this result is to be sent to the printer,
the ASCII code of each decimal digit is easily found by adding 30H to each byte.
(xvi) AAS: ASCII adjust after subtraction—This instruction always follows the
subtraction of one unpacked BCD operand from another in AL. It changes the
content of AL to a valid unpacked BCD number and clears the top four bits of AL.
CF is set and AH is decremented if a decimal borrow occurs.
Example:
(a) Let AL = 09 BCD = 00001001
CL = 05 BCD = 00000101
AH = OOH
Consider the execution of the following instructions:
SUB AL, CL ; AL = 04 BCD
AAS ; AL = 04 BCD and CF = 0
; AH = OOH
(b)Let AL = 05 BCD
CL = 09 BCD
AH = OOH
Consider the execution of the following instructions:
SUB AL, CL ; AL = -4 BCD (in 2’s complement form AL = FCH) and
CF = 1
AAS ; AL = 04 BCD
: CF = 1 indicating that a borrow is needed and
AH = FFH = 2’s complement of -1
AAA and AAS affect the AF and CF flags and OF, PF, SF, and ZF are left
undefined. Another salient feature of these two instructions is that it is possible
to take input data in the ASCII form of the unpacked decimal number, obtain
the result as an unpacked decimal number, and then convert it to ASCII form by
adding 30H to it.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 449
(xvii) AAD: ASCII-adjust before the division instruction modifies the dividend in
AH and AL, to prepare for the division of two valid unpacked BCD operands. After
the execution of AAD, AH is cleared and AL contains the binary equivalent of the
original unpacked two-digit numbers. Initially, AH contains the most significant
unpacked digit and AL contains the least significant unpacked digit.
Example:
To perform the operation 32 (decimal)/08 (decimal)
Let AH = 03H ; Upper decimal digit in the dividend
AL = 02H ; Lower decimal digit in the dividend
CL = 08H ; Divisor
Consider the execution of the following instructions:
AAD ; AX = 0020H (binary equivalent of the decimal value
32 in 16-bit form)
DIV CL ; Divide AX by CL. AL contains the quotient and AH the
remainder.
AAD affects the PF, SF, and ZF flags. AF, CF, and OF are undefined after
execution of AAD.
(xviii) AAM: The AAM (ASCII adjust AX after multiplication) instruction corrects
the value obtained by multiplication of two valid unpacked decimal numbers. The
higher-order digit is placed in AH and the lower-order digit in AL.
Example:
Let AL = 05 (decimal)
CL = 09 (decimal)
Consider the execution of the following instructions:
MUL CH ; AX = 002DH = 45 (decimal)
AAM ; AH = 04 and AL = 05 (unpacked BCD form of the decimal
number 45)
OR AX, 3030H ; To get the ASCII code of the result In AH and AL
(Note: This instruction is used only when the result
is needed in ASCII form.)
AAM affects the same flags as AAD.
(v) TEST: This instruction ANDs the content of a source byte or word with the
content of the specified destination byte or word. The flags are updated, but
neither operand is changed. The TEST instruction is often used to set flags before
a conditional jump instruction. The general form of TEST instruction is TEST
destination, source. The rules for the source and destination and the way flags are
affected are the same as the AND instruction.
Example:
Let AL = 0111 1111 =7FH
TEST AL, 80H ; AL = 7FH (unchanged)
ZF = 1 since (AL) AND (80H) = OOH; SF = 0; PF = 1
Mnemonics Function
Mnemonics Function
PUSHF Push the flag register’s content onto the stack
POPF Pop the top word of the stack onto the flag register
CMC Complement the carry flag (CF = complement of CF)
CLC Clear the carry flag (CF = 0)
STC Set the carry flag (CF = 1)
CLD Clear the direction flag (DF = 0)
STD Set the direction flag (DF = 1)
CLI Clear the interrupt flag (IF = 0)
STI Set the interrupt flag (IF = 1)
Mnemonics Description
Unconditional transfers
Mnemonics Description
In this table, ‘addr’ is the target address in the memory, to which the 8086 has
to jump, if the condition is satisfied while executing conditional jump instructions,
‘addr’ is also the target address to which the 8086 has to jump while executing
unconditional jump instructions. In the CALL instruction, ‘addr’ indicates the
address where the subroutine is located. In the case of conditional jump instructions,
the target address must be located at a relative address, which is in the range of
+127 bytes to -128 bytes from the instruction following the conditional jump
instruction.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 4 53
Mnemonics Description
LOOP addr Decrement CX. Go to addr if CX # 0.
LOOPE addr Loop while equal (Decrement CX. Go to addr if CX # 0 and
ZF= 1.)
LOOPZ addr Same as LOOPE
LOOPNE addr Loop while not equal (Decrement CX. Go to addr if CX # 0 and
ZF = 0.)
LOOPNZ addr Same as LOOPNE
In this table, ‘addr’ is the target address, which must be located at a relative
address in the range of+127 bytes to -128 bytes from the instruction following the
LOOP instruction.
The use of the LOOP instruction in a program is explained here with an
example:
MOV CX, 100
AGAIN : MOV AL, BL
Mnemonics Description
(ii) SAR: The general format of the SAR instruction is SAR destination, count.
The destination can be a register or a memory location and a byte or a word. This
instruction shifts each bit in the destination a specified number of bit positions to
the right. As a bit is shifted out of the MSB position, a copy of the old MSB is put
in the MSB position (i.e., the sign bit is copied into the MSB). The LSB will be
shifted into the carry flag (CF) as follows:
MSB------► MSB-------- > LSB ----- > CF
The rules for the count value in the instruction are the same as those for the SAL
instruction. CF, SF, and ZF are affected according to the result. PF has meaning
only when AL is used as the destination.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 455
(iii) SHR: The general format of the SHR instruction is SHR destination, count.
The destination can be a register or a memory location and a byte or a word. This
instruction shifts each bit in the destination a specified number of bit positions
to the right. As a bit is shifted out of the MSB position, a 0 is placed in the MSB
position. The LSB is shifted into the carry flag (CF) as follows:
0----- > MSB-------- > LSB ----- > CF
The rules for the count value in the instruction are same as those for the SHL
instruction. CF, SF, and ZF are affected according to the result. PF has meaning
only when an 8-bit destination is used.
(iv) ROR: This instruction rotates all the bits of the specified byte or word by
a specified number of bit positions to the right. The operation done when ROR is
executed is as follows:
The general format of the ROR instruction is ROR destination, count. The data
bit moved out of the LSB is copied into CF. ROR affects only CF and OF. In the
single-bit rotate operation, if the sign bit (i.e., the MSB) changes after the execution
of ROR, OF is set. This is applicable only for the single-bit rotate operation. ROR
is used to swap nibbles in a byte and to swap bytes in a word. It can also be used
to rotate a bit in a byte/word into CF, where it can be checked and acted upon by
the JC and JNC instructions. CF contains the bit most recently rotated out of the
LSB, in the case of a multiple bit rotate operation. The rules for the count value
are same as those for the shift instruction.
Example:
(a) ROR CH, 1 ; Rotate right the byte in CH by one bit
posi ti on.
(b)ROR BX, CL ; Rotate right the word in BX by the
number of bit positions given by CL.
(c) ROR BYTE PTR [SI], 1 ; Rotate right the byte in the memory at
offset [SI] by one bit position.
(d) ROR WORD PTR [BX], CL ; Rotate right the word in the memory at
offset [BX] by the number of bit
positions given by CL.
(v) ROL: ROL rotates all the bits in a byte or word in the destination to the left, by
one or more bit positions, using CL, as follows:
rotate operation. ROL is used to swap nibbles in a byte or swap bytes in a word.
It can also be used to rotate a bit in a byte/word into CF, where it can be checked
and acted upon by the JC and JNC instructions. CF contains the bit most recently
rotated out of the LSB, in the case of the multiple bit rotate operation.
(vi) RCR: RCR rotates the byte or word in the destination right, through the carry
flag (CF), either by one bit position or by the number of bit positions given by CL,
as follows:
The flags affected are the same as those affected during the execution of
ROR.
(vii) RCL: RCL rotates the byte or word in the destination left through the carry
flag (CF), either by one bit position or by the number of bit positions given by CL,
as follows:
Mnemonics Function
Mnemonics Function
CMPSW Compare string words (done by subtracting the word at
ES: [DI] from the word at DS: [SI]). Only flags are affected;
the content of the words compared is unaffected.
LODSB Load the string byte at DS:[SI] into AL.
LODSW Load the string word at DS:[SI] into AX.
STOSB Store the string byte in AL at ES:[DI].
STOSW Store the string word in AX at ES:[DI],
SCASB Compare string bytes (done by subtracting the byte at ES: [DI]
from the byte at AL). Only flags are affected; the content of the
bytes compared is unaffected.
SCASW Compare string words (done by subtracting the word at ES:
[DI] from the byte at AX). Only flags are affected; the content
of the words compared is unaffected.
REP Decrement CX and repeat the following string operation if
CX # 0.
REPE or REPZ Decrement CX and repeat the following string operation if
CX # 0andZF= I.
REPNE or REPNZ Decrement CX and repeat the following string operation if
CX 0 and ZF = 0.
The REP (repeat) prefix placed before a string instruction causes the string
instruction to be executed CX times.
Example:
(a)MOV CX, 32H ; Load 32H (= decimal 50) in CX.
(b)REP MOVSW ; Execute MOVSW instruction 50 times.
Execution of these two instructions causes the moving of a string having 50
words from the data segment to the extra segment.
Note:
(i) To initialize a segment register with a value, the value is first loaded in one of
the general-purpose registers such as AX or BX. It is then moved to the segment
register. In this example, AX is used to load 3000H into DS.
(ii) Sometimes, instead of using the HLT instruction at the end, the software
interrupt instruction (INT) may be used to return the control to the monitor program
after executing the program.
Example 14.2:
Write a program to subtract the byte content of the memory location 3000H: 4000H
from the byte content of the memory location 4000H: 5000H and store the result at
the location 2000H: 3000H. Assume that the input data and the result lie between
-128 and +127, and that the negative numbers are represented in 2’s complement
form. (Note: 3000H: 4000H represents the segment address of 3000H and the
offset address 4000H in that segment).
460 MICROPROCESSORS AND MICROCONTROLLERS
Solution:
MOV BX, 3000H
MOV DS, BX ; Initialize DS with the segment address
3000H.
MOV CL, [4000H] ; Get the subtrahend from the offset address
4000H.
MOV BX, 4000H
MOV DS, BX ; Initialize DS with the segment address
4000H.
MOV AL, L5000H] ; Get the minuend at the offset address 5000H
to AL.
SUB AL, CL : AL <- AL - CL
MOV BX, 2000H
MOV DS, BX ; Initialize DS with the segment address
2000H.
MOV [3000H], AL ; Store AL at the offset address 3000H.
HLT : Stop.
Note: After the execution of the program, if the result is positive, its MSB and
carry are 0. If the result is negative, it is represented in 2’s complement form; its
MSB and carry are 1.
Example 14.3:
Write a program to move a word string 200 bytes (i.e., 100 words) long from the
offset address 1000H to the offset address 3000H in the segment 5000H.
Solution:
Using the REP prefix with the MOVSW instruction, the program size can be
reduced in comparison with a program that uses the MOV instruction for the same
task.
MOV AX, 5000H
MOV DS, AX ; Initialize DS with the segment address 5000H.
MOV ES, AX ; Initialize ES with the segment address 5000H.
MOV SI, 1000H ; Initialize SI with the offset address of the
source (i.e., 1000H).
MOV DI, 3000H ; Initialize DI with the offset address of the
destination (i.e., 3000H).
MOV CX, 100 ; Initialize CX with the numberof words in the string
(decimal value of 100 or 64H).
CLD ; Clear the D flag for auto-increment mode.
REP MOVSW ; Execute MOVSW instruction CX times.
HLT : Stop.
Note:
(i) The MOVSB instruction can be used in this program instead of MOVSW, but
CX must be loaded with the value 200.
(ii) As D is 0, every time MOVSW is executed, the SI and DI registers are
incremented by 2, to point the next word in the string.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 461
Example 14.4:
Write a program to find the smallest word in an array of 100 words stored
sequentially in the memory, starting at the offset address 1000H in the segment
address 5000H. Store the result at the offset address 2000H in the same segment.
Solution:
MOV CX, 99 ; Initialize CX with the number of
comparisons (= 100 - 1).
MOV AX, 5000H
MOV OS, AX : Initialize DS with the segment address
5000H.
MOV SI, 1000H ; Initialize SI with the offset address
1000H.
MOV AX, [SI] : Move the first word to AX.
START: INC SI
INC SI : Increment SI twice to point the next
word.
CMP AX, [SI] ; Compare the next word with the word in AX.
JC REPEAT ; If AX is smaller, jump to REPEAT.
MOV AX, [SI] ; Replace the word in AX with the smaller
word.
REPEAT: LOOP START ; Repeat the operation from START.
MOV [2000H], AX : Store the smallest number in AX at the
offset address 2000H.
HLT ; Stop.
Examples 14.5:
Write a program to find the number of positive and negative data items in an array
of 100 bytes of data stored from the memory location 3000H: 4000H. Store the
result in the offset addresses 1000H and 1001H in the same segment. Assume that
the negative numbers are represented in 2’s complement form.
(Note: The basic principle used here is that the MSB for a positive number is 0 and
that for a negative number is 1.)
Solution:
MOV AX, 3000H
MOV DS, AX ; Initialize DS with 3000H.
MOV CX, 100 ; Move the number of data items to CX.
MOV BX, 4000H ; Move the starting offset address of the
array to BX.
MOV DH, OOH ; Initialize DH with OOH to store the number of
positive data items.
MOV DL, OOH ; Initialize DL with OOH to store the number of
negative data items.
L2: MOV AL, [BX] ; Move a byte data from the array to AL.
ROL AL, 01 ; Rotate AL left by one bit. Now the MSB in AL
goes to the carry flag and also to the LSB of
AL.
462 MICROPROCESSORS AND MICROCONTROLLERS
Example 14.6:
Write a program to find the seven-segment code of a digit between 0 and 9 or a
character between A and F. Assume that the seven-segment code of the characters
is stored in the memory starting at the address 2000H: 1000H. The result must be
stored at the offset address 2000H in the same segment.
Solution:
MOV AX, 2000H
MOV DS, AX ; Initialize DS with the value 2000H.
MOV BX, 1000H : Initialize BX with the starting offset address
of the table containing the seven - segment
codes.
MOV AL, 03 ; Load the number (here ‘3’) whose seven - segment
code is to be found in AL.
XLAT ; Using XLAT instruction, move the seven
segment code of 03 to AL.
MOV [2000H], AL ; Store the result at the offset address
2000H.
HLT ; Stop.
Memory Address
Seven-segment code of 0 2000H: 1OOOH
Seven-segment code of 1 2000H: 1001H
Seven-segment code of 2 2000H: 1002H
Note: When the XLAT instruction is executed in this example, the content of BX
(= 1000H) is added to the content of AL (= 03H) to form an offset address (=
1003H) and the data in that offset address (seven-segment code of 03H) is moved
to AL. This technique is called look-up table technique.
Example 14.7:
Write a program to convert the 8-bit packed BCD number stored in the memory
location 3000H : 2000H into a binary number and store it in the offset address
2001H in the same segment.
Solution:
MOV AX, 3000H
MOV DS, AX ; Initialize DS with 3000H.
MOV AL, E2000HJ ; Move the 8-bit BCD number to AL.
MOV BL, AL ; Store a copy of the BCD number in BL.
AND AL, OFOH ; Mask the lower-order nibble in AL.
MOV CL, 04
ROR AL, CL ; Rotate AL right four times, to get the upper
nibble of the BCD number.
MOV BH, OAH ; Move OAH to BH.
MUL BH ; Multiply AL and BH, and store the result in
AL.
AND BL, OFH ; Mask the upper nibble in BL.
ADD AL, BL ; Add the contents of AL and BL.
MOV [2001H], AL ; Store the result in AL at the offset address
2001H.
HLT ; Stop.
Note:
(i) When the most significant digit of a hexadecimal data is any one of the digits
between A and F, it is preceded by 0, while writing in the program.
(ii) The binary number corresponding to an 8-bit packed BCD number is obtained
by multiplying the decimal value 10 (= OAH) with the upper digit of the BCD
number and adding the result with the lower digit of the BCD number. Since the
maximum 8-bit BCD number is 99 and the corresponding binary number is 63H
(= 9 x OAH + 9), the result in this program is also 8 bits. The result in AH is ignored
in the MUL BH instruction in this program, as AH = OOH after multiplication.
Example 14.8:
Write a program to convert the given 8-bit binary number into ASCII codes. The 8-
bit binary number is present in the memory location 2000H: 5000H and the result
is to be stored at the offset addresses 1000H and 1001H in the same segment.
Solution:
MOV AX, 2000H
MOV DS, AX : Initialize DS with 2000H.
MOV AL, [5000H] ; Move the binary data to AL.
MOV BL, AL ; Save a ropy of AL in BL.
464 MICROPROCESSORS AND MICROCONTROLLERS
Note: The ASCII code of the 8-bit binary number, say F8H, is obtained by first
splitting the binary number into two digits, F and 8, and then finding the ASCII
codes of F and 8 separately. The ASCII code of a digit between 0 and 9 is obtained
by adding 30H to the digit and the ASCII code of a digit between A and F is
obtained by adding 37H to the digit. The ASCII codes of F and 8 are 46H and
38H, respectively.
Example 14.9:
Write a program to add the two BCD data 29H and 98H and store the result in
BCD form in the memory locations 2000H: 3000H and 2000H: 3001H.
Solution:
MOV AL, 29H ; Move the first BCD data to AL.
ADD AL, 98H ; Add the second BCD data with AL.
DAA ; Decimal-adjust AL to get the result in
BCD form.
MOV BX, 2000H
MOV DS, BX ; Initialize DS with 2000H.
MOV E3000H], AL; Store the content of AL, which is the lower
byte of the result in the memory.
JC LI ; If the carry flag is 1, go to LI.
MOV E3001HJ, OOH ; Store OOH in the memory, since the carry is
0.
JMP L2 ; Go to L2.
LI: MOV E3001H], 01H Store 01H in memory, since the carry is 1.
L2: HLT ; Stop.
Example 14.10:
Write a program to convert the 8-bit binary number FFH into a BCD number. The
result is to be stored at memory locations 3000H: 2000H and 3000H: 2OO1H.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 465
Solution:
MOV AX, OOFFH : Move the data FFH to AX, with the upper byte
as OOH.
MOV BL, 100 ; Store the decimal value 100 (or 64H) in BL.
DIV BL ; Divide AX by BL to find the number of hundreds
in the binary number.
MOV DL, AL ; Move the quotient in AL (number of hundreds)
to DL.
MOV AL, AH ; Move the remainder in AH to AL.
MOV AH, 00 ; Clear AH.
MOV BL, 10 ; Store the decimal value 10 (or OAH) in BL.
DIV BL ; Divide AX by BL to find the number of tens in
the binary number. AH has the remainder, which
is the number of ones in the binary number.
MOV CL, 04
ROR AL, CL ; Rotate content of AL right by 4 bits to
place number of tens in upper nibble of AL.
OR AL, AH ; Perform OR operation on AL and AH to
concatenate the number of tens and ones.
MOV BX, 3000H
MOV DS, BX : Initialize DS with 3000H.
MOV L2000H], DL ; Move the value of DL to the memory.
MOV E2001H], AL : Move the value of AL to the memory.
HLT ; Stop.
Note: The binary number FFH when converted to BCD gives the result 255, as
there are two hundreds, five tens, and five ones in it. In this program, 02H is stored
in the offset address 2000H and 55H is stored in the offset address 2001H in the
data segment.
In addition, there are a few operators that perform the addition or subtraction
operation on constants or labels. The assembler directives commonly used in
Microsoft Macro Assembler or Turbo Assembler are as follows:
Assembler Directives for Variable and Constant Definition
The assember directives for variable and constant definition are as follows:
(i) DB, DW, DD, DQ, and DT: The directives DB (define byte), DW (define word),
DD (define double word), DQ (define quad word), and DT (define ten bytes) are
used to reserve one byte, one word (i.e., 2 bytes), one double word (i.e., 2 words),
one quad word (i.e., 4 words), and ten bytes in the memory, respectively, for
storing constants, variables, or strings.
Example:
(a) DATA1 DB 20H ; Reserve one byte for storing
DATA1 and assign the value 20H
to it.
(b) ARRAY1 DB 10H, 20H, 30H ; Reserve three bytes for storing
ARRAY1 and initialize it with
the values 10H, 20H, and 30H.
(c) CITY DB “MADURAI” ; Store the ASCII code of the
characters specified within
double quotes in the array or
list named CITY.
(d) DATA2 DW 1020H ; Reserve one word for storing
DATA2 and assign the value
1020H to it.
(e) ARRAY2 DW 1030H, 2000H,
3000H, 4000H : Reserve four words for storing
ARRAY2 and initialize them with
the specified values.
(f) DATA3 DD 1234ABCDH ; Initialize DATA3 as a double
word with 123ABCDH.
(g) DATA4 DQ 1234ABCD5678EFBBH ; Initialize DATA4 as a quad word
with 1234ABCD5678EFBBH.
(h) DATA5 DT 123456789ABCDEFl2345H; Initialize DATA5 as a series
of 10 bytes having the value
123456789ABCDE Fl2345H.
The directive DUP (duplicate) is used to reserve a series of bytes, words,
double words, or ten bytes and is used with DB, DW, DD, and DT, respectively.
The reserved area can be either filled with a specific value or left uninitialized.
Example:
(a) Array DB 20 DUP (0) ; Reserves 20 .bytes in the memory
for the array named ARRAY and
initializes all the elements
of the array to 0 (due to the
presence of 0 within the bracket
near the DUP directive).
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 467
(ii) EQU: The directive EQU (equivalent) is used to assign a value to a data
name.
Example:
(a) NUMBER EQU 50H ; Assign the value 50H to NUMBER.
(b) NAME EQU “RAMESH” ; Assign the string “RAMESH” to NAME.
Example:
The EVEN directive can also be used at the beginning of a procedure, so that the
instructions in it can be fetched quickly by the 8086 during execution.
EVEN
RESULT PROC NEAR
; instructions in the RESULT procedure
RESULT ENDP
Here the procedure RESULT, which is of type NEAR, is stored starting at an
even address in the code segment. The ENDP directive indicates the end of the
RESULT procedure.
(iii) LENGTH: This directive is used to determine the length of an array or string
in bytes.
Example:
MOV CX, LENGTH ARRAY
CX is loaded with the number of bytes in the ARRAY.
(iv) OFFSET: This operator is used to determine the offset of a data item in a
segment containing it.
Example:
MOV BX, OFFSET TABLE
If the data item named TABLE is present in the data segment, this statement places
the offset address of TABLE, in the BX register.
(v) LABEL: The LABEL directive is used to assign a name to the current value
in the location counter. It is used to specify the destination of the branch-related
instructions such as jump and call. When LABEL is used to specify the destination,
it is necessary to specify whether it is NEAR or FAR. When the destination is in
the same segment, the label is specified as NEAR and when the destination is in
another segment, it is specified as FAR.
Example:
REPEAT LABEL NEAR
CALCULATE LABEL FAR
LABEL can also be used to specify a data item. When it is used to specify a
data item, the type of the data item must be specified. The data may have the type
—byte or word.
Example:
A stack segment having 100 words of data is defined using the following
statements:
STACK SEGMENT
DW 100 DUP (0)
STACK_T0P LABEL WORD ; reserve 100 words for stack
STACK ENDS
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 469
The second statement reserves 100 words in the stack segment and fills them
with 0. The third statement assigns the name STACK_TOP to the location present
just after the hundredth word. The offset address of this label can then be assigned
to the stack pointer in the code segment using the following statement:
MOV SP, OFFSET STACK_TOP
Assembler Directives for Segment Declaration
The assember directives for segment declaration are as follows:
(i) SEGMENT and ENDS: The SEGMENT and ENDS directives indicate the start
and end of a segment, respectively. In some cases, the segment may be assigned a
type such as PUBLIC (i.e., it can be used by other modules of the program while
linking) or GLOBAL (i.e., it can be accessed by any other module).
Large assembly language programs are usually developed as separate assembly
modules. Each assembly module is individually assembled, tested, and debugged.
When all the assembly modules are working correctly, their object code files are
linked together to form the complete program. For the modules to link together
correctly, any segment, label, or variable name referred to in other modules must be
declared PUBLIC in the module in which it is defined. For example, the statement
DATA1 SEGMENT WORD PUBLIC makes the segment named DATA1
available to other assembly modules. Here, the term WORD is used to inform
the linker to locate the segment in the first available even address. Similarly, the
statement PUBLIC XI, X2 makes the two variables XI and X2 available to other
assembly modules. If an instruction in an assembly module refers to a variable or
label which is present in another assembly module, the assembler must be told that
it is external, using the EXTRN directive.
The GLOBAL directive can be used in place of the PUBLIC or EXTRN
directive. For a symbol or name defined in the current assembly module, the
GLOBAL directive is used to make that symbol or name available to other
assembly modules. For example, the statement GLOBAL MULTIPLIER makes
the variable MULTIPLIER public so that it can be accessed from other assembly
modules. The statement GLOBAL MULTIPLIER: WORD informs the assembler
that MULTIPLIER is a variable of type ‘word’, which is in another assembly
module.
Example:
CODEI SEGMENT
; instructions of CODE 1 segment
CODEI ENDS
This example indicates the declaration of a code segment named CODEI.
(ii) ASSUME: The ASSUME directive is used to inform the assembler, the name of
the logical segments to be assumed for different segments used in the program.
Example:
ASSUME CS: CODEI , DS: DATA1
470 MICROPROCESSORS AND MICROCONTROLLERS
This statement informs the assembler that the segment address where the logical
segments CODEI and DATA1 are loaded in memory during execution is to be
stored in the CS and DS registers, respectively.
(iii) GROUP: This directive is used to form a logical group of segments with a
similar purpose. The assembler passes information to the linker/loader to form
the code, such that the group declared segments or operands lie within a 64 KB
memory segment. All such segments can be addressed using the same segment
address.
Example:
PROGRAMI GROUP CODEI. DATA1, STACK1
This statement directs the loader/linker to prepare an executable (EXE) file such
that the CODEI, DATA1, and STACK 1 segments lie within a 64 KB memory
segment that is named PROGRAM 1. Now, for the ASSUME statement, we can use
the label PROGRAMI rather than CODEI, DATA1, and STACK1, as follows:
ASSUME CS: PROGRAMI, DS: PROGRAMI, SS: PROGRAMI
(iv) SEG: The segment operator is used to decide the segment address of the label,
variable, or procedure and substitute the segment address in place of the SEG
label.
Example:
MOV AX, SEG ARRAY1 ; Load the segment address in which ARRAY1 is
present, in AX.
MOV DS, AX ; Move the content of AX to DS.
Assembler Directives for Declaring Procedures
The assember directives for declaring procedures are as follows:
(i) PROC: The PROC directive indicates the start of a named procedure. The
NEAR and FAR directives specify the type of the procedure.
Example:
SQUARE_R00T PROC NEAR
This statement indicates the beginning of a procedure named SQUARE_ROOT,
which is to be called by a program located in the same segment. The FAR directive
is used for the procedures to be called by the programs present in code segments
other than the one in which this procedure is present. For example, SALARY
PROC FAR indicates the beginning of a FAR type procedure named SALARY.
(ii) ENDP: The ENDP directive is used to indicate the end of a procedure. To
mark the end of a particular procedure, the name of the procedure may appear as
a prefix with the directive ENDP.
Example:
SALARY PROC NEAR
: code of SALARY procedure
SALARY ENDP
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 471
(iii) EXTRN and PUBLIC: The directive EXTRN (external) informs the assembler
that the procedures, label/labels, and names declared after this directive has/have
already been defined in some other segments and in the segments where they
actually appear, they must be declared public, using the PUBLIC directive.
Example:
M0DULE1 SEGMENT
PUBLIC SQUARE-ROOT
SQUARE-ROOT PROC FAR
; code of SQUARE-ROOT procedure
SQUARE-ROOT ENDP
M0DULE1 ENDS
M0DULE2 SEGMENT
EXTRN SQUARE-ROOT FAR
; code of M0DULE2
CALL SQUARE-ROOT
M0DULE2 ENDS
If one wants to call the procedure named SQUARE_ROOT appearing in
MODULE 1 from MODULE2, it must be declared public using the statement
PUBLIC SQUARE-ROOT in MODULE1 and it must be declared external using
the statement EXTRN SQUARE-ROOT in MODULE2. If a jump or call address
is external, it must be represented as NEAR or FAR. If data are defined as external,
their size must be represented as BYTE, WORD, or DWORD.
Other Assembler Directives
(i) PTR: The PTR (pointer) operator is used to declare the type of a label, variable,
or memory operand. The operator PTR is prefixed by either BYTE or WORD. If
the prefix is BYTE, the particular label, variable, or memory operand is treated as
an 8-bit quantity, while if the prefix is WORD, it is treated as a 16-bit quantity.
Example:
(a) INC BYTE PTR LSI] ; Increment the byte contents of the
memory location addressed by SI.
(b) INC WORD PTR LBX] ; Increment the word contents of the
memory location addressed by BX.
The PTR directive is also used to declare a label either as FAR or NEAR type.
The FAR PTR directive indicates to the assembler that the label following FAR
PTR is not available within the same segment and the address of the label is of size
32 bits (2 bytes offset, followed by 2 bytes segment address).
Example:
(a) JMP FAR PTR DIVIDE
472 MICROPROCESSORS AND MICROCONTROLLERS
Example:
The following statement declares the variables DATA1, DATA2, and ARRAY1
as GLOBAL variables.
GLOBAL DATA1, DATA2, ARRAY1
(iv) NAME: The NAME directive is used to assign a name to an assembly language
program module. The module may now be referred to by its declared name. The
names, if selected properly, may indicate the function of the different modules,
and hence help in good documentation.
(v) SHORT: The SHORT operator indicates to the assembler that only one byte is
required to code the displacement for a jump (i.e., the displacement is within -128
to +127 bytes from the address of the byte present next to the JMP opcode). This
method of specifying the jump address saves memory. Otherwise, the assembler
may reserve 2 bytes for the displacement in the jump instructions.
Example:
JMP SHORT MULTIPLY
where MULTIPLY is a label.
(vi) TYPE: The TYPE operator directs the assembler to decide the data type of
the specified label and replaces the TYPE label with the decided data type. For the
word type variable, the data type is 2. For the double word type, it is 4, and for the
byte type, it i® 1.
Example:
If DATA 1 is an array having word type data, the instruction MOV BX, TYPE DATA1
moves the value 0002H to BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 473
After the cross reference file name is entered, the assembly process starts. If the
program contains syntax errors, they are displayed using the error code number
and the corresponding line numbers at which the errors have occurred. Once
these errors are corrected by the programmer, the assembly process is completed
successfully.
The DOS linking program LINK.EXE is used to link the different object
modules of a source program and the function library routines to produce an
integrated executable code for the source program. The linker is invoked using the
following command:
C :\> LINK MSI.OBJ
After entering this command, the linker asks for the name of the following
files:
Run file [.EXE]:
List files [NUL.MAP]:
Libraries [LIB]:
If no file names are entered for these files, by default, the source file name is
considered. The optional input ‘Libraries’ expects the name of a special library (if
any) from which the functions were used by the source program. The output of the
linker program is an executable file with either the file name entered by the user or
the default file name, and .EXE extension. The executable file name can be entered
at the DOS prompt to execute the file as follows:
C :\> MSI.EXE
In the advanced version of MASM, both assembling and linking are combined
under a single menu-invocable compile function.
DEBUG.com is a DOS utility program that is used for debuggihg and
troubleshooting 8086 assembly language programs. The DEBUG utility enables
us to have control over the hardware resources and the memory in the computer
(PC) up to a certain extent, as the PC uses one of the INTEL processors (80486,
Pentium, etc.) as the CPU. DEBUG enables us to use the PC as a low-level 8086
microprocessor kit. Typing the DEBUG command at the DOS prompt and pressing
the enter key invokes the debugging facility. A (dash) appears DEBUG is
successfully invoked, as follows:
C :\> DEBUG
Now, by typing ‘R’ at the line and pressing the enter key, we can see
the content of the different registers and flags present in the CPU of the PC, as
follows:
-R
AX = 0000H BX = 0005H CX = OOODH DX = SOOOH
SP = 8500H BP = 9800H SI = 2000H DI = 7000H
DS = SOOOH ES = 3000H SS = 4000H CS = 2000H
IP = 2000H FLAGS = 0024H
7'he remaining DEBUG commands can be referred to from any book that
discusses assembly language programming in personal computers. In this section,
476 MICROPROCESSORS AND MICROCONTROLLERS
a few examples for writing 8086 assembly language programs while using an
assembler are given.
Example 14.11:
Write a program to add two 8-bit data (FOH and 50H) in the 8086 and to store the
result in the memory, when the assembler is used.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT ; Beginning of data segment
0PER1 DB FOH ; First operand
0PER2 DB 50H ; Second operand
RESULT DB 01 DUP (?) : A byte of memory is reserved for the
result.
CARRY DB 01 DUP (?) A byte is reserved for storing the
ca rry.
DATA ENDS End of data segment.
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET ARRAY : Move the offset of the array to SI.
MOV CL, COUNT : Load COUNT in CL.
DEC CL : Decrement CL as the number of
comparisons is one less than the
count.
MOV AX, ESI] : Move the first word to AX.
AGAIN: ADD SI, 02 : Add 2 to SI to get the next word.
MOV BX, [SI] : Move the next word to BX.
CMP AX, BX : Compare the word in AX with BX.
JC NEXT : If AX i s sma11, go to NEXT.
MOV AX, BX : Move the small word in BX to AX.
478 MICROPROCESSORS AND MICROCONTROLLERS
Example 14.13:
Write a program to find the number of even and odd data bytes present in the given
array having one hundred byte type data.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DB 40H, 31H, 23H, 52H... ; Enter all the data items of ARRAY
here.
COUNT EQU 64H ; Initialize COUNT with the value 100.
EVEN_NOS DB OOH ; Reserve a byte for storing the number
of even data items.
ODD_NOS DB OOH : Reserve a byte for storing the number
of odd data items.
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV BL, OOH ; Initialize BL with OOH, to store
the number of even data items.
MOV DL, OOH ; Initialize DL with OOH, to store the
number of odd data items.
MOV CL, COUNT ; Initialize CL with COUNT.
MOV SI, OFFSET ARRAY ; Move the offset address of ARRAY to
the SI register.
AGAIN: MOV AL, [SI] ; Move one byte from ARRAY to AL.
RCR AL, 1 ; Rotate AL right through the carry
by 1 bit.
JC ODD ; If carry = 1, the number is odd. So go
to ODD.
INC BL ; Otherwise, the number is even and
hence increment BL.
JMP LI ; Jump to LI.
ODD: INC DL ; Increment DL by 1 as the number is
odd.
LI: INC SI ; Increment SI to point to the next
data.
LOOP AGAIN ; Go to AGAIN, CL times.
MOV EVEN-NOS, BL ; Store the content of BL in EVEN_NOS.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 479
Example 14.14:
Write a program to arrange the given array having ten word type data in ascending
order.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DW 1234H , 4050H, 0035H... ; Enter all the data items of ARRAY
here.
COUNT EQU OAH
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CL, COUNT ; Load the number of data items in
CL.
DEC CL ; Decrement CL as the number of
passes is one less than the
number of data.
NEXT_PASS: MOV BL, CL ; Initialize BL with the number of
comparisons to be done in each
pass.
MOV SI, OFFSET ARRAY : Move the offset address of ARRAY to
the SI register.
COMPARE: MOV AX, [SI] ; Move one data item to AX.
ADD SI, 02H ; Add 02 to SI to point to the next
data.
CMP AX, [SI] ; Compare the next data with the
content of AX.
JC LI ; If the first data item is lesser
than the second, go to Ll.
XCHG AX, [SI] ; Otherwise, exchange the data in
AX and the memory.
SUB SI, 02H ; Subtract 02 from SI to point to
the previous memory location.
MOV [SI], AX ; Store the content of AX (smaller
data) in the memory.
ADD SI, 02 ; Increment SI by 2 to compare the
next data with AX.
Ll; DEC BL : Decrement the number of comparisons
in BL by 1.
480 MICROPROCESSORS AND MICROCONTROLLERS
The algorithm used here is explained with the following simple example. Let
us consider arranging four words stored in an array in ascending order. Since there
are 4 (= N) words, 3 (= N - 1) passes have to be done. In the first pass, 3 (= N - 1)
comparisons are made and the highest number is brought to the end of the array. In
the second pass, 2 (= N - 2) comparisons are made since only the top three words
of the array need to be compared and in the third pass, only one comparison is
needed to compare the first two data in the array.
Let us assume that the data in the array is as follows:
3200H
4F35H
2350H
1FC2H
The comparisons done in each pass and the exchange of data for arranging
them in ascending order are shown here:
PASS I:
3200H <— 3200H 3200H 3200H
4F35H । 4F35H <-। 2350H 2350H
2350H 2350H <- 4F35H <-] 1FC2H
1FC2H 1FC2H 1FC2H 4F35H
PASS II:
3200H 2350H 2350H
2350H 3200H <-1 1FC2H
1FC2H 1FC2H ' 3200H
4F35H 4F35H 4F35H
PASS III:
2350H IFC2H
1FC2H 2350H
3200H 3200H
4 F35H 4F35H
Sorted array
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 481
POINTS TO REMEMBER
____________________________ V
• The addressing modes in the 8086 are classified as register, immediate, data memory,
stack memory, and program memory addressing modes.
• The data memory addressing modes are classified as direct, base, index, base plus
indexed, base-relative, index-relative, and base-relative plus index addressing modes.
• The program memory addressing modes are classified as direct, relative, and indirect
addressing modes.
■ The 8086 instructions are classified as data transfer, arithmetic, logical, shift/rotate, flag
manipulation, control transfer, string, and machine control instructions.
■ The assembly language programming of the 8086 can be done with a line assembler or
an assembler.
■ Assembler directives are used while writing an assembly language program that is to be
assembled by using an assembler.
KEY TERMS
Addressing mode This mode is the way in which the microprocessor addresses the
operands while fetching data during the execution of an instruction or the way in which
the microprocessor calculates the memory address from where the next instruction to be
executed is taken, in the case of jump or call instructions.
Assembler It is a software that is used to convert assembly language programs into
machine language programs.
Assembler directives These are commands to the assembler, which give various details
in a program such as the required storage class for a particular constant or variable (byte,
word, or double word), logical name of the segments (CODE, STACK, or DATA segment),
type of procedures or routines (FAR, NEAR, PUBLIC, or EXTRN), end of a segment
(ENDS), and macro definition (MACRO, ENDM).
Inter-segment jump This refers to the operation of jumping from one code segment to
another.
Intra-segment jump This refers to the operation of jumping within the same code
segment.
Line assembler It converts each line in an assembly language program into the
corresponding machine language program, as soon as it is entered in the system.
REVIEW QUESTIONS
8. Write the operation performed by the 8086 when it executes the XLAT instruction.
What is the use of XLAT?
9. What is the difference between fixed port and variable port addressing in the 8086?
10. Which instructions of the 8086 are used to communicate with the I/O devices in the
I/O-mapped I/O scheme?
11. Write the function of the assembler directives BYTE PTR and WORD PTR.
12. What is the difference between the MUL and IMUL instructions in the 8086?
13. What is the difference between the DIV and IDIV instructions in the 8086?
14. What are the default operand and result locations for 8- and 16-bit data multiplication
instructions in the 8086?
15. What are the default operand and result locations for 8- and 16-bit data division
instructions in the 8086?
16. What is the function of the DAA instruction in the 8086?
17. Write the operations performed when the instruction AAD is executed in the 8086.
18. Which instructions of the 8086 are used to set and reset the D and I flags?
19. What is the range of the relative address that is used in the conditional jump
instructions?
20. What is the function of the INT n instruction? Which instruction of the 8086 is used to
return from the interrupt service routine to the main program?
21. What are the operations performed when the instructions LOOP and LOOPNE are
executed in the 8086?
22. What is the function of the D and I flags in the 8086?
23. Which registers are used as offset registers and segment registers for pointing to the
source and destination during the execution of the string instructions in the 8086?
24. What is the function of the REP and REPE prefixes used with string instructions in the
8086?
25. What is the function of the LOCK prefix used with an 8086 instruction?
26. What is the function of the assembler and assembler directives?
27. What is the function of the assembler directives ORG and DB?
28. What is a macro? Give an example.
29. What is the difference between a macro and a subroutine?
30. What is the need for passing parameters to a macro?
31. Describe the different data memory addressing modes in the 8086 giving an example
for each.
32. Describe the different program memory addressing modes in the 8086 giving an
example for each.
33. Explain the stack memory addressing modes in the 8086 giving examples.
34. Explain the different data transfer instructions in the 8086 giving examples for each.
35. Explain the different arithmetic instructions in the 8086 giving examples for each.
36. Describe the different logical instructions in the 8086 giving examples for each.
37. Write the assembler directives that are used to define variables and constants, with an
example for each.
38. What are the assembler directives that are related to segment declaration? Explain
with examples.
39. Write the function of assembler directives that are related to code location, with an
example for each.
40. What are the assembler directives that are related to procedure declaration? Explain
with examples.
41. Explain the function of the assembler directives PTR, TYPE, SHORT, GLOBAL, and
LOCAL with an example for each.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 483
1. Let the content of the different registers in the 8086 be as follows: DS = 1000H,
SS = 2000H, ES = 3000H, BX = 4000H, SI = 5000H, DI = 6000H, and BP = 7000H.
Find the memory address/addresses from where the 8086 accesses the data while
executing the following instructions:
(i) MOVAX, [BX]
(ii) MOVBX, [SI]
(iii) MOV CX, [BP]
(iv) MOV AL, [DI]
(v) MOV BH, SS: [SI]
(vi) MOV CX, ES: [DI]
(vii) MOV AX, [BX + DI]
(viii) MOV BX, [BP + DI + 5]
(ix) MOV AH, [BX + 10H]
(x) MOV CX, DS: [BP + 4]
(xi) MOVBX, [SI-5]
(xii) MOV AX, [BX + 10]
2. Which registers of the 8086 are modified while executing inter-segment and intra
segmentjump instructions?
3. Is it possible to exchange the content of two memory locations or the content of two
segment registers using the XCHG instruction? Why?
4. If the content of BP = 1000H and SI = 2000H, what is the value present in CX after
the 8086 executes the instructions LEA CX, [BP + SI], and LEA CX, [SI].
5. Is it possible to use two memory operands in the ADD and SUB instructions?
6. Is the carry flag affected by the execution of the INC and DEC instructions in the
8086?
7. What is the difference between SUB and CMP instructions?
8. What is the difference between TEST and AND instructions?
9. Which instructions of the 8086 are used to handle procedure or subroutine?
10. What is the difference between arithmetic and logical right-shift?
11. What are the common applications of left-shift and right-shift operations?
12. When is the CL register used with the shift and rotate instructions?
13. Consider the following pair of partial programs:
(i) MOV AX, 4000H (ii) MOV AX, 4000H
ADD AX, AX ADD AX, AX
ADC AX, AX RCL AX, 1
JZ DOWN JZ DOWN
For each case, what is the data in AX after execution of the third instruction and
from where does the processor fetch the next instruction after execution of the fourth
instruction?
14. How is the WAIT instruction used to coordinate the operation between the 8086 and
the 8087?
484 MICROPROCESSORS AND MICROCONTROLLERS
PROGRAMMING EXERCISES
1. Write an 8086 assembly language program to find the sum of 100 words present in an
array stored from the address 3000H: 1000H in the data segment and store the result
from the address 3000H: 2000H.
2. Write an 8086 assembly language program to find the prime numbers among 100
bytes of data in an array stored from the address 4000H: 1000H in the data segment
and store the result from the address 4000H: 3000H.
3. Write an 8086 assembly language program to find the number of occurrences of the
character ‘A’ among 50 characters of a string type data stored from the address 5000H:
1000H in the data segment and store the result in the address 2000H: 5000H.
4. Write an 8086 assembly language program to check whether the two strings, one
stored from the address 2000H: 1000H in the data segment and the other stored from
the address 2000H: 3000H are equal or not. If they are equal, store the value OOH in
AL. Otherwise, store the value 01H in AL.
5. Write an 8086 assembly language program to find the number of bytes that have the
hexadecimal digit ‘F’ in their upper nibble among 100 bytes of data in an array stored
from the address 8000H: 1000H in the data segment. Store the result in the address
8000H: 3000H.
6. Write an 8086 assembly language program to complement the lower nibble of each
byte in 100 bytes of data in an array stored from the address 8000H: 1000H in the data
segment. Store the result from the address 8000H: 3000H.
7. Write an 8086 assembly language program to add two matrices having word type data
in each element of the matrix. Assume that each element of the result after addition
of the corresponding elements of the matrix is also word type data. The data for one
matrix is present in an array stored from the address 8000H: 1000H in the data segment
and the corresponding data for another matrix is present in an array stored from the
address 8000H: 3000H in the data segment. The result is to be stored from the address
7000H: 1000H.
8. Write an 8086 assembly language program to multiply two square matrices having
byte type data in each element of the matrix. Assume that each element of the resultant
matrix is of word type. The data for one matrix is present in an array stored from
the address 8000H: 1000H in the data segment and the corresponding data for the
other matrix is present in an array stored from the address 8000H: 3000H in the data
segment. The result is to be stored from the address 7000H: 1000H.
9. Write an 8086 assembly language program to find the factorial of the given byte of
data using a recursive algorithm. The result is to be stored in the address 7000H:
1000H.
10. Write a non-recursive assembly language subroutine for the 8086, to evaluate the
number
Fn = Fn'l + Fn 2 for giVCn D > 1
given that Fo = 0 and F, = 1. Consider the number n to be such that Fn is not more than
a 16-bit number.
11. Solve problem 1 assuming that the program is to be assembled by an assembler.
12. Solve problem 7 assuming that the program is to be assembled by an assembler.
13. Solve problem 10 assuming that the program is to be assembled by an assembler.
CHAPTER 15]
8086 INTERRUPTS
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Different types of interrupts in the 8086, such as hardware and software interrupts
• Processing of an interrupt by the 8086
• Interrupt vector table and interrupt vectors in the 8086
• Functions of the different interrupts in the 8086
• Priority among the interrupts in the 8086
• Writing interrupt service routines
• A few BIOS (basic input/output system) interrupts or function calls
15.1 INTRODUCTION
The 8086 allows normal program execution to be interrupted in one of the
following ways:
(i) An external signal given through one of its interrupt pins (INTR/NMI)
(ii) A special instruction in the program, such as the software interrupt
instruction (INT N)
(iii) The occurrence of an error condition such as divide-by-0
(iv) A trap interrupt
After receiving the interrupt, the microprocessor stops the execution of the
current program and calls a procedure called interrupt service routine (ISR), which
services that interrupt. The IRET instruction executed at the end of the interrupt
service routine returns the execution to the interrupted program.
Now, let us see in detail how the 8086 does an indirect far jump to the start
of the ISR of the received interrupt. When the 8086 responds to an interrupt, it
refers to four memory locations present in the interrupt vector table (IVT), to get
the new values of CS and IP. These memory locations are used to find the starting
address of the ISR of the received interrupt in the memory. In an 8086 system,
the first 1 KB of memory from the addresses 00000H-003FFH is set aside as a
table called interrupt vector table (IVT) for storing the interrupt vector (IV). Each
interrupt vector indicates the starting address of the ISR of a particular interrupt
in the memory. It contains four bytes, in which the lower two bytes are called
offset and the upper two bytes are called segment. The offset part of the interrupt
vector is loaded in the IP register and the segment part is loaded in the CS register.
While using interrupts in the 8086, the ISR of the different interrupts must be
initially stored in the memory at the desired locations. Then the interrupt vectors
corresponding to the various interrupts are stored in the IVT. For example, if the
ISR of interrupt type 0 is stored in the memory starting at the address 30000H,
8086 INTERRUPTS 487
the segment part of the interrupt vector is entered as 3000H and its offset part is
entered as 0000H in the IVT. When these two values are loaded in the CS and
IP registers, respectively, the 8086 calculates the address of the next instruction
to be executed using the relation CS x 10H + IP, and obtains 30000H, which is
the starting address of the ISR of interrupt type 0. Since four bytes are required
to store the CS and IP values for each ISR in the IVT, and the IVT must hold the
interrupt vector for a maximum of 256 interrupts, the maximum size of the IVT
is 1 KB. Each interrupt vector is also called interrupt pointer and the IVT is also
referred to as the interrupt pointer table.
Figure 15.2 shows the 256 interrupt vectors arranged in the IVT in the
memory. The IP value
Address
is inserted as the lower-
order word of the 003FFH Type FFH vector (available)
interrupt vector and the
Available interrupt
CS value is inserted as vectors (224) 003FCH
the higher-order word Type 21H vector (available)
In general, when the 8086 executes the INT n instruction where n is the
interrupt type, the 8086 pushes the content of the flag register, CS, and IP values
into the stack register, and clears IF and TF. Then the 8086 goes to the memory
address (given by 4 x n) to obtain the interrupt vector for the type n from the IVT
and loads it in the IP and CS registers. This makes the 8086 execute the ISR for the
interrupt type n. The IRET instruction at the end of the ISR makes the 8086 return
to the main program to the instruction next to the INT n instruction, to continue
the execution of the main program.
Software interrupts produced by the INT instruction have the following uses:
(i) Inserting break points in a program for debugging. The INT 03H instruction
is used for this purpose.
(ii) Testing the function correctness of various ISRs. For example, the INT 02H
instruction can be used to test the ISR for the NMI interrupt, without giving
any input signal to the NMI pin of the 8086.
Figure 15.4 shows the simplified diagram for interfacing the 8259 with the 8086.
When the 8259 receives an interrupt signal on one of its IR inputs (IR0-IR7), it
sends an interrupt signal (INT) to the INTR input of the 8086. If the INTR interrupt
is enabled (in the 8086) by setting IF, the 8086 responds as shown in Fig. 15.3.
Fig. 15.4 Simplified diagram of interfacing the 8259 with the 8086
The 8086 does two interrupt acknowledge cycles when it receives the INTR
interrupt. During the first acknowledgement, the 8086 floats the data bus AD 15-
AD0 and sends out an Interrupt Acknowledgement (INTA) pulse through its INTA
pin. This pulse instructs the 8259 to perform certain internal operations to get the
interrupt type related to the interrupt received by it. The interrupt type for the IR0
interrupt in the 8259 is pre-programmed in it during its initialization process. The
interrupt type for successive interrupts in the 8259 (IR1, IR2,.. .IR7) is one greater
than the interrupt type of the previous interrupt. For example, if the interrupt type
assigned to IR0 is 50H, the interrupt type assigned to IR1 is 51H, that assigned to
IR2 is 52H, and so on. During the second acknowledge cycle, the 8086 sends out
another pulse on its INTA pin. In response to this second INTA pulse, the 8259
places the interrupt type on the lower eight lines of the data bus (AD7-AD0),
which is read by the 8086. After receiving the interrupt type, the 8086 goes on to
execute the ISR of the received interrupt type. The advantage of using the 8259
with the 8086 is the ability of the 8086 to handle multiple hardware interrupts and
not merely two (INTR and NMI).
492 MICROPROCESSORS AND MICROCONTROLLERS
While using the tri-state octal buffer (IC 74244) with its inputs connected to an
8-bit DIP switch and outputs connected to the data bus (AD0-AD7), the required
interrupt type is set in the 8-bit DIP switch and the INTA signal of the 8086 is
connected to the enable inputs of the octal buffer (1G and 2G). When the 8086
receives the INTR interrupt, it makes INTA low, which enables the octal buffer
IC. The interrupt type, which was set in the 8-bit DIP switch, is now placed in the
data bus (D7-D0) and the 8086 reads it.
Example 15.1:
Figure 15.6 shows the interfacing of an ASCII keyboard with the 8086 through a
port in the 8255 having the address FFEOH. When a key is pressed on the keyboard,
the ASCII code of that key is available on its data lines (D7-D0) and the KBINT
pin is pulled low for some time. This causes the NMI input of the 8086 to go high,
thereby interrupting the 8086. In the NMI interrupt’s ISR, the ASCII code of the
key pressed can be read through the 8255.
8086 INTERRUPTS 493
Solution:
ASSUME CS: CODE, DS: DATA, SS: STACK
DATA SEGMENT WORD PUBLIC ; This segment can be accessed by
any other module.
ASC_STRING DB 50 DUP (0) : Reserve 50 bytes for storing the
ASCII codes.
ASC—POINTER DW OFFSET ASC_STRIN ; Pointer to ASCII strin g
CHR_COUNT DB 50 Assign the number of ASCII codes
to CHR_COUNT
DONE DB OOH Initialize DONE to OOH.
DATA ENDS End of the data segment.
STACK SEGMENT Set up the stack segment needed
for handling the interrupt.
DW 100 DUP (0) Reserve 100 words for the stack.
STACK_TOP LABEL WORD Assign the label STACK_TOP to the
top of the stack.
STACK ENDS End of the stack segment.
PUBLIC CHR_COUNT, DONE Make the variables available to
other modules.
EXTRN KEYBRD: FAR KEYBRD procedure (which is the NMI
ISR) is present in another module.
CODE SEGMENT WORD PUBLIC
START: MOV AX, STACK Initialize the SS register with
the segment address of the STACK.
MOV SS, AX
MOV SP, OFFSET STACK_TOP Initialize the SP register.
MOV AX, DATA Initialize the DS register with
the segment address of DATA.
494 MICROPROCESSORS AND MICROCONTROLLERS
Example 15.2:
Write a program that displays the message ‘IRQ2 IS WORKING’, in the monitor
of the personal computer (PC), if a hardware interrupt signal appears on the IRQ2
pin present in the I/O channel of the PC, and the message ‘IRQ3 IS WORKING’ if
a hardware interrupt signal appears on the IRQ3 pin present in the I/O channel of
the PC. Make use of the DOS (disk operating system) interrupt INT 21H.
Solution:
When a hardware interrupt signal appears on the IRQ2 pin present in the I/O
channel of the PC, it activates the INTR pin of the CPU (8086). When the CPU
sends the INTA pulse, the interrupt type OAH is supplied to the CPU by the I/O
channel of the PC. Hence, the effect of this action is the same as that of executing
the software instruction INT OAH. Similarly, when a hardware interrupt signal
appears at the IRQ3 pin present in the I/O channel of the PC, it activates the INTR
pin of the CPU (i.e., processor), and when the CPU sends the INTA pulse, the
interrupt type OBH is supplied to the CPU by the I/O channel of the PC. Hence,
the effect of this action is the same as that of executing the software instruction
INT OBH.
The DOS interrupt or function call INT 21H, which comes along with the DOS
program, is used for performing various functions in the PC such as accessing
the printer, monitor, and keyboard, and creating files. Before using INT 21H for
executing a specific instruction, the register AH, DX, or DS, or a combination of
these registers has to be loaded with a specific value. Now the specified operation
is carried out and a particular value is returned in specific registers or in flags, after
the execution of the INT 21H instruction, to reflect the result of the operation.
Main program:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
MESSAGE1 DB “IRQ2 IS WORKING”, OAH, ODH, ”$”
MESSAGE2 DB “IRQ3 IS WORKING”. OAH, ODH, ”$”
DATA ENDS
496 MICROPROCESSORS AND MICROCONTROLLERS
CODE SEGMENT
START:
MOV AX, CODE
MOV DS, AX ; Set DS with the segment address of
CODE, for setting the IVT.
MOV DX, OFFSET IRQ2_I SR ; Set DX with the offset of IRQ2_ISR.
MOV AX, 250AH ; Set the IVT using the function value
250AH in AX (AH = 25H, AL = OAH
(interrupt type))
INT 21H ; Call the DOS interrupt INT 21H to set
the IVT.
MOV DX, OFFSET IRQ3_ISR ; Set DX with the offset address of
IRQ3_ISR.
MOV AX, 250BH ; Set IVT using the function value 250BH
in AX (AH = 25H, AL = OBH (interrupt
type)).
INT 21H ; Call the DOS interrupt INT 21H to
set the IVT.
HERE: JMP HERE
IRQ2_ISR PROC NEAR
MOV AX, DATA
MOV DS, AX ; Set DS with the segment address of
DATA.
MOV DX, OFFSET MESSAGE1 : Set DX with the offset of MESSAGE1.
MOV AH, 09H ; Display MESSAGEl in the monitor.
INT 21H
IRET ; Return from ISR.
IRQ2_ISR ENDP
IRQ3_ISR PROC NEAR
MOV AX, DATA
MOV DS, AX ; Set DS with the segment address of
DATA.
MOV DX, OFFSET MESSAGE2 ; Set DX with the offset of MESSAGE2.
MOV AH, 09H ; Display MESSAGE2 in the monitor.
INT 21H
IRET ; Return from ISR.
IRQ3_ISR ENDP
CODE ENDS
END START
In this program, a data segment is first defined with the messages to be displayed
in the monitor of the PC when the interrupt signal is given in the I/O channel of
the PC. Then, storing the segment address of CODE in the DS, the offset address
of the ISR (IRQ2_LSR) in the DX, the function value 250AH in AX (i.e., AH =
25H and AL = OAH (interrupt type)), and by using the DOS interrupt INT 21H,
8086 INTERRUPTS 497
the interrupt vector for the interrupt type OAH is created in IVT. Similarly, the
interrupt vector for the interrupt type OBH is created in the IVT. In the IRQ2 ISR,
the segment address of DATA is placed in DS, the offset address of MESSAGE 1
is placed in DX, AH is loaded with the value 09H, and by calling the DOS interrupt
INT 21H, MESSAGE 1 is displayed in the monitor of the PC. A similar procedure
is used in IRQ3_ISR as well.
The OAH, ODH, and $ characters given in MESSAGEl and MESSAGE2
represent the ASCII code of line feed (LF), ASCII code of carriage return (CR),
and end of string, respectively.
Example 15.3:
Write a program to create a file named AGE in the PC and store 100 bytes of data
in it, which have to be taken from the memory block starting at 3000H: 2000H,
if the software instruction INT OAH is executed by the PC. Make use of the DOS
interrupt INT 21H.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
FILENAME DB “AGE”, “$”
MESSAGE DB “FILE CREATION WAS NOT SUCCESSFUL", OAH, ODH,”$”
DATA ENDS
CODE SEGMENT
START:
MOV AX, CODE
MOV DS, AX ; Set DS with the segment address of
CODE, for setting the IVT.
MOV DX, OFFSET ISR ; Set DX with the offset address of
ISR.
MOV AX, 250AH ; Set the IVT using the function value
250AH in AX.
INT 21H ; Execute the DOS interrupt INT 21H to
set the IVT.
MOV DX, OFFSET FILENAME ; Set DX with the offset address of
FILENAME.
MOV AX, DATA
MOV DS, AX ; Load the segment address of DATA in
DS.
MOV CX, OOH
MOV AH , 3CH
INT 21H ; Create a file with the file name ‘AGE’,
using INT 21H.
JNC SUCCESS ; If there is no carry, the file creation
operation was successful. So go to the
location SUCCESS.
498 MICROPROCESSORS AND MICROCONTROLLERS
In this program, a data segment is first defined with the file name to be assigned
to the file and the message to be displayed in the monitor, if the file creation is not
successful. Then, storing the segment address of CODE in DS, the offset address
of the ISR in DX, and the function value 250AH in AX (i.e., AH = 25H and AL
= OAH (interrupt type)), and by using the DOS interrupt INT 21H, the interrupt
vector for the interrupt type OAH is created in the IVT. Next, storing the offset
address of the file name in DX, the segment address of DATA in DS, OOH in CX,
and 3CH in AX, and by using the DOS interrupt INT 21H, the file named AGE is
created.
If the file creation operation is successful, the carry flag is cleared after the
execution of INT 21H and the AX register is loaded with the file handle information.
Otherwise, the carry flag is set. If the carry flag is cleared, the processor goes to the
location named SUCCESS in the program and executes the INT OAH instruction,
which causes the execution of the ISR, to store 100 bytes of data taken from the
memory block starting at 3000H: 2000H into the file. If the carry flag is set after
the execution of INT 21H, the processor executes INT 21H with DX having the
8086 INTERRUPTS 499
offset address of the message and AH having the value 09H, to display the message
‘FILE CREATION WAS NOT SUCCESSFUL’ in the monitor of the PC.
In the ISR, the file handle information in AX is first transferred to BX, followed
by the loading of CX with the number of data bytes to be stored into the file.
Then DX and DS are loaded with the offset address and the segment address,
respectively, of the memory block from where the data is to be taken. By loading
AH with the value 40H and by using INT 21H, data is moved into the file.
15.9.1 INT10H
The INT 10H BIOS interrupt, which is also called video services interrupt, directly
controls the video display in a system. INT 10H uses register AH to select the
video service provided by this interrupt. The video BIOS ROM is located on the
video board and varies from one video card to another used in the PC.
15.9.1.1 Video Mode Selection
The mode of operation for the video display is selected by placing OOH in AH,
followed by one of the mode numbers in AL. Table 15.1 shows the mode of
operation found in VGA (video graphics array) type video display systems using
standard video modes.
Table 15.1 Video display modes
The set of instructions used to place the video display in mode 2 is as follows.
After the instructions are executed in the PC, the mode of the display is changed
and the screen is cleared.
MOV AH, OOH Video mode service
MOV AL, 02H Select mode 2.
INT 10H Call BIOS interrupt.
To know the current video mode used in the display, AH is set to OFH and
INT 1OH is executed. After execution, AL has the current video mode, AH has the
number of character columns, and BH has the page number. The instructions are
as follows:
MOV AH, OFH ; Select read video mode.
INT 10H ; Call BIOS interrupt.
15.9.2 INT11H
This interrupt is used to determine the type of equipment installed in the system.
To use this interrupt, the AX register is loaded with FFFFH and then the INT
11H instruction is executed. In return, INT 11H provides information in the AX
register, as given in Fig. 15.7.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 BitO
P1 PO G S2 S1 SO D2 D1
15.9.3 INT12H
The memory size present in the system is obtained by the INT 12H interrupt.
After executing the INT 12H instruction, the AX register contains the number of
1 KB blocks of memory (conventional memory in the first 1 MB of address space)
installed in the computer.
15.9.4 INT13H
This interrupt controls diskettes that are within 5.25 or 3.5 inches in size and
also hard disk drives attached to the system. Table 15.4 shows the functions
available to this interrupt via register AH. The direct control of the hard disk drive
by a programmer using INT 13H leads to problems, including the alteration or
corruption of important programs such as operating system programs, compilers,
and other software that are stored on the disk. This may result in system failure.
Only upon reinstallation of the operating system programs in the hard disk will the
PC function normally. This wastes a lot of time for the programmer. Therefore, the
functions are listed without details about their usage. Before using these functions,
the BIOS literature available from the company that produced the particular
version of the BIOS ROM in the system should be referred to.
15.9.5 INT14H
The INT 14H interrupt controls the serial COM (communication) ports attached
to the computer. There are two COM ports—COM1 and COM2—in a computer
system. In newer AT style machines, the number of COM ports is extended to
four (including COM3 and COM4). Communication ports are normally controlled
using software packages that allow programming of microcontrollers/digital signal
processors (DSPs) serially, or by transmitting and receiving data through a modem
and a telephone line. The INT 14H instruction is used to control these ports, as given in
Table 15.5.
15.9.6 INT15H
The INT 15H interrupt controls various I/O devices interfaced with the computer.
It also allows access to protected mode operation and the extended memory system
on an 80286, Pentium Pro, etc., but it is not recommended for use by the normal
user; it is commonly used by programmers to develop OS-related programs. The
functions provided by INT 15H are given in Table 15.6.
15.9.7 INT16H
The INT 16H interrupt is used to control the keyboard in a system. This interrupt is
usually accessed by the DOS interrupt INT 21H, but can also be accessed directly.
Table 15.7 indicates the functions provided by INT 16H.
15.9.8 INT17H
The INT 17H interrupt accesses the parallel printer port, called LPT 1 in most systems.
Table 15.8 shows the functions provided by INT 17H.
8086 INTERRUPTS 503
Table 15.4 Functions provided by INT 13H Table 15.6 Functions provided by INT 15H
AH Function AH Function
OOH Reset the system disk OOH Cassette motor on
01H Read disk status to AL 01H Cassette motor off
02H Read sector 02H Read cassette
03H Write sector 03H Write cassette
04H Verify sector OFH Format ESDI periodic
05H Format track interrupt
06H Format bad track 21H Keyboard intercept
07H Format drive 80H Device open
08H Get drive parameters 81H Device closed
09H Initialize fixed disk 82H Process termination
characteristics
83H Event wait
OAH Read long sector
84H Read joystick
OBH Write long sector
85H System request key
OCH Seek
86H Delay
ODH Reset fixed disk system
87H Move extended block of
OEH Read sector buffer
memory
OFH Write sector buffer
88H Get extended memory size
1OH Get drive status
89H Enter protected mode
11H Re-calibrate drive
90H Device wait
12H Controller RAM diagnostics
91H Device power on self test
13H Controller drive diagnostics (POST)
14H Controller internal diagnostics COH Get system environment
15H Get disk type C1H Get address of extended BIOS
16H Get disk changed status data area
17H Set disk type C2H Mouse pointer
18H Set media type C3H Set watchdog timer
19H Park heads C4H Programmable opinon
1AH Format ESDI drive select
AH Function
OOH Initialize communications port
01H Send character
02H Receive character
03H Get COM port status
04H Extended initialize communications port
05H Extended communications port control
504 MICROPROCESSORS AND MICROCONTROLLERS
Table 15.7 Functions provided by INT 16H Table 15.8 Functions provided by INT 17H
AH Function AH Function
POINTS TO REMEMBER
KEY TERMS
INTR It is a maskable hardware interrupt in the 8086 that can be enabled/disabled using
the I flag.
Non-maskable interrupt (NMI) It is an interrupt that cannot be disabled by software.
Software interrupt It is an interrupt generated by the execution of the software interrupt
instruction in the microprocessor.
Trap interrupt It is used for performing single-step operations in the 8086 and can be
enabled/disabled using the T flag.
REVIEW QUESTIONS
1. For what purpose is the NMI interrupt commonly used in an 8086-based system?
2. What is the minimum duration for which the INTR signal must be kept high for being
recognized by the 8086?
506 MICROPROCESSORS AND MICROCONTROLLERS
3. Is it possible to store the IVT starting from the address 20000H in the memory of the
8086? Why?
4. If the ISR of interrupt type 0 is stored from the memory address 2000: 3000H, what is
the segment and offset part of the interrupt vector?
5. If the ISR of interrupt type 40H is stored from the memory address 8000: 4500H, what
is the segment and offset part of the interrupt vector?
6. Is it possible to enable the INTR and the trap interrupts again when the 8086 starts
executing the ISR of an interrupt? How?
7. How does the 8086 obtain the specific interrupt type when it receives the INTR
interrupt?
8. If the interrupt type allotted for the interrupt IR0 is 70H in the 8259, what is the
interrupt type allotted for IR2 and IR4?
9. How can the breakpoint technique for debugging a program be implemented in the
8086?
10. Is it possible to access the divide-by-0 ISR by using a software interrupt in the 8086?
How?
11. If both INTR and NMI occur simultaneously in the 8086, which interrupt is processed
first? Why?
PROGRAMMING EXERCISES
1. Write an 8086 ISR to add the byte type data stored in an array starting at the address
2000H: 5000H in the memory with the corresponding data in another array stored in the
memory starting at the address 3000H: 5000H and store the result in another array in
the memory starting at the address 4000H: 5000H, when the NMI interrupt is given to
the 8086. The number of byte type data in the array is 100. Assume that the result after
addition of all the data in the array is an 8-bit data. The ISR must be accessible by any
module.
2. Write an 8086 ISR to send the byte type data stored in the address 6000H: 5000H in the
memory, to port A in the 8255, whose address is FF00H, when the IRQ2 interrupt in the
I/O channel of the PC is activated.
3. Write an 8086 ISR to receive byte type data through port B of the 8255, whose address
is FF01H. Store the data in the address 7000H: 5000H in the memory, when the software
interrupt INT 0BH is executed by the PC.
CHAPTER 16
Address Address
00000H
00002H
00004H
FFFFEH
Two memory locations are needed to store a word in the memory in an 8086
system. While reading or writing word data (16 bits), the bus interface unit of the
8086 requires one or two memory cycles, depending upop whether the lower-
order byte of the word is located at an even or odd memory address, respectively.
It is better to store the word type data in the memory such that its lower-order byte
is stored at an even memory address, since only one read cycle is required to read
the data through the 16-bit data bus (D15-D0) of the 8086. If the lower-order byte
of the word is located at an odd memory address, the first read cycle is required
for accessing the lower-order byte of the word through the higher-order data bus
(D15-D8), and the second is required for accessing the higher-order byte of the
word through the lower-order data bus (D7-D0). Thus, two bus cycles are required
to access a word whose lower-order byte is stored in an odd memory address in the
memory. While initializing data structures such as an array of word type data or a
stuck, they should be initialized at an even address for efficient operation. This is
also applicable to the memory write operation.
The use of the BHE and AO signals to fetch data or instruction from the memory
and to write data in the memory is given in Table 16.1.
Table 16.1 Function of BHE and AO signals
BHE AO Operation
0 0 16-bit data is read from or written into the memory.
0 1 8-bit data is read from or written into the odd memory bank.
1 0 8-bit data is read from or written into the even memory bank.
1 1 Memory is not accessed.
The BHE and AO signals, along with a few higher-order address lines of the
8086, are used to generate the Chip Select (CS) or Chip Enable (CE) signal for
different memory chips.
Fig. 16.3 Buffering the data bus of the 8086 using IC 74245
If DEN is low, it indicates that the data is available on the multiplexed address/
data bus (ADO-AD 15). Both the bidirectional buffers (74245s) are enabled to
transfer that data since their enable inputs are activated at that time. When the DIR
pin goes high, the data available at the X pins of the 74245 are transferred to the Y
pins, i.e., data is transmitted from the 8086 to either the memory or the I/O device
(write operation). If the DIR pin goes low, the data available at the Y pins of the
74245 are transferred to the X pins, i.e., data is received by the microprocessor
from the memory or the I/O device (read operation). For generating the Memory
Read (MEMR) and Memory Write (MEMW) control signals, the RD, WR, and
M/TO signals of the 8086 are used along with the combinational circuit (as shown
in Fig. 16.4) during the minimum operation of the 8086. In the case of maximum
mode operation of the 8086, a bus controller chip (8288) derives all the memory
control signals using the status signals SO, Si, and S2.
Certain locations in the memory are reserved for specific CPU operations.
After resetting the 8086, CS and IP are initialized to FFFFH and 0000H,
510 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 16.4 Generation of control signals for the memory in the 8086
respectively; the first instruction for execution is taken from the address FFFFOH
in the memory. Hence, the locations from FFFFOH to FFFFFH in the memory are
reserved for storing instructions, execution of which causes the 8086 to jump to
the initialization program of the system. The memory locations 00000H-003FFH
are reserved for the interrupt vector table. These memory locations are assigned to
the ROM/EPROM chips in an 8086-based system, so that the programs stored in
them are permanent. The interrupt vector table may be located in the RAM chips
in some systems. The memory chips can be interfaced with the 8086 using only
logic gates, or using both logic gates and the decoder IC 74138. This is explained
in Sections 16.3 and 16.4.
1 6.3 INTERFACING RAM AND EPROM CHIPS USING ONLY LOGIC GATES
When RAM and ROM/EPROM chips with the same or different storage capacities
have to be interfaced with the 8086, it can be easily done using logic gates. The
following example illustrates this concept.
Example 16.1:
Interface two 8K x 8 EPROMs (2764) and two 8K x 8 RAM chips (6264) with the
8086 using logic gates, such that the memory address ranges assigned to them are
FC000H-FFFFFH and 00000H-03FFFH, respectively.
Solution:
First, let us see the interfacing of the two 8K x 8 EPROM chips with the 8086,
so that they have the address range FC000H-FFFFFH. The addresses FC000H-
FFFFFH are given in binary form in Table 16.2.
A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 AO Address
1 1 1 1 1 1 00000000000000 FC000H
1 1 1 1 1 1 00000000000001 FC001H
1 1 1 1 1 1 00000000000010 FC002H
1 1 1 1 1 1 00000000000011 FC003H
11111111111111111111 FFFFFH
MEMORY AND I/O INTERFACING 511
It can be noted from Table 16.2 that even addresses such as FCOOOH, FC002H,
and FC004H are assigned to one 8K x 8 EPROM chip (say, 2764-A), which acts as
an even memory bank and odd addresses such as FC001H, FC003H, and FC005H are
assigned to another 8K x 8 EPROM chip (say, 2764-B), which acts as an odd memory
bank. Since the address line AO is 0 for all even addresses, it is used to generate the
Chip Select or Chip Enable signal for 2764-A, along with some of the higher-order
address lines of the 8086. Similarly, BHE is used along with some of the higher-order
address lines of the 8086 to select the odd memory bank formed by 2764-B.
First, the number of address lines in the 8K x 8 EPROM chip is noted, which is
13 (A12-A0) since 213= 8K. The address lines A 1-Al3 of the 8086 are connected
to the address lines A0-A12 of 2764-A and 2764-B, since the address line AO
of the 8086 is used for selecting the even memory bank. The remaining address
lines A19-A14 of the 8086 are used for address decoding. Figure 16.5 shows the
interfacing of two EPROM chips with the 8086.
Fig. 16.5 Interfacing EPROMs with the 8086 using logic gates
Since all the address lines A 14-Al9 are 1 for the addresses FC000H-FFFFFH,
these address lines are directly connected to an AND gate to produce the output
‘1’. The AND gate output and the inverted A0 signal are given to a NAND gate
and the output of this NAND gate is connected to the chip enable pin of 2764-A,
which is the even memory bank. Similarly, the same AND gate output and the
inverted BHE signal are given to another NAND gate, whose output is used to
select the 2764-B chip, which is the odd memory bank.
I
When the 8086 wants to access a byte from any odd address in the address
range FC000H-FFFFFH, the value in the address lines A 1-Al3 of the 8086 is
used to select one of the locations within 2764-B, as A 1-Al3 of the 8086 are
connected to A0-A12 of 2764-B. The address lines A 14-Al9 contain the value
1, which makes the AND gate output 1. The 8086 now activates the BHE signal
(i.e., BHE is made 0), due to which the CE pin of 2764-B goes low and is selected.
Since A0 = 1 for odd memory addresses (as it is the LSB of the address), CE of
2764-A is high and is not selected.
When the 8086 wants to access a byte from any even address in the address
range FC000H-FFFFFH, the values in the address lines A 1-Al3 and A14-Al9
are used for the purposes we have just discussed. Now, the address line A0 is 0
while the 8086 sends out an even address. BHE is made 1 by the 8086. Due to
this, CE of 2764-A is made low and is selected. Since BHE = 1, CE of 2764-B is
in high state and is not selected. While accessing a byte from either an odd or an
even memory bank, the 8086 activates MEMR after sending the memory address
to get the data.
When a word (16-bit data) whose lower-order byte is stored in an even address
is accessed by the 8086, both A0 and BHE are made 0, due to which both the chips
are selected. One byte from each memory bank is placed in the data bus (D15-D0)
when the 8086 activates the MEMR signal. The 8086 processor then reads the
entire word in the data bus. For example, if the 8086 wants to read the word whose
lower-order byte is stored in the address FFFFEH, all the address lines (Al-A 19)
of the 8086 contain 1. A0 and BHE are made 0. This makes the A0-A12 lines of
both the memory chips 1 and the CE input to both the chips 0. Due to this, the data
in the last memory location in both the chips are placed in their data buses, when
the MEMR signal is activated by the 8086.
Now, let us discuss the interfacing of the two 8K * 8 RAM chips with the 8086,
so that they have the address range 00000H-03FFFH. The addresses 00000H-
03FFFH are given in binary form in Table 16.3.
Table 16.3 Memory addresses assigned to the RAM chips
A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0 Address
00000000000000000000 00000H
00000000000000000001 00001H
00000000000000000010 00002H
00000000000000000011 00003H
The even addresses such as 00000H and 00002H are assigned to one 8K x 8 RAM
chip (say, 6264-A), which acts as an even memory bank and the odd addresses
MEMORY AND I/O INTERFACING 513
such as 00001H and 00003H are assigned to another 8K x 8 RAM chip (say, 6264-
B), which acts as an odd memory bank.
As discussed in the interfacing of the EPROM chip, the inverted AO line
and the output of the address decoder formed using the AND gate are given to a
NAND gate, and the NAND gate’s output is connected to the CE input of 6264-A.
The inverted BHE line and the same address decoder output are given to another
NAND gate, and the output of that NAND gate is connected to the CE input of
6264-B. The interfacing of the RAM chips with the 8086 is shown in Fig. 16.6.
Fig. 16.6 Interfacing RAM chips with the 8086 using logic gates
Since the address lines A 14-Al 9 contain 0 for the addresses 00000H-03FFFH,
the signals in these lines are inverted and then given to the AND gate, so that
they produce an output of 1 for the same addresses. This AND gate output and
the inverted A0 signal through the NAND gate activate the CE input of the even
memory bank. The same AND gate output and the inverted BHE signal activate
the CE input of the odd memory bank. The CE input of both the memory chips are
activated when the 8086 wants to access a word whose lower-order byte is stored
in the even memory bank.
The MEMR signal of the 8086 is connected with the OE (Output Enable) input.
The 8086 activates the MEMR signal while reading a byte or word from the RAM,
after sending the address through the address bus. The MEMW signal of the 8086
is connected with the WE (Write Enable) input. The 8086 activates the MEMW
signal while writing a byte or word in the RAM after sending the address through
the address bus and placing the data in the data bus.
514 MICROPROCESSORS AND MICROCONTROLLERS I
Example 16.2:
Interface two 8K x 8 EPROM chips with the 8086, such that the memory address
range assigned to them is FC000H-FFFFFH, using an address decoder made up
of the 74138 IC and logic gates.
Solution:
The 13 address lines A0-A12 in the 2764 are connected to the address lines
A 1-Al3 of the 8086. For the entire address range FC000H-FFFFFH, the value
in the address lines A 19-Al4 is equal to 1. The address lines A 19-Al5 are used
to enable the 74138 decoder IC, and the address lines A14, AO, and BHE are
connected to the selection lines of the 74138 IC.
Figure 16.7 shows the interfacing of the EPROM chips with the 8086 chips
using the 74138 decoder. For simplification, only the decoder and EPROM chips
are shown in the figure. The connection of the EPROM chips with the 8086 is the
same as in Example 16.1.
Y5 Y1'^. r. _>rY3 Y1
। । Decoder"^ । ।
Fig. 16.7 Interfacing EPROM chips with the 8086 using 74138 decoder
When the address lines A19-A14 are 1, the decoder is enabled. The selection
of a particular EPROM chip under that condition is explained in Table 16.4.
When we want to interface more RAM and EPROM chips of the same capacity
with the 8086, we can use two separate decoders (74138), one for accessing the
lower bank and the other for accessing the upper bank. A0 and BHE are used to
enable the two decoders.
MEMORY AND I/O INTERFACING 515
CE OF CE OF
BHE AO A14 Y5 Y3 Y1 Operation
2764-A 2764-B
0 0 1 1 1 0 0 0 A word is read from the memory.
0 1 1 1 0 1 1 0 A byte is read from the odd memory bank.
1 0 1 0 1 1 0 1 A byte is read from the even memory bank.
Example 16.3:
Interface four 8K x 8 RAM chips (6264) with the 8086, to assign the address range
80000H-87FFFH using two 74138 ICs.
Solution:
The addresses assigned to various memory chips are written in binary form as
shown in Table 16.5.
Address in
A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 AO
hex
(For
6264-IL)
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80000H
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 80002H
(For
6264-2L)
1 0000 1 00000000000000 84000H
1 0000 1 00000000000010 84002H
Address in
A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 AO 1
nex
(For
6264-2H)
1 0000 1 00000000000001 84001H
1 0000 1 00000000000011 84003H
Here, 6264-IL and 6264-2L are the RAM chips forming the lower banks.
6264-1H and 6264-2H are the 6264 RAM chips forming the higher banks.
For simplification, only the decoder and RAM chip connections are shown in
Fig. 16.8. The connection of the RAM chips with the 8086 is as explained in
Example 16.1.
The data for selection of the different chips is shown in Table 16.6.
Table 16.6 Data for selection of different RAM chips
A19 A18 A17 A16 A15 A14 A0 BHE RAM chips and byte/word
selected
1 0 0 0 0 0 0 0 6264-IL and 6264-1H;
a word is read/written
1 0 0 0 0 0 0 1 6264-IL; a byte is read/
written
1 0 0 0 0 0 1 0 6264-1H; a byte is
read/written
1 0 0 0 0 1 0 0 6264-2L and 6264-2H;
a word is read/written
1 0 0 0 0 1 0 1 6264-2L: a byte is read/
written
1 0 0 0 0 1 1 0 6264-2H: a byte is
read/written
Instruction Operation
IN AL, XXH Read a byte from the input device with address XXH and store it
in AL.
(Contd)
518 MICROPROCESSORS AND MICROCONTROLLERS
Instruction Operation
IN AL, DX Read a byte from the input device with the address specified by
DX and store it in AL.
IN AX, XXH Read a word from the input device with the address XXH and
store it in AX.
IN AX, DX Read a word from the input device with the address specified by
DX and store it in AX.
OUT XXH, AL Send a byte from AL to the output device with the address XXH.
OUT DX, AL Send a byte from AL to the output device with the address
specified by DX.
OUT XXH, AX Send a word from AX to the output device with the address XXH.
OUT DX, AX Send a word from AX to the output device with the address
specified by DX.
Address
FFH
(a) (b)
Fig.16.9 Memory and I/O maps for the 8086 (a) l/O-mapped I/O (b) memory-mapped I/O
The address for isolated I/O devices, called ports, is separate from the memory
in the isolated I/O scheme. As a result, the user can expand the memory to its full
MEMORY AND I/O INTERFACING 519
size (i.e., 1 MB) without using any of its address space (00000H-FFFFFH) for I/O
devices. A disadvantage of I/O-mapped I/O is that the data is transferred between
the 8086 and the I/O devices only by the IN and OUT instructions. Separate control
signals for the I/O devices are generated, which indicate an I/O read or an I/O
write operation. The generation of the IOR and IOW signals in the minimum mode
operation of the 8086 is shown in Fig. 16.10. In the maximum mode operation of
the 8086, the IOWC and IORC signals generated by the 8288 bus controller are
used to interface the I/O devices with the 8086.
Fig. 16.10 Generation of IOR and IOW signals in minimum mode operation of the 8086
16.6.1 Assigning 8-bit Address to 8-bit Input Device using Address Decoder
having only Logic Gates
Let us interface an 8-bit DIP switch with the 8086 operating in the minimum
mode, such that the address assigned to it is 8FH, using an address decoder having
only logic gates. Figure 16.11 shows the required interfacing circuitry. When the
8086 has to read the data from the 8-bit DIP switch, the instruction IN AL, 8FH or
520 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 16.11 Interfacing an 8-bit DIP switch with the 8086 (8-bit address)
IN AL, DX with DX already loaded with the value 008FH has to be executed by
it. During the execution of any one of these instructions, the address lines A7-A0
contain 8FH and the IOR signal is made low for some duration (a few ps) by the
8086. As a result, the enable inputs (1G and 2G) of the 74LS244 are activated (i.e.,
made low), and the data from the DIP switch is placed on the data bus (D15-D8).
The 8086 reads that data and places it in the AL register. The data bus D7-D0 of
the 8086 is used if the I/O device address is an even number. The reason for this
is explained in Section 16.9.
16.6.2 Assigning 8-bit Address to 8-bit Input Device using Address Decoder IC
74LS138
In Fig. 16.11, if we want to assign the address 8FH to the DIP switch using an
address decoder IC such as the 74LS138, the design of the address decoder is
done as shown in Fig. 16.12. When the 8086 places the address 8FH (10001111
in binary form) in the address lines A7-A0, the inputs C = B = A=1,G1 = 1, and
G2A = G2B = 0 in the 74LS138 IC, due to which the decoder IC is enabled, its Y7
output goes low, and other outputs remain high. This Y7 output of the decoder IC
along with the IOR signal of the 8086 is used to enable the 74LS244 IC, thereby
transferring data from the DIP switch to the AL register of the 8086 when the
instruction IN AL, 8FH is executed. The same decoder IC’s other outputs (i.e.,
Y0-Y6) can be used to assign the addresses 88H-8EH to other I/O devices.
MEMORY AND I/O INTERFACING 521
16.6.3 Assigning 16-bit Address to 8-bit DIP Switch using Address Decoder
having only Logic Gates
The interfacing of an 8-bit DIP switch with the 8086, such that the address assigned
to the DIP switch is FFFOH, is shown in Fig. 16.13 on page 522.
When the 8086 executes the instruction IN AL, DX with DX already loaded
with the value FFFOH (this is done using the MOV DX, FFFOH instruction), it
places the address FFFOH in the address lines A15-A0 and activates the IOR
signal for some duration (a few ps). This makes 1G and 2G of the 74LS244 low,
thereby enabling the 74LS244. Data from the DIP switch is placed in the data bus
(D7-D0) of the 8086. The 8086 reads that data and places it in the AL register.
The 16-bit address decoder can be designed using a combination of logic gates and
decoder ICs (74LS138), as explained using the 8-bit address decoder.
Fig. 16.13 Interfacing an 8-bit DIP switch with the 8086 (16-bit address)
Fig. 16.14 Interfacing eight LEDs with the 8086 (8-bit address)
MEMORY AND I/O INTERFACING 523
8086 has to send the data in the AL register to the LEDs, either OUT FOH, AL or
OUT DX, AL with DX already loaded with the value 00F0H has to be executed
by it. During the execution of any one of these instructions, the address lines
A7-A0 contain FOH and the data lines D7-D0 contain the data in the AL register.
The IOW signal (assuming that the 8086 is operating in minimum mode) is made
low for some duration (a few ps) by the 8086. This activates (i.e., makes high) the
clock (CLK) signal of 74LS373 IC. The data in the data bus D7-D0, which is the
content of the AL register, is latched in the 74LS373 IC and held there until the
OUT instruction with the same address is again executed by the 8086. The OC pin
in the 74LS373 IC is made low to enable the tri-state inverter connected to each
output pin.
15 NC — Not used
18 NC — Not used
19- GND — This is the twisted pair return (ground)
30 signal for the STROBE, DATA, ACK,
Busy, and PE signals.
31 INIT Input When the INIT signal is made low (for
more than 50 ps), the printer controller
is reset to its initial state and the printer
buffer is cleared.
(Contd)
MEMORY AND I/O INTERFACING 525
Table 16.8 Pin connections and signals in the Centronics interface (Contd)
Figure 16.15 shows the timing diagram of the important signals involved in
interfacing of the Centronics printer with the microprocessor. When the Busy
signal is 0, which means that the printer is ready for accepting the character from
the microprocessor, the microprocessor places the ASCII code of a character or the
code of a special command in the data lines (DATA1-DATA8). After a minimum
time of 0.5 ps, it activates the STROBE signal (i.e., makes it 0) for a minimum
period of 0.5 ps. The data in the data lines is kept at the same value for a minimum
period of 0.5 ps after the STROBE signal is deactivated (i.e., made 1). When the
STROBE signal is activated, the Busy signal from the printer immediately goes
high (i.e., becomes 1). It remains high until the printer sends the ACK signal, as
shown in Fig. 16.15. This is done because the microprocessor should not send
another data to the printer before the first data is processed in the printer. During
the rising edge of the ACK signal, the Busy signal goes low (as shown in Fig.
16.15) and now, another data can be sent to the printer.
Fig. 16.15 Timing diagram of important signals in the Centronics printer interface
526 MICROPROCESSORS AND MICROCONTROLLERS
Table 16.9 indicates the signals that are mainly required for interfacing the
Centronics printer with the microprocessor. There are totally ten output signals
that have to be sent from the microprocessor to the printer and four input signals
that have to be received by the microprocessor from the printer. Microprocessors
such as the 8085 and the 8086 can use one 8255 IC (programmable peripheral
interface) to interface a Centronics printer.
Table 16.9 Signals needed to interface the Centronics printer with the microprocessor
Figure 16.16 shows the interfacing of 8086 microprocessor with the Centronics
printer using one 8255 IC. It shows the main signals involved in the data transfer.
In Fig. 16.16, port A is used to send the data (eight bits) to the printer and hence
it should be configured as an output port. Port B is used to send the INIT and
STROBE signals to the printer and hence it should be configured as an output port.
Port C is used to receive status signals such as ACK, Busy, ERROR, and PE from
the printer and hence it should be configured as an input port.
Fig. 16.16 Interfacing the Centronics printer with the 8086 using the 8255
MEMORY AND I/O INTERFACING 527
The complete sequence of steps to be carried out in software, for the Centronics
printer to print a message having several lines, is given in the flowchart shown in
Fig. 16.17. The ASCII code of various characters in the message to be printed
is first stored in some portion of the RAM in the microprocessor system. The
microprocessor has to send the ASCII code of characters in the RAM to the printer
Fig. 16.17 Software sequence for interfacing the Centronics printer with the microprocessor
528 MICROPROCESSORS AND MICROCONTROLLERS
one by one. with the line feed and carriage return characters (OAH and ODH,
respectively) as the last code. Printers are often capable of executing commands
that are sent through the data lines by the microprocessor. The difference between
the data and the command is achieved by means of escape (ESC) codes. Whenever
the microprocessor sends an ESC code, the printer interprets the following code as
a command. Such commands are needed to specify the desired font, the size of the
margin, the line spacing, etc., in the message that is printed.
16.9 INTERFACING 8-BIT AND 16-BIT I/O DEVICES OR PORTS WITH 8086
Let us see how data are transferred between the 8086 and 8- or 16-bit I/O devices.
Data transferred to an 8-bit I/O device or port exists in one of the I/O banks of
the 8086. The I/O system contains two 8-bit I/O banks, just like the memory
system of the 8086. This is shown in Fig. 16.18, which indicates the separate
I/O banks for a 16-bit system. When an 8-bit address is used for I/O devices,
the even bank contains even addresses such as OOH, 02H, and 04H and the odd
bank contains odd addresses such as 01H, 03H, and 05H. When a 16-bit address
is used for I/O devices, the even bank contains even addresses such as 0000H,
0002H, and 0004H and the odd bank contains odd addresses such as 0001H,
0003H, and 0005H.
Since two I/O banks
exist, any 8-bit I/O
write operation requires
separate write strobes to
function correctly. These
are generated as shown in
Fig. 16.19. I/O read
operations do not require
separate read strobes
because as with the
memory, the 8086 only Fig. 16.18 I/O banks in an 8086-based
reads the byte it expects system with 16-bit addresses
and ignores the other byte.
Figure 16.20 shows a system
that contains two different 8-bit
output devices located at the 8-bit
I/O addresses FOH and FlH. Since
these are 8-bit devices and appear
in different I/O banks, separate 1/
O write signals are needed. In Fig.
16.20, the connections of only the
address decoder and the 74LS373 IOWL—Write strobe for low I/O bank
ICs are shown. The remaining
connections to the 8086 are the Fig. 16-19 Generation of write strobes for I/O banks
Fig. 16.20 I/O port decoder to select 8-bit output ports FOH and F1H
When selecting 16-bit wide I/O devices, the AO and BHE pins have no function
because both I/O banks are selected together. To interface the 16-bit ADC or DAC
ICs with the 8086, 16-bit ports are needed. Here, two successive addresses are
assigned for the same I/O device. One address is an even number such as OOH (for
8-bit address) or 0000H (for 16-bit address), where the lower-order byte of the
16-bit data is present. The other address is an odd number such as 01H (for 8-bit
address) or 0001H (for 16-bit address), where the higher-order byte of the 16-bit
data is present. In the IN or OUT instruction, only the address of the lower-order
byte of the 16-bit data is specified either directly or implicitly through DX. Figure
16.21 shows the interfacing of a 16-bit input device connected to function at the 8-
bit I/O addresses F4H and F5H. In the figure, only the connections for the address
decoder and the 74LS244 ICs are shown. The remaining connections to the 8086
are as shown in Fig. 16.11. Using the instructions IN AX, F4H or IN AX, DX with
DX already loaded with the value 00F4H, the data from the 16-bit input port can
be read and placed in AX.
Fig. 16.21 16-bit input port decoded at I/O addresses F4H and F5H
is used for receiving data from the microprocessor into the CRT. The GND
(ground) signal in the CRT interface is connected to the GND (ground) signal in
the microprocessor. The RS-232C interface transmits or receives data by serial
communication, i.e., one bit of data is transmitted or received at a time. Each byte
of data transmitted or received by the RS-232C interface is enclosed by one start bit
and 1, 1.5, or 2 stop bits. Figure 16.22 shows the RS-232C format for transmission
or reception of one byte of data, 4DH (which is equal to 01001101 in binary form),
with one start bit and two stop bits. When no data is transmitted or received, the
TXD and RXD lines remain high. In the RS-232C standard, any voltage between
+3V and +12 V in the data lines (TXD and RXD) is used to represent binary 0
and any voltage between -3 V and -12 V is used to represent binary 1. Due to this
reason, the RS-232C standard is said to be using negative true logic.
Fig. 16.22 RS-232C format for transmission or reception of a byte of data (4DH)
MEMORY AND I/O INTERFACING 531
There are three methods by which a CRT terminal can be interfaced with the
microprocessor:
(i) Direct connection of the microprocessor with the CRT terminal
A microprocessor (e.g., 8085) that has facilities for serial input/output (through
its SID/SOD pins), can be directly connected to the CRT terminal through level
translators. The SID pin of the 8085 is connected to the TXD pin of the CRT
terminal; the SOD pin of the 8085 is connected to the RXD pin of the 8085
through level translators, as shown in Fig. 16.23. The reason for using the level
translators is the mismatch in the voltage levels for representing binary 1 and 0 in
the microprocessor and the CRT terminal. We already know that the CRT terminal
uses the RS-232C interface. Microprocessors such as the 8085 and 8086 use TTL
(transistor-transistor logic) standard, in which +5 V is used to represent binary 1
and 0 V is used to represent binary 0. The level translators convert the TTL signal
to an RS-232C signal and vice versa. One example for such a level translator that
is available in an integrated circuit (IC) form is MAX-232. Each MAX-232 can
convert two TTL signals to the corresponding RS-232C signals and two RS-232C
signals to the corresponding TTL signals.
Fig. 16.23 Direct connection of the microprocessor with the CRT terminal
(ii) Connection of the microprocessor with the CRT terminal through serial-to-
parallel converter and parallel-to-serial converter
We can interface a microprocessor that does not have serial input/output
pins (e.g., 8086) with the CRT terminal using a serial-to-parallel converter, a
parallel-to-serial converter, and level translators, as shown in Fig. 16.24. Level
translators are used here because the serial-to-parallel converter and parallel-to-
serial converter operate only with TTL signals. The data (8 bits or 16 bits) that is
transmitted from the microprocessor through its data bus to the CRT terminal is
first converted to serial data using a parallel-to-serial converter and then sent to
the CRT terminal through the level translator, which converts the TTL signal into
an RS-232C signal. Similarly, the serial data that is transmitted from the CRT
532 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 16.24 Connection of the microprocessor with the CRT terminal using serial-to-parallel
converter and parallel-to-serial converter
(iii) Connection of the microprocessor with the CRT terminal through USART
(universal synchronous asynchronous receiver-transmitter)
There exists a special IC chip such as USART (IC 8251), which has a built-in
parallel-to-serial converter (eight bits) and a built-in serial-to-parallel converter
(eight bits). Figure 16.25 shows the connection of the 8086 microprocessor with
the CRT terminal through USART and level translators. The level translators are
used here because the USART operates only with TTL signals.
>■
Fig. 16.25 Connection of the microprocessor with the CRT terminal using USART
MEMORY AND I/O INTERFACING 533
The CRT terminal transmits or receives data at a fixed baud rate. Baud rate
represents the number of bits transmitted or received per second. There are some
standard values for baud rate, such as 600, 1200, 2400, 4800, and 9600. One of
these speeds can be selected in the CRT terminal by properly configuring certain
switches present in it. The microprocessor must also be programmed to the same
baud rate as the CRT terminal, for proper data transfer between them. The time
between transmitting or receiving two consecutive bits is known as bit time in
serial communication and it is the reciprocal of the baud rate. The required bit time
can be obtained using a delay program in the microprocessor. Based on the baud
rate input given to the microprocessor, the delay count used in the delay program
can be found using look-up table technique.
The microprocessor software that controls the data transfer between the
microprocessor and the CRT terminal does the following operations sequentially:
(i) During the transmission of data from the microprocessor to the CRT
terminal, the microprocessor first sends the start bit, then the data bits one
by one, and finally, the stop bit(s).
(ii) During the reception of data from the CRT terminal, the microprocessor
first checks whether start bit has occurred (i.e., whether RXD is made 0). If
start bit is received, then the microprocessor receives the data bits one by
one. Then it checks for the reception of stop bit(s).
The CRT terminal uses the parity bit along with the data, to ensure that the
transmission or reception of data does not involve any error. Some terminals use
odd parity and some use even parity. The number of Is in the data is made odd
or even using the seventh bit of the data, depending upon whether odd or even
parity, respectively, is needed. The software in the microprocessor should be able
to generate odd or even parity data during transmission and check for the same
parity of data during reception.
POINTS TO REMEMBER
• The maximum memory that can be connected with the 8086 is 1 MB, which is organized
as two separate banks—even/low memory bank and odd/high memory bank.
• The BHE signal is used to enable the odd memory bank and the data lines of the odd
memory bank are connected with the data lines D15-D8 of the 8086.
• The address line A0 is used to enable the even memory bank and the data lines of the
even memory bank are connected to the data lines D7-D0 of the 8086.
• When the lower-order byte of a word is stored in the even memory bank, the 8086 can
access both bytes of that word in a single memory read cycle. Otherwise, it takes two
memory read cycles to read the same word. Therefore, while storing an array of word
type data in the memory or while initializing the stack, the lower-order bytes of the
words are stored in the even addresses.
• There are two methods that can be used to interface I/O devices with the 8086—memory
mapped I/O and I/O-mapped I/O.
• In the memory-mapped I/O method, the I/O device is treated as if a memory location
and the instructions used for transferring data between the memory and the 8086 can be
used for data transfer between the 8086 and the I/O devices. The MEMR and MEMW
534 MICROPROCESSORS AND MICROCONTROLLERS
signals are used to activate the input device and output device, respectively. The I/O
devices have a 20-bit address in memory-mapped I/O and the design of the address
decoder is same as that of the memory address decoder.
• The I/O-mapped I/O scheme is commonly used to interface I/O devices with the 8086.
Here, there are two methods of addressing I/O devices—fixed port addressing (in which
the 8-bit address of an I/O device is specified in the IN or OUT instruction directly) and
variable port addressing (in which the 16-bit address of an I/O device is specified in the
IN or OUT instruction implicitly through the DX register). In I/O-mapped I/O, only the
IN and OUT instructions are used to communicate with the I/O devices. The advantage
of this method is that the user can fully utilize the 1 MB memory space, which is not
possible in memory-mapped I/O.
• The 8086 can be interfaced with either an 8-bit or a 16-bit I/O port. The I/O space in
the 8086 is also organized as two separate I/O banks—odd and even I/O bank, which
is the same as the memory organization in the 8086. The odd I/O bank contains odd
I/O addresses and the data lines of the odd I/O bank are connected to the D15-D8 lines
of the 8086. The even I/O bank contains even I/O addresses and the data lines of the
even I/O bank are connected to the D7-D0 lines of the 8086. The BHE signal is used to
enable the odd I/O bank and A0 is used to enable the even I/O bank, which is the same
as the process for enabling the memory in the 8086. The IOR and IOW signals are used
to activate the input and output devices, respectively, in the I/O-mapped I/O scheme.
KEY TERMS
16-bit input device It is an input device that sends 16-bit data to the 8086.
16-bit output device It is an output device that receives 16-bit data from the 8086.
8-bit input device It is an input device that sends 8-bit data to the 8086.
8-bit output device It is an output device that receives 8-bit data from the 8086.
BHE This is the Bus High Enable signal, which is used to enable the upper bank of the
memory in the 8086.
Even/low memory bank The even/low memory bank is a memory chip (or chips) that
contains even memory addresses; its data lines are connected to the D7-D0 lines of the
8086.
High/odd I/O bank This is the VO bank that contains odd addresses and is connected to
the data lines D15-D8 of the 8086.
I/O-mapped I/O This is a method of interfacing an VO device with the 8086, in which
an VO device is treated differently from the memory.
IN and OUT instructions These are the instructions used for transfer data between the
accumulator and the VO devices in VO-mapped VO.
IOR This is the VO read control signal that is activated during the VO read operation.
IOW This is the VO write control signal that is activated during the VO write operation.
Latch The latch is used for interfacing output device with microprocessor.
Low/even I/O bank This is the VO bank that contains even addresses and is connected
to the data lines D7-D0 of the 8086.
Memory address space or memory map The memory addresses that can be generated
by the 8086 (00000H-FFFFFH) together constitute the memory map.
Memory-mapped I/O This is a method of interfacing an I/O device with the 8086, in
which an I/O device is treated as if a memory location.
MEMORY AND I/O INTERFACING 535
MEMR This is the Memory Read control signal that is activated during the memory read
operation.
MEMW This is the Memory Write control signal that is activated during the memory
write operation.
Odd/high memory bank The odd/high memory bank is a memory chip (or chips) that
contains odd memory addresses; its data lines are connected to the D15-D8 lines of the
8086.
Physical memory address The memory address in the physical memory such as the
RAM or EPROM chip is called physical memory address.
Tri-state buffer The tri-state buffer is used for interfacing the input device with the
microprocessor.
REVIEW QUESTIONS
1. What is the maximum memory, in terms of bytes, that can be interfaced with the
8086? Why?
2. What is the memory address space in the 8086?
3. How is the physical memory organized in the 8086?
4. How are the AO and BHE signals in the 8086 used in the selection of memory banks?
5. Why should the data structures such as array of word type data or stack be stored from
an even address in the memory?
6. How is the multiplexed address bus in the 8086 separated into address bus and data
bus? Draw the diagram for the same.
7. What are the functions of IC 74244 and IC 74245?
8. How are the Memory Read and Memory Write control signals generated in the
minimum mode of operation of the 8086?
9. What is the importance of the memory address ranges 00000H-003FFH and FFFF0H-
FFFFFH in the 8086?
10. What are the differences between memory-mapped I/O and I/O-mapped I/O?
11. Write the different forms of the IN instruction in the 8086.
12. Write the different forms of the OUT instruction in the 8086.
13. What is meant by fixed port addressing in the 8086 and how many I/O devices can be
connected to the 8086 by this method?
14. What is meant by variable port addressing in the 8086 and how many I/O devices can
be connected to the 8086 by this method?
15. Draw a diagram showing the memory and I/O map when memory-mapped I/O and
I/O-mapped I/O schemes are used.
16. Draw a circuit showing the generation of I/O read and write control signals in the
minimum mode operation of the 8086.
NUMERICAL/DESIGN-BASED EXERCISES
1. Interface two 16K x 8 EPROM chips with the 8086, such that the memory address
range assigned to the EPROM chips is F8000H-FFFFFH, using an address decoder
having only logic gates.
536 MICROPROCESSORS AND MICROCONTROLLERS
2. Interface two 16K x 8 RAM chips with the 8086, such that the memory address range
assigned to the RAM chips is 00000H-07FFFH, using an address decoder having only
logic gates.
3. Interface two 8K x 8 EPROM chips with the 8086, such that the memory address
range assigned to the EPROM chips is F0000H-F3FFFH, using an address decoder
that employs the 74138 IC and logic gates.
4. Interface two 8K x 8 RAM chips with the 8086, such that the memory address range
assigned to the RAM chips is 20000H-23FFFH, using an address decoder that employs
the 74138 IC and logic gates.
5. Interface four 16K x 8 EPROM chips with the 8086, such that the memory address
range assigned to the EPROM chips is 90000H-9FFFFH, using an address decoder
that employs two 74138 ICs and logic gates.
6. Interface four 16K. x 8 RAM chips with the 8086, such that the memory address
range assigned to the RAM chips is A0000H-AFFFFH, using an address decoder that
employs two 74138 ICs and logic gates.
7. Interface an 8-bit DIP switch with the 8086 operating in minimum mode, such that the
address assigned to it is FOH, using an address decoder having only logic gates. Write
the instructions needed to read the data from the DIP switch into AL, in fixed port and
variable port addressing.
8. Interface an 8-bit DIP switch with the 8086 operating in minimum mode, such that
the address assigned to it is FOH, using an address decoder that employs the 74138
decoder and logic gates.
9. Interface a seven-segment LED in common cathode connection with the 8086
operating in minimum mode, such that the address assigned to it is 7FH, using an
address decoder having only logic gates. Write the instructions needed to display the
number 5 in the LED, using fixed port and variable port addressing.
10. Interface a seven-segment LED in common cathode connection with the 8086 operating
in minimum mode, such that the address assigned to it is 3FH, using an address
decoder that employs the 74138 decoder and logic gates. Write the instructions needed
to display the number 7 in the LED, using fixed port and variable port addressing.
11. Interface an 8-bit DIP switch with the 8086 operating in minimum mode, such that
the address assigned to it is FF80H, using an address decoder having only logic gates.
Write the instructions needed to read the data from the DIP switch into AL.
12. Interface a seven-segment LED in common anode connection with the 8086 operating
in minimum mode, such that the address assigned to it is 7FFFH, using an address
decoder having only logic gates. Write the instructions needed to display the number
5 in the LED.
13. Interface a 16-bit DIP switch with the 8086 operating in minimum mode, such that the
addresses assigned to it are 80H and 81H, using an address decoder having only logic
gates. Write the instructions needed to read the data from the DIP switch into AX, in
fixed port and variable port addressing.
14. Interface two seven-segment LEDs with common cathode connection with the 8086
operating in minimum mode, such that the addresses assigned to them are 70H and
71H, using an address decoder having only logic gates. Write the instructions needed
to display the number F5 in the LEDs, using fixed port and variable port addressing.
CHAPTER 17
MULTIPROCESSOR CONFIGURATION
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Necessity and advantages of a multiprocessor system
• Difference between closely-coupled and loosely-coupled multiprocessor systems
• Interconnection topologies between processors and memories in a multiprocessor system
• Physical interconnections between processors in a multiprocessor system
• Multiprocessor system containing 8086 and 8087 (numeric coprocessor)
• Multiprocessor system containing 8086 and 8089 (I/O processor)
17.1 INTRODUCTION
The speed of any microprocessor-based system depends upon the clock frequency
at which it is operating, amongst other factors such as the presence of a pipeline
execution unit and the microprocessor’s on-chip cache. For example, when
bulk I/O data transfer is done under the control of the microprocessor alone, the
processor has to spend most of its time idle due to the slow operating speed of
the peripherals. A single processor system has an upper limit on its processing
capability. For further enhancement of the speed of operation, an appropriate
system involving several connected processors using a certain topology may
provide the solution. Such a system is called multiprocessor system. If a system
having a single processor takes a particular duration to complete a task, a system
having more than one processor may require lesser time.
The simplest type of multiprocessor system consists of a CPU (such as the 8086)
and a numeric data processor (NDP), also called numeric coprocessor or input/
output processor (IOP). The NDP is an independent processing unit that is capable
of performing complicated numeric calculations in comparatively lesser time than
the microprocessor. The NDP works in coherence with the microprocessor. The
input/output operations in a microprocessor-based system are slow due to the low
operating speed of the I/O devices. An I/O processor (IOP) takes care of the I/O
activities of the 8086-based system and thus saves the time of the main processor
(the 8086). The NDP and IOP work in synchronism with the main processor to
complete specific tasks and are called coprocessors. Coprocessors do not work
independently, as they cannot fetch code from the memory. They work under the
control of the main processor. Additional hardware circuits such as bus arbiter
and bus controller may be needed to coordinate the activities of all the processors
working at a time in the system.
538 MICROPROCESSORS AND MICROCONTROLLERS
processor shares the system memory and the I/O devices through a common
system bus, extra logic must be included to ensure that only one processor has
access to the system bus at a time. For one processor to send a task or return a
result to another processor, an unambiguous way must be provided for the two
processors to interact. The connections between the processors are dictated by
how the bus contention and processor communication problems are resolved.
master runs independently and there are no direct connections between them.
Inter-processor communication is made possible through the shared resources. In
addition to the shared resources, each module may include its own memory and
I/O devices. The processors in the separate modules can simultaneously access
their private subsystems through their local buses and perform their local data
references and instruction fetches independently, thus improving the degree of
concurrent processing.
(iv) Each bus master may have a local bus to access dedicated memory or I/O
devices, so that a greater degree of parallel processing can be achieved.
In a loosely-coupled multiprocessor system, more than one bus master
module may have access to the shared system bus. Since each master is running
independently, extra bus control logic must be provided to resolve the bus arbitration
(i.e., allotment of system bus to a particular requesting master) problem. This extra
logic is called bus access logic and its responsibility is to make sure that only one
bus master at a time has control of the bus. Simultaneous bus requests are resolved
on a priority basis. There are three schemes for establishing priority—daisy
chaining, polling, and independent requesting. The three schemes are discussed in
Sections 17.4.1-17.4.3.
Compared to the other two methods, the daisy chain scheme requires the least
number of control lines and this number is independent of the number of modules
in the system. However, the arbitration time is slow due to the propagation delay
of the Bus Grant signal through the different masters. This delay is proportional
to the number of modules and therefore, a daisy-chain-based system is limited to
multiprocessor systems having only a few modules. Further, the priority of each
module is fixed by its physical location and the failure of a module in the system
causes the whole system to fail.
17.4.2 Polling
The polling scheme, which is shown in Fig. 17.4, uses a set of lines sufficient to
address each module. In response to a bus request, the controller generates and
542 MICROPROCESSORS AND MICROCONTROLLERS
A module’s host 8086 lacks the capability of requesting bus access and
recognizing bus grants. Therefore, it is necessary for each module containing a bus
master to have extra logic for sending and receiving the bus access signals. The
Intel bus arbiter (8289) is specifically designed to provide the necessary bus access
MULTIPROCESSOR CONFIGURATION 543
handshaking. The 8289 operates in conjunction with the bus controller (8288) and
controls the access of its associated master to the bus by using either the daisy
chain or the independent requests scheme.
m= 1
where N is the total number of processors.
for the 8087 are identified by the 8086, they are assigned to the 8087 for further
execution. After the 8087 executes that instruction, the results may be sent to the
8086 or stored in the memory. The 8087 adds 68 new instructions to the instruction
set of the 8086.
as in the 8086. These lines act as the input lines for the 8086-driven bus
cycles and become the input/output lines for the NDP-initiated bus cycles.
(ii) A19/S6-A16/S3: These lines are time multiplexed address/status lines and
are the same as in the 8086. S3, S4, and S6 are permanently high, while S5
is permanently low.
(iii) BHE/S7: During TI, the BHE/S7 pin is used to enable the data on the
higher-order byte of the 8086 data bus. During the T2-T4 clock cycles, it
acts as the status line S7.
(iv) QS1 and QSO: The queue status input signals QS1 and QSO enable the
8087 to keep track of the instruction queue status of the 8086, to maintain
synchronism with it. Their function is same as that of the QS1 and QSO
pins in the 8086. These lines are connected to the corresponding lines of the
8086.
(v) INT: The interrupt output is used by the 8087 to indicate that unmasked
exceptions, such as invalid operation, divide-by-0, overflow, etc., have
been received during the execution of the instruction.
(vi) BUSY: This output signal indicates to the 8086 that the 8087 is busy with
the execution of an allotted instruction. This is usually connected to the
TEST input of the 8086.
(vii) READY: This input signal may be used to inform the 8087 that the addressed
device such as the memory or the I/O device will complete the data transfer
from its side. Usually this signal is synchronized by the clock generator
(8284).
(viii) RESET: This input signal is used to disc ard the internal activiti es of the
coprocessor and prepare it for further extjcution, whenever need<3d by the
8086.
(ix) CLK: The CLK input provides gnd E1 X------- X 40 —I ^cc
(xiii) RQ/GTO: The request/grant pin is a bidirectional pin used by the 8087 to
gain control of the bus from the host 8086 for operand or data transfers. It
must be connected to one of the request/grant pins of the 8086. The request/
grant sequence is as follows:
An activate low pulse of one Table 17.1 Functions of S2, S1, and SO in 8087
clock duration is generated
by the 8087 for the host S2 S1 so Queue status
8288
CLK
Control bus
S2 S1 SO
maintains a parallel queue, identical to the instruction queue of the 8086. The
8087 uses the QSO and QS1 pins to obtain and identify the instructions fetched by
the 8086. The 8086 identifies the coprocessor instructions using the escape code
bits embedded in them. The first five bits of the escape code are 11011. Once
the 8086 recognizes the escape code, it initiates the execution of the coprocessor
instructions in the 8087. Each coprocessor instruction also has the opcode of the
WAIT instruction of the 8086 as its first byte. So the 8086 waits in a loop, checking
its TEST pin, to go low. The TEST pin of the 8086 is connected to the BUSY
pin of the 8087, which remains high until the 8087 finishes the execution of the
recently received instruction from the 8086. Once the 8087 finishes the execution,
it makes its BUSY pin low and the 8086 starts its normal operation.
552 MICROPROCESSORS AND MICROCONTROLLERS
During the execution of a coprocessor instruction in the 8087, the escape code
identifies the coprocessor instruction that requires a memory operand and also
the one that does not require any memory operands. If the instruction requires a
memory operand to be fetched from the memory, the physical memory address
of the operand is calculated by the 8086 and a dummy read cycle is initiated.
However, the 8086 does not read the operand. The 8087 reads it and proceeds for
execution. If the coprocessor instruction does not require any memory operand,
it is directly executed by the 8087. When the 8087 is ready with the execution
results, the control unit of the 8087 gets the control of the bus from the 8086
and executes a write cycle to write the results in the memory at the pre-specified
address. The numeric extension unit of the 8087 executes all the instructions
including arithmetic, logical, transcendental, and data transfer instructions.
The term biased exponent in all the real data types is obtained by adding
a bias to the exponent of the real number. The value of the bias is 127,
1023, and 16,383 for short, long, and temporary real data, respectively. The
general form of representation of the real number in the all the three cases
is
(-l)s x 2 (E~bias) x l.F
where S is the sign bit, E is the biased exponent, and F is the fraction part,
(vii) Packed BCD
Number of bytes =10
Approximate range = (-1018 + 1) - (1018- 1)
S—Sign bit
Bit 79 Bits 78-72 Bits 71-68 Bits 67-64 ... Bits 8-5 Bits 7-4 Bits 3-0
S 0 D17 D16 D2 DI DO
DO, DI, D2... DI6, and D17 represent the BCD code of each digit in
the packed BCD number. Further details of the 8087 can be obtained by
referring to the data sheet of the 8087.
through memory-based control blocks. The CPU prepares control blocks that
describe the task to be performed, and then sends the task to the 8089 through
an interrupt-like signal. The 8089 reads the control blocks to locate a program
called a channel program, which is written using the 8089 instruction set. Then
the 8089 performs the assigned task by fetching and executing instructions from
the channel program. When the 8089 has finished the task, it informs the CPU
either through an interrupt or by updating a status location in the memory.
The status lines Table 17.3 Status bits for defining IOP activity
are utilized by the S2 SI SO Significance
bus controller and
the bus arbiter to 0 0 0 Instruction fetch (I/O space)
generate all the 0 0 1 Data fetch (I/O space)
memory and I/O 0 1 0 Data store (I/O space)
control signals.
The signals change 0 1 1 Not used
during T4 if a 1 0 0 Instruction fetch (System memory)
new cycle is to be 1 0 1 Data fetch (System memory)
entered, while the
1 1 0 Data store (System memory)
return to passive
state in T3 or TW 1 1 1 Passive
indicates the end
of the cycle. The pins are floated after system reset and when the bus is not
acquired.
(v) READY: The Ready signal received from the addressed device indicates
that the device is ready for data transfer. The signal is active high and is
synchronized by the 8284 clock generator.
(vi) LOCK: The Lock output signal indicates to the bus controller that the bus is
needed for more than one contiguous cycle. It is set via the channel control
register and during the TSL instruction. The pin floats after reset and when
the bus is not acquired. The output is active low.
(vii) RESET: The receipt of a Reset signal causes the IOP to suspend all its
activities and enter idle state until a Channel Attention signal is received.
The signal must be active for at least four clock cycles.
(viii) CLK (clock): The clock provides all the timing needed for internal IOP
operation.
(ix) CA (Channel Attention): This signal gets the attention of the IOP. When
the falling edge of this signal is encountered, the SEL input pin is examined
to determine master/slave or CH1/CH2 information. This input is active
high.
(x) SEL (Select): The first CA received after system reset informs the IOP
via the SEL line, whether it is a master or a slave (0 and 1, respectively),
and starts the initialization sequence. During any other CA, the SEL line
signifies the selection of CHI and CH2 (0 and 1, respectively).
(xi) DRQ1 and DRQ2 (Data Request): The DMA requests inputs, which signal
to the IOP that a peripheral is ready to transfer and receive data using
CHI and CH2, respectively. The signals must be held active high until the
appropriate fetch/stroke is initiated.
(xii) RQ/GT (Request Grant): The Request Grant signal implements the
communication dialogue required to arbitrate the use of the system bus
(between IOP and CPU in local mode) or I/O bus when two lOPs share the
same bus (remote mode). The RQ/GT signal is active low. An internal pull-
up permits RQ/GT to be left floating, if not used.
556 MICROPROCESSORS AND MICROCONTROLLERS
(xiii) SINTR-1 and SINTR-2 (Signal Interrupt): The Signal Interrupt signal
outputs from CHI and CH2, respectively. The interrupts may be sent
directly to the CPU or through the 8295A interrupt controller. They are
used to indicate to the system the occurrence of user-defined events.
(xiv) EXT1 and EXT2 (External Terminate): The External Terminate signal inputs
from CHI and CH2, respectively. The EXT signal causes the termination
of the current DMA transfer operation, if the channel is so programmed by
the channel control register. The signal must be held active high until the
termination is complete.
Vcc: Supply voltage (+5 V) Vss: Ground
data transceivers for accessing the shared system bus. The RQ/GT pin on the 8089
can be used to interact with another 8089, which acts as the slave and shares the
buses with the host 8089. The 8089 accesses I/O devices dedicated to it through
the local bus, while it communicates with the CPU through the system memory.
A high speed controller may request a transfer through one of the two DRQ pins
(DRQ1 and DRQ2) in the 8089, and terminate a DMA operation through one of
two EXT pins (EXT1 and EXT2) in the 8089. To reduce the system bus loading and
enhance concurrent processing, local memory can be included to store the channel
programs or to provide storage areas. However, the local memory must respond
to I/O bus commands instead of memory read and memory write commands (i.e.,
it must act as the I/O-mapped memory). Unlike the 8086, the 8089 I/O bus need
not have the same data width as the memory bus. This allows the 8089 to transfer
data from an 8-bit source to a 16-bit destination and vice versa. Since the I/O
bus has only 16 address lines, the capacity of the local space (I/O space) is only
64 KB. On the other hand, the system space (i.e., memory space), which is
addressed by the system bus, has a capacity of 1 MB. The 8089 instructions access
I/O ports using the same addressing modes as are used for the memory operands.
Whether an address is in the I/O space or in the system space is determined by the
tag bit of the pointer register used.
Figure 17.21 shows the registers in the 8089 IOP. Each of the two channels can
be programmed and operated independently while sharing the common control
logic and ALU. The channel control pointer (CCP) cannot be manipulated by
the user. It stores the address of the control block (CB) for channel 1 during the
initialization sequence. For channel 2, its CB starts at the address that is indicated
by adding 8 to the contents of the CCP. To
dispatch a task to either channel, the CPU
(8086) sends out a Channel Attention
(CA) signal along with the Select (SEL)
signal, which selects channel 1 (if SEL
- 0) or channel 2 (if SEL =1). Since the
channels occupy two consecutive I/O
port addresses, the AO address line of
the 8086 is connected to the SEL pin, so
that when A0 = 0, one channel is selected
and when A0 = 1, another channel is
selected.
Each channel has an identical set of
registers, each set being divided into two
(always points to system memory)
groups according to size. The pointer
19 |
group consists of those registers having
Parameter pointer (PP)
20 bits and the register group consists
of those registers having 16 bits. Each Channel control pointer (CP)
pointer, with the exception of parameter
pointer (PP), has an associated tag bit.
Fig. 17.21 Registers in 8089 IOP
When used to access a memory operand,
MULTIPROCESSOR CONFIGURATION 559
the tag bit indicates whether the contents of that pointer represent a 20-bit system
(i.e.. memory) space address (if tag = 0) or a 16-bit local (i.e., I/O) space address (if
tag = I). In accessing the local space, only the lower-order 16 bits of the pointer are
used as the address. Register PP always points to an address in the system space.
The registers GA, GB, GC, IX, BC, and MC can be used as general-purpose
registers for arithmetic and logical operations in a channel program. In addition,
they perform special functions when addressing memory operands and executing
DMA operations. A memory operand can only be addressed by using one of the
pointers GA, GB, GC, or PP as a base register. During a DMA operation, GA and
GB are used as the source and destination pointers. If GA points to the source. GB
points to the destination, and vice versa. When a translation operation is performed
along with the DMA transfer, the contents of GC are used as the base address of a
256-byte translation table. Register BC is used as the byte counter during a DMA
transfer, and is decremented by 1 after every byte transfer and by 2 after every
word transfer.
For a masked compare operation, register MC contains the bit pattern to be
compared against in bits 7-0 and a mask in bits 15-8. A masked compare operation
is done according to the following expression:
((OPERAND BYTE) © (MC)7J A (MC)15 8
The results of the masked compare operation can be used as a DMA termination
condition or to determine whether or not a branch is to be made by a masked
compare branch instruction. The register IX is used as an index register. In two of
the memory operand addressing modes, its contents are added to those of a base
register to form the operand address. The task pointer (TP) stores the address of
the next instruction to be executed and is equivalent to the PC in a CPU. It also
has a tag bit for indicating whether the next instruction to be executed is stored in
the system or VO space. The parameter pointer (PP) is not programmable by the
user, but is automatically filled by the 8089 while initializing a task. PP points to
the address of the parameter block.
Each channel also has an 8-bit status register (PSW), which contains the
current channel status. This status indicates status descriptors such as the source
and destination address widths, channel activity, interrupt control and servicing,
bus load limit, and priority. The PSW cannot be manipulated by the user, but can
be modified by a channel command. It is saved with TP and the four tag bits in
the first two words of the parameter block, when a channel program is suspended.
This allows the channel to resume the suspended channel program upon receipt of
a resume command.
The 8089 has the capability to perform DMA transfers using different options.
The transfer direction can be specified as I/O to VO, memory to memory, or
memory to/from VO. For each transfer, the 8089 fetches a byte or word, stores
the data in the destination and updates GA, GB, and BC accordingly. If data are
transferred from an 8-bit source to a 16-bit destination, the 8089 can fetch two
bytes and store them as one word. Conversely, if the transfer is from a 16-bit
source to an 8-bit destination, a word can be split into two bytes, betore the data
are sent to the destination. Between the fetch and the store cycles of the DMA,
560 MICROPROCESSORS AND MICROCONTROLLERS
the data byte can be compared or translated. Further, a DMA operation can be
terminated by an external request, a zero byte count, or a match/mismatch detected
by a masked compare. These options are specified by the contents of the channel
control register (CC) whose format is as follows:
Termination control bits 6-0 These bits specify how the DMA is to be
terminated and where to fetch the next instruction from, upon completion of the
DMA operation. A DMA transfer can be terminated after the current transfer
cycle, based on the comparison (bits 2,1, and 0) shown in Table 17.4 (a), the
byte count (bits 4 and 3) shown in Table 17.4 (b), and the external control (bits 5
and 6) shown in Table 17.4 (c), or a combination of the three. If external control
is selected, the channel terminates the DMA when the channel’s EXT (external
termination) input is activated. If the byte count is specified, a 0 in the channel’s
BC register causes the DMA to terminate. Whether or not a comparison is to result
in a termination and whether a match or mismatch is to cause the termination is
determined by bits 2 to 0.
0 0 No external termination
0 1 Terminates when EXT = 1; offset is set to 0
1 0 Terminates when EXT = 1; offset is set to 4
1 1 Terminates when EXT = 1; offset is set to 8
MULTIPROCESSOR CONFIGURATION 561
The channel executes the instruction whose address is the contents of TP, plus
an offset upon the termination of a DMA operation. Therefore, when more than
one termination condition is specified, it is possible to use the offset in conjunction
with the branch instruction to enter different DMA completion routines, depending
on the actual cause of the DMA termination. If more than one of the selected
conditions occurs at the same time, the largest offset that corresponds to a satisfied
condition is used. To initiate a DMA transfer, the channel program should contain
instructions for setting up the source and destination pointers CC, BC and, if
necessary, GC, MC, and the I/O bus width.
Single transfer mode (bit 7) This bit is used to terminate the DMA after a single
transfer if it is set to 1 and then execute the next instruction pointed to by TP.
Chaining control (bit 8) This bit gives the other channel (i.e., channel 2, when
channel 1 is programmed and vice versa) the highest priority. This bit is not used
for DMA operation.
Lock control (bit 9) This bit activates the LOCK output of the 8089 during the
DMA transfer cycle, if it is set to 1.
Source/destination indicator (bit 10) This bit specifies whether the register
GA is used as the source pointer (i.e., the bit is 0) or the destination pointer (i.e.,
the bit is 1). In either case, GB is used as the other pointer.
Synchronization control (bits 12 and 11) These bits specify how the data
transfer is to be synchronized. An unsynchronized transfer (if the bits are 00) begins
the next transfer cycle whenever a bus cycle is available. A source-synchronized
transfer (if the bits are 01) starts the read operation of the next transfer cycle upon
receiving the DRQ signal. A destination-synchronized transfer (if the bits are 10)
starts the write operation of the next transfer cycle when the DRQ is received.
Translation mode (bit 13) This bit indicates that the data bytes are to be
translated through a 256-byte look-up table during DMA (if the bit is 1). The base
address of the translation table should be stored in register GC.
Function control (bits 15 and 14) These bits specify one of four data transfer
modes—memory to memory (if the bits are 11), I/O port to memory (if the bits are
10), memory to I/O port (if the bits are 01), and I/O port to I/O port (if the bits are
00). During a memory to memory data transfer, both the destination and source
pointers are auto-incremented, but during and after an I/O to I/O data transfer,
both pointers remain unchanged. These two modes are not supported by most
conventional DMA controllers. They are useful in moving a block of code or data
from one memory area to another and in direct device communications.
each of which is pointed to by the previous control block. The IOP communication
areas are shown in Fig. 17.22. The system configuration pointer block (SCPB)
contains three words starting at location FFFF6H in the system memory. The least
significant byte (SYSBUS) specifies the width of the system bus, which is eight
bits if SYSBUS = 0 and 16 bits if SYSBUS = 1. The succeeding two words store
the offset and segment address of the location of the system configuration block
(SCB). The SCB does not need to be stored at a fixed location. However, it must
reside in the system space. The least significant byte of the SCB is the system
operation command (SOC). Bits 0 and 1 of the system operation command define
the width of the VO bus and the RQ/GT mode as follows:
Bit 1 = 0 indicates the standard RQ/GT mode.
Bit 1 = 1 indicates the modified RQ/GT mode for use with multiple 8089s.
Bit 0 = 0 indicates an 8-bit I/O bus.
Bit 0 = 1 indicates a 16-bit I/O bus.
The last two words in the SCB contain the offset and segment address of the
beginning of two consecutive channel control blocks (CBs) in the system space.
There is one control block for each channel and the first byte of each control block
is called the channel command word (CCW), which indicates the action to be taken
by the channel. The next byte (BUSY) indicates the busy status of the channel (00
for not busy and FF for busy) and the last two words contain the address of a
parameter block. A parameter block is used for providing the beginning address of
the channel program and passing information to and from this program.
Control block
(CB) for channel 1
Control block
(CB) for channel 2
System configuration
block (SCB)
System configuration
pointer block (SCPB)
The format of a CCW is shown in Fig. 17.23. It includes a 3-bit command field,
a 2-bit interrupt control held, a bus load limit bit, and a priority bit.
P 0 B ICF CF
The command held specihes one of the six possible commands shown in
Table 17.5.
CF Command field
000 Update PSW—causes PSW to be updated
001 Start channel program (VO space)—initiates the execution of a channel
program that is stored in the VO space
010 Reserved
011 Start channel program (system space)—initiates the execution of a channel
program that is stored in the system space
100 Reserved
101 Resume suspended channel operation—causes a suspended operation to be
continued from the point at which it was stopped
110 Suspend channel operation—suspends the operation currently being
performed by the channel until a resume command is given
111 Halt channel operation—aborts the current channel operation
The interrupt control held (ICF) is used for enabling (ICF =10) and disabling
(ICF = 11) interrupt requests and for removing previous interrupt requests
(ICF = 01). When an IOP sends out an interrupt request, it sets a service bit in its
PSW. While the interrupt is being serviced, this bit must be cleared by sending the
IOP a command with 01 in the interrupt control held. Otherwise, a request would
block other requests. If ICF = 00, it has no effect on interrupts.
When the bus load limit (B) bit is 0, there is no bus load limit and when it
is 1, there exists a bus load limit, during which the IOP can execute only one
instruction every 128 clock cycles. This prevents the IOP from monopolizing the
bus in situations in which there is no need for it to be the dominant processor.
The priority bit in the PSW is set or cleared according to the priority bit (P) in the
CCW. Further details about the 8089 can be obtained from the data sheet of the
8089.
564 MICROPROCESSORS AND MICROCONTROLLERS
KEY TERMS
Bus arbitration This is the method by which allotment of a system bus to a particular
requesting master is done amongst the many requesting masters at a time. There are three
methods of bus arbitration—daisy chain, polling, and independent requests.
Closely-coupled system A closely-coupled multiprocessor system is one in which the
external processor or coprocessor shares not only the entire memory and I/O subsystem in
the system, but also the same bus control logic and clock generator of the main processor.
Distributed operating system This is an operating system used in a multiprocessor
system to efficiently run a task in the system and to protect the program and data of different
modules in the system from unauthorized accesses.
Interconnection topology This is the way in which communication among the
microprocessors is performed in a multiprocessor system. There are different methods of
interconnection between processors—shared bus architecture, multi-port memory, linked
input/output, and crossbar switch.
I/O processor (IOP) An I/O processor is a processor that is mainly used to perform I/O-
related operations, to relieve the microprocessor from the relatively slow I/O operations.
IOP is also a coprocessor.
Loosely-coupled system Each module in a loosely-coupled system may act as the system
bus master and may consist of an 8086 or another processor, capable of being a bus master,
a coprocessor, or a closely-coupled configuration. Several modules may share the system
resources and the system bus control logic must resolve the bus contention problem.
Multiprocessor system A multiprocessor system is one that contains more than one
processor for improving the performance of the system.
Numeric coprocessor or numeric data processor A numeric coprocessor is a processor
that works in conjunction with a microprocessor to perform floating-point operations
quickly.
MULTIPROCESSOR CONFIGURATION 565
REVIEW GUESTIONS
18.1 INTRODUCTION
To adapt to different situations, the 8086 processors can be operated either in the
minimum or the maximum mode. The minimum mode is used for a small system
with a single processor (8086) and in any system in which the 8086 generates all
the necessary bus control signals directly, thereby minimizing the required bus
control logic. The maximum mode is for medium to large size systems, which
often include two or more processors. In the maximum mode, the 8086 encodes
the basic bus control signals into three status bits (S2, ST, and SO) and uses the
remaining control pins to provide the additional information that is needed to
support the multiprocessor configuration.
Fig. 18.2 Formation of separate address bus (A19-A0) and data bus (D15-D0) in the 8086
568 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 18.3 Formation of buffered address bus and data bus in 8086
8086-BASED SYSTEMS 569
Fig. 18.4 (a) pin details of the 8284A (b) Typical 8284A connection with the 8086
570 MICROPROCESSORS AND MICROCONTROLLERS
In the minimum mode system, the control lines (RD, WR, and M/IO) need not
be passed through transceivers, but can be used directly. The RD, WR, and M/IO
lines indicate the type of data transfer, as shown in Table 18.1.
Since the content of CS and IP are
FFFFH and 0000H after reset, the first Table 18.1 Function of the 8086 control signals
instruction for execution is fetched in minimum mode operation
from the memory address FFFFOH M/IO RD WR Operation
(= CSX10H + IP) by the 8086. Hence,
0 0 1 I/O read
the system start-up program must be
0 1 0 I/O write
stored from the address FFFFOH in
1 0 1 Memory read
the memory. Normally, this address
1 1 0 Memory write
is assigned to a ROM type memory
chip, so that the system start-up
program is available permanently. The interrupt vector table is stored from the
address 00000H in the memory, whenever the interrupt(s) is (are) to be used in the
8086-based system. In addition, depending upon the system requirement, specific
interfacing ICs can be used along with the 8086.
(i) For interfacing the keyboard and the seven-segment display with the 8086,
the 8279 IC can be used.
(ii) To increase the number of hardware interrupts that can be handled by the
8086, the 8259 IC can be used.
(iii) To interface I/O devices such as DIP switches, ADCs, DACs, LEDs, relays,
and stepper motors with the 8086, the 8255 IC is used.
(iv) For performing serial communication, the 8251 IC is used with the 8086.
can determine which instruction is currently executed by the 8086. The LOCK pin
indicates that an instruction with a LOCK prefix is being executed and that the bus
is not to be used by another master. These pins are needed only in multiprocessor
systems.
The HOLD and HLDA pins become the bus request and the bus grant (RQ/GTO
and RQ/GT1) pins in the maximum mode. Both bus requests and bus grants can be
given through these pins. Both the pins function in exactly the same way, except
that if requests are seen on both the pins at the same time, the one on RQ/GTO is
given higher priority. A request consists of an active low pulse arriving before the
start of the current bus cycle. The grant is an active low pulse that is issued at the
beginning of the current bus cycle provided that
(i) The previous bus transfer in the 8086 was not the lower-order byte of a
word to or from an odd address.
(ii) The first low pulse of an interrupt acknowledgement (INTA) did not occur
during the previous bus cycle.
(iii) An instruction with a LOCK prefix is not being executed.
572 MICROPROCESSORS AND MICROCONTROLLERS
If condition (i) or (ii) is not met, the grant is not given until the next bus cycle; if
condition (iii) is not met, the grant waits until the locked instruction is completed.
In response to the grant, the tri-state pins of the 8086 (i.e., address, data, and
control pins) are placed in their high impedance state and the next bus cycle is
given to the requesting master. The processor is effectively disconnected from
the system bus until the master sends a second pulse to the processor through the
RQ/GT pin.
The ALE, DT/R, DEN, and INTA pins provide the same outputs that are sent
by the 8086 processor when it is in minimum mode (except that DEN is inverted).
The CLK input permits the bus controller activity to be synchronized with that
of the 8086 processor. The remaining pins given in Fig. 18.5 have the following
functions:
(i) MRDC (memory read command)—This signal instructs the memory to
place the contents of the addressed location on the data bus.
(ii) MWTC (memory write command)—This signal instructs the memory to
accept the data on the data bus and place the data in the addressed memory
location.
(iii) IORC (I/O read command)—This signal instructs an I/O interface to place
the data contained in the addressed port on the data bus.
(iv) IOWC (I/O write command)—This signal instructs an I/O interface to
accept the data on the data bus and place the data in the addressed port.
(v) INTA (Interrupt Acknowledge)—This signal is used to send two interrupt
acknowledgement pulses to an interrupt controller such as the 8259 or an
interrupting device, when SO = SI = S2 = 0.
These five signals are active low and are outputted during the middle portion of
a bus cycle. Only one of them is issued during a bus cycle. There are two more
signals— AIOWC (advanced I/O write command) and AMWC (advanced memory
write command). They do the same function as the IOWC and MWTC pins.
However, they are activated one clock pulse earlier. This gives slow interfaces an
extra clock cycle to prepare for accepting the input data. The 8288 requires +5 V
power supply and has TTL-compatible inputs and outputs.
is filled with idle state clock cycles denoted by TI. During data transfer, the wait
states are inserted between T3 and T4, when a memory or I/O interface is not able
to respond quickly.
| T1 | T2 | T3 | T4 | T1 | T2 | T3 | TW | T4 | T1 | T2 |
| T1 | T2 | T3 | Tw | Tw | T4 | T1 | T2 | T3 |
| T1 | T2 | T3 | T4 | T1 | T1 | T2 | T3 | T4 | T1 |
| T1 | T2 | T3 | T4 | T1 | T1 | T1 | T2 | T3 | T4 |
Fig. 18.6 8086 bus cycles (a) normal bus cycle and bus cycle with one wait state
(b) bus cycle with two wait states (c) bus cycle with one idle state, and
(d) bus cycle with two idle states
The timing diagram for general bus operation of the 8086 in minimum mode is
shown in Fig. 18.7. If the Ready signal is still in low state at the beginning of T3,
one or more wait states (TW) will be inserted between T3 and T4, until a Ready
has been received (i.e., Ready is made 1). The bus activity during TW is the same
as the activity during T3. A signal applied to an RDY input of the 8284A causes a
Ready output to the 8086 at the falling edge of the current clock cycle.
The simplified timing diagram for the memory or I/O read cycle, which requires
one wait state in the minimum mode is shown in Fig. 18.8.
The simplified timing diagram for the memory or I/O write cycle, which
requires one wait state in the minimum mode is shown in Fig. 18.9.
When the processor is ready to initiate a bus cycle, it places the address in the
lines AD15-AD0 and A19/S6-A16/S3, and the status of BHE in the line BHE/S7,
and applies a pulse to the ALE pin during T1. Before the falling edge of the ALE
signal, the signals in the address lines DEN, DT/R, M/IO, and BHE are made
574 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 18.7 Timing diagram for general bus operation of the 8086 in minimum mode
Fig. 18.8 Memory or I/O read cycle in minimum mode operation of the 8086
8086-BASED SYSTEMS 575
Fig. 18.9 Memory or I/O write cycle in minimum mode operation of the 8086
stable (i.e., the appropriate value, 1 or 0, is placed on the address lines), with DT/R
= 0 for the read operation, and DT/R = 1 for the write operation. At the falling
edge of the ALE signal, the 74LS373s latches the address in the lines AD 15-
ADO and A19/S6-A16/S3, and the status of BHE in the line BHE/S7. During T2,
the address in these lines is removed and the status signals S3-S7 are outputted
on the A16/S3-A19/S6 and BHE/S7 pins. DEN is made logic 0 to enable the
74LS245 transceivers. The logic value in the line M/IO (which is not shown in
Figs 18.7, 18.8, and 18.9) is 1 for memory-related operation and 0 for I/O-related
operations.
If an input operation (i.e., read operation) is to be performed, RD is active
low during T2, and the AD15-AD0 pins should enter a high impedance state in
preparation for the receiving of input data. If the memory or I/O interface is ready
to transfer data immediately, there are no wait states and the data are put on the bus
during T3. After the input data are accepted by the 8086, RD is raised to 1 at the
beginning of T4 and the memory or I/O interface removes its data upon detecting
this transition.
For an output operation (i.e., write operation), the 8086 makes the signal
WR = 0 and places the output data in the pins AD15-AD0 during T2. During T4,
WR is made logic 1 and the data are removed.
For both input and output operation, DEN is made logic 1 during T4, to disable
the transceivers. The M/IO signal is set according to the next data transfer at this
time.
The bus timing of the 8086 has been designed such that the memory or I/O
interface involved in a data transfer can control when data are to be placed on or
576 MICROPROCESSORS AND MICROCONTROLLERS
taken from the bus by the interface. This is done by having the interface send a
Ready signal to the 8086 (via the 8284), when it has placed data on the data bus or
has accepted data from the data bus.
T1 | T2 | T3 | T4 | T1
Fig. 18.10 Memory or I/O read cycle in maximum mode operation of the 8086
The timing diagram for the memory write cycle without any wait state, in a
maximum mode 8086 system, is shown in Fig. 18.11. The status bits SO, SI, and
S2 are set just prior to the beginning of the bus cycle. Upon detecting a change
from the passive state (SO = ST = S2 = 1), the 8288 outputs a pulse on its DT/R
pin during TI. In T2, the 8288 sets DEN = 1, thus enabling the transceivers. For
memory read operation, it activates MRDC, which is maintained until the end
of the clock period T4. For a memory write operation, AMWC is activated from
T2 to T4 and MWTC is activated from T3 to T4. The status bits SO, SI, and S2
remain active until the end of T3 and become passive (all Is) during T3 and T4.
As with the minimum mode, if the Ready input of the 8086 is not activated before
the beginning of T3, the wait states are inserted between T3 and T4.
Similar to the memory read cycle, while performing the I/O read cycle, the
control signal IORC is activated instead of MRDC. Similar to the memory write
8086-BASED SYSTEMS 577
cycle, while performing the I/O write cycle, the control signals AIOWC and IOWC
are activated instead of AMWC and MWTC, respectively.
T1 | T2 | T3 | T4
Fig. 18.11 Memory or I/O write cycle in maximum mode operation of the 8086
Fig. 18.13 Bus request and bus grant timing in minimum mode operation of the 8086
8086-BASED SYSTEMS 579
Fig. 18.14 Bus request and bus grant timing in maximum mode operation of the 8086
POINTS TO REMEMBER
• There exist different timing diagrams in the 8086, such as the timing diagram for general
bus operation (i.e.. the memory or I/O read cycle and the memory or I/O write cycle) of
the 8086. interrupt acknowledgement, and bus request/grant in minimum and maximum
modes.
KEY TERMS |
Bus controller (8288) This IC is used to generate the control signals for the memory and
I/O device using the status signals SO, SI, and S2 in the maximum mode operation of the
8086.
Bus cycle Each bus cycle of the 8086 has four clock periods, T1-T4 plus any number of
wait state clock cycles denoted by Tw.
Bus request/grant cycle This cycle is needed to perform the DMA operation and also
when the bus is needed for another processor.
Clock generator (8284) This IC is used to supply a clock pulse with 33% duty cycle and
also to synchronize the Reset and Ready signals given to the 8086.
Idle state If the bus is to be inactive or idle after the completion of a bus cycle, the gap
between successive bus cycles is filled with idle state clock cycles, denoted by TI.
Interrupt acknowledgement (INTA) cycle During this cycle, the 8086 sends two INTA
pulses to an external interface after receiving the INTR interrupt, to get the interrupt type
number for the INTR interrupt.
I/O read cycle During this cycle, the 8086 reads data from the input device.
I/O write cycle During this cycle, the 8086 writes data into the output device.
Maximum mode In this mode operation, there is more than one processor, and all the
control signals for the memory and the I/O device are generated by the bus controller
(8288) chip.
Memory read cycle During this cycle, the 8086 reads data or instructions from the
memory.
Memory write cycle During this cycle, the 8086 writes data into the memory.
Minimum mode In this mode operation, there is only one 8086 processor, and all the
control signals for the memory and the I/O device are generated by the processor itself.
Wait states These states are inserted between T3 and T4, when a memory or I/O interface
is not able to respond quickly enough during a data transfer. This is achieved with the help
of the Ready input in the 8086.
REVIEW QUESTIONS
5. How are the control signals IOR and IOW generated using the M/IO, RD, and WR
signals of the 8086?
6. What is the function of the chip select logic and what are the inputs given to it?
7. Write the function of the clock generator IC (8284).
8. What is the role of the bus controller IC (8288)?
9. What is meant by memory read and memory write cycles?
10. What is meant by I/O read and I/O write cycle?
11. Write the function of the signals DEN and DT/R in the 8086.
12. What is the function of the signals M/IO and BHE in the 8086?
13. What is the role of the pin F/C in the 8284A?
14. Explain the minimum mode configuration of the 8086-based system with the necessary
block diagram.
15. Describe the maximum mode configuration of the 8086 with the necessary block
diagram.
16. Explain the signals in the 8284A and the 8288 in detail.
17. Explain the bus timings for general bus operation in the 8086 under minimum mode
with necessary waveforms.
18. Explain the bus timings for general bus operation in the 8086 under maximum mode
with necessary waveforms.
19. With necessary waveforms, describe the bus timings for bus request and grant in
minimum and maximum modes.
20. How does the 8086 respond to the INTR interrupt in minimum mode operation of the
8086?
NUMERICAL/DESIGN-BASED EXERCISES
INTEL 8096—16-BIT
MICROCONTROLLERS
19.1 INTRODUCTION
The microprocessor or CPU contains an ALU, a program counter, a stack pointer,
some working registers, a clock timing circuit, and an interrupt circuit. To make
a complete microcomputer, one must add memory, usually ROM and RAM, a
memory decoder, an oscillator, and a number of input/output (I/O) devices such as
parallel and serial data ports. In addition, special purpose devices such as interrupt
handlers and counters may be added to relieve the CPU from time consuming
counting or timing chores.
A microcontroller, popularly called computer-on-a-chip, can act as a complete
controller, but cannot become a complete digital computer by itself. The design
incorporates all the features found in a microprocessor CPU, such as arithmetic
and logic unit (ALU), program counter (PC), stack pointer (SP), and registers. In
addition, it also has the other features such as ROM, RAM, parallel I/O, serial I/O,
counters, and clock circuit.
Like the microprocessor, the microcontroller is a programmable device, but
is meant to read data, perform limited calculations on that data, and control the
environment based on those calculations. The prime use of the microcontroller is
to control the operation of a machine using a program that is stored in its ROM and
does not change over the lifetime of the system.
Table 19.1 shows the difference between a microprocessor and a microcontroller.
It compares the pin configuration, architecture, and instruction set of a common
8-bit microprocessor, the Zilog Z80, and Intel’s 8051 and 8096 microcontroller
series.
An 8-bit microcontroller can be used in a variety of applications that involve
limited calculations and relatively simple control strategies. As the requirement
for faster response and more complex calculations grows, the 8-bit design begins
to hit a limit that is inherent with byte-wide data words. One solution is to increase
clock speed; another is to increase the size of the data word.
586 MICROPROCESSORS AND MICROCONTROLLERS
The 16-bit microcontroller is used to solve high speed control problems of the
type that might typically be confronted in the control of servo mechanisms such
as robot arms. In the first line of development, manufacturers have produced 16-
and 32-bit processors, which in turn are used to develop more powerful personal
computers. They have become the backbone of workstations, which are becoming
the revolutionary tool for engineers. Due to their processing power and speed,
these 16- and 32-bit processors are used to design products such as electronic
instruments.
In the second mode of development, instead of focusing upon the larger word
width and address spaces, emphasis has been laid on much faster real time control.
It has focused upon the integration of the facilities needed to support fast control
in a single chip.
In the past, the highest performance real time control applications have
employed 16-bit and 32-bit microcontrollers, together with interrupt handler
chips, programmable timer chips, and ROM and RAM chips, to achieve what
we can now achieve in a single microcontroller chip. Even for those real time
control applications for which the resources of a single chip are not adequate, such
a chip still offers the optimal design approach. Its on-chip resources provide an
integrated approach to a variety of real time control tasks. The applications of the
microcontroller have no limitations and depend almost entirely on the designer’s
imaginative skills.
OVERVIEW OF INTEL 8096 MICROCONTROLLERS 587
In general, all the 8X9X series microcontrollers have the following built-in
features:
(i) 16-bit CPU (ii) On-chip clock generator
(iii) 256 bytes RAM (iv) Special function registers
(v) Parallel ports (vi) Analog input channels and ADC
(vii) High speed inputs (viii) High speed outputs
(ix) Two timers (x) Interrupts
(xi) Serial ports (xii) Watchdog timer
The control unit provides the necessary timing and control signals for all
operations. It controls the flow of data between the controller, the memory, and
the peripherals. When the Reset signal is activated, all internal operations are
suspended, and the program counter is cleared. After reset, the program execution
can begin from the zero memory address.
The sequencing of the execution of instructions is carried out using the
program counter (PC). This register is a memory pointer, which always points
to the memory address from which the next instruction is to be fetched. When
an instruction is being fetched, the PC is incremented by one, to point to the next
instruction.
The microcontroller can be interrupted from its normal execution of routines and
can be asked to execute some other instructions of higher priority, called interrupt
service routine. The controller would return to its normal operations after completing
the service routine. There are many interrupt sources for a microcontroller system
and the programmer can set the priority for each interrupt.
The clock circuit unit generates the clock signal and synchronizes all operations
within the chip. It also supplies the clock necessary for communication between
the CPU and the peripheral units.
complement method), the complemented output of the data can be placed in the
temporary register.
Two of the temporary registers have their own shift logic. These registers are
used for operations that require logical shifts including normalize, multiply, and
divide. The lower word register is used only when double word (32-bit) quantities
are being shifted. The upper word register is used whenever a shift is performed.
It is also used as a temporary register for many instructions. The 5-bit loop counter
is another temporary register used to count repetitive shifts.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
A watchdog timer is an internal timer that can be used to reset the system if the
software fails to operate properly, i.e., it resets the 8096 if a malfunction occurs.
OVERVIEW OF INTEL 8096 MICROCONTROLLERS 593
FFFFH
External memory or I/O
6000H
OFFH 255--------
Power down RAM 5FFFH
0F0H 240 External memory or I/O (6X9XBH)
Internal program storage ROM/
OEFH 239 EPROM or external memory (8X9XJF)
Internal register file (RAM) 4000H
Location 2019H must contain 20H. Locations 1FFEH and 1FFFH are reserved for
P3 and P4, respectively. This is to allow easy construction of these ports if external
memory is used in the system. If P3 and P4 are not going to be reconstructed, these
locations can be treated as any other external memory locations.
The nine interrupt vectors are stored in the locations 2000H-2011H. Locations
2012H-2017H are reserved for future use. Location 2018H is the chip configuration
byte, which gives configuration information. Locations 2020H-202FH hold the
security key used with the ROM lock feature.
All the unspecified addresses in locations 2000H-207FH, including those
marked reserved, should be considered reserved for use by Intel.
Special function registers All the input/output on the 8X9X are controlled
through SFRs. Many of these registers are double-buffered, i.e., reading from an
address is different from writing to the same address. There are several restrictions
on using special function registers:
(i) Neither the source nor the destination addresses of the multiply and divide
instructions can be writable special function registers.
OVERVIEW OF INTEL 8096 MICROCONTROLLERS 595
(ii) These registers may not be used as base or index registers for indirect or
indexed instructions.
(iii) Some of the byte registers can be accessed only as bytes, while some can be
accessed only as words.
Table 19.4 gives the list of all special function registers available in the 8X9X
series.
Table 19.4 Special function registers in the 8X9X series
Register Description
RO Zero register—Always reads zero; useful as a base when indexing
and as a constant for calculations and comparisons
AD_RESULT A/D results Hi/Lo—Low and high order results of the A/D
converter (byte read only)
AD_COMMAND A/D command register—Controls the A/D
HSI_MODE HSI mode register—Sets the mode of the high speed input unit
HSI-TIME HSI time Hi/Lo—Contains the time at which the high speed input
unit was triggered (word-read only)
HSO_TIME HSO time Hi/Lo—Sets the time or count for the high speed output
to execute the command register (word-write only)
HSO_COMMAND HSO command register—Determines what happens at the time
loaded in the HSO time registers
HSI_STATUS HSI status registers—Indicates which HSI pins were detected at
the time in the HSI time registers and the current state of the pins
SBUF (TX) Transmit buffer for the serial port; holds the contents to be sent out
SBUF (RX) Receive buffer for the serial port; holds the byte just received by
the serial port
INT.MASK Interrupt mask register—Enables or disables the individual
interrupts
INT_PENDING Interrupt pending register—Indicates that an interrupt signal has
occurred on one of the sources and has not been serviced
Watchdog Watchdog timer register—Written periodically to hold off
automatic reset every 64K state times
Timer 1 Timer 1 Hi/Lo—Timer 1 higher-order and lower-order bytes
(word-read only)
Timer 2 Timer 2 Hi/Lo—Timer 2 higher-order and lower-order bytes
(word-read only)
IO Port 0 Port 0 register—Indicates the levels on the pins of port 0
BAUD_RATE Determines the baud rate. This register is loaded sequentially.
Register Description
10 Port 2 Port 2 register—Used to read or write to port 2
SP.STAT Serial port status—Indicates the status of the serial port
SP_CON Serial port control—Sets the mode of the serial port
IOS0 VO status register 0—Contains information about the HSO status
POINTS TO REMEMBER
• The 8096 is the basic 16-bit microcontroller manufactured by Intel in the 8X9X series.
The RAM and ROM differ for each member in the 8X9X family.
• The 8096 has many parallel VO ports, serial ports, ADCs, timers, and HSI/HSO units,
in addition to the CPU unit and the memory.
• The 64K memory map registers of the 8096 consist of internal registers, special function
registers, and internal and external memory.
• The special function registers are used to control the various internal hardware resources
of the 8096.
KEY TERMS
CPU buses These are the internal buses of the microcontroller used for transfer of
address and data within it.
OVERVIEW OF INTEL 8096 MICROCONTROLLERS 597
High speed I/O unit This unit can be used to transfer data at a predefined time and to
note the time of data input.
Memory controller It is the unit that controls the address and data buses to implement
data transfer between the CPU section and other peripherals.
Microcontroller It is also known as computer-on-a-chip, incorporates all of the features
found in a microprocessor CPU (ALU, PC, SP, and registers) and has all the other features
needed to make a complete computer (ROM, RAM, parallel I/O, serial I/O, counters, and
clock circuit).
Program counter It is a memory pointer, which always points to the memory address of
the next instruction to be fetched.
Program status word It is the register with a collection of flags, which indicate the
status of the data after any operation in the microcontroller.
Pulse width modulating unit This unit is used to generate pulses of varying width, the
width being proportional to the content of a particular internal register.
Register arithmetic and logic unit This unit is inside the CPU of the 8096, performs
arithmetic and logic operations using a set of 232 register arrays.
Special function registers These are the registers in the microcontroller that have a
specific control or function over the various peripherals of the chip.
Watchdog timer It is an internal timer that is used to reset the system if the software
fails to operate properly.
REVIEW QUESTIONS
Long integers The signed form of the double word is called long integer. As the
most significant bit (MSB) is the sign bit, the magnitude of long integers varies
from -2,147,483,648 to +2,147,483,647. Long integers are used in the signed
multiply and divide instructions. The storing and addressing of long integers is
similar to the procedure followed for words. Normalization or sign extension can
be done for converting words into long words. The least significant byte must be
stored in a memory address that is evenly divisible by 4.
Example:
LD AX, [BX] + ; AX = MEMWORD_CONTENT (BX)
: BX = BX + 2 (incremented by 2 as a word
operand is accessed)
ADDB AL, BL, [CX] + : AL = BL + MEMBYTE.CONTENT (CX)
; CX = CX + 1
Example:
ADD AX, BX, LOOKUPECXJ
AND AX, TABLEEBX]
can be used in the long-indexed addressing mode. This allows the direct addressing
of all memory locations.
Example:
ADD AX, 9000[0]
As discussed in Section 20.3.1, the instructions vary with data type. For example,
the instructions that operate on 8-bit data (byte) are different from the instructions
that operate on 16-bit data (word) and 32-bit data (double word or long word).
Arithmetic operations on signed numbers are done by a separate set of instructions.
The instructions supported by the 8096 for arithmetic and logical operations are
as follows:
(i) ADDB Add bytes
ADDCB Add bytes and carry
ADD Add words
ADDC Add words and carry
(ii) SUBB Subtract both signed and unsigned bytes
SUBCB Subtract a byte from another, along with the borrow (if any)
SUB Subtract a word from another
SUBC Subtract a word from another, along with the borrow (if any)
(iii) MULUB Multiply unsigned bytes
MULB Multiply signed bytes
MULU Multiply unsigned words
MUL Multiply signed words
(iv) DIVUB Divide bytes using unsigned arithmetic
DIVB Divide short integers using signed arithmetic
DIVU Divide words using unsigned arithmetic
DIV Divide integers (word data) using signed arithmetic
(v) AND Logical AND operation on word data
ANDB Logical AND operation on byte data
OR Logical OR operation on word data
ORB Logical OR operation on byte data
XOR Logical XOR operation on word data
XORB Logical XOR operation on byte data
NOT Logical NOT operation on word data
NOTB Logical NOT operation on byte data
(vi) DEC Decrement word data
DECB Decrement byte data
INC Increment word data
INCB Increment byte data
NEG Negate the word data (sign change)
NEGB Negate the byte data (sign change)
(vii) CMPB Compare bytes (flags set according to subtraction operation)
CMP Compare words (flags set according to subtraction operation)
(viii) EXT Sign-extend integer into long integer (16-bit data to 32-bit data)
EXTB Sign-extend short integer into integer (8-bit data to 16-bit data)
operation has no options and operates on one of the following types of data—short
integer (8-bit). integer (16-bit), or long integer (32-bit)—at a time. However, the
shift-right operation has two options, based on the manipulation of the signed
number. Logical right-shift instructions (SHRB, SHR, and SHRL) are used to
shift unsigned numbers right. When signed numbers are shifted right, the sign bit
should not be lost. Therefore, arithmetic right shift instructions (SHRAB, SHRA,
and SHRAL) are used to shift signed numbers right, as they restore the sign bit.
The shift instructions of the 8096 provide another option—for initializing the
number of shifts in the instruction. The second operand in the shift instructions can
be either immediate addressed or register-direct addressed; this second operand
specifies the number of shifts to be performed on the first operand. Figure 20.1
lists the shift/rotate instructions.
If V = O JNV Label
If VT = 1 JVT Label
If VT = 0 JNVT Label
(Contd)
608 MICROPROCESSORS AND MICROCONTROLLERS
Note:
BS, BS1, BS2 Byte type source operand having a page 0 address
WS, WS1, WS2 Word type source operand having a page 0 even address
BD Byte type destination operand having a page 0 address
WD Word type destination operand having a page 0 even address
LD Long word type destination operand having a page 0 address
(divisible by 4)
Label 8-bit offset address
Example 20.2:
Subtract the two 16-bit hexadecimal numbers stored in the registers B and C. Store
the result in the memory.
LD AX, #8500H
LD BX, #9999H
LD CX, #369CH
SUB DX, BX, CX
ST DX, LAX J +
here: SJMP here
Example 20.3:
Multiply the two 16-bit hexadecimal numbers stored in the registers B and C.
Store the result in the memory.
LD AX, #8500H
LD BX, tfOFEDCH
LD CX, #0BA98H
MUL LX, BX, CX
ST LL, [AXJ+
ST LH, [AX]+
here: SJMP here
Example 20.4:
Divide a 32-bit hexadecimal number by a 16-bit hexadecimal number. Store the
quotient and remainder in the memory.
LD AX, #8500H
LD BX, tfOFFFEH
LD LL, tfOEFFFH
LD LH, tfOFFFFH
DIV LX, BX
ST LL, [AXJ+
ST LH, EAXJ+
here: SJMP here
Example 20.5:
Multiply and divide the given 16-bit hexadecimal numbers by powers of 2 using
the arithmetic shift operation.
The arithmetic shift of binary numbers means multiplication or division by
a power of 2. For instance, doubling a number involves shifting it left by one place
and halving a number involves shifting it right by one place.
610 MICROPROCESSORS AND MICROCONTROLLERS
Example 20.6:
Store numbers from FFH to OOH in consecutive memory locations.
The program involves storing 256 numbers, from FFH to OOH, in successive
memory locations. Hence, the process has to be done 256 times. The BL register
is initialized with FFH and used for looping, i.e., continuation of the process.
In this example, the number array is assumed to start at the location 8500, as
indicated by AX. After execution, [8500] = FFH, [8501] = FEH, and so on up to
[85FF] = OOH.
LD AX, #8500H
LDB BL, 7A0FFH
Ll: STB BL, [AX] +
DJNZ Bl, Ll
STB Bl, [AX]+
here: SJMP here
Example 20.7:
Determine the greater of two given bytes, using a subroutine.
LD AX, #8500H
LDB BL, [AX] +
LDB BH, [AX] +
SCALL FINDHGH
ST B CL, [AX]+
here: SJMP here
Subroutine FINDHGH:
CMPB BL, BH
JGT fval
LDB CL, BH
RET
fval : LDB CL, BL
RET
Example 20.8:
Change the sign of a given number and store the new number in the memory.
8096 INSTRUCTION SET AND PROGRAMMING 611
LD REGI, #8500H
LD REG2, #9999H
NEG REG2
ST REG2, [REGI]
here: SJMP here
Example 20.9:
Normalize the given data and store it in the memory.
This example uses two registers—REGI and REG2. NORML REGI, REG2
means that REGI is shifted left until its MSB is 1. REG2 contains the number of
shifts required to perform this operation.
LDB REGI, #data
NORML REGI, REG2
STB REG2, 8500H
STB REGI, 8501H
move: SJMP move
Example 20.10:
Find the number of characters in a string. The string is terminated by a null (0)
character.
In this example, AX register is used to address the string and BH is used
to store the number of characters. Hence, each character is fetched from the
memory and compared with 0. If the zero flag is set, the end of the string has been
reached. The count is incremented after each comparison, till the end of the string
is reached.
In this example, the string is assumed to start at 8500H and the end of string has
the value 0. Let the content of the location 8540H be zero. Then the result, which
is the length of the string to be checked, is 40. This result is stored in the memory
location 8550H.
LD AX, #8500H
LDB BH, #0
LI: LDB BL, [AX] +
INCB BH
CMPB BL, #0
JNE LI
LD AX, #8550H
STB BH, [AX]
here: SJMP here
Example 20.11:
Check the status of a bit in a byte using the JBS instruction. The byte to be checked
is stored in memory location 85OOH. Check the status of bit 0. Store this status in
memory location 8501H.
LD AX, //8500H
LDB BL, [AX] +
JBS BL, 0, LI
LDB CL, #0
612 MICROPROCESSORS AND MICROCONTROLLERS
Example 20.12:
Generate switching pulses for a single-phase inverter.
The following program generates the switching pulses for a single-phase
inverter through a parallel port of the 8255. Figure 20.2 shows the switching
pulses generated for the inverter. Changing the corresponding count can change
the width of the switching pulse and hence the frequency of the output signals.
Algorithm:
(i) Initialize the 8255 programmable peripheral interface (PPI) to configure the
I/O port.
(ii) Store the port A and port B addresses in an internal register.
(iii) Output ‘FFH’ to port A and ‘OOH’ to port B.
(iv) Introduce a delay for the required half-period duration.
(v) Output ‘OOH’ to port A and ‘FFH’ to port B.
(vi) Introduce a delay for the required half-period duration.
(vii) Repeat this process for the next cycle.
Program:
I n i t: LD 80, //Control - port-8255
LDB 82, //Control -word
LD 84, //portA-8255-addr ; Initialize the 8255 port A
address.
LD 86, Z/portB-8255-addr ; Initialize the 8255 port B
address.
LDB 88, //Delay count : Delay count for 180 degrees
STB 82, 80 : Write control word to 8255
Start: LDB 8C, //FFH : Load data to switch on the
device in port A.
LDB 8E, //OOH : Load data to switch off the
device in port B.
8096 INSTRUCTION SET AND PROGRAMMING 613
Example 20.13:
Generate switching pulses for a three-phase bridge inverter (for 180° conduction
mode).
The following program generates switching pulses for the three-phase inverter
in 180° switching mode. In this mode, the pulse must be on for a period of T/2 of
the switching frequency and must be spaced at an angle of 60°. The pulse for each
switch is the output on each nibble of the port. Here, the 8255 PPI IC is assumed
to be interfaced with the 8096. With three ports, six pulses are output through six
nibbles of the port pins. Figure 20.3 shows the gating signal pattern for a three-
phase bridge inverter for 180° conduction mode.
Port A
gi Lower
Port A
gz Upper
PortB
‘93 Lower
PortB
9* Upper
PortC
Lower
PortC
0 Upper
Program:
Ini t: LD 80, ^Control-port-addr
LDB 82, ^Control word
STB 82, 80 ; Write control word to 8255
LD 84, #portA-addr
LD 86, #portB-addr
LD 86, #portC-addr
LDB 8A, #00H ; Output data are stored from
8A to 8D.
LDB 8B, #F0H
LDB 8C, #0FH
LDB 8D, #FFH
LDB 8E, #delay count ; Load delay corresponding to
T/6 secs
Start: STB 8C, [84]
STB 8A, [86]
STB 8D, [88]
SCALL Del ay
STB 8D, [84]
STB 8A, [86]
STB 8B, [88]
SCALL Del ay
STB 8D, [84]
STB 8C, [86]
STB 8A, [88]
SCALL Del ay
STB 8B, [84]
STB 8D, [86]
STB 8A, [88]
SCALL Del ay
STB 8A, [84]
STB 8D, [86]
STB 8C, [88]
SCALL Del ay
STB 8A, [84]
STB 8B, [86]
STB 8D, [88]
SCALL Del ay
SJMP Start
Del ay: LDB 90, 8E
DECB 90
JNE Del ay
RET
8096 INSTRUCTION SET AND PROGRAMMING 615
POINTS TO REMEMBER
• The Intel 8096 instruction set supports many data types such as bytes, words, double
words, short integers, long integers, and bits.
• The addressing modes supported by the 8096 include immediate, direct, indirect, and
indexed addressing. The auto increment option is available for indirect addressing.
• The instruction set of the 8096 is more flexible in comparison with that of the 8051, with
more arithmetic and compare operations on both signed and unsigned numbers.
KEY TERMS
Bits These are operands that can take only the Boolean values true and false.
Bytes These are unsigned 8-bit variables, which can take values between 0 and 255.
Double words These are unsigned 32-bit variables, which can take values between 0 and
4,29,49,67,295.
Long integers These are 32-bit signed variables, which can take values between
-2,14,74,83,648 and +2,14,74,83,647.
Short integers These are 8-bit signed variables, which can take values from -128 to
+127.
Stack pointer It points to the top of the stack and facilitates the accessing of operands
in the stack.
Words These are unsigned 16-bit variables, which can take values between 0 and
65535.
REVIEW QUESTIONS 1
1. What is addressing?
2. Compare short-indexed and long-indexed addressing.
3. Discuss zero register addressing.
4. What is conditional and unconditional branching?
5. What is a subroutine? Explain with an example.
6. Give examples for single-operand instructions.
7. What is the difference between signed number arithmetic and unsigned number
arithmetic?
8. What is the difference between logical shift and arithmetic shift in the 8096?
PROGRAMMING EXERCISES1
21.1.1 PortO
Port 0 is an input-only port, which is also used as the analog input port for the
analog-to-digital converter (ADC). So if the analog input features of the 8096 are
not used, port 0 can be used as the input port. The status/voltage of port 0 can be
read from its address 0EH, which lies in the on-chip memory. Figure 21.1 shows
the structure of ports 0 and 1.
21.1.2 Port 1
Port 1 is a quasi-bidirectional port. This means that port 1 can be used as either
the input port or the output port. It is mapped at the internal memory address OFH.
If any one of the port 1 pins is to be used as an input port, the data ‘ 1 ’ should be
written onto the corresponding bit in the address OFH, before the status of that
bit is read. For example, if bits 0 and 1 of port 1 are to be used as input ports, the
data byte 00000011 is first written onto port 1. Then the status of the bits 0 and 1
is read.
Example:
The following program inputs the status of the least significant nibble of port 1 and
outputs the complement of the input to the most significant nibble of port 1.
LDB AL, P0RT1 AL: 10010101
ANDB AL, #OFH AL: 00000101
NOTB AL:(FF-AL) AL: 11111010
SHLB AL, #4 AL: 10100000
STB AL, P0RT1
OVER: SJMP OVER
21.1.3 Port 2
Port 2 has three types of port lines—
input-only, output-only, and quasi-
bidirectional. Except for P2.6 and
P2.7 (the sixth and seventh pins of
port 2), the remaining port 6 pins have
alternative functions. The address of
port 2 is 10H. Figure 21.2 shows the
structure of port 2.
The functions of the port 2 bits are
given in Table 21.1.
Table 21.1 Bit functions of port 2
If a particular alternative function is not used, the corresponding port 2 pin can be
used as an input or an output pin. The function of P2.6 and P2.7 is similar to that
of the port 1 pins.
Register 0
Bit Function
IOCO is located at the memory
location 0015H. The four HSI DO HSI.O input enable/Disable
lines can be enabled or disabled DI Timer 2 reset at every write
by setting or clearing the bits in D2 HSI.l input enable/Disable
IOCO. The timer 2 functions,
D3 Timer 2 external reset enable/Disable
including clock and reset sources,
are also determined by IOCO. The D4 HSI.2 input enable/Disable
control bit definitions are shown D5 Timer 2 reset source HSI.0/T2RST
in Table 21.2. IOCO is used for
D6 HSI.3 input enable/Disable
the initialization of the timer and
HSI only. D7 Timer 2 clock source HSI.1/T2CLK
21.3.1 Timer 1
The timer 1 of the 8096, shown in Fig. 21.4, is a 16-bit counter, which is clocked
every two microseconds (i.e., every eight internal clock cycles). It can be read
Timeri (OOOBH,000AH)
(word read)
from at any time, but must never be written into. Further, while 000BH contains
the upper byte of timer 1, 000AH contains its lower byte.
The hardware accepts only reads of the entire two-byte word, as in the following
example:
ADD HSO-TIME. Timed, #15
The example reads the timer 1 value, adds 15 to that value, and stores the
result in HSO-TIME. Timer 1 is used in conjunction with the HSI/O system. This
makes up the 8096’s programmable timer capability, which times input events and
controls the timing for output events. Timer 1 can be cleared only by executing a
reset. IOC1 is used to enable the interrupts when timer 1 overflows. The overflow
can be read from the status register IOS 1.
21.3.2 Timer 2
Timer 2 of the 8096, shown in Fig. 21.5, is a 16-bit event counter. It must be clocked
by a signal coming into the chip. Timer 2 is counted on both the rising edges and the
falling edges of the input signal. The minimum time between edges is 2.0 ps. This
corresponds to a square wave input, having a maximum frequency of 250 kHz.
T2CLK
bit 3 on port 2) can be selected, releasing HSI.l. The choice between these two
clock sources is made by setting or clearing bit 7 of IOCO. The operation and
control of timer 2 can be easily understood from Fig. 21.6.
7 6 5 4 3 2 1 o
All these options are controlled by what is written into the input 1OC0.
21.4 INTERRUPTS
There are 21 sources of interrupts in the 8096. These sources are grouped into
eight interrupt types. The I/O control registers that control some of the sources are
indicated in Fig. 21.7. Each of the eight types of interrupts has its own interrupt
vector. In addition to the eight standard interrupts, there is a TRAP instruction,
which acts as a software-generated interrupt.
EXTINT
RI flag
Software timer
Software timer 0
Software timer 1
Software timer 2
Software timer 3
Reset timer 2
Start ADC
HSI unit can be programmed to record the timing of any input appearing at the
HSI pins. After the recording of any event on the HSI pins, the processor can be
interrupted with the interrupt HSI data available. The ADC unit can also interrupt
the processor after completion of the conversion. The last group of interrupts is
the timer overflow interrupt. The timers are programmed to count up to the desired
count value and then interrupt the processor.
Table 21.6 gives the priority of the eight different groups of interrupts and their
interrupt vector addresses. The program at these vector locations must identify the
actual interrupt source. For example, the serial port interrupt has a single vector
address. The interrupt service routine must read the status register and identify
whether the receive interrupt or the transmit interrupt has occurred.
Table 21.6 Interrupt vector locations and their priorities
Vector location
Vector Priority
Higher-order byte Lower-order byte
Software trap 2011H 201 OH Not applicable
External interrupt 200FH 200EH 7 (highest)
Serial port 200DH 200CH 6
Software timers 200BH 200AH 5
HSI.O 2009H 2008H 4
High speed outputs 2007H 2006H 3
HSI data available 2005H 2004H 2
A to D conversion complete 2003H 2002H 1
Timer overflow 2001H 2000H 0 (lowest)
i-
t
routine 3 comes in and gets serviced, after which 5 resumes operation. The right
side of the figure is intended to illustrate the higher priority interrupt service
routine 3 being serviced and causing the servicing of the interrupt source 5 to be
delayed until it is done.
Time
Main line
program
Higher priority
interrupt service
routine 3
Lower priority
interrupt service
routine 5
The three registers that control the interrupt system are as follows:
INT_PENDING register
INT.MASKING register
(iii) Program status word
Interrupts
operation is indivisible. The easiest way to Table 21.7 Bit format of interrupt
do this is to use the logical instructions in the pending register
mask register. The format of the PSW is shown in Fig. 21.12. The PSW can be
saved in the system stack with a single operation (PUSHF) and restored to the
corresponding register (POPF).
Z N V VT C - I ST INT_MASK register
The designers of the 8096 have provided users the flexibility of installing their
own priority scheme in place of an existing priority. For example, if the highest
priority is to be given to the analog to digital conversion complete interrupt and the
next priority is to be given to the HSO interrupt, the analog to digital conversion
complete interrupt service routine, ADCC-ISR, will take the following form:
ADCC-ISR: PUSHF ; Save the PSW, then clear it
; Service A-to-D converter (with I = 0)
This recasting of interrupt priorities depends upon the fact that when an interrupt
service routine is entered, its first instruction is always executed. When this is
a PUSHF instruction, its execution clears the PSW, including the bit I, disabling
further interrupts. In addition, both PUSHF and the following EI instruction
postpone any potential interrupt servicing until the instruction changing the
INT_MASK register is executed.
At the end of the interrupt service routine, the POPF instruction restores the original
value of the PSW, thereby restoring the interrupt priority scheme to the same
state it was in when the interrupt occurred. Since interrupts are not acknowledged
immediately after a POPF instruction, the RET instruction is executed (clearing
the stack) before another interrupt is serviced.
It consists of 10 bits—a start bit (0), eight data bits (LSB first), and a stop bit
(1). If parity is enabled (the PEN bit is set to ‘ 1’), an even parity bit is sent instead
of the eighth data bit, and parity is checked on reception.
21.5.1.2 Mode 2
Mode 2 is the asynchronous ninth bit recognition mode. Figure 21.14 shows the
data frame used in this mode.
It consists of a start bit (0), 9 data bits (LSB first), and a stop bit (1). While
transmitting data, the ninth bit can be set to ‘1’, by setting the TB8 bit in the
control register, before writing to SBUF (TX). The TB8 bit is cleared on every
transmission of data. During reception, the serial port interrupt and the receive
interrupt (RI) bits are not set unless the ninth bit being received is set. This provides
an easy way to have selective reception on a data link. Parity cannot be enabled in
this mode.
21.5.1.3 Mode 3
Mode 3 is the asynchronous ninth bit mode. The data frame for this mode is
identical to that of mode 2. The transmission difference between mode 3 and mode
2 is that parity can be enabled (PEN = 1), and can cause the ninth data bit to take
the even parity value. In this mode, the TB8 bit can still be used if parity is not
enabled (PEN = 0). The reception always causes an interrupt, regardless of the
state of the ninth bit. The ninth bit is stored if PEN = 0 and can be read in the bit
RB8. If PEN = 1, RB8 becomes the receive parity error (RPE) flag.
HARDWARE FEATURES OF 8096 631
Bit position D7 D6 D5 D4 D3 D2 D1 DO
Bit 5 of IOC1 must be set to assign bit 0 of port 2 to the TXD serial port
function. The baud rate is selected by writing two consecutive bytes to a single
address, 000EH, designated BAUD-RATE. The 16 bits are used in two parts.
The serial port control register is used to select the serial port’s mode of
operation. Its REN bit (bit 3) is used to enable the receive function.
Data is transmitted by writing it to SBUF. When data has been received, it is
read from SBUF. Although Intel uses the name SBUF for both of these functions,
an SBUF read operation actually accesses a different register from that accessed
by an SBUF write operation.
The unsigned integer represented by the lower 15 bits of the baud rate register
defines a number B, where B has a maximum value of 32,767. The baud rate for
the four serial modes, using either XTAL1 or T2CLK as the clock source, is given
as follows:
Using XT ALT.
XTAL1 freq.
Mode 0: Baud rate = 4 x (g + ;B/0
XTAL1 freq.
Other modes: Baud rate = ;B 0
64 x (B + 1)
Using T2CLK:
T2CLK freq.
Mode 0: Baud rate = ------ 5-------- ; B # 0
T2CLK freq.
Other modes: Baud rate = —iv j~d— ; B # 0
to x Jd
Table 21.9 Baud rate selection
Baud rate is determined by the contents
of a 16-bit register at the location 000EH. Baud rate Baud register content
This register must be loaded sequentially 9600 8013H
with two bytes (least significant byte first).
4800 8026H
Common baud rate values are shown in
Table 21.9. The most significant bit written 2400 804DH
in the baud rate register selects the clock 1200 809BH
source. 8270 H
300
There is an option to use T2CLK as one
of the possible clock sources for timer 2. The
remaining bits, designated B, set up a divider for the selected clock source, to
obtain the desired baud rate. Even if a crystal clock frequency other than 12 MHz
is used with the chip, this approach still permits standard baud rates to be achieved.
The registers associated with the serial port are shown in Fig. 21.15 on page 633.
IOC1
16H (write)
200CH
200DH
Location 02H
7 6 5 4 3 2 1 0
X X X X Go
AD result register
1 0
_______ I
---------- > A/D channel
number
Status
-> 0= A/D conversion over
1 = conversion in progress
Data
bus x 8
i
Overflow
The output line (Q) is set when the PWM counter equals 0. The same output
line is cleared when the PWM counter equals the pulse width modulation control
register. When the counter overflows, the output is once again switched high. A
typical PWM output waveform and the registers used are shown in Fig. 21.20 (a)
and 21.20 (b), respectively.
Any waveform shown in Fig. 21.20 (a) can be generated, depending upon what
is written into the PWM control register. Changes in the duty cycle are made by
writing into the PWM register at the memory location 17H. The output is used to
drive a CMOS buffer, which in turn, has a DC component that is proportional to
the content of the PWM control register.
The PWM output shares a pin with pin 5 of port 2, so that the port output and
the PWM output cannot be used at the same time. The LSB in the register IOC1
has to be set to 1 for selecting the PWM function instead of the standard port
function.
Program for PWM output:
LDB AL, I0C1
ORB AL, #1
STB AL, I0C1 ; Select P2.5 for PWM output.
LDB PWM-CONTROL, #DUTY CYCLE ; Enter the required duty cycle
OVER: SJMP OVER
identification bits. These four bits identify which one (or more) of the four inputs
caused the input capture to occur.
The FIFO has a width of 16 + 4 = 20 bits for each entry and is seven entries
deep. In addition, the oldest entry is moved out of the FIFO to the HSI holding
register. Thus the FIFO and the HSI holding register combination can record up
to eight input events before an overrun of the FIFO occurs. The HSI lines can be
individually enabled or disabled using the bits in IOCO as shown in Fig. 21.22.
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
HSI.3 mode HSI.2 mode HSI.l mode HSI.O mode
In the HSI mode control register, each 2-bit field defines one of the four possible
modes for HSI, as shown in Table 21.10.
Table 21.10 HSI modes
The registers associated with the HSI lines are as shown in Fig. 21.24.
7 6 5 4 3 2 1 0
I/O control register 1
IOC1 (0016H) (write)
7 6 5 4 3 2 1 o
I/O status register 1
IOSI (0016H)
(read: clears bits 0-5)
7 6 5 4 3 2 1 0
Control of the HSI pin functions using IOCO is shown in Fig- 21.25. The
functions have been listed in Table 21.2.
Timer 2 CLK
HSI
7 6 5 4 3 2 1 0
Set when HSI holding register data Set when HSI FIFO X X X X X X
is available is full
...
Fig. 21.26 Input/output status register 1
Reading the HSI is done in two steps. First, the HSI status register is read. This
is done to obtain the current status of the HSI pins and to identify which pins have
changed at the recorded time. The HSI status register is at the memory location
06H. Its format is given in Fig. 21.27.
7 6 5 4 3 2 1 0
For each 2-bit status field, the lower bit indicates whether or not an event has
occurred on this pin at the time stored in HSI-TIME; the upper bit indicates the
current status of the pin.
After reading the HSI status register, the HSI time register is read. The HSI-
time register is located at 04H and 05H.
Program description:
One of the most frequent uses of HSI is to measure the time between events.
This can be used for frequency determination in lab instruments or for speed/
acceleration information when connected to pulse-type encoders.
The following program can be used to determine the duration for which the
signals on HSI.O remain high/low.
LD B I0C0. #1 ; Enable HSI.O.
LD B HSI-MODE, #OFH ; Look for either positive edge or
negative edge.
WAIT: ADD PERIOD. HIGH TIME. LOW TIME
JBC I0S1, 7, WAIT ; Wait while no pulse is entered.
LD B AX. HSI-STATUS ; Load status. Note that reading
HSI-TIME clears HSI-STATUS.
LD BX. HSI-TIME ; Load the HSI time.
JBS AX, 1. HSI-HI ; Jump if HSI.O is high.
HSI-LO:ST BX. LO-EDGE
SUB HIGH TIME, LO-EDGE, HI-EDGE
SJMP WAIT
HSI-HI:ST BX, HI-EDGE
SUB LOW TIME, HI-EDGE, LO-EDGE
SJMP WAIT
END
Procedure:
Connect a square wave source to the HSI.O line. The HSI.O line is terminated at
642 MICROPROCESSORS AND MICROCONTROLLERS
the 50-pin frame rate converter (FRC) connector Pl. The square wave amplitude
should be in the range of 0-5 V.
Note that this program is in a continuous loop. It records the on time and off
time, and calculates the period, which is the sum of the on and off times. To see
the result, reset the kit and view the content of the period register (58H). Period is
in terms of the number of timer 1 clock pulses.
Timer 2 input
Timer 2 reset
The heart of the HSO unit is the 23 x 8 CAM. Up to eight entries in the CAM is
looked at, once every 2ps. Each entry takes the form shown in Fig. 21.29.
Command Time
7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Channel
0-5-HSO.0-HSO.5
6-HSO.O and HSO.1
7-HSO.2 and HSO.3
8-B—Software timers 0,1,2, and 3
E—Timer 2 reset
F—Start A/D conversion
The 16 bits of each entry hold a value, which will be compared with the
content of either timer 1 or timer 2, every 2 ps. When a match occurs between a
CAM entry and the selected timer, the specified action is invoked and the entry is
automatically removed from the CAM.
The registers associated with the HSO unit are shown in Fig. 21.30.
While the HSO.O, HSO.l, HSO.2, and HSO.3 pins on the chip are permanently
dedicated to timer outputs, the HSO.4 and HSO.5 pins need not be wasted if the
six-timer output is not needed. Either of these two lines can serve as the timer
input. However, if we do want to use HSO.4 and/or HSO.5, we need to enable
these outputs in the IOC 1 register.
CAM is accessed through the HSO holding register shown in Fig. 21.31. Before
writing to the HSO holding register, we need to ensure that it is ready for an entry,
by checking bit 7 of IOSO as follows:
WAIT: JBS IOSO.7, WAIT
When the holding register is ready, the desired command is written to HSO-
command. Then the desired time is written to HSO time. To enter a command into
the CAM file, write the 7-bit command tag into the memory location 0006H. Then
write the time at which the action is to be carried out into the word address 0004H.
The typical code is as follows:
LDB HSO-COMMAND, #what-to-do
ADD HSO-TIME, TIMERI, #when-to-do-it
Upon writing the time value into the HSO time register, the HSO loading register
is initialized with the time and the command together. The command does not
actually enter the CAM file when an empty CAM register becomes available.
644 MICROPROCESSORS AND MICROCONTROLLERS
7 6 5 4 3 2 1 0
I/O control register 1
IOC1 (0016H) (write)
।_________________________________________ HSO-TIME_________
HSO holding register
Memory ready
Instruction fetch
Tie to +5V to use internal Address valid
ROM/EPROM tie to GND to +5 V/GND Read control
disable internal ROM or EPROM Write high control
Multiplexed bus
(b)
Fig. 21.33 (a) Expanded mode with 16-bit multiplexed bus; (b) Expanded mode with 8-bit
multiplexed bus
HARDWARE FEATURES OF 8096 647
Another choice is for the use of the data bus width. The chip can be configured
to access the 16-bit data bus or the 8-bit data bus. The hardware connections for
these two choices are to be designed and made accordingly.
Bit 1 of the CCB works with the external BUSWIDTH pin to determine the
data bus width. Note that bus width selection is necessary only when the EA pin is
tied low or when the external memory is accessed. While the BUSWIDTH pin is
tied either high or low, it can actually be changed during each bus cycle of normal
operation.
During 16-bit bus cycles, ports 3 and 4 contain the address multiplexed with
data, using address latch enable (ALE) to latch the address. In 8-bit bus cycles,
port 3 contains address multiplexed with data, while port 4 contains address
bits 8-15. The address bits on port 4 are valid throughout an 8-bit bus cycle.
Figure 21.35 shows the two options.
648 MICROPROCESSORS AND MICROCONTROLLERS
WRH signals are provided in place of WR and BHE, as shown in Fig. 21.37.
The WRL signal goes low for all byte writes to an even address and all word
writes. The WRH signal goes low for all byte writes to an odd address and all
word writes.
The write strobe mode is particularly well suited for the memory systems
latching data on the falling edge of the Write signal. The WRL signal is provided
for all 8-bit bus write cycles.
ADV ADV
Fig. 21.40 Intel 8096 EPROM memory expansion with 8-bit external data bus
Figure 21.41 shows the hardware connections for interfacing the 16K memory
with the 8096. Ports 3 and 4 are used as the multiplexed address and data bus. To
access one 16-bit data item at a time, two memory chips are needed—one for the
lower-order eight bits and the other for the higher-order eight bits.
Figure 21.42 shows the 16-bit memory interfacing using four 16K memory
chips, of which two 8K chips are for the lower-order data and two 8K chips are for
the higher-order data.
652 MICROPROCESSORS AND MICROCONTROLLERS
Fig. 21.41 Intel 8096 EPROM memory expansion with 16-bit external data bus
HARDWARE FEATURES OF 8096 653
POINTS TO REMEMBER
• Intel 8096 has five parallel ports—port 0, 1,2, 3, and 4. The parallel port pins are
also shared with the other peripherals such as ADC, PWM, and timers. Port 1 is used
exclusively for input/output purposes.
• The special function registers control all the internal peripherals of the 8096
microcontroller.
• There are two 16-bit timers in the 8096 and they can be clocked internally or from
external sources.
• The 8096 has 21 sources of interrupts.
• The 8096 has a serial port that can be operated in one synchronous mode and three
asynchronous modes.
• The 8096 has an 8-channel ADC with 1 O-bit data output. The programmer can program
to convert one or four channels at a time.
• The PWM output of the 8096 can be used to get an analog output corresponding to the
digital data.
• The high speed input unit is used to record the time at which an input pulse appears.
• The high speed output unit can be programmed to give the desired output at a desired
time. ADC conversion can be initiated by programming the HSO unit.
• The 8096 has many bus structure options for interfacing the external memory with it.
KEYTERMS^
HSI pins These pins can be used to record the time at which an external event occurs.
HSO unit This unit is used to trigger events at specific times.
Input/output control register 1 (IOC1) This register is used to choose the functions of
select port 2 pins, and enable or disable some interrupt sources.
Input/output status register 0 (IOSO) IOSO holds the current status of the HSO lines
and CAM.
Interrupt mask register This register is used to enable or disable the individual
interrupts, by setting or clearing the bits in it.
Interrupt pending register This register holds ‘ 1 ’ for the detected interrupts.
Mode 1 This is the standard asynchronous communication mode in the serial port.
Mode 2 This is the asynchronous ninth bit recognition mode.
Mode 3 This is the asynchronous ninth bit mode.
Port 0 This is an input-only port.
Port 1 This can be used for input or output.
Port 2 This port has three types of port lines—input-only, output-only, and quasi-bi-
directional.
Program status word (PSW) This register is a collection of Boolean flags, which
contain information concerning the state of the user’s program.
HARDWARE FEATURES OF 8096 655
REVIEW QUESTIONS |
MICROPROCESSOR SYSTEM
DEVELOPMENTS AND RECENT
TRENDS
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Developments in the features and manufacturing of the microcontroller family
• Development tools used for microcontroller applications
• C cross compiler for the 8051 series microcontrollers
• Programming the 8051 microcontrollers in C language
22.1 INTRODUCTION
Different types of microprocessors and microcontrollers are commercially
available in the market. General-purpose microprocessors have been developed
and improved by manufacturers and are largely used in personal computers.
However, with the advent of very large-scale integration in IC fabrication and the
tremendous decrease in the cost of these devices, microcontrollers have almost
replaced microprocessors in most applications.
Embedded systems are electronic systems consisting of microcontrollers or
other dedicated processor chips, which perform specific tasks. Embedded systems
differ from generic PC systems, in that they are made for a specific application and
cannot be used or programmed for general applications. They are supported by a
wide array of processors and processor architectures that are usually cost-sensitive
and have real-time constraints. Microcontrollers are used in embedded systems
such as computer printers, plotters, fax machines, photocopiers, telephones, and
automotive engine control mechanisms, and in electronic instruments such as
oscilloscopes, multimeters, planimeters, and IC testers. They are also found in
domestic appliances such as washing machines, microwave ovens, iPods, and
other music systems. An advanced automobile has 25 or more microcontrollers
in different control applications. The market for microcontrollers is so large that
they occupy about 80% of all CPU market in the world. A typical home in a
developed country such as the United States is likely to have around three dozen
microcontrollers embedded in various appliances. This chapter is dedicated to the
introduction of microcontroller development tools and the latest developments in
microcontroller chips.
660 MICROPROCESSORS AND MICROCONTROLLERS
with a basic debugger and other related features. IDE is basically front-end software
that integrates an editor, debugger, emulator, and downloader, along with features
such as animation and visualization. In general, most IDE softwares have two
modes—the build mode and the run/debug mode. The build mode supports source
code creation and revision. All the project, module, and edit functions are enabled
in this mode. The run/debug mode facilitates code execution and debugging.
Project management and source code editing functions are disabled. Commands
such as run, single step, set/clear breakpoints, and watch variables are enabled
only in this mode.
22.3.2 Debugger
A debugger is a software supported by IDE and is used to test run and debug the
user-written programs. The application code developed by the programmers can
be examined by running or executing the code on an instruction set simulator
(ISS). The debugger allows the user to halt the program simulation when specific
conditions are encountered. For example, a program may have errors and may crash
and as a result, the program cannot be executed any further. When the program
crashes, the debugger shows the position of the bug in the program code. Typical
debuggers offer more sophisticated functions such as running a program step by
step (single-stepping) and stopping (breaking) the program at user-defined points.
These break points are useful to the programmer to examine the current state of the
variables and data at that point in the program. Some debuggers have the ability
to modify the state of the program while it is running, rather than merely examine
it. A good debugger is very important and almost all microcontroller chip vendors
offer a debugger for the program developers. The availability of a good debugger
decides the selection of a specific microcontroller chip for an application.
22.3.3 Emulator
An emulator is another tool available in IDE to duplicate the functions of a
microcontroller system. Emulation refers to the ability of a computer program or
an electronic device to imitate another program or device. It makes the software
believe that the real device is running the software. A hardware emulator is an
emulator that takes the form of a hardware device.
An in-circuit emulator (ICE) is a hardware device that is used to debug the
software of an embedded system. It is usually in the form of a processor board
MICROPROCESSOR SYSTEM DEVELOPMENTS AND RECENT TRENDS 663
that has many internal signals brought out for the purpose of debugging. These
signals provide information about the state of the processor. In-circuit emulation
can also refer to the use of hardware emulation, where the emulator is plugged into
a system in place of a yet-to-be-built chip. The ICE allows the software element to
be run and tested on the actual hardware on which it is to be run, but still allows
single-stepping and isolation of faulty code. Recent ICEs enable a programmer
to access the on-chip debug circuit that is integrated into the CPU via JTAG, to
debug the software of an embedded system. They are sometimes called in-circuit
debuggers (ICDs). The ICE emulates the CPU. From the system’s point of view,
it has a real processor fitted, but from the programmer’s point of view, the system
being tested is under full control, allowing the developer to load, debug, and test
the code directly.
Host system
Writing a program, Target system
compiling, and Downloading with the
linking to create machine code microcontroller
machine code to the target, via
Microcontroller in
real application
while(l)
Example:
Write a C program to read the status of the switches on the port 3 lines and display
them on the LEDs connected to the port 1 lines. Use the hardware interface diagram
shown in Fig. 22.2.
unsigned int a;
P3 = OXFF;
whi1e(1)
A = P3;
Pl = A;
666 MICROPROCESSORS AND MICROCONTROLLERS
The program uses a header file, 805l.h. This file contains information about the
processor registers and special function registers. For example, ‘Pl’ is assigned to
point to port 1 through its port address in the SFR area. The variable ‘A’ is used to
temporarily store the data read from port 3 and then pass it on to port 1. Port 3 is
defined as an input port by writing all Is (OXFF) to it.
Example:
Write a C program to turn on alternate LEDs after a fixed time delay. Use the
hardware interface diagram shown in Fig. 22.2 and assume that the LEDs are
connected to the port 1 lines.
The C program for the example follows.
//include <8051.h>
voi d del ay ()
Uns1gned inti;
for (i = 0; i < 10000; i++);
1
void main(void)
whi led)
This program uses the bit data 01010101 to turn on alternate LEDs. The delay
routine is written as a separate function and is called from the main function. The
count value, 10,000, can be changed to change the delay.
Example:
Write a C program to scroll the lighting of the LEDs for the interface diagram
shown in Fig. 22.2.
The C program for the example follows:
//include <8051. h>
void main(void)
Nl = 0X01;
while (Nl! = 0x00) // Routine for scrolling interval.
MICROPROCESSOR SYSTEM DEVELOPMENTS AND RECENT TRENDS 667
Example:
Write a C program to count from 0 to 9 in the seven-segment display connected to
the port 1 pins, with a one second delay. Assume the connection diagram shown
in Fig- 22.3.
Table 22.1 shows the display data for the interfacing diagram shown in Fig. 22.3.
Table 22.1 LED segments and display data for the interface diagram 22.3
D7 D6 D5 D4 D3 D2 D1 DO Data for
Decimal
number f e d c b a
display (hex)
dP g
0 0 0 1 1 1 1 1 1 3F
1 0 0 1 1 0 0 0 0 30
2 0 1 0 1 1 0 1 1 5B
3 0 1 0 0 1 1 1 1 4F
0 1 1 0 0 1 1 0 66
4
0 1 1 0 1 1 0 1 6D
5
0 1 1 1 1 1 0 1 7D
6
0 0 0 0 0 1 1 1 07
7
0 1 1 1 1 1 1 1 7F
8
0 1 1 0 1 1 1 1 6F
9
668 MICROPROCESSORS AND MICROCONTROLLERS
The LSB of port 1 is connected to the ‘a’ segment and the MSB is connected to the
decimal point. The program shown uses a delay routine with a delay count. The
count used here is 33,000, but this count value has to be checked and changed for
different systems, clock frequencies, and compilers. A dummy ‘for’ loop is used to
achieve this delay. The data for display is stored in the disp_data array. This data
is taken from Table 22.1. The ‘disp’ count is incremented and the corresponding
display data is taken from the array and then passed to the port 1 pins. The counter
is reset to 0 when the count exceeds 9.
Unsigned int i ;
for (i = 0; i < count; 1++) ;
main ( )
Example:
Energy efficient lighting using a microcontroller.
The lighting in a house is automated using infra red (IR) sensors and a
microcontroller. With a microcontroller-controlled switching of lights, an energy
saving of 50% can be achieved. IR sensors are placed at the entrance to each
room. Whenever a person enters the room, the light in the room is automatically
turned on. Similarly, when a person leaves the room, the light is switched off. The
hardware circuit using the IR sensors, the microcontroller, and the relays is shown
in Fig. 22.4. Write a C program for the interface diagram shown in the figure, to
achieve efficient working of the system.
The system uses many arrays of IR LED and phototransistor pairs throughout
the house. They are placed at the entrance to the rooms or in the corridors. The IR
LEDs emit IR rays continuously. The corresponding IR phototransistors are placed
MICROPROCESSOR SYSTEM DEVELOPMENTS AND RECENT TRENDS 669
such that they receive the IR rays after reflection. The value of the resistance
connected to the collector of the phototransistor decides its sensitivity. When the
IR link between the IR LED and the transistor breaks due to a person passing
through the door, a low to high transition takes place at the transistor collector and
this is detected by the port 0 lines. The corresponding bulbs connected to port 1
are turned on or off.
The program given assumes that the absence of an IR signal from the IR detector
indicates whether a person enters or leaves the room. This means that the relay
switching is complemented upon receipt of every signal from the IR sensor. If the
relay has turned on the light, upon receipt of the next IR sensor signal, the light is
turned off. This system can be improved by introducing one more sensor at each
entrance to detect the direction of the person passing through (i.e., whether he/she
is entering or leaving the room). Accordingly, the program has to be changed.
P0 = 0X00;
Pl = 0X00;
while (1)
if (sensorl == 1)
ll Check the first sensor’s current
state.
RL1 = ~RL1;
// If its state is high, switch the
relay from on to off and off
to on.
else if (sensor2
!! Otherwise, check the second sensor.
RL2 = ~RL2;
RL3 = ~RL3;
RL6 = ~RL6;
elseif (sensor? == 1)
I
RL7 = ~RL7;
elseif (sensor8 == 1)
RL8 = ~RL8;
1
These programs are only indicative of the use of C language used for
programming 8051-based microcontrollers. Different C compilers have their
own syntax and the readers are requested to go through the manual of the
C compiler used by them. There are several compiling options for the programmer.
The programmer must be aware of all the options of the compiler, linker, and
optimizer.
POINTS TO REMEMBER^
REVIEW QUESTIONS
1. Write a program in C for the control of a stepper motor using the 8051 microcontroller.
Assume relevant hardware interfacing.
2. Write a program in C for a real-time clock with hours and minutes display, using seven
segment displays interfaced to the 8051 microcontroller.
CHAPTER 23 1
ADVANCED MICROPROCESSORS
AND MICROCONTROLLERS
LEARNING OUTCOMES
After studying this chapter, you will be able to understand the following:
• Architecture of the Intel 80186, 80286, 80386, 80486, and Pentium
• Protected mode operation in all microprocessors from the 80286 to the Pentium
• Paging mechanism present in all microprocessors from the 80386 to the Pentium
• Integer and floating-point pipeline operation in the Pentium
• Different versions of the Pentium microprocessor
• Hardware features of the PIC16F877 microcontroller and its family
23.1 INTRODUCTION
After the release of the 8086, Intel introduced the 80186, which is a 16-bit processor
having an architecture identical to that of the 8086. In addition, the 80186 has more
built-in hardware units, such as three timers, two DMA controllers, one interrupt
controller, and peripheral- and memory-select logic.
After the 80186, Intel released the 80286, which is also a 16-bit processor. In the
80286, Intel introduced protected mode addressing, also known as protected virtual
address mode (PVAM), which is an important milestone in the development of the
Intel X86 family. The concepts of four-level protection mechanism, descriptors,
and descriptor tables, the use of selectors, interrupt gates, and task management
were first introduced in the 80286. The 80286 processor can access 16 MB of
memory, as it has 24 address lines. The 80286 uses the numeric coprocessor 80287
for performing floating point operations.
After the 80286, Intel released the 80386, which was Intel’s first 32-bit
microprocessor. Most of the registers in the 80386 are 32 bits wide. The address
bus and data bus are also 32 bits wide. The 80386 processor can access 4 GB (1G
= 230) of memory, as it has a 32-bit address bus. The paging mechanism was first
introduced in the 80386, to efficiently handle virtual memory. The 80386 uses the
numeric coprocessor 80387 for performing floating-point operations.
After the 80386, Intel released the 80486, which is also a 32-bit microprocessor.
Most of the registers in the 80486 are 32-bits wide. The address bus and data bus
are also 32 bits wide. In addition, the 80486 has an on-chip Floating Point Unit
(FPU) and 8 KB of on-chip unified cache (both code and data are present in the
same cache). There is no need for a coprocessor in the 80486-based systems, due
to the inclusion of the FPU in the 80486 chip itself. The presence of cache memory
within the 80486 chip reduces the program execution time.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 673
After the 80486, Intel released the Pentium, which was Intel’s first two-issue
superscalar processor (i.e., two instructions that process integer type data can be
simultaneously decoded and executed in its U and V integer pipelines, when certain
conditions are satisfied). The Pentium has 8 KB on-chip dual cache (8 KB code
cache and 8 KB data cache) and on-chip FPU, which uses an eight-stage floating
point pipeline. The Pentium has a 64-bit data bus and a 32-bit address bus. Since the
demand for embedded applications is increasing, the number of microcontrollers
with more on-chip peripherals introduced in the market is also increasing day by
day. The PIC16F877 is one such microcontroller and is a product of Microchip
Corporation. The architecture and salient features of the microprocessors from the
80186 to the Pentium, and the features of the PIC16F877 are explained in Sections
23.2-23.8.
23.2.1 Architecture
The 80186 is a highly integrated 68-pin chip, which includes a CPU with
architecture identical to that of the 8086. The 80186 is available in 6, 8, 12, 16,
and 25 MHz versions. The functional block diagram of the 80186 is shown in
Fig. 23.1.
The 80186 processor includes the following subsystems:
(i) Clock generator
(ii) Programmable interrupt controller
(iii) Three 16-bit programmable timers/counters
(iv) Two programmable DMA controllers
(v) Chip select unit
(vi) Programmable control registers
(vii) Bus interface unit
(viii) Six-byte prefetch queue
The two-channel DMA unit of the 80186 performs transfers to or from any
combination of the FO space and memory space in either byte or word units. Each
DMA channel maintains independent source and destination pointers, which are
used to access the source and destination of the transferred data. The 80186 timer
unit contains three independent 16-bit timers/counters. Two of these timers can be
used to count external events, provide waveforms derived from either the CPU or
an external clock, or interrupt the CPU after a specified number of timer events.
The third timer counts only CPU clock cycles and can be used to interrupt the CPU
after a certain number of CPU clocks, give a count pulse to either or both of the
other two timers, or give a DMA request pulse to the integrated DMA unit, after a
programmable number of CPU clock cycles.
674 MICROPROCESSORS AND MICROCONTROLLERS
INT3/INTA1
The 80186 interrupt controller processes the interrupt requests from all the
internal and external sources. It can be directly cascaded as the master to two
external 8259As (programmable interrupt controllers). The 80186 integrated chip
select logic can be used to enable the memory or peripheral devices. Six output lines
from the integrated chip select logic are used for memory addressing and seven
output lines are used for peripheral device addressing. The integrated peripheral
and chip select circuitry is controlled by sets of 16-bit registers, accessed using
standard input, output, and memory access instructions. These peripheral control
registers are all located within a 256-byte block, which can be placed in either the
memory or the I/O space.
23.3.1 Architecture
The functional block diagram of the 80286 is shown in Fig. 23.2.
3 decoded
Registers Control instruction Instruction Instruction
Execution unit (EU) queue decoder unit(IU)
BUSY
INTR ERROR
There are four separate processing units in the 80286—bus unit (BU), instruction
unit (IU), execution unit (EU), and address unit (AU).
(i) The execution unit includes the ALU, general registers (which are the same
as in the 8086 and the 80186), and the control unit. The execution unit uses
its 16-bit ALU to execute instructions that are received from the instruction
unit.
(ii) The address unit includes the segment registers (which are the same as in
the 8086 and the 80186), an offset adder, and a physical address adder. The
address unit in the 80286 computes the physical addresses that will be sent
out to the memory or the I/O devices by the bus unit.
(iii) The bus unit includes the address latches and data transceivers, bus interface
and control circuitry, instruction prefetcher, and a six-byte instruction queue.
The bus unit performs all memory and I/O reads and writes, prefetches
instruction bytes, and controls the transfer of data to and from processor
extension devices such as the 80287.
(iv) The instruction unit includes an instruction decoder and a queue having
three decoded instructions. The instruction unit fully decodes up to three
676 MICROPROCESSORS AND MICROCONTROLLERS
prefetched instructions and holds them in a queue, where the execution unit
can access them.
1
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 677
base address of the segment present in the segment descriptor with the offset in the
logical address, the physical memory address is obtained, from where instruction
or data is transferred by the microprocessor, as shown in Fig. 23.3.
Fig. 23.3 Virtual to physical address conversion in the 80286 in protected mode
The different fields present in the 80286 segment descriptor are shown in
Fig. 23.4. The base (B23-B0) field represents the 24-bit base address of a segment
in the memory and hence, a segment can begin at any location in its 16 MB of
memory. The limit field (L15-L0) represents the last offset address found in a
segment. For example, if a segment starts at the memory location 800000H and
ends at the location 800F00H, the base field of that segment descriptor contains
800000H and the limit field contains 0F00H. The size of a segment can be between
1 byte and 64 KB in the 80286.
Byte no. <— 8 bits —> <— 8 bits —> Byte no.
7 OOH OOH 6
5 Access rights Base (B23-B16) 4
3 Base (B15-B0) 2
1 Limit (L15-L0) 0
The access rights byte in the segment descriptor, which is shown in Table 23.1,
indicates the complete characteristics of a segment. The privilege level of the
segment is encoded in the descriptor privilege level (DPL) bits. Information about
whether the segment is currently present in the physical memory or not is encoded
in the P bit. Information about whether the segment described by the segment
descriptor is code, a data segment, or a system segment (call gate or task segment)
is encoded in the S bit. Information about whether the segment is accessed by the
CPU or not is encoded in the A bit of the segment descriptor. For code segment or
data/stack segment, some more details are encoded in the bits E, ED/C, and W/R
in the segment descriptor.
678 MICROPROCESSORS AND MICROCONTROLLERS
In the protected mode operation of the 80286, the selector, which is located in
the segment register (CS, DS, SS, and ES) selects one of the segment descriptors,
either from the global descriptor table (GDT) or the local descriptor table (LDT).
The GDT contains global descriptors, which are segment descriptors of those
segments (that belong to the compiler, assembler, etc.) that can be used by all
programs in a multi-user system. There will be only one GDT in the system. The
LDT contains segment descriptors of those segments that belong to a particular
user or task. In a multi-user or multitasking environment, there will be as many
LDTs as the number of users or the number of tasks handled by the CPU. Since
GDT and LDT can contain a maximum of 8192 descriptors each, a total of 16,384
descriptors are available for an application (i.e., a task or a user) at any time. Since
the maximum size of a segment is 64 KB in the 80286, the maximum size of the
virtual memory available for a particular user or task is equal to 1 GB (= 16,384
x 64 KB).
Figure 23.5 shows the functioning of the 80286 segment register in the
protected mode. The upper 13 bits in the segment register are known as index-, it
selects one of the descriptors from among a maximum of 8192 descriptors present
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 679
in either the GDT or the LDT. The TI bit is known as table indicator. If TI = 0,
the 80286 refers to the GDT to select a descriptor, and if TI = I, the 80286 refers
to the LDT to select a descriptor. The two least significant bits of the 80286 form
the requestor privilege level (RPL), where 00 is the highest privilege level and 11
is the lowest privilege level. The RPL bits reflect the privilege level of the task
requesting memory access.
Selector
15 0 Physical base address Segment limit Other segment attributes from descriptor
Selector CS —
Selector SS — —
Selector DS — — — £
Selector ES — — —
the cache portion of the LDTR. The selection of an operand from the memory by
the microprocessor, using GDT, is shown in Fig. 23.8.
Fig. 23.10 Accessing memory using different addressing modes in protected mode in the 80286
The 80286 can execute all the instructions of the 80186. In addition, the
following new instructions were introduced in the 80286:
CTS Clear task-switched flag in machine status word.
LGDT address Load GDTR from address in the memory.
SGDT address Store GDTR in address in the memory.
LIDT address Load IDTR from address in the memory.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 685
Fig. 23.11 Flag register present in the 8086 to the Pentium microprocessors
point numbers (32 bits) from or to the memory in a single memory read or write
cycle. This increases the speed of execution of any program that manipulates
real numbers in the 80386. Most high-level language programs and database
management systems use real numbers for data storage. The 80386 processor uses
the 80387 (numeric coprocessor) to perform floating point operations.
The internal architecture of the 80386 is divided to three units—bus interface
unit, memory management unit, and the central processing unit. The central
processing unit is further divided into the execution unit and the instruction unit.
The execution unit has eight general-purpose and eight special-purpose registers,
which are used either for handling data or for calculation of the offset addresses.
The instruction unit decodes the opcode bytes received from the 16-byte instruction
queue and arranges them in a three-instruction decoded instruction queue, so as to
pass it to the control section for deriving the necessary control signals. The barrel
shifter increases the speed of execution of the shift and rotate instructions. The
32-bit multiplication operation can be executed within 1 ps by the multiply/divide
logic. The memory management unit (MMU) consists of a segmentation unit
and a paging unit. The segmentation
unit allows the use of two address General-purpose registers
components—segment and offset, 31 24 23 16 15 8 7 0
for relocability and sharing of code AH AX AL EAX
8086. There are two additional segment registers, FS and GS, which provide two
additional segments that can be accessed by a program.
The 80386 includes a memory management unit (MMU) that allows memory
resources to be allocated and managed by the operating system. The segment
registers, system address registers (GDTR and IDTR), and system segment
registers (LDTR and TR), with their corresponding descriptor registers, as present
in all processors from the 80386 to the Pentium, are shown in Fig. 23.14. The
segment descriptor registers are not available for the programmer; they are
internally used to store segment descriptor information such as base address, limit,
and attributes of different segments. These registers are automatically loaded when
the corresponding segment registers are loaded with new selectors. GDTR, IDTR,
LDTR, and TR are used to access the descriptor tables GDT, IDT, LDT, and TSS
descriptor, respectively.
Fig. 23.14 Segment registers and MMU registers with corresponding descriptor registers
The 80386 has three 32-bit control registers—CRO, CR2, and CR3 (which are
shown in Fig. 23.15), to hold the global machine status independent of the executed
task. The load and store instructions are available to access these registers. The
control register CR1 is reserved for use in future Intel processors. The bits PE and
PG (bits 0 and 31) in the CRO are used to enable protected mode operation and
paging, respectively. The CR3 is used to hold the base address of the page directory
in the memory. The CR2 is used to hold the linear address for which a page fault
(required page not being present in the physical memory) has occurred, and using
this address the operating system can load the required page in the physical memory
from the secondary memory. CR4 is present only in the Pentium.
690 MICROPROCESSORS AND MICROCONTROLLERS
The segment registers and their default offset Test control TR6
Fig. 23.17 Addressing of the memory when the 80386-Pentium operates in the
protected mode
Fig. 23.19 Accessing memory using different addressing modes in protected mode operation
Example:
(a)MOV EAX, [ESI x 4] ; EA is ESI x 4
(b)MOV ECX, [EBX + ESI x 2]; EA is EBX + ESI x 2
(c)MOV EDX, [EBX + EDI x 8 + 5]; EA is EBX + EDI x 8 + 5
of 1024 (= 2l0)PDEs. Hence, the size of a page directory is also 4 KB. Thus, Intel
uses a uniform size of 4 KB for each page, page table, and page directory.
Two-level paging scheme
The linear address is divided into three fields—directory, table, and offset. The
directory field in the linear address is 10 bits wide and is used to select one of the
PDEs stored in the page directory that gives the base address of a page table in
the memory. If all the bits in the directory field are 0, the first entry in the page
directory will be selected; if all the bits are 1, the last entry in the page directory
(1,024th entry) will be selected, and so on.
The table field in the linear address is 10 bits wide and is used to select one
of the PTEs stored in a page table that gives the base address of a page in the
memory. If all the bits in the table field are 0, the first entry in the page table will
be selected; if all the bits are 1, the last entry in the page table (1,024th entry) will
be selected, and so on.
The offset field in the linear address is 12 bits wide and is used to select one
of the bytes stored in a page in the memory. If all the bits in the offset field are 0,
the first byte in a page will be selected; if all the bits are 1, the last byte in the page
will be selected, and so on. It is to be noted that the offset field, in both the linear
address and the physical address, is the same during the address translation.
The page tables and page directory are also present in the memory along with
the pages. The control register CR3 in the 80386 is loaded with the base address
of the page directory to address it.
When the required page needed by the CPU is not present in the memory,
a page fault is said to have occurred. CR2 is used to hold the linear address for
which the page fault (required page not being present in the physical memory) has
occurred and using this address, the operating system can load the required page
in the physical memory from the secondary memory.
The formats of each PTE and PDE are shown in Figs 23.23 (a) and 23.23 (b).
696 MICROPROCESSORS AND MICROCONTROLLERS
The PTE contains a 20-bit page frame address, which indicates the base address
of a page in the memory that is obtained by appending 12 binary Os (or three
hexadecimal Os) to the right of the page frame address. For example, if the page
frame address of a page is 2FFFFH, the base address of a page in memory is
2FFFF000H. This is done because each page is stored from a 4 KB boundary in
the memory (i.e., the base address of each page contains 12 binary Os or three
hexadecimal Os). In addition, the PTE also contains some page information that is
also common to the PDE.
The P-bit indicates whether the PTE or PDE entry can be used in address
translation, i.e., converting a linear address to a physical address (P = 1) or not (P
= 0).
The access bit (A) is set by the processor before accessing the page. If A = 0
for a page, it means that that page has not been accessed by the processor so far.
The dirty (D) bit is set before a write operation to the page is carried out. The
D bit is undefined for a PDE.
The OS reserved bits are defined by the OS software.
31 12 11 10 9 8 7 6 5 4 3 2 1 0
U R
Page Frame Address 31-12 OS Reserved 0 0 D A 0 0 P
s W
(a)
31 12 11 10 9 8 7 6 5 4 3 2 1 0
LI R
Page Table Address 31-12 OS Reserved 0 0 D A 0 0 P
S W
(b)
The user/supervisor (U/S) Table 23.3 Function of the U/S and R/W bits in PTE
and read/write (R/W) bits are and PDE
used to provide protection under
R/W Permitted for Permitted for
the four-level protection mode 0 U/S
level 3 levels 2,1, orO
as shown in Table 23.3. Level 0
is the highest privilege level and 0 0 None Read-write
level 3 is the least privilege level. 0 1 None Read-write
Each PDE contains a 20-bit page
1 0 Read-only Read-write
table address, which indicates
1 1 Read-write Read-write
the base address of a page table
in the memory. The address is
obtained by appending 12 binary Os (or three hexadecimal Os) to the right of the
page table address. This is done because each page table is also stored from a 4
KB boundary in the memory (i.e., the base address of each page table contains 12
binary Os or three hexadecimal Os).
The conversion of a linear address to a physical address by the paging mechanism
in the 80386 involves certain time, since the processor has to refer to a page
directory and page table to execute the conversion. To reduce the time involved in
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 697
the conversion, a small cache memory called translation lookaside buffer (TLB) is
present in the 80386, which will contain the 32 recently-used linear addresses and
their corresponding physical addresses. Whenever a linear address is to be converted
to a physical address, the 80386 first refers to the TLB to check whether the linear
address is present. If it is not present, the condition is called ‘miss’, and the processor
refers to the page directory and page table to get the physical address; it also stores
both of them in the TLB for future reference. Instead, if the TLB contains the required
linear address, the condition is called hit, and the processor gets the physical address
from the TLB itself. This is illustrated in Fig. 23.24.
Fig. 23.24 Conversion of linear address to physical address using TLB and page table
(iii) The cache unit, which includes an 8 KB cache, storing both code and data,
and cache management logic. It is connected through a 64-bit inter-unit
transfer (data) bus to the segmentation unit, ALU, and FPU. The cache unit
is also directly connected to the paging unit, bus interface, and prefetcher
through 128 lines, permitting the prefetching of 16 bytes of instructions
simultaneously. The cache is four-way set-associative write-through with
16 bytes/line. In the write-through policy of cache, when there is a hit during
a write operation, both the cache and main memory are updated together.
(iv) The instruction decode unit, which receives three bytes of undecoded
instructions from the prefetcher queue and transmits the decoded instructions
to the control and protection test unit
(v) The control and protection test unit, which generates micro instructions
transmitted to other units and performs protection testing
(vi) The ALU, which includes general purpose register file, a barrel shifter, and
registers for microcode use
(vii) The FPU, which includes floating point registers, an adder, a multiplier, and
a shifter
(viii) The segmentation unit, which includes segmentation management logic,
descriptor registers, and break point logic.
(ix) The paging unit, which includes paging management logic and a 32-bit
entry TLB
Here also, there are four memory banks, each storing one byte of a double
word, as in the 80386. Bytes within a 32-bit double word are selected by the four-
byte enable signals BE3-BE0, as follows:
(i) BE3 is used to enable the bank containing the bits D31-D24.
(ii) BE2 is used to enable the bank containing the bits D23-D16.
(iii) BE1 is used to enable the bank containing the bits D15-D8.
(iv) BEO is used to enable the bank containing the bits D7-D0.
The 80486 cache is unified, holding both code and data. The 80486 uses a
unified cache due to its simplicity of design and higher hit rate in comparison with
a dual cache of the same total size. The 80486 cache has a four-way set-associative
organization, to increase the hit ratio. Each line in cache is 16 bytes long. During a
‘miss’ in the cache, a line is replaced with the missing line using the pseudo least
recently used (LRU) algorithm. There are two control bits—page cache disable
(PCD) and page write-through (PWT) in the control register CR3, and in all PTEs
and PDEs in the 80486 shown in Figs 23.26 (a) and 23.26 (b), which influence the
on-chip cache operation. The value of the PCD and PWT bits is also sent out on
pins PCD and PWT, respectively, of the 80486.
When PCD = 0, the on-chip caching of a page is enabled. The bit PCD alone does
not enable caching; it depends on the activation of the cache enable (KEN) input
signal and the status of the CD (cache disable) bit in the CRO. Thus, for the caching
to be enabled, we must have PCD = 0, CD = 0, and KEN = 0. When PWT = 1, we
have a write-through policy. In the write-through policy, whenever a write operation
is to be done in the cache memory, both the cache memory and the corresponding
700 MICROPROCESSORS AND MICROCONTROLLERS
12 11 10 9876543200
P P U R
Page Frame Address 31-12 OS Reserved 0 0 D A C W P
D T S W
(a)
12 11 10 9876543200
P P U. R
Page Table Address 31-12 OS Reserved 0 0 D A C W P
D T s W
Fig. 23.26 (a) Format of the 80486 PTE (b) Format of the 80486 PDE
location in the main memory are updated together. This causes the main memory
to obtain the results of a program immediately after the instructions of the program
are executed by the CPU. When PWT = 0, we have a write-back policy, in which
the result is first stored in the cache memory alone when an instruction is executed.
The result is copied in the corresponding location in the main memory only when
a ‘miss’ occurs in the cache memory. Since the internal cache is inherently write-
through, PWT is intended for an external second-level cache.
The Pentium has a five-stage integer pipeline, branching out into two paths U
and V in the last three stages as shown in Fig. 23.28. The Pentium pipeline stages
are as follows:
(i) PF (Prefetch)—The CPU prefetches the code from the code cache and
aligns the code to the initial byte of the next instruction to be decoded.
(ii) DI (First decode)—The CPU decodes the instruction to generate a control
word. A single control word causes direct execution of an instruction. More
complex instructions require microcoded control sequencing.
(iii) D2 (Second decode)—The CPU decodes the control word generated in
stage DI for subsequent use in the next execution (E) stage. In addition,
addresses for data memory references are generated.
702 MICROPROCESSORS AND MICROCONTROLLERS
hazard RAW arises when 12 starts reading the content of the destination of
Il before II writes the result in it, due to the overlapping of operations that
occur in pipeline operation.
(ii) WAW: Let II and 12 be two consecutive instructions in a program. Let
us assume that the destination (register or memory) of II is also used as
destination by 12. When the two instructions are executed without pipeline
operation, the result of II is first stored in the destination, and then 12 store
its result in the same destination. The data hazard WAW arises when 12
starts writing its result in the destination of II before II writes the result in
it, due to the overlapping of operations that occur in the pipeline operation.
This may be due to II having an addressing mode that needs more clock
cycles to calculate the effective address of the destination and 12 having
a simple addressing mode that needs lesser clock cycles to calculate the
effective address of the destination. This hazard is also known as out-of-
order write.
If the first of the two decoded instructions is a jump, it is forwarded to the U
pipeline for execution and no instruction is forwarded to the V pipeline. In the
case of wrongly predicted jump, the pipeline is delayed by three or four clock
cycles. When a jump instruction is first taken, the CPU allocates an entry in the
256-entry branch target buffer (BTB), to associate the jump instruction’s address
with its target address and to initialize the history used in the branch prediction
algorithm (an algorithm used to predict whether jump will take place or not, using
the previous history of execution of the same jump instruction). As instructions
are decoded, the CPU searches the BTB to determine whether it holds an entry for
a corresponding jump instruction. When there is a hit, the CPU uses the history
to determine whether the jump should be taken. If it should, the CPU uses the
target address to begin fetching and decoding instructions from the target path.
The jump is resolved early in the WB (write back) stage, and if the prediction was
incorrect, the CPU flushes the pipeline and resumes fetching instructions along the
correct path. The CPU updates the history of the jump instruction in the WB stage.
Correctly predicted jumps execute without any delay.
The Pentium has an eight-stage floating-point pipeline, illustrated in
Fig. 23.29.
PF D1 D2 E X1 X2 WF ER
D31-O24
between the microprocessor and the main memory) was either 60 MHz or 66 MHz,
depending on the version of the Pentium.
(Instruction fetch)
RTCU Instruction
queue
RTCL
8 Words
Instruction Instruction
Issue logic
JL_
IU BPU FPU
1 Words 2 Words
Data
Address
MMU 32-kbyte
cache
Physical address
UTLB n™~i TAGS (instruction and
BAT data)
array
Address
Data
Memory unit
4 Words
Read Write queue Data
queue Snoop 8 Words
Address
, Data
2 Words
System interface
instructions in parallel and the use of simple instructions with rapid execution
times yield high efficiency and throughput for this system. PowerPC 601 consists
of the following three execution units:
(i) A 32-bit integer unit (IU) having 32 general-purpose registers (GPRs)
(ii) A branch processing unit (BPU) featuring static branch prediction
710 MICROPROCESSORS AND MICROCONTROLLERS
MCLR/Vpp/THV E 1 40 RB7/PGD
RAO/ANO 2 39 RB6/PGC
RA1/AN1 3 38 RB5
RA2/AN2/VREf. E 4 37 □ RB4
RA3/AN3/Vreh 5 36 □ RB3/PGM
RA4/TOCKI E 6 35 RB2
RA5/AN4/SS E 7 34 RB1
RE0/RD/AN5 E 8 33 RB1/INT
RE1/WR/AN6 E 9 32 □
PIC 18F877
RE2/CS/AN7 E 10 31
Vdo E 11 30 □ RD7/PSP7
Vss 12 29 □ RD6/PSP6
OSC1/CLKIN 13 28 □ RD5/PSP5
OSC2/CLKOUT E 14 27 RD5/PSP4
RC0/T10S0/T1CKI E 15 26 E RC7/RX/DT
RC1/T10S1/CCP2 E 16 25 □ RC6/TX/CK
RC2/CCP1 E 17 24 □ RC5/SDO
RC3/SCK/SCL E 18 23 □ RC4/SDI/SDA
RDO/PSPO E 19 RD3/PSP3
RD1/PSP1 E 20 21 □ RD2/PSP2
From the pin diagram, it is observed that except for few pins, the others have
many functions multiplexed together. By proper programming of the PIC16F877,
we can use each pin for a particular function. Figure 23.33 shows the block diagram
of the PIC16F877, in which all the internal components and on-chip peripherals
present in the IC are seen. There are five ports in the PIC16F877—ports A, B. C,
D, and E. The flash program memory is used for storing programs and the RAM
(also called register file) is used for storing data. In the RAM, some portions are
used as special function registers, which are needed to program the various on-
chip peripherals of the PIC16F877. Direct or indirect addressing mode is used
for accessing the data in the RAM. The file selection register (FSR) is used for
indirect addressing. The status register contains the flags that are affected during
the execution of arithmetic and logical instructions. The W register acts as the
accumulator. There is an eight-level stack, which is used during subroutine call
and return, and during interrupt processing.
Table 23.4 Features of PIC16F87X family
Device Program memory Data ;EEPROM I/O 10-bit CCP MSSP USART Timers Comparators
SRAM l(bytes) A/D (PWM) 8/16-bit
Bytes # Single word SPI Master
(bytes ) (ch)*
instructions PC
Program 14 lb Port B
bus JI RAM Addr<1>
RBO/INO
[ Instruction reg [ /Addr MUX \ RB1
RB2
Direct Addr Indirect RB3/PGM
8fl addr RB4
RB5
| FSRreg RB6/PGC
RB7/PGD
Status reg
8 Port C
RC0/T10SO/T1CLK1
Power-up MUX RC1/T10SI/CCP2
timer I RC2/CCP1
RC3/SCK/SCL
Instruction Oscillator RC4/SDI/SDA
decode and start-up timer ALU RC5/SDO
control Power-on RC6/TX/CK
reset RC7/RX/DT
Timing Watchdog I Wreg 1 Port D
generation timer
OSC1/CLK1 Brown-out RDO/PSPO
OSC2/CLKO reset RD1/PSP1
R02/PSP2
In-circuit RD3/PSP3
debugger RD4/PSP4
Low-voltage RD5/PSP5
RD6/PSP6
programming RD7/PSP7
PortE
RE0/RD/AN5
MCLR V^V. RE1/WR/AN6
RE2/CS/AN7
Synchronous Voltage
Data EEPROM CCP1,2 serial port USART Comparator
reference
Table 23.5 lists the different PIC instructions recognized by the MPASM™
assembler of Microchip Corporation.
Table 23.5 Instruction set of the PIC16F877
For byte-oriented instructions, ‘F’ represents a file register designator and ‘D’
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 715
If ‘D’ is 1, the result is placed in the file Bit-oriented file register operations
register specified in the instruction. For bit- 10 9
13__________ 7 6
oriented instructions, ‘B’ represents a bit Opcode B
field designator, which selects the bit (one bit B = 3-bit bit address
between 0 and 7) affected by the operation, F = 7-bit file register address
while ‘F’ represents the address of the file Literal and control operations
General
register in which the bit is located. For literal
and control operations, ‘K’ represents an 8-
2____8 7 0
Opcode K (literal)
bit or 11 -bit constant or literal value.
K = 8-bit immediate value
One instruction cycle consists of four
Call and go to instructions only
oscillator periods. For an oscillator frequency
13 11 10 0
of 4 MHz, a normal instruction takes an
Opcode K (literal)
execution time of 1 ps. All instructions
K = 11 -bit immediate value
are executed within a single instruction
cycle, unless a conditional test is true or the
Fig. 23.34 General format of PIC
program counter is changed as a result of an instructions
instruction. When this occurs, the execution
takes two instruction cycles, with the second cycle executed as a NOP.
Any instruction that specifies a file register as part of the instruction performs a
read-modify-write (R-M-W) operation. The register is read, the data is modified,
and the result is stored according to either the instruction or the destination
designator ‘D’. A read operation is performed on a register even if the instruction
writes into that register. For example, the CLRF PORTC instruction will read port
C, clear all the data bits, and then write the result back into the port.
instructions.
23.9.4.2 Data Memory Organization
The data memory is partitioned into four Stack level 8
JH- Bank 1
.10 - Bank 2
n Bank 3
Figure 23.36 shows the register file map of the PIC16F877, showing the SFRs and
general-purpose registers.
General-purpose register file The register file can be accessed either directly
or indirectly through the file select register (FSR), as shown in Fig. 23.37. In the
direct addressing mode, the RP1 and RPO bits along with the seven bits in the
opcode of the instruction, select one of the register files. In indirect addressing
mode, the content of the FSR register along with the IRP bit is used for selecting
one of the registers. The bit IRP is located in the seventh bit of the status register.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 717
Indirect address* OOH Indirect address* 80H Indirect address* 100H Indirect address* 180H
To use indirect addressing to access a particular register in the register file, the
IRP bit and the FSR register must first be loaded with the value corresponding to
the address of the particular register. For example, to access the register with the
address 120H, the IRP bit is loaded with 1 (the MSB of the address) and FSR is
loaded with the value 20H. To access the content of the register through indirect
addressing, INDF (indirect through FSR) is used in the instruction. For example,
to clear the content of the register with the address 120H using indirect addressing,
the following instructions are used:
BSF STATUS. IRP ; Set the IRP bit in the status register.
MOVLW H’20 ; Move the 1i teral 20H to W.
MOVWF FSR ; Move the content of W to FSR.
CLRF INDF : Clear th e content of the register with the
address 120H.
Special function registers The SFRs are registers used by the CPU and
peripheral modules in the PIC for controlling the desired operation of the device.
These registers are implemented as static RAM. The SFR STATUS is discussed
here in detail. The bits in the STATUS register and their meaning are shown in
Fig. 23.38. The STATUS register contains the different flags (C, Z, DC, TO*, and
PD*) that are affected during the execution of the instructions.
bit 7 IRP: Register bank select bit (used for indirect addressing)
1 = Bank 2, 3 (100H-1FFH)
0 = Bank 0,1 (00H-FFH)
bit 6-5 RP1 :RPO (Register bank select bits (used for direct addressing)
11 = Bank 3 (180H-1FFH)
10 = Bank 2 (100H-17FH)
01 = Bank 1 (80H-FFH)
00 = Bank 0 (00H-7FH)
Each bank is 128 bytes
bit 4 TO: Time out
1 = After power-up, CLRWDT instruction or SLEEP instruction
0 = A WDT time-out occurred
bit 3 PD: Power-down bit
1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction
bit 2 Z: Zero bit
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
bit 1 DC: Digit carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)
(for borrow, the polarity is reversed)
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
bit 0 C: Carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)
1 = A carry-out from the most significant bit of the result occurred
0 = No carry-out from the most significant bit of the result occurred
Note: For borrow, the polarity is reversed. A subtraction is executed by adding
the two's complement of the second operand. For rotate (RRF, RLF)
instructions, this bit is loaded with either the high, or low order bit of
the source register.
Legend—R/W-n
R = Readable bit W = Writable bit PoR = Power on reset
n = Value at POR ‘T = Bit is set ‘0’= Bit is cleared * = Bit is unknown
Fig. 23.38 Bits in STATUS register located at addresses 03H, 83H, 103H, and 183H
mnemonics are present from the second column onwards. The statement written
after the semicolon in each instruction is the comment.
Example 23.1:
Write a PIC16F877 ALP to add two data, 90H and 8FH, and store the result in the
internal register file in the addresses 5OH (least significant byte) and 51H (most
significant byte).
L0W_BYTE EQU H’50 : Assign the address 50H to the lower_byte.
HIGH-BYTE EQU H’51 ; Assign the address 51H to the higher byte,
STATUS EQU 3 ; The address of the status register is 03H.
C EQU 0 ; The bit address of the carry (C) is 0.
ORG 0 ; The program starts at address 0 in the
flash memory.
MOVLW H’90 : Move the value 90H to W.
ADDLW H’8F : Add 8FH to W.
720 MICROPROCESSORS AND MICROCONTROLLERS
Example 23.2:
Write a PIC16F877 ALP to multiply two data, 85H and 1OH, and store the result
in the internal register file in the addresses 40H (least significant byte) and 41H
(most significant byte).
The program is based on repeated addition. By adding DATA1 repeatedly, i.e.,
as many times as the value of DATA2, the result is obtained.
DATA1 EQU H’85 ; DATA1 is equal to 85H.
DATA2 EQU H’10 ; DATA2 is equal to 10H.
RE61 EQU H ’ 30 ; The address of REG1 is 30H
REG2 EQU H’31 ; The address of REG2 is 31H.
L0W_BYTE EQU H’40 : The address of L0W_BYTE is 40H.
HIGH-BYTE EQU H’41 : The address of HIGH_BYTE is 41H.
STATUS EQU 3 ; The address of the status register
is 03H.
C EQU 0 ; The bit address of the carry (C) is
0.
ORG 0 : The program starts at address 0 in
the flash memory.
MOVLW DATA1 : Move DATA1 to W.
MOVWF REG1 : Move the data in W to REG1.
MOVLW DATA2 : Move DATA2 to W.
MOVWF REG2 ; Move the data in W to REG2.
CLRF LOW_BYTE ; Clear the contents of LOW_BYTE.
CLRF HIGH-BYTE : Clear the contents of HIGH_BYTE.
MOVLW H’O : Clear W.
ADD: ADDWF REG1. W ; Add DATA1 to W and store the result
1 n W.
BTFSS STATUS.C ; If carry is 1. skip the next
i nstructi on.
GOTO NO_CARRY : Go to NO-CARRY.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 721
Example 23.3:
Interface an 8-bit bipolar DAC with the PIC16F877 and write ALPs to generate
a square wave and a sine wave, and to vary the frequency and amplitude of the
signals.
Figure 23.39 shows the simplified
layout for interfacing an 8-bit bipolar DAC
with the PIC16F877. The 8-bit digital data
to the DAC is sent through port B of the
PIC16F877. If the digital data sent to the
DAC is OOH, the analog output voltage (Vo)
from the DAC will be -5 V. If the digital
data sent to the DAC is linearly increased,
the output voltage (Vo) linearly increases
from -5 V towards +5 V. When the digital
data is FFH, the analog output voltage from Fig. 23.39 Simplified layout for
the DAC will be +5 V. Let a 4 MHz crystal interfacing DAC with the 16F877
be connected to the PIC16F877. Under
this condition, the PIC instructions that need one instruction cycle for execution
get executed in 1 ps and those instructions that need two instruction cycles for
execution get executed in 2 ps.
(i) ALP to generate a square wave of 1 KHz with amplitude of 5 V
The number within brackets in the program indicates the number of instruction
cycles needed to execute the instruction. First, the value OOH is sent to the DAC
and the PIC waits for 0.5 ms. Then the value FFH is sent to the DAC and the
PIC waits for 0.5 ms. This is repeated to produce a square wave of 1 kHz with an
amplitude of 5 V.
PORTB EQU 6 ; The address of PORTB is 06.
STATUS EQU 3 : The address of STATUS is 03.
Z EQU 2 ; The address of the Z bit is 02.
COUNT EQU H’7C ; COUNT is equal to 7CH.
DATA1 EQU H’FF : DATA1 is equal to FFH.
REG1 EQU H’30 : The address of REG1 is 30H.
ORG 0 : The program starts at address 0 in
the flash memory.
START: MOVLW H’0 : Move OOH to W.
722 MICROPROCESSORS AND MICROCONTROLLERS
Varying the amplitude and frequency of the square wave By changing the
value of COUNT in the program, using this calculation, the frequency of the
square wave can be varied. By changing the value of DATA1 in the program, the
amplitude of the square wave can be varied. For example, if the value of DATA1
is H’7F (which is half of H’FF), the amplitude of the square wave signal is
2.5 V.
(ii) ALP to generate sine wave of 1 kHz with amplitude of 5 V
To generate a sine wave using a DAC, the number of samples in one cycle
of the sine wave is first chosen, based on the accuracy needed (more number of
samples in a cycle gives higher accuracy). The samples are sent to the DAC one
by one, with a delay time between the samples. The delay time is based on the
frequency of the signal. If the frequency of the signal is low, the delay time will be
high; if the frequency is high, the delay time will be low.
Let us assume that we want to generate a sine wave of 1 kHz with an amplitude
of 5 V. Let the number of samples in a cycle be 20. This means that each sample
must be taken with an angle interval of 18°, which is obtained by dividing the total
angle for one cycle of sine wave (360°) by the total number of samples in a cycle.
The 20 analog samples in a cycle and their corresponding digital value to be sent
to the DAC are shown in Table 23.7. The analog value is obtained by substituting
the angle 0 in the expression for the sine wave, 5sin0.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 723
Sample no. Angle in degree (0) Analog value of the sample Digital value to be sent to the
(V) DAC
1 0 0 7FH
2 18 1.55 A7H
3 36 2.94 CAH
4 54 4.05 E6H
5 72 4.76 F8H
6 90 5 FFH
7 108 4.76 F8H
8 126 4.05 E6H
9 144 2.94 CAH
10 162 1.55 A7H
11 180 0 7FH
12 198 -1.55 58H
13 216 -2.94 35H
14 234 -4.05 18H
15 252 -4.76 06H
16 270 -5 OOH
17 288 -4.76 06H
18 306 -4.05 18H
19 324 -2.94 35H
20 342 -1.55 58H
The digital value corresponding to the analog value of the sample during the
positive half cycle is calculated using the following formula:
Digital value in decimal form = 127 + (255/10) x analog value of the sample
(23.1)
In Equation 23.1, the value 127 represents the digital value corresponding to
the analog voltage of 0 V. The factor (255/10) represents the increment in the
digital value corresponding to a 1 V increment in the analog signal, since the
analog voltage range of 10 V (-5 V to 5 V) corresponds to the digital value range
of 255 (FFH to OOH) in the DAC.
The digital value corresponding to the analog value of the sample during the
negative half cycle is calculated using the following formula (since the analog
value is negative during the negative half cycle):
Digital value in decimal form = (5 + analog value of the sample) x (255/10)
(23.2)
Program
REG1 EQU H’50
REG2 EQU H’51
COUNT EQU H’OD
724 MICROPROCESSORS AND MICROCONTROLLERS
The count used in the delay subroutine is calculated using the following concept:
The total delay time between two samples sent to the DAC is the time spent in
the execution of the delay subroutine, plus the time spent in taking a sample value
from the register file, sending it to the DAC, calling the delay subroutine, and
checking whether all samples are sent, so as to begin the next cycle of the sine
wave. Let the first part of the total delay time be denoted by ‘m’ and the second
part by ‘n’.
Total delay time = Td = m + n (23.3)
The values of m and n are calculated as follows, with the approximation
that the DECFSZ instruction takes one instruction cycle for execution. This
approximation is valid because the condition in the DECFSZ instruction is satisfied
only once in one cycle of the sine wave in the main program, and only once in the
delay subroutine (when the count becomes zero, during which time it takes two
instruction cycles for execution). For a more accurate calculation of the count, this
factor must be considered and another 6 cycles (first 4 cycles at the label START
in the program and 2 cycles due to the GOTO START instruction at the end of the
start loop) must also be included in each cycle of the sine wave.
m = (1 + 1 + count (1 + 2) + 2) x time for one instruction cycle (23.4)
n = (l + l+ 2+ l + l + 2)x time for one instruction cycle (23.5)
Since the frequency (f) of the sine wave is 1 KHz, its time period (T) is 1 ms.
Since there are 20 samples in a cycle, the time between sending two samples (Td)
to the DAC must be 1ms divided by 20, which gives a value of 50 ps. If each
726 MICROPROCESSORS AND MICROCONTROLLERS
Varying the amplitude and frequency of the sine wave By changing the value
of COUNT in the program using these calculations, the frequency of the sine wave
can be varied. By changing the digital value of the samples stored in the register file
in the program, the amplitude of the sine wave can be varied. For example, if the
amplitude of the sine wave is 2.5 V, the analog value of the samples is calculated
using the relation 2.5sin9 for different values of 9, and their corresponding digital
values are stored in the register file.
The delay time in these programs is created using the software delay technique.
It can also be created using the on-chip timer in the PIC. It is left to the reader to
write the programs using the on-chip timer to generate different waveforms.
POINTS TO REMEMBEtf
■ The 80186 has the same architecture as the 8086, but it also has more on-chip peripherals
such as three timers, two DMA controllers, one interrupt controller, and peripheral and
memory select logic.
■ The protected mode operation was introduced in the 80286 by Intel; it exists in all the
Intel processors that were developed after the 80286 such as the 80386, the 80486, and
the Pentium.
■ There are four privilege levels present in protected mode operation—levels 00, 01, 02,
and 03. Level 00 is considered the highest privilege level and 11 the lowest.
■ A segment is accessed using a segment descriptor, which is present either in the GDT
or the LDT in the protected mode.
■ There is only one GDT and as many LDTs as there are tasks currently being executed
by the CPU in the 80X86-based system.
■ The segment descriptor contains the complete details of a segment such as base address,
size, access rights, and other information regarding the segment.
■ With the help of the task register and the task state segment, task switching is easily
accomplished in all the microprocessors from the 80286 to the Pentium.
■ The register size is increased to 32 bits and there are two additional segments, FS and
GS, present in the range of processors from the 80386. In addition, they can access 4
GB of main memory.
■ The paging mechanism introduced in the 80386 makes the handling of virtual memory
easier.
• The 80486 microprocessor has an on-chip 8 KB unified cache and floating point unit.
• The Pentium is the first two-issue superscalar processor introduced by Intel with the
help of its two-integer U and V pipelines.
• The floating-point pipeline in the Pentium makes the operation on floating-point
numbers faster.
ADVANCED MICROPROCESSORS AND MICROCONTROLLERS 727
KEY TERMS
BOR It helps reset the microcontroller when the supply voltage falls below a specified
value.
Dual cache This refers to the presence of two cache memories, one for code and the
other for data.
EEPROM It is a non-volatile memory that can be electrically erased and programmed.
Flash memory It is a non-volatile memory and a specific type of EEPROM that can be
electrically erased programmed in large blocks.
FPU It is used to perform floating point operations quickly.
FSR It is used for indirect addressing in the PIC.
GDT It contains the segment descriptor of those segments (that belong to the operating
system, the compiler, the assembler, etc.) that can be used by all programs in a multi-user
system. There is only one GDT in the system.
GDTR It holds the base address of the GDT in the memory.
I2C It is a standard for serial communication among many I/O devices and
microcontrollers.
IDT The interrupt descriptors for the different interrupt types, starting from the interrupt
type OOH, are successively stored in the IDT present in the memory.
IDTR It holds the base address of the IDT in the memory.
LDT It contains the segment descriptor of those segments that belong to a particular
user or task. In a multi-user or a multi-tasking environment, there are as many LDTs as the
number of users or the number of tasks handled by the CPU.
LDTR It holds the base address of the LDT in the memory.
Linear address When paging is enabled, the address obtained by adding the base address
from the segment descriptor and the offset address is called linear address.
Multitasking It is the ability of a CPU to execute many user programs simultaneously.
Paging It is an efficient method to handle virtual memory, in which a segment is divided
in to equal sized pages. Intel uses a page size of 4 KB.
PD It holds the page directory entry (PDE) of different page tables in the memory. The
base address of the PD is indicated by the control register CR3.
PDE It contains the base address of a page table and control information related to that
page table in the memory.
Physical address It is the address assigned to each location in the physical memory such
as RAM or ROM.
Pipeline operation It is a type of operation that simultaneously performs different stages
of an operation on different instructions in a pipeline by overlapping the fetching, decoding,
and execution of the different instructions.
728 MICROPROCESSORS AND MICROCONTROLLERS
Pointer In PVAM mode, the 16-bit selector and the 16/32-bit offset are combined to
form a 32/48-bit pointer type data.
Postscaler It is a hardware used to divide the output clock frequency of the timer by
some factor.
Prescaler It is a hardware used to divide the input clock frequency of the timer by some
factor.
Protected virtual address mode (PVAM) or protected mode addressing In this
mode of addressing, the 80286 can access 16 MB of physical memory and 1 GB of virtual
memory, whereas all processors from the 80386 to the Pentium can access 4 GB of physical
memory and 64 TB of virtual memory.
PT It holds the page table entry (PTE) of different pages in the memory.
PTE It contains the base address of a page and control information related to that page
in memory.
PWM It is a type of modulation in which the duty cycle of the output square pulse is
varied.
Real mode addressing In this mode addressing, the range of microprocessors from the
80286 to the Pentium can access only 1 MB of physical memory, similar to the 8086.
Register file This is the collection of registers that is in the data RAM of the PIC.
Segment descriptor It is an eight-byte entry in the LDT or the GDT, which contains the
physical base address for a segment, the size of the segment, and the access rights allotted
to the segment.
Selector In protected mode, the 16-bit value in a segment register is called selector; it is
used to select one of the segment descriptors in the LDT or the GDT.
SPI It is a standard for serial communication and can be used for I/O expansion in a
microcontroller.
Task switching When the time slot for the execution of the current task is over, the
current state of the CPU is saved in a TSS and then the CPU state for the next task is loaded
into the CPU registers from its TSS and execution begins. This is called task switching.
TLB It is a small cache memory that contains the recently used linear addresses and their
corresponding physical addresses, used with the paging mechanism.
TR It is the register that is used to select one of the TSS descriptors from the GDT.
TSS It is the segment in which the current state of the CPU is saved during task
switching.
Two-issue superscalar execution In this type of execution, two instructions are
simultaneously decoded and executed.
Unified cache It is a cache that contains both code and data.
Watchdog timer It is a timer used for resetting the microcontroller when it hangs while
executing the software.
REVIEW QUESTIONS
Decrement Specia
DCR A 3D 4 All except DAA 27 4 All flags
Cany CMA 2F 4 No flags
DCR B 05 4 All except STC 37 4 Only Carry
Carry CMC 3F 4 Only Carry
DCR C 0D 4 All except
Carry Rotate
DCR D 15 4 All except RLC 07 4 Only Carry
Carry RRC OF 4 Only Carry
DCR E ID 4 All except RAL 17 4 Only Carry
Carry RAR IF 4 Only Carry
DCR H 25 4 All except
Carry Immediate
DCR L 2D 4 All except ADI Byte C6 7 All flags
Carry ACI Byte CE 7 All flags
DCR M 35 10 All except SUI Byte D6 7 All flags
Carry SBI Byte DE 7 All flags
POPB Cl 12 NOP 00 4
POPD DI 12 HLT 76 5
POPH El 12
POP PSW Fl 12 Interrupt-relatec
RIM 20 4
XTHL E3 12 SIM 30 4
■ ... BAl
DPTR = 16-bit yj
MOV DPTR, #datal6
immediate data
INC SP:
PUSH <src>
MOV ‘@SP’, <scr>
MOV <dest>, ‘@SP’: y/
POP <dest>
DEC SP
Accumulator and yj y/
XCH A, <byte>
<byte>—exchange data
Accumulator and @Ri— y/
XCHD A, @Ri
exchange low nibbles
Read 8 bits addressed
MOVX A, @Ri Only indirect addressing mode
external RAM @Ri
Write 8 bits addressed
MOVX @Ri, A Only indirect addressing mode
external RAM @Ri
Read 16 bits addressed
MOVX A, @DPTR Only indirect addressing mode
external RAM @DPTR
Write 16 bits addressed
MOVX @DPTR, A Only indirect addressing mode
external RAM @DPTR
Read program memory
MOVC A, @A+DPTR Only indexed addressing mode
at (A + DPTR)
Read program memory
MOVC A, @A+PC Only indexed addressing mode
at (A + PC)
Addressing modes
Mnemonics Operation
Direct Indirect Register Immediate
CJNE A, <byte>, rel Jump if A <byte> yf yf
Key: —
Reg—8-bit or 16-bit register Offset 16—16-bit offset address
Mem—8-bit or 16-bit data in memory Segl6—16-bit segment address
Imm—8-bit or 16-bit immediate data Disp8—Signed 8-bit number
Port8—8-bit port address N—8-bit number
Displ6—Signed 16-bit number
APPENDIX
Key:
BD—Byte operand for destination BS—Byte operand for source
WD—Word operand for destination WS—Word operand for source
LD—Long word operand
APPENDIX E
CASE STUDIES
E.1 8051-BASED WASHING MACHINE CONTROL
The washing machine is an example of an appliance that uses modem technology.
It has developed over the years—from manual and semi-automatic to fully
automatic with advanced intelligent control algorithms. The automation of a
washing machine is easily achieved using programmed microcontrollers. In
general, washing in a washing machine is achieved by alternate agitation of the
clothes in detergent soaked water.
Let us see the important parts of the washing machine (Fig. E.l), so that we can
understand how a microcontroller can be used in the automation of the washing
machine.
There are two types of tubs in the washing machine—perforated inner tub and
solid outer tub. The clothes are loaded in the inner tub along with the water and
detergent powder. The holes in the inner tub are used for draining the water. The
external tub is used to cover the inner tub and support it.
The agitator located inside the inner tub of the washing machine is used for
cleaning the clothes. The rotation of the agitator disc and the blades produces
strong currents within the detergent soaked water. The rotation of the clothes
within detergent water enables the removal of the dirt particles from the fabric of
the clothes.
The agitator is coupled to a motor to produce rotary motion. The motors used
are multi-speed motors. The speed of the motor can be changed according to the
washing cycle, load, etc.
The timer is available in the washing machine to select the wash time for
the clothes by the user. In automatic washing machines, the time is selected
automatically by a controller, depending upon parameters such as the amount of
clothes inside and the dirt wash cycle.
In addition to these basic components for washing, washing machines have a
water inlet control valve, drain control valve, and pipe and rinsing mechanism.
The water flow control is done using solenoid valves. The coils in the solenoid
valve, when energized, open the valve and allow the water through it.
Some automatic washing machines may have a water pump to pump the
water automatically, heating units to heat the water before washing, and circuits
to determine wash cycles and duration. The advanced controllers in automatic
washing machines calculate the total weight of the clothes, find the quantity of
water and detergent required, and the total time required for washing the clothes.
After these calculations, the controls are issued to various components to effectively
complete washing and rinsing.
APPENDIX E—CASE STUDIES 753
Now let us consider the basic operations of the washing machine and see how
they can be implemented using an 8051 microcontroller. Washing is achieved
by alternate phases of agitation and stoppage. The duration of the agitation and
stoppage phases is selected differently for different wash cycles such as normal
and heavy. For example, during normal washing, agitation may take place for four
minutes and stoppage for four minutes. Heavy washing will then take six minutes
for agitation and two minutes for stoppage. Washing is followed by the rinse and
drain phases.
754 MICROPROCESSORS AND MICROCONTROLLERS
It is assumed that the user has to fill the inner tub with the clothes, detergent
powder, and required amount of water before starting the washing. Washing is
started once these parameters are set by the user. The inputs connected to the
microcontroller are as follows:
(i) Inputs from the wash cycle selection switch such as normal and heavy
(ii) Water level indicators—one for full and the other for empty
(iii) Start operation button
(iv) Stop operation button
The outputs connected to the microcontroller are as follows:
(i) Drive control signal to turn on the agitator motor
(ii) Drive control signal to reverse the motor direction
(iii) Water inlet control signal
(iv) Drain valve control signal
The hardware set-up for this type of semi-automatic washing machine control
is given in Fig. E.2.
-^vcc
Indicator +5V
8051 74LS240 ' >EDs \/\/y\7
P3.0 P1.0 $■
Wash
cycle P3.1 P1.1
selector P3.2 P1.2
P3.3 P1.3 WA------+6VDC
220Q
470Q
230VAC
-GND
50 Hz
SK100 L N
N/C, L—
Agitator on +6VDC ■
GND
The hardware uses two ports of the 8051—port 3 for inputs and port 1 for
outputs. Wash cycle selection is done through the least significant four bits of
port 3. There can be four types of wash cycles—gentle, normal, heavy, and rinse.
This selection can be done through a rotary switch. The higher-order four bits
of the port are used by the user to start and stop the washing. The water level is
detected for the full and empty levels using two level sensors. The level sensors
can be either a float with switch arrangement or of capacitance/contact type. The
contact type sensor uses two metal electrodes, one at the bottom of the tub and
connected to the ground terminal of the supply and the other placed at the point
where the water level is to be sensed. The water sensor relies upon the principle
that water is a good conductor and an open circuit or short circuit can be sensed
between the electrodes depending upon the water level. Here, three electrodes are
used—one for ground, one to sense whether the tub is full, and another to sense
whether the tub is empty.
Based on the reading sensed on the port 3 pins, the control algorithm gives
the output on the port 1 lines. The least significant bits of port 1 are used to light
indicator lamps. Here, four LEDs are used as indicator lamps—one for washing,
one for rinsing, one for draining water, and one for indicating the end of the wash
operation. The motor given in the figure is a single phase induction motor. The
supply to this motor is given through the relay, which is driven by the signal from
the microcontroller. It has two coils; the supply to them is phase-shifted using the
capacitor arrangement. The direction of motor rotation can be reversed by changing
the current direction in one of the coils. This is done by a relay arrangement, which
is again energized by the signal from the microcontroller.
The designers can change these options and also add other indicators according
to their requirements. The port 1 higher-order four bits are used for control
purposes. One bit is used for turning on the agitator motor, while the other decides
its direction. The other two in the present design are used to control the water inlet
valve and drain outlet valve.
The flowchart for the control of a washing machine is given in Fig. E.3.
The flowchart assumes that the user starts the washing process after selecting
the appropriate wash cycle—gentle, normal, heavy, and rinse. The algorithm, upon
sensing the start input, reads the wash cycle and decides the timings according to
the wash cycle set. After this the algorithm checks whether water is full in the tub
and if not, turns on the water inlet valve and fills the tub. Then the washing process
is done automatically by alternate agitate and stoppage phases of the agitator
motor for the predetermined duration. Once the wash cycle is over, the drain pipe
is opened and the tub water is emptied. The algorithm returns back to reading the
wash/rinse cycle. During this operation, the user can stop the process at any time
by giving a signal at the stop input. The algorithm scans the stop input periodically
for stopping the operation. When the stop input is available, the algorithm resets
the entire process and is reinitialized.
This algorithm is primitive; several hardware and software techniques are added
to make the washing machine a functionally complete versatile machine.
756 MICROPROCESSORS AND MICROCONTROLLERS
The requested floor calculation in this algorithm can be done by many methods.
This can be done based on pre-laid conditions such as moving the lift cabin in the
same direction until the requests in that direction are cleared. These conditions can
be changed based on the priority assigned to the floors. So the programmer can
decide the method for requested floor calculation based on the situation and the
requirements.
BIBLIOGRAPHY
Instruction cycle 51. 490, 710, 715, 721, Machine or processor control
726 instructions 49, 66, 303, 439
Instruction queue 425, 570, 675 MACRO 473
In-system programming (ISP) 661,662, MASM (Microsoft macro assembler)
664 458, 474, 475
Integrated development environment Matrix keyboard 234-238, 248, 249, 378
(IDE) 661,662, 664 Matrix keypad 234, 377-380
Interrupt Maximum mode 426-429, 519, 539,
hardware interrupt 29, 166, 168, 268, 570, 572, 576-579
351,485,495,570 MCS-51 series 304-306
non-maskable interrupt (NMI) 169, Memory
427, 488 cache memory 10-12, 672, 697, 699,
software interrupt 166, 167, 485, 489, 700
490 data memory 157, 306, 307, 336
Interrupt structure dynamic RAM (DRAM) 3, 230,
hardware 168 385-387, 706
interrupt service routine 156, 165, electrically erasable and
268, 344, 485, 492 programmable ROM (EEPROM)
interrupt vector address 165, 274, 13,401,404, 660,711
348, 624 erasable and programmable ROM
maskable 166, 168, 169, 268 (EPROM) 13, 175-183, 186, 187,
non-maskable 29, 166, 168, 268, 627 190, 423, 510, 511, 513, 579, 646,
non-vectored 165 650
software 166 flash memory 13, 401, 660, 661, 710
vectored 165 memory organization 166, 294,
Interrupt-driven data transfer 155-158, 306, 307, 507, 593, 693, 751, 716
260 primary memory 11,12
processor memory 11, 12
Liquid crystal display (LCD) 228-233, program memory 306, 317, 334, 338,
384-388,401 , 435, 593, 650,715,716
Logical instructions 46, 47, 318, 319, programmable ROM (PROM) 12, 13,
449,601,628,712 135, 175-177, 183-187, 242-250
Logical operation 35, 47, 67, 151, 314X random access memory (RAM) 3, 12,
559,602 . .j 13, 293, 294, 307-309, 409, 510,
Loosely-coupled configuration 539-541, 516, 593,594,716
556 read/write (R/W) memory 3, 228,
Low/even I/O bank 511-515 229, 384, 404, 405,696,719
r read-only memory (ROM) 3, 12, 20,
Machine control operation 35 175,294, 304, 458, 502,646
Machine cycle secondary memory 12, 689, 695
I/O read cycle 55, 572-576 static RAM (SRAM) 3,407, 661, 712
I/O write cycle 56, 572, 573, 575, 577 Memory control signals (MEMR) and
memory read machine cycle 52, 54, (MEMW) 176-180, 183, 184, 190,
59 279, 509
memory write machine cycle 54, 57 Memory direct addressing 38, 43, 315
opcode fetch machine cycle 52, 53 Memory-mapped I/O 156, 176, 190, 193
Machine language 4, 35, 40, 458, 465, Memory-mapped I/O access 155
664
764 MICROPROCESSORS AND MICROCONTROLLERS