20201222-Mpmc Digtal Notes
20201222-Mpmc Digtal Notes
LECTURE NOTES
B.TECH
Prepared by:
Mrs.G.Jyothi, Assistant Professor
Course Objectives: This course provides the students to understand operation and programming
of 8085 Microprocessor, develops real time applications using 8086 processor, understand the
basic concepts of 8051 Microcontroller and interfacing with I/O devices.
Text Books:
1. Ramesh Gaonkar, “Microprocessor Architecture, Programing and Application with 8085” ,
Penram, 5th Edition, 2002.
2. A.K.Ray, “Advanced Micro processors and Peripherals” 3rd Tata McGraw-Hill,Edition.
3. Mazidi, Mazidi&McKinlay, “The 8051 Microcontroller and Embedded Systems using
Assembly and C” 2nd Edition,PHI.
Reference Books:
1. D. V Hall TMH, “Microprocessors and Interfacing” 2nd Edition, 2006
2. K. Uday Kumar, B.S. Umashankar, “The 8085 Microprocessor: Architecture, programming
and Interfacing” Pearson, 2008.
3. Liu and Gibson, “Micro Computer System 8086/8088 Family Architecture, Programming and
Design” PHI, 2nd Edition
4. Kenneth. J. Ayala, Cengage Learning, “The 8051 Microcontroller” 3rd Edition, 2004.
E-Resources:
1. https://www.tutorialspoint.com › Microprocessor › Microprocessor – 8085 Architecture
2. http://www.cpu-world.com/CPUs/8086/
3. https://www.journals.elsevier.com/microprocessors-and-microsystems/
4. http://rtcmagazine.com/technologies/view/Microcontrollers
5. http://nptel.ac.in/courses/106108100/
6. http://nptel.ac.in/courses/108107029/
7. nptel.ac.in/courses/106108100/
Microprocessor and Microcontrollers
Module I
8085 Architecture
• Address Bus: It carries the address, which is a unique binary pattern used to identify
a memory location or an I/O port. For example, an eight bit address bus has eight lines
and thus it can address 28 = 256 different locations. The locations in hexadecimal
format can be written as 00H – FFH.
• Data Bus: The data bus is used to transfer data between memory and processor or
between I/O device and processor. For example, an 8-bit processor will generally
have an 8-bit data bus and a 16-bit processor will have 16-bit data bus.
• Control Bus: The control bus carry control signals, which consists of signals for
selection of memory or I/O device from the given address, direction of data transfer
and synchronization of data transfer in case of slow devices.
A typical microprocessor consists of arithmetic and logic unit (ALU) in association with
control unit to process the instruction execution. Almost all the microprocessors are based on
the principle of store-program concept. In store-program concept, programs or instructions
are sequentially stored in the memory locations that are to be executed. To do any task using a
microprocessor, it is to be programmed by the user. So the programmer must have idea about
its internal resources, features and supported instructions. Each microprocessor has a set of
instructions, a list which is provided by the microprocessor manufacturer. The instruction set
of a microprocessor is provided in two forms: binary machine code and mnemonics.
Microprocessor communicates and operates in binary numbers 0 and 1. The set of instructions
in the form of binary patterns is called a machine language and it is difficult for us to
understand. Therefore, the binary patterns are given abbreviated names, called mnemonics,
which forms the assembly language. The conversion of assembly-level language into binary
machine-level language is done by using an application called assembler.
Microprocessor and Microcontrollers
Evolution of Microprocessors
4-bit Microprocessors
The first microprocessor was introduced in 1971 by Intel Corp. It was named Intel
4004 as it was a 4 bit processor. It was a processor on a single chip. It could perform simple
arithmetic and logic operations such as addition, subtraction, boolean AND and boolean OR.
It had a control unit capable of performing control functions like fetching an instruction from
memory, decoding it, and generating control pulses to execute it. It was able to operate on 4
bits of data at a time. This first microprocessor was quite a success in industry. Soon other
microprocessors were also introduced. Intel introduced the enhanced version of 4004, the
4040.
8-bit Microprocessors
The first 8 bit microprocessor which could perform arithmetic and logic operations on
8 bit words was introduced in 1973 again by Intel. This was Intel 8008 and was later followed
by an improved version, Intel 8088. Some other 8 bit processors are Zilog-80 and Motorola
M6800.
16-bit Microprocessors
The 8-bit processors were followed by 16 bit processors. They are Intel 8086 and 80286.
32-bit Microprocessors
The 32 bit microprocessors were introduced by several companies but the most popular
one is Intel 80386.
Pentium Series
Instead of 80586, Intel came out with a new processor namely Pentium processor. Its
performance is closer to RISC performance. Pentium was followed by Pentium Pro CPU.
Pentium Pro allows multiple CPUs in a single system in order to achieve multiprocessing.
The MMX extension was added to Pentium Pro and the result was Pentiuum II.
The Pentium III provided high performance floating point operations for certain types of
computations by using the SIMD extensions to the instruction set. These new instructions
makes the Pentium III faster than high-end RISC CPUs.
Microprocessor and Microcontrollers
Registers
The 8085 includes six registers, one accumulator and one flag register, as shown in
Fig. 3. In addition, it has two 16-bit registers: stack pointer and program counter. They are
briefly described as follows.
The 8085 has six general-purpose registers to store 8-bit data; these are identified as
B, C, D, E, H and L. they can be combined as register pairs - BC, DE and HL to perform
some 16-bit operations. The programmer can use these registers to store or copy data into the
register by using data copy instructions.
Microprocessor and Microcontrollers
Accumulator
The accumulator is an 8-bit register that is a part of ALU. This register is used to store
8-bit data and to perform arithmetic and logical operations. The result of an operation is stored
in the accumulator. The accumulator is also identified as register A.
Flag register
The ALU includes five flip-flops, which are set or reset after an operation according to
data condition of the result in the accumulator and other registers. They are called Zero (Z),
Carry (CY), Sign (S), Parity (P) and Auxiliary Carry (AC) flags. Their bit positions in the flag
register are shown in Fig. 4. The microprocessor uses these flags to test data conditions.
Control Unit
Generates signals on data bus, address bus and control bus within microprocessor to carry
out the instruction, which has been decoded. Typical buses and their timing are described as
follows:
• Data Bus: Data bus carries data in binary form between microprocessor and other
external units such as memory. It is used to transmit data i.e. information, results of
arithmetic etc between memory and the microprocessor. Data bus is bidirectional in
nature. The data bus width of 8085 microprocessor is 8-bit.
• Address Bus: The address bus carries addresses and is one way bus from
microprocessor to the memory or other devices. 8085 microprocessor contain 16-bit
address bus and are generally identified as A0 - A15. The higher order address lines
(A8 – A15) are unidirectional and the lower order lines (A0 – A7) are multiplexed
(time-shared) with the eight data bits (D0 – D7) and hence, they are bidirectional.
• Control Bus: Control bus are various lines which have specific functions for
coordinating and controlling microprocessor operations. The control bus carries
control signals partly unidirectional and partly bidirectional. The following control
and status signals are used by 8085 processor:
• 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.
• RD (active low output): The Read signal indicates that data are being read
from the selected I/O or memory device and that they are available on the data
bus.
• WR (active low output): The Write signal indicates that data on the data bus
are to be written into a selected memory or I/O location.
• IO/M (output): It is a signal that distinguished between a memory operation
and an I/O operation. When IO/M = 0 it is a memory operation and IO/M = 1
it is an I/O operation.
• S1 and S0 (output): These are status signals used to specify the type of
operation being performed; they are listed in Table 1.
S1 S0 States
0 0 Halt
0 1 Write
1 0 Read
1 1 Fetch
2. Bus organization
The schematic representation of the 8085 bus structure is as shown in Fig. 5. The
microprocessor performs primarily four operations:
shown in Fig. 5.
The logic pin layout and signal groups of the 8085nmicroprocessor are shown in Fig. 6. All
the signals are classified into six groups:
• Address bus
• Data bus
• Control & status signals
• Power supply and frequency signals
• Externally initiated signals
• Serial I/O signals
Microprocessor and Microcontrollers
it goes high.
• HOLD: This signal indicates that a peripheral like DMA (direct memory access) controller is
requesting the use of address and data bus.
• HLDA: This signal acknowledges the HOLD request.
• INTR: Interrupt request is a general-purpose interrupt.
• INTA: This is used to acknowledge an interrupt.
• RST 7.5, RST 6.5, RST 5,5 – restart interrupt: These are vectored interrupts and have highest
priority than INTR interrupt.
• TRAP: This is a non-maskable interrupt and has the highest priority.
Serial I/O Signals:
• SID: Serial input signal. Bit on this line is loaded to D7 bit of register A using RIM instruction.
• SOD: Serial output signal. Output SOD is set or reset by using SIM instruction.
Fig. shows the circuit which generates MEMR, MEMW, IOR and IOW signals.
We know that for OR gate, when both the inputs are low then only output is low.
Microprocessor and Microcontrollers
The signal IO/M goes low for memory operation. This signal is logically ORed with RD and WR to get
MEMR and MEMW signals. When both RD and IO/M signals go low, MEMR signal goes low.
Similarly, when both WR and IO/M Signals go low, MEMW signal goes low. To generate IOR and
IOW signals for I/O operation, IO/M signal is first inverted and then logically ORed with RD and WR
signals.
Same truth table can be implemented using 3:8 decoder as shown in Fig
Each instruction in 8085 microprocessor consists of two part- operation code (opcode) and
operand. The opcode is a command such as ADD and the operand is an object to be
operated on, such as a byte or the content of a register.
Instruction Cycle: The time taken by the processor to complete the execution of an
instruction. An instruction cycle consists of one to six machine cycles.
Machine Cycle: The time required to complete one operation; accessing either the memory
or I/O device. A machine cycle consists of three to six T-states.
T-State: Time corresponding to one clock period. It is the basic unit to calculate execution
of instructions or programs in a processor.
• Opcode fetch
Microprocessor and Microcontrollers
• Operand fetch
• Memory read/write
• I/O read/write
• Memory read/write
• I/O read/write
• Interrupt request acknowledge
It is the first step in the execution of any instruction. The timing diagram of this cycle is
given in Fig. 7.
The following points explain the various operations that take place and the signals that are
changed during the execution of opcode fetch machine cycle:
T1 clock cycle
i. The content of PC is placed in the address bus; AD0 - AD7 lines contains lower bit
address and A8 – A15 contains higher bit address.
ii. IO/M signal is low indicating that a memory location is being accessed. S1 and S0
also changed to the levels as indicated in Table 1.
iii. ALE is high, indicates that multiplexed AD0 – AD7 act as lower order bus.
T2 clock cycle
i. The opcode available on the data bus is read by the processor and moved to the
instruction register.
ii. The RD signal is deactivated by making it logic 1.
T4 clock cycle
i. The processor decode the instruction in the instruction register and generate the
necessary control signals to execute the instruction. Based on the instruction further
operations such as fetching, writing into memory etc takes place.
Microprocessor and Microcontrollers
The memory read cycle is executed by the processor to read a data byte from memory. The
machine cycle is exactly same to opcode fetch except: a) It has three T-states b) The S0
signal is set to 0. The timing diagram of this cycle is given in fig 8
Microprocessor and Microcontrollers
The I/O write cycle is executed by the processor to write a data byte to I/O port
or to a peripheral, which is I/O mapped in the system. The processor takes three
T-states to execute this machine cycle. The timing diagram of this cycle is
given in Fig. 11.
The instruction and the corresponding codes and memory locations are given in Table 5.
Table 5 IN instruction
i. During the first machine cycle, the opcode DB is fetched from the
memory, placed in the instruction register and decoded.
ii. During 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
Microprocessor and Microcontrollers
bus and the data read from that port address is placed in the
accumulator.
8085 has 16 address lines (A0 - A15), hence a maximum of 64 KB (= 216 bytes)
of memory locations can be interfaced with it. The memory address space of
the 8085 takes values from 0000H to FFFFH.
The 8085 initiates set of signals such as IO/M , RD and WR when it wants to
read from and write into memory. Similarly, each memory chip has signals
such as CE or CS(chip enable
or chip select), OE or RD (output enable or read) and WE or WR (write enable
or write) associated with it.
When the 8085 wants to read from and write into memory, it activates IO/M , RD and WR
signals as shown in Table 8.
Table 8 Status of IO/M , RD and WR signals during memory read and write operations
Operation
IO/M RD WR
0 0 1 8085 reads data from memory
0 1 0 8085 writes data into memory
Microprocessor and Microcontrollers
When is IO/M high, both memory control signals are deactivated irrespective of the status
of RD and WR signals.
Ex: Interface an IC 2764 with 8085 using NAND gate address decoder such
that the address range allocated to the chip is 0000H – 1FFFH.
Specification of IC 2764:
Module II
Based on the design of the ALU and decoding unit, the microprocessor
manufacturer provides instruction set for every microprocessor. The instruction
set consists of both machine code and mnemonics.
An instruction is a binary pattern designed inside a microprocessor to
perform a specific function. The entire group of instructions that a
microprocessor supports is called instruction set. Microprocessor instructions
can be classified based on the parameters such functionality, length and
operand addressing.
Instructions, which are used to transfer data from one register to another register, from
memory to register or register to memory, come under this group.
EXAMPLES:
1. MOV r1, r2 (Move Data; Move the content of the one register to another). [r1] <-- [r2]
6. LXI rp, data 16. (Load register pair immediate). [rp] <-- data 16 bits, [rh] <-- 8 LSBs of
data.
9. LHLD addr. (Load H-L pair direct). [L] <-- [addr], [H] <-- [addr+1].
10. SHLD addr. (Store H-L pair direct) [addr] <-- [L], [addr+1] <-- [H].
13. XCHG. (Exchange the contents of H-L with D-E pair) [H-L] <--> [D-E].
2.Arithmetic instructions:
The instructions of this group perform arithmetic operations such as addition, subtraction;
increment or decrement of the content of a register or memory.
Microprocessor and Microcontrollers
Examples:
3. ADC r. (Add register with carry to accumulator). [A] <-- [A] + [r] + [CS].
4. ADC M. (Add memory with carry to accumulator) [A] <-- [A] + [[H-L]] [CS].
5 .ADI data (Add immediate data to accumulator) [A] <-- [A] + data.
6 .ACI data (Add with carry immediate data to accumulator). [A] <-- [A] + data + [CS].
7.DAD rp. (Add register pair to H-L pair). [H-L] <-- [H-L] + [rp].
10.SBB r. (Subtract register from accumulator with borrow). [A] <-- [A] – [r] – [CS].
11.SBB M. (Subtract memory from accumulator with borrow). [A] <-- [A] – [[H-L]] – [CS].
12.SUI data. (Subtract immediate data from accumulator) [A] <-- [A] – data.
13. SBI data. (Subtract immediate data from accumulator with borrow). [A] <-- [A] – data –
[CS].
3)Logical instructions:
The Instructions under this group perform logical operation such as AND, OR, compare,
rotate etc.
Microprocessor and Microcontrollers
Examples:
3.ANI data. (AND immediate data with accumulator) [A] <-- [A] ^ data.
6.ORI data. (OR immediate data with accumulator) [A] <-- [A] ˅ data.
7.XRA r. (EXCLUSIVE – OR register with accumulator) [A] <-- [A] xor [r]
8.XRA M. (EXCLUSIVE-OR memory with accumulator) [A] <-- [A] xor [[H-L]]
9.XRI data. (EXCLUSIVE-OR immediate data with accumulator) [A] <-- [A] xor data.
15. CPI data. (Compare immediate data with accumulator) [A] – data.
16. RAL (Rotate accumulator left) [An+1] <-- [An], [A0] <-- [A7],[CS] <-- [A7].
The content of the accumulator is rotated left by one bit. The seventh bit of the accumulator is
moved to carry bit as well as to the zero bit of the accumulator. Only CS flag is affected.
17. RAR. (Rotate accumulator right) [A7] <-- [A0], [CS] <-- [A0], [An] <-- [An+1].
The content of the accumulator is rotated right by one bit. The zero bit of the accumulator is
moved to the seventh bit as well as to carry bit. Only CS flag is affected.
18. RLC. (Rotate accumulator left through carry) [An+1] <-- [An], [CS] <-- [A7], [A0] <--
[CS].
Microprocessor and Microcontrollers
19. RRC. (Rotate accumulator right through carry) [An] <-- [An+1], [CS] <-- [A0], [A7] <--
[CS].
4)Branching Instructions:
This group includes the instructions for conditional and unconditional jump, subroutine call
and return, and restart.
Examples:
Unconditional jump:
MP addr (label). (Unconditional jump: jump to the instruction specified by the address).
[PC] <-- Label.
Conditional Jump:
Conditional Jump addr (label): After the execution of the conditional jump instruction the
program jumps to the instruction specified by the address (label) if the specified condition is
true. The program proceeds further in the normal sequence if the specified condition is not
true.
1.JZ addr (label). (Jump if ZF=1)
2.JNZ addr (label) (Jump if ZF=0)
3.JC addr (label). (Jump if CF=1)
4.JNC addr (label). (Jump if CF=0)
5.JP addr (label). (Jump if the result is plus)
6.JM addr (label). (Jump if the result is minus)
7.JPE addr (label) (Jump if even parity)
8. JPO addr (label) (Jump if odd parity)
9. CALL addr (label) (Unconditional CALL: call the subroutine identified by the operand)
10.CALL instruction is used to call a subroutine.
11. RET (Return from subroutine).
12. RST n (Restart) Restart is a one-word CALL instruction. The content of the program
counter is saved in the stack. The program jumps to the instruction starting at restart location.
1.IN port-address. (Input to accumulator from I/O port) [A] <-- [Port]
2.OUT port-address (Output from accumulator to I/O port) [Port] <-- [A]
3.PUSH rp (Push the content of register pair to stack)
4. PUSH PSW (PUSH Processor Status Word)
5. POP rp (Pop the content of register pair, which was saved, from the stack)
6.POP PSW (Pop Processor Status Word)
7. HLT (Halt)
8. XTHL (Exchange stack-top with H-L)
9. SPHL (Move the contents of H-L pair to stack pointer)
10.EI (Enable Interrupts)
11. DI (Disable Interrupts)
12. SIM (Set Interrupt Masks)
Microprocessor and Microcontrollers
Instruction Formats:
The Instruction Format of 8085 set consists of one, two and three byte instructions. The first byte is always
the opcode; in two-byte instructions the second byte is usually data; in three byte instructions the last two
bytes present address or 16-bit data.
For Example : MOV A, B whose opcode is 78H which is one byte. This Instruction and Data Format of 8085
copies the contents of B register in A register.
For Example : MVI B, 02H. The opcode for this instruction is 06H and is always followed by a byte data
(02H in this case). This instruction is a two byte instruction which copies immediate data into B register.
For Example : JMP 6200H. The opcode for this instruction is C3H and is always followed by 16 bit address
(6200H in this case). This instruction is a three byte instruction which loads 16 bit address into program
counter.
The 8085A microprocessor has 8-bit opcodes. The opcode is unique for each Instruction and Data Format of
8085 and contains the information about operation, register to be used, memory to be used etc. The 8085A
identifies all operations, registers and flags with a specific code. For example, all internal registers are
identified as shown in the Tables 2.1(a) and 2.2(b).
Microprocessor and Microcontrollers
Similarly, there are different codes for each opera are identified as follows :
Note : DDD defines the destination register, SSS defines the source register and DD defines the register pair.
• Addresses
• Numbers/Logical data and
• Characters
Addresses : The address is a 16-bit unsigned integer ,number used to refer a memory location.
Numbers/Data : The 8085 supports following numeric data types.
• Signed Integer : A signed integer number is either a positive number or a negative number. In 8085,
8-bits are assigned for signed integer, in which most significant bit is used for sign and remaining
seven bits are used for Sign bit 0 indicates positive number whereas sign bit 1 indicates negative
number.
• Unsigned Integer : The 8085 microprocessor supports 8-bit unsigned integer.
• BCD : The term BCD number stands for binary coded decimal number. It uses ten digits from 0
through 9. The 8-bit register of 8085 can store two digit BCD
Characters : The 8085 uses ASCII code to represent characters. It is a 7-bit alphanumeric code that
represents decimal numbers, English alphabets, and other special characters.
The stack is a LIFO (last in, first out) data structure implemented in the RAM area and is used to store
addresses and data when the microprocessor branches to a subroutine. Then the return address used to get
pushed on this stack. Also to swap values of two registers and register pairs we use the stack as well.
In the programmer‘s view of 8085, only the general purpose registers A, B, C, D, E, H, and L, and the
Flags registers were discussed so far. But in the complete programmer’s view of 8085, there are two more
special purpose registers, each of 16-bit width. They are the stack pointer, SP, and the program counter, PC.
The Stack Pointer register will hold the address of the top location of the stack. And the program counter is a
register always it will hold the address of the memory location from where the next instruction for execution
will have to be fetched. The complete programmer's view of 8085 is shown in the following figure.
Microprocessor and Microcontrollers
SP is a special purpose 16-bit register. It contains a memory address. Suppose SP contents are FC78H, then
the 8085 interprets it as follows.
Memory locations FC78H, FC79H, ..., FFFFH are having useful information. In other words, these locations
are treated as filled locations. Memory locations FC77H, FC76H, ..., 0000H are not having any useful
information. In other words, these locations are treated as empty locations.
On a stack, we can perform two operations. PUSH and POP. In case of PUSH operation, the SP register gets
decreased by 2 and new data item used to insert on to the top of the stack. On the other hand, in case of POP
operation, the data item will have to be deleted from the top of the stack and the SP register will get increased
by the value of 2.
Thus, the contents of SP specify the top most useful location in the stack. In other words, it indicates the
memory location with the smallest address having useful information. This is pictorially represented in the
following figure –
Subroutine:
In computers, a subroutine is a sequence of program instructions that perform a specific task, packaged
as a unit. This unit can then be used in programs wherever that particular task have to be performed. A
subroutine is often coded so that it can be started (called) several times and from several places during one
execution of the program, including from other subroutines, and then branch back (return) to the next
instruction after the call, once the subroutine’s task is done. It is implemented by using Call and Return
instructions. The different types of subroutine instructions are
Unconditional Call instruction –
CALL address is the format for unconditional call instruction. After execution of this instruction program
control is transferred to a sub-routine whose starting address is specified in the instruction. Value of PC
(Program Counter) is transferred to the memory stack and value of SP (Stack Pointer) is decremented by 2.
Conditional Call instruction – Microprocessor and Microcontrollers
In these instructions program control is transferred to subroutine and value of PC is pushed into stack only if
condition is satisfied.
INSTRUCTION PARAMETER COMMENT
Unconditional Return instruction –RET is the instruction used to mark the end of sub-routine. It has
no parameter. After execution of this instruction program control is transferred back to main program
from where it had stopped. Value of PC (Program Counter) is retrieved from the memory stack and
value of SP (Stack Pointer) is incremented by 2.
Conditional Return instruction –
By these instructions program control is transferred back to main program and value of PC is popped
from stack only if condition is satisfied. There is no parameter for return instruction.
INSTRUCTION COMMENT
Advantages of Subroutine –
1. Decomposing a complex programming task into simpler steps.
2. Reducing duplicate code within a program.
3. Enabling reuse of code across multiple programs. Microprocessor and Microcontrollers
4. Improving tractability or makes debugging of a program easy.
Definition: Assembler directives are the instructions used by the assembler at the time of assembling a
source program. More specifically, we can say, assembler directives are the commands or instructions that
control the operation of the assembler.
Assembler directives are the instructions provided to the assembler, not the processor as the processor has
nothing to do with these instructions. These instructions are also known as pseudo-instructions or pseudo-
opcode.
So, assembler directives:
Assembler:
We know that assembly language is a less complex and programmer-friendly language used to program the
processors.
In assembly language programming, the instructions are specified in the form of mnemonics rather in the
form of machine code i.e., 0 and 1.
But the microprocessor or microcontrollers are specifically designed in a way that they can only understand
machine language.
Thus assembler is used to convert assembly language into machine code so that it can be understood and
executed by the processor.
Therefore, to control the generation of machine codes from the assembly language, assembler directives are
used.
However, machine codes are only generated for the program that must be provided to the processor and not
for assembler directives because they do not belong to the actual program.
Ex:Num2 DW 1020H,4216H
These two 16-bit data 1020H and 4216H are stored at 4 consecutive locations in the memory Num2.
EQU: Equate
Microprocessor and Microcontrollers
It is used to assign any numerical value or constant to the variable.
ORG: Origin
This directive is used at the time of assigning starting address for a module or segment.
In this section, we are going to study how to implement these Programming Techniques in Microprocessor
8085 assembly language and some programming examples using them.
Looping : In this Programming Techniques in Microprocessor 8085, the Program is instructed to execute
certain set of instructions repeatedly to execute a particular task number of times. For example, to add ten
numbers stored in the consecutive memory locations we have to perform addition ten times.
Counting : This technique allows programmer to count how many times the instruction/set of instructions are
executed.
Indexing : This Programming Techniques in Microprocessor 8085 allows programmer to point or refer the
data stored in sequential memory locations one by one. Let us see the program loop to understand looping,
counting and indexing.
The Program loop is the basic structure which forces the processor to repeat a sequence of instructions. Loops
have four sections.
• Initialization section.
• Processing section.
• Loop control section
• Result section.
Microprocessor and Microcontrollers
As you know microprocessor system consists of two basic components, Hardware and software. The software
component controls and operates the hardware to get the desired output with the help of instructions. To
execute these instructions, microprocessor takes fix time as per the instruction, since it is driven by constant
frequency clock. This makes it possible to introduce delay for specific time between two events. In the
following section we will see different delay implementation techniques.
3. Code Conversion:
This Programming Techniques in Microprocessor 8085 is to translate a number represented using one coding
system to another. For example,, when we accept any number from the keyboard it is in ASCII code. But for
processing, we have to convert this number in its hex equivalent. The code conversion involves some basic
conversions such as
The process of specifying the data to be operated on by the instruction is called addressing.
The various formats for specifying operands are called addressing modes. The 8085 has the
following five types of addressing:
I. Immediate addressing
II. Memory direct addressing
III. Register direct addressing
IV. Indirect addressing
V. Implicit addressing
Immediate Addressing:
In this mode, the operand given in the instruction - a byte or word – transfers to the
destination register or memory location.
Ex: MVI A, 9AH
• The operand is a part of the instruction.
• The operand is stored in the register mentioned in the instruction.
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.
Ex: LDA 850FH
This instruction is used to load the content of memory address 850FH in the accumulator.
Register Direct Addressing:
Register direct addressing transfer a copy of a byte or word from source register to
destination register.
Ex: MOV B, C
It copies the content of register C to register B. Microprocessor and Microcontrollers
Indirect Addressing:
Indirect addressing transfers a byte or word between a register and a memory location.
Ex: MOV A, M
Here the data is in the memory location pointed to by the contents of HL pair. The data is
moved to the accumulator.
Implicit Addressing
In this addressing mode the data itself specifies the data to be operated upon.
Ex:CMA
The instruction complements the content of the accumulator. No specific data or operand is
mentioned in the instruction.
MVI A,72
MOV B,A
ANI 0FH
MOV C,A
MOV A,B
ANI 0F0H
JZ BCD1
RRC
RRC
RRC
RRC
MOV D,A
XRA A
MVI E,0AH
SUM:ADD E
DCR D
JNZ SUM
BCD1:ADD C
MOV A,C
STA 2000H
HLT
RESULT: 48
MVI A,8AH
MVI B,64H
MVI C,0AH
MVI D,00H
MVI E,00H
STEP1:CMP B
JC STEP2
SUB B
INR E
JMP STEP1 Microprocessor and Microcontrollers
STEP2:CMP C
JC STEP3
SUB C
INNR D
JMP STEP2
STEP3:STA 2000H
MOV A,D
STA 2001H
MOV A,E
STA 2002H
HLT
Input:8AH
Output:2000h-8,2001h-3,2002h-1
Binary to ASCII
LDA 2050
CALL 2500
STA 3050
LDA 2050
RLC
RLC
RLC
RLC
CALL 2500
STA 3051
HLT
2500:ANI 0FH
CPI 0AH
JNC NEXT
ADI 30
RET
NEXT:ADI 37H
RET
Input:2050-4AH
Output:3050-41H
3051-34H
ASCII to Binary
LDA 2000H
SUI 30H
CPI 0AH
JC NEXT
SUI 07H
NEXT:STA 2001
HLT
LXI H,2050
MOV A,M
MOV D,A
LXI B,3050
ANI 0F0H Microprocessor and Microcontrollers
RRC
RRC
RRC
RRC
CALL SUB
INX B
MOV A,D
ANI 0FH
CALL SUB
INX B
INX H
SUB:LXI H,4050
ADD L
MOV L,A
MOV A,M
STAX B
RET
Input:2050h-34
Output:3050h-4F
3051h-66
Ascending Order
LXI H,2300
MVI C,03
DCR C
START:MOV D,C
CHECK:MOV A,B
INX H
CMP M
JC NEXTBYTE
MOV B,M
MOV M,A
DCX H
MOV M,B
INX H
NEXTBYTE:DCR D
JNZ CHECK
DCR C
JNZ START
HLT
Descending Order
LXI H,2300
MVI C,03
DCR C
START:MOV D,C
CHECK:MOV A,B
INX H
CMP M
JNC NEXTBYTE
MOV B,M
MOV M,A
DCX H
MOV M,B Microprocessor and Microcontrollers
INX H
NEXTBYTE:DCR D
JNZ CHECK
DCR C
JNZ START
HLT
Addition:
MVI C,00
LDA 4000H
MOV B,A
LDA 4001H
ADD B
JNC LOOP
INR C
LOOP:STA 4002H
MOV A,C
STA 4003H
HLT
Subtraction
MVI C,00H
LDA 4000H
MOV B,A
LDA 4001H
SUB B
JNC LOOP
CMA
INR A
INR C
LOOP:STA 4002H
MOV A,C
STA 4003H
HLT
Multiplication:
MVI D,00H
MVI A,00H
LXI H,4150H
MOV B,M
INX H
MOV C,M
LOOP:ADD B
JNC NEXT
INR D
NEXT:DCR C
JNZ LOOP
STA 4152H
MOV A,D
STA 4153H
HLT
Division:
LXI H,4150H Microprocessor and Microcontrollers
MOV B,M
MVI C,00H
INX H
MOV A,M
NEXT:CMP B
JC LOOP
SUB B
INR C
JMP NEXT
LOOP:STA 4152H
MOV A,C
STA 4153H
HLT
Logical Operations:
LDA 4000H
MOV B,A
LDA 4001H
ANA B
STA 4002H
LDA 4001H
ORA B
STA 4003H
LDA 4001H
CMA
STA 4004H
LDA 4001H
XRA B
STA 4005H
HLT
***********************************************************************
Microprocessor and Microcontrollers
UNIT-III
8086 Architecture
Introduction to Microprocessors
A microprocessor is a computer processor which incorporates the functions of
a computer's central processing unit (CPU) on a single integrated circuit (IC), or at
most a few integrated circuits
The microprocessor is a multipurpose, clock driven, register based, digital-
integrated circuit which accepts binary data as input, processes it according to instructions
stored in its memory, and provides results as output. Microprocessors contain both
combinational logic and sequential digital logic. Microprocessors operate on numbers and
symbols represented in the binary numeral system.
Generation of Microprocessors:
➢ INTEL 4004 ( 1971)
4-bit microprocessor
4 KB main memory
45 instructions
PMOS technology
was first programmable device which was used in calculators
➢ INTEL 8008 (1972)
8-bit version of 4004
16 KB main memory
48 instructions
PMOS technology
Slow
➢ Intel 8080 (1973)
• 8-bit microprocessor
64 KB main memory
2 microseconds clock cycle time
500,000 instructions/sec
10X faster than 8008
NMOS technology
Drawback was that it needed three power supplies.
Microprocessor
Small computers (Microcomputers) were and Microcontrollers
designed in mid
1970’s
Using 8080 as CPU.
➢ INTEL 8086/8088
Buses
Microprocessor and Microcontrollers
The registers AX, BX, CX, and DX are the general 16-bit registers.
Segment registers:
To complete 1Mbyte memory is divided into 16 logical segments. The complete 1Mbyte
memory segmentation is as shown in fig 1.5. Each segment contains 64Kbyte of memory.
There are four segment registers.
Stack segment (SS) is a 16-bit register containing address of 64KB segment with
program stack. By default, the processor assumes that all data referenced by the stack
pointer (SP) and base pointer (BP) registers is located in the stack segment. SS register
can be changed directly using POP instruction. It is used for addressing stack segment of
memory. The stack segment is that segment of memory, which is used to store stack data.
Data segment (DS) is a 16-bit register containing address of 64KB segment with
program data. By default, the processor assumes that all data referenced by general
registers (AX, BX, CX, DX) and index register (SI, DI) is located in the data segment. DS
register can be changed directly using POP and LDS instructions. It points to the data
segment memory where the data is resided.
Extra segment (ES) is a 16-bit register containing address of 64KB segment, usually
with program data. By default, the processor assumes that the DI register references the
ES segment in string manipulation instructions. ES register can be changed directly using
POP and LES instructions. It also refers to segment which essentially is another data
segment of the memory. It also contains data.
Microprocessor and Microcontrollers
The pointers contain within the particular segments. The pointers IP, BP, SP usually
contain offsets within the code, data and stack segments respectively
Stack Pointer (SP) is a 16-bit register pointing to program stack in stack segment.
Base Pointer (BP) is a 16-bit register pointing to data in stack segment. BP register is
usually used for based, based indexed or register indirect addressing.
Source Index (SI) is a 16-bit register. SI is used for indexed, based indexed and register
indirect addressing, as well as a source data addresses in string manipulation instructions.
Destination Index (DI) is a 16-bit register. DI is used for indexed, based indexed and
register indirect addressing, as well as a destination data address in string manipulation
instructions.
Microprocessor and Microcontrollers
Flag Register:
Flags Register determines the current state of the processor. They are modified
automatically by CPU after mathematical operations, this allows to determine the type of
the result, and to determine conditions to transfer control to other parts of the program.
The 8086 flag register as shown in the fig 1.6. 8086 has 9 active flags and they are
divided into two categories:
1. Conditional Flags
2. Control Flags
Carry Flag (CY): This flag indicates an overflow condition for unsigned integer
arithmetic. It is also used in multiple-precision arithmetic.
Parity Flag (PF):This flag is used to indicate the parity of result. If lower order 8-bits of
the result contains even number of 1’s, the Parity Flag is set and for odd number of 1’s,
the Parity flag is reset.
Zero Flag (ZF):It is set; if the result of arithmetic or logical operation is zero else it is
reset.
Sign Flag (SF):In sign magnitude format the sign of Microprocessor and Microcontrollers
number is indicated by MSB bit. If
the result of operation is negative, sign flag is set.
Control Flags
Control flags are set or reset deliberately to control the operations of the execution unit.
Control flags are as follows:
Trap Flag (TF): It is used for single step control. It allows user to execute one
instruction of a program at a time for debugging. When trap flag is set, program can be
run in single step mode.
Direction Flag (DF):It is used in string operation. If it is set, string bytes are accessed
from higher memory address to lower memory address. When it is reset, the string bytes
are accessed from lower memory address to higher memory address.
Microprocessor and Microcontrollers
8086 Architecture
➢ Executes instructions
Control Circuitry:
➢ It directs internal operations.
Microprocessor
➢ A decoder in the EU translates instructions fetched from memoryandInto
Microcontrollers
series
of actions which the EU carries out
SUBTRACT
XOR
INCREMENT
DECREMENT
COMPLEMENT
FLAG REGISTERS:
A flag is a flip flop that indicates some condition produced by execution of an
instruction or controls certain operation of the EU.
It is 16 bit
2. Control flags
Conditional Flags
Carry Flag (CY): This flag indicates an overflow condition for unsigned integer
arithmetic. It is also used in multiple-precision arithmetic.
Parity Flag (PF):This flag is used to indicate the parity of result. If lower order 8-bits of
the result contains even number of 1’s, the Parity Flag is set and for odd number of 1’s,
the Parity flag is reset.
Zero Flag (ZF):It is set; if the result of arithmetic orMicroprocessor andisMicrocontrollers
logical operation zero else it is
reset.
Sign Flag (SF):In sign magnitude format the sign of number is indicated by MSB bit. If
the result of operation is negative, sign flag is set.
Control Flags
Control flags are set or reset deliberately to control the operations of the execution unit.
Control flags are as follows:
Trap Flag (TF): It is used for single step control. It allows user to execute one instruction
of a program at a time for debugging. When trap flag is set, program can be run in single
step mode.
Direction Flag (DF):It is used in string operation. If it is set, string bytes are accessed
from higher memory address to lower memory address. When it is reset, the string bytes
are accessed from lower memory address to higher memory address.
Instruction Queue:
BIU gets upto 6 bytes of next instructions and stores them in the instruction queue.
When EU executes instructions and is ready for its next instruction, then it simply reads
the instruction from this instruction queue resulting in increased execution speed.
Fetching the next instruction while the current instruction executes is called pipelining.(
based on FIFO) .This is much faster than sending out an addresses to the system memory
and waiting for memory to send back the next instruction byte or bytes .Here the Queue
will be dumped and then reloaded from the new Address.
Segment Register:
The 8086 20 bit addresses So it can address upto 220 in memory ( 1 Mbyte) but at any
instant it can address upto 4 64 KB segments. This four segments holds the upper 16 bits
of the starting address of four memory segments that the 8086 is working with it at
particular time .The BIU always inserts zeros for the lowest 4 bits of the 20 bit starting
address
Example : If the code segment register contains 348AH then the code segment starts at
348A0H .In other words a 64Kbyte segment can be located anywhere within 1MByte
address Space but the segment will always starts at an address with zeros in the lowest 4
bits
Stack: is a section of memory set aside to store addresses and data while subprogram
executes is often called segment base . The stack segment register always holds the upper
16 bit starting address of program stack.
The extra segment register and data segment register is used to hold the upper 16 bit
starting addresses of two memory segments that are used for data .
Instruction Pointer holds the 16 bit address or offset of the next code byte within the
code segment. The value contained in the Instruction Pointer called as Offset because the
value must be added to the segment base address in CS to produce the required 20 bit
address.
CS register contains the Upper 16 bit of the starting address of the code segment
in the 1 Mbyte address range the instruction pointer contains a 16 bit offset which tells
wherein that 64 Kbyte code segment the next instruction byte has to be fetched from.
Stack Register and Stack Pointer:
Stack: is a section of memory set aside to store addresses and data while
subprogram executes is often called segment base . The stack segment register always
holds the upper 16 bit starting address of program stack. The Stack pointer (SP) holds the
16 bit offset from the starting of the segment to the memory location where a word was
most recently stored
.The memory location where the word is stored is called as top of the stack
Microprocessor and Microcontrollers
These three registers are used to store temporary storage of data like general purpose
registers .They hold the 16 bit offset data of the data word in one of the segment
Programming model
How can a 20-bit address be obtained, if there are only 16-bit registers?
However, the largest register is only 16 bits (64k); so physical addresses have to be
calculated. These calculations are done in hardware within the microprocessor.
The 16-bit contents of segment register gives the starting/ base address of particular
segment. To address a specific memory location within a segment we need an offset
address. The offset address is also 16-bit wide and it is provided by one of the associated
pointer or index register.
To be able to program a microprocessor, one does not need to know all of its
hardware architectural features. What is important to the
programmer is being aware of the various registers within the device and to understand
their purpose, functions, operating capabilities, and limitations.
The above figure illustrates the software architecture of the 8086 microprocessor. From
this diagram, we see that it includes fourteenl6-bit internal registers: the instruction
pointer (IP), four data registers (AX, BX, CX, and DX), two pointer registers (BP and
SP), two index registers (SI and DI), four segment registers (CS, DS, SS, and ES) and
status register (SR), with nine of its bits implemented as status and control flags.
Microprocessor and Microcontrollers
The point to note is that the beginning segment address must begin at an address
divisible by 16.Also note that the four segments need not be defined separately. It is
allowable for all four segments to completely overlap (CS = DS = ES = SS).
Addresses within a segment can range from address 00000h to address 0FFFFh.
This corresponds to the 64K-bytelength of the segment. An address within a segment is
called an offset or logical address.
A logical address gives the displacement from the base address of the segment to
the desired location within it, as opposed to its "real" address, which maps directly
anywhere into the 1 MByte memory space. This "real" address is
called the physical address.
What is the difference between the physical and the logical address? The physical
address is 20 bits long and corresponds to the actual binary code output by the BIU on the
address bus lines. The logical address is an offset from location 0 of a given segment.
Microprocessor and Microcontrollers
Logical address is in the form of: Base Address: Offset Offset is the displacement
of the memory location from the starting location of the segment. To calculate the
physical address of the memory, BIU uses the following formula:
Example:
To convert this 16-bit address into 20-bit, the BIU appends 0H to the LSB (by
multiplying with 16) of the address. After appending, the starting address of the Data
Segment becomes 22220H.
Where 0016H is the offset, 2222 H is the value of DS Therefore the physical
address:22220H + 0016H
: 22236 H
The following table describes the default offset values to the corresponding memory
segments.
Address bits A1 through A19 select the storage location that is to be accessed.
They are applied to both banks in parallel. A0and bank high enable (BHE) are used as
bank-select signals.
The 8086 Microprocessor is a 16-bit CPU available in 3 clock rates, i.e. 5, 8 and
10MHz, packaged in a 40 pin CERDIP or plastic package. The 8086 Microprocessor
operates in single processor or multiprocessor configurations to achieve high
performance. The pin configuration is as shown in fig1. Some of the pins serve a
particular function in minimum mode (single processor mode) and others function in
maximum mode (multiprocessor mode) configuration.
Microprocessor and Microcontrollers
The 8086 signals can be categorized in three groups. The first are the signals
having common functions in minimum as well as maximum mode, the second are the
signals which have special functions in minimum mode and third are the signals having
special functions for maximum mode.
The following signal description is common for both the minimum and maximum
modes.
AD15-AD0:
These are the time multiplexed memory I/O address and data lines. Address
remains on the lines during T1 state, while the data is available on the data bus during T2,
T3, TW and T4. Here T1, T2, T3, T4 and TW are the clock states of a machine cycle. TW
is await state. These lines are active high and float to a tristate during interrupt
acknowledge and local bus hold acknowledge cycles.
A19/S6, A18/S5, A17/S4, A16/S3:
These are the time multiplexed address and status lines. During T1, these are the
most significant address lines or memory operations. During I/O operations, these lines
are low. During memory or I/O operations, status information is available on those lines
for T2, T3, TW and T4 .The status of
the interrupt enable flag bit(displayed on S5) is updated Microprocessor and Microcontrollers
at the beginning of each clock
cycle. The S4 and S3 combinedly indicate which segment register is presently being used
for memory accesses as shown in Table 1.1.
These lines float to tri-state off (tristated) during the local bus hold acknowledge.
The status line S6 is always low(logical). The address bits are separated from the status
bits using latches controlled by the ALE signal.
Read signal, when low, indicates the peripherals that the processor is performing a
memory or I/O read operation. is active low and shows the state for T2, T3, TW of
any read cycle. The signal remains tristated during the 'hold acknowledge'.
READY:
This is the acknowledgement from the slow devices or memory that they have
completed the data transfer. The signal made available by the devices is synchronized by
the 8284A clock generator to provide ready input to the 8086. The signal is active high.
INTR-Interrupt Request: Microprocessor and Microcontrollers
This is a level triggered input. This is sampled during the last clock cycle of each
instruction to determine the availability of the request. If any interrupt request is pending,
the processor enters the interrupt acknowledge cycle. This can be internally masked by
resetting the interrupt enable flag. This signal is active high and internally synchronized.
TEST:
This input is examined by a 'WAIT' instruction. If the TEST input goes low,
execution will continue, else, the processor remains in an idle state. The input is
synchronized internally during each clock cycle on leading edge of clock.
NMI-Non-maskable Interrupt:
This is an edge-triggered input which causes a Type2 interrrupt. The NMI is not
maskable internally by software. A transition from low to high initiates the interrupt
response at the end of the current instruction. This input is internally synchronized.
RESET:
This input causes the processor to terminate the current activity and start execution
from FFFF0H. The signal is active high and must be active for at least four clock cycles.
It restarts execution when the RESET returns low. RESET is also internally synchronized.
CLK-Clock Input:
The clock input provides the basic timing for processor operation and bus control
activity. Its an asymmetric square wave with 33% duty cycle. The range of frequency for
different 8086 versions is from 5MHz to 10MHz.
VCC :
+5V power supply for the operation of the internal circuit. GND ground for the
internal circuit.
MN/MX :
The logic level at this pin decides whether the processor is to operate in either
minimum (single processor) or maximum (multiprocessor) mode. The following pin
functions are for the minimum mode operation of 8086.
M/IO -Memory/IO: Microprocessor and Microcontrollers
-Interrupt Acknowledge:
This signal is used as a read strobe for interrupt acknowledge cycles. In other
words, when it goes low, it means that the processor has accepted the interrupt. It is active
low during T2, T3 and TW of each interrupt acknowledge cycle.
This output signal indicates the availability of the valid address on the
address/data lines, and is connected to latch enable input of latches. This signal is active
high and is never tristated.
-Data Transmit/Receive:
This output is used to decide the direction of data flow through the transreceivers
(bidirectional buffers). When the processor sends out data, this signal is high and when
the processor is receiving data, this signal is low. Logically, this is equivalent to S1 in
maximum mode. Its timing is the same as M/I/O. This is tristated during 'hold
acknowledge'.
This signal indicates the availability of valid data over the address/data lines. It is
used to enable the transreceivers (bidirectional buffers) to separate the data from the
multiplexed address/data signal. It is active from the middle ofT2 until the middle of T4
DEN is tristated during 'hold acknowledge' cycle.
When the HOLD line goes high, it indicates to the processor that another master is
requesting the bus access. The processor, after receiving the HOLD request, issues the
hold acknowledge signal on HLDA pin, in the middle of the next clock cycle after
completing the current bus (instruction)
Microprocessor
cycle. At the same time, the processor floats the local andlines.
bus and control Microcontrollers
When the
processor detects the HOLD line low, it lowers the HLDA signal. HOLD is an
asynchronous input, and it should be externally synchronized.
These are the status lines which reflect the type of operation, being carried out by
the processor. These become active during T4 of the previous cycle and remain active
during T1 and T2 of the current bus cycle. The status lines return to passive state during
T3 of the current bus cycle so that they may again become active for the next bus cycle
during T4. Any change in these lines during T3 indicates the starting of a new cycle, and
return to passive state indicates end of the bus cycle. These status lines are encoded in
table 1.3
This output pin indicates that other system bus masters will be prevented
from gaining the system bus, while the signal is low. The signal is
activated by the 'LOCK' prefix instruction and remains active until the completion of
the next instruction. This floats to tri-state off during "hold acknowledge". When the
CPU is executing a critical instruction which requires the system bus, the LOCK
prefix instruction ensures that other processors connected in the system will not gain
the control of the bus. The 8086, while executing the prefixed instruction, asserts the
bus lock signal output, which may be connected to an external bus controller.
These lines give information about the status of the codeprefetch queue. These are
active during the CLK cycle after which the queue operation is performed. These are
encoded as shown in Table 1.4.
Microprocessor and Microcontrollers
ReQuest/Grant:
These pins are used by other local bus masters, in maximum mode, to force the
processor to release the local bus at the end of the processor's current bus cycle. Each
of the pins is bidirectional with having higher priority than
pins have internal pull-up resistors and may be left unconnected. The
request! Grant sequence is as follows:
1. A pulse one clock wide from another bus master requests the bus access to 8086.
2. During T4 (current) or T1 (next) clock cycle, a pulse one clock wide from 8086 to the
requesting master, indicates that the 8086 has allowed the local bus to float and that it will
enter the "hold acknowledge" state at next clock cycle. The CPU's bus interface unit is
likely to be disconnected from the local bus of the system.
3. A one clock wide pulse from the another master indicates to 8086 that the 'hold'
request is about to end and the 8086 may regain control of the local bus at the next clock
cycle.
The latches are generally buffered output D-type flip-flops, like, 74LS373 or
8282. They are used for separating the valid address from the multiplexed address/data
signals and are controlled by the ALE signal generated by 8086.
Transreceivers
Transreceivers are the bidirectional buffers and some times they are called as data
amplifiers. They are required to separate the valid data from the time multiplexed
address/data signal. They are controlled by two signals, namely, DEN* and DT/R*. The
DEN* signal indicates that the valid data is available on the data bus, while DT/R
indicates the direction of data,
i.e. from or to the processor.
Memory:
The system contains memory for the monitor and users program storage. Usually,
EPROMS are used for monitor storage, while RAMs for users program storage.
IO Devices:
A system may contain I/O devices for communication with the processor as well as some
special purpose I/O devices.
Clock Generator:
The clock generator generates the clock from the crystal oscillator and then shapes
it and divides to make it more precise so that it can be used as an accurate timing
reference for the system. The clock generator also synchronizes some external signals
with the system clock.
Microprocessor and Microcontrollers
The general system organization is shown in above fig .Since it has 20 address
lines and 16 data lines, the 8086 CPU requires three octal address latches and two octal
data buffers for the complete address and data separation.
The working of the minimum mode configuration system can be better described
in terms of the timing diagrams rather than qualitatively describing the operations. The
opcode fetch and read cycles are similar. Hence the timing diagram can be categorized in
two parts.
The read cycle begins in T1 with the assertion of the address latch enable (ALE)
signal and also M/IO* signal. During the negative going edge of this signal, the valid
address is latched on the local bus. The BHE* and A0 signals address low, high or
both bytes. From Tl to T4, the M/IO* signal indicates a memory or I/O operation. At T2
the address is removed from the local bus and is sent to the output. The bus is then
tristated. The read (RD*) control signal is also activated in T2 .
The read (RD) signal causes the addressed deviceMicroprocessor
to enable its and
data Microcontrollers
bus drivers.
After RD* goes low, the valid data is available on the data bus.
The addressed device will drive the READY line high, when the processor returns
the read signal to high level, the addressed device will again tristate its bus drivers.
A write cycle also begins with the assertion of ALE and the emission of the
address. The M/IO* signal is again asserted to indicate a memory or I/O operation. In T2
after sending the address in Tl the processor sends the data to be written to the addressed
location. The data remains on the bus until middle of T4 state. The WR* becomes active
at the beginning of T2.
Microprocessor and Microcontrollers
The BHE* and A0 signals are used to select the proper byte or bytes of memory or
I/O word to be read or written. The M/IO*, RD* and WR* signals indicate the types of
data transfer as specified in Table
The HOLD pin is checked at the end of the each bus cycle. If it is received active
by the processor before T4 of the previous cycle or during T1 state of the current cycle,
the CPU activities HLDA in the next clock cycle and for the succeeding bus cycles, the
bus will be given to another requesting master The control control of the bus is not
regained by the processor until the requesting master does not drop the HOLD pin low.
When the request is dropped by the requesting master, the HLDA is dropped by the
processor at the trailing edge of the next clock as shown in fig
Microprocessor and Microcontrollers
In the maximum mode, the 8086 is operated by strapping the MN/MX* pin to
ground. In this mode, the processor derives the status signals S2*, S1* and S0*. Another
chip called bus controller derives the control signals using this status information. In the
maximum mode, there may be more than one microprocessor in the system configuration.
The other components in the system are the same as in the minimum mode system. The
general system organization is as shown in the fig1.1
The basic functions of the bus controller chip IC8288, is to derive control signals
like RD* and WR* (for memory and I/O devices), DEN*, DT/R*, ALE, etc. using the
information made available by the processor on the status lines. The bus controller chip
has input lines S2*, S1* and S0* and CLK. These inputs to 8288 are driven by the CPU.
It derives the outputs ALE, DEN*, DT/R*, MWTC*, AMWC*, IORC*, IOWC* and
AIOWC*. The AEN*, IOB
and CEN pins are specially useful for multiprocessor systems. AEN* and IOB are
generally grounded. CEN pin is usually tied to +5V.
Microprocessor and Microcontrollers
INTA* pin is used to issue two interrupt acknowledge pulses to the interrupt
controller or to an interrupting device.IORC*, IOWC* are I/O read command and I/O
write command signals respectively. These signals enable an IO interface to read or write
the data from or to the addressed port. The MRDC*, MWTC* are memory read command
and memory write command signals respectively and may be used as memory read and
write signals. All these command signals instruct the memory to accept or send data from
or to the bus. For both of these write command signals, the advanced signals namely
AIOWC* and AMWTC* are available. They also serve the same purpose, but are
activated one clock cycle earlier than the IOWC* and MWTC* signals, respectively. The
maximum mode system is shown in fig. 1.1.
The maximum mode system timing diagrams are also divided in two portions as
read (input) and write (output) timing diagrams. The address/data and address/status
timings are similar to the minimum mode. ALE is asserted in T1, just like minimum
mode. The only difference lies in the status signals used and the available control and
advanced command signals. The fig. 1.2 shows the maximum mode timings for the read
operation while the fig. 1.3 shows the same for the write operation.
Input port:
It is used to read data from the input device such as keyboard. The simplest form
of input port is a buffer. The input device is connected to the microprocessor through
buffer, as shown in the fig.1. This buffer is a tri-state buffer and its output is available
only when enable signal is active. When microprocessor wants to read data from the
input device (keyboard), the control signals from the microprocessor activates the buffer
by asserting enable input of the buffer. Once the buffer is enabled, data from the input
device is available on the data bus. Microprocessor reads this data by initiating read
command.
Microprocessor and Microcontrollers
Output port:
SIt is used to send data to the output device such as display from the
microprocessor. The simplest form of output port is a latch. The output device is
connected to the microprocessor through latch, as shown in the fig.2. When
microprocessor wants to send data to the output device is puts the data on the data bus
and activates the clock signal of the latch, latching the data from the data bus at the
output of latch. It is then available at the output of latch for the output device.
the message. A parity bit can help to reduce this. However, electrical wire parallel data
transmission is therefore less reliable for long distances because corrupt transmissions
are far more likely.
The interrupt driven I/O technique allows the CPU to execute its main program
and only stop to service I/O device when it is told to do so by the I/O system as shown in
fig.3. This method provides an external asynchronous input that would inform the
processor that it should complete whatever instruction that is currently being executed
and fetch a new routine that will service the requesting device. Once this servicing is
completed, the processor would resume exactly where it left off.
An analogy to the interrupt concept is in the classroom, where the professor serves
as CPU and the students as I/O ports. The classroom scenario for this interrupt analogy
will be such that the professor is busy in writing on the blackboard and delivering his
lecture.
The student raises his finger when he wants to ask a question (student requesting for
service). The professor then completes his sentence and acknowledges student‟s
request by saying “YES” (professor acknowledges the interrupt request). After
acknowledgement from the professor, student asks the question and professor gives
answer to the question (professor services the interrupt). After that professor continues
its remaining lecture form where it was left.
Microprocessor and Microcontrollers
PIO 8255:
The two groups of I/O pins are named as Group A and Group B. Each of thesetwo
groups contains a subgroup of eight I/O lines called as 8-bit port and anothersubgroup of
four lines or a 4-bit port. Thus Group A contains an 8-bit port Aalong with a 4-bit port C
upper.
The port A lines are identified by symbols PA0-PA7 while the port C lines are
identified as PC4-PC7 similarly. Group B contains an 8-bit port B, containing lines PB0-
PB7 and a 4-bit port C with lower bits PC0-PC3. The port C upper and port C lower can be
used in combination as an 8-bit port C. Both the port Cs is assigned the same address.
Thus one may have either three 8-bit I/O ports or two 8-bit and two 4-bit I/O ports from
8255. All of these ports can function independently either as input or as output ports.
This can be achieved by programming the bits of an internal register of 8255 called as
control word register (CWR). The internal block diagram and the pin configuration of
8255 are shown in figs.
Microprocessor and Microcontrollers
The 8-bit data bus buffer is controlled by the read/write control logic. The read/write
control logic manages all of the internal and external transfer of both data and control
words. RD, WR, A1, A0 and RESET are the inputs, provided by the microprocessor to
READ/WRITE control logic of 8255. The 8-bit, 3-state bidirectional buffer is used to
interface the 8255 internal data bus with the external system data bus. This buffer
receives or transmits data upon the execution of input or output instructions by the
microprocessor. The control words or status information is also transferred through the
buffer.
The port A lines are identified by symbols PA0-PA7 while the port C lines are
Identified as PC4-PC7. Similarly, Group B contains an 8-bit port B, containing
lines PB0-PB7 and a 4-bit port C with lower bits PC0- PC3. The port C upper
and port C lower can be used in combination as an 8-bit port C.
Both the port C is assigned the same address. Thus one may have either three
8-bit I/O ports or two 8-bit and two 4-bit ports from 8255. All of these ports
can function independently either as input or as output ports. This can be
Microprocessor and Microcontrollers
PA7-PA0: These are eight port A lines that acts as either latched output or buffered
input lines depending upon the control word loaded into the control word register.
PC7-PC4: Upper nibble of port C lines. They may act as either output latches or input
buffers lines.
This port also can be used for generation of handshake lines in mode1 or mode2.
PC3-PC0: These are the lower port C lines; other details are the same as PC7-PC4
lines.
PB0-PB7: These are the eight port B lines which are used as latched output lines or
buffered input lines in the same way as port A.
RD: This is the input line driven by the microprocessor and should be low to indicate
read operation to 8255.
WR: This is an input line driven by the microprocessor. A low on this line indicates
write operation.
CS: This is a chip select line. If this line goes low, it enables the 8255 to respond to RD
and WR signals, otherwise RD and WR signal are neglected.
D0-D7: These are the data bus lines those carry data or control word to/from the
microprocessor.
RESET:Logic high on this line clears the control word register of 8255. All ports are
set as input ports by default after reset.
A1-A0: These are the address input lines and are driven by the microprocessor.
These lines A1-A0 with RD, WR and CS from the following operations for 8255. These
address lines are used for addressing any one of the four registers, i.e. three ports
and a control word register as given in table below.
In case of 8086 systems, if the 8255 is to be interfaced with lower order data bus,
the A0 and A1 pins of 8255 are connected with A1 and A2 respectively.
Microprocessor and Microcontrollers
These are two basic modes of operation of 8255. I/O mode and Bit Set-Reset mode
(BSR).
In I/O mode, the 8255 ports work as programmable I/O ports, while in BSR mode only
port C (PC0-PC7) can be used to set or reset its individual port bits.
Under the I/O mode of operation, further there are three modes of operation of 8255,
so as to support different types of applications, mode 0, mode 1 and mode 2.
BSR Mode: In this mode any of the 8-bits of port C can be set or reset depending on D0
of the control word. The bit to be set or reset is selected by bit select flags D3, D2 and D1
of the CWR as given in table.
I/O Modes:
a) Mode 0 (Basic I/O mode): This mode is also called as basic input/output Mode. This
mode provides simple input and output capabilities using each of the threeports. Data
can be simply read from and written to the input and output portsrespectively, after
appropriate initialization.
Microprocessor and Microcontrollers
1. Two 8-bit ports (port A and port B) and two 4-bit ports (port C upper and lower)
are available. The two 4-bit ports can be combined used as a third 8-bit port.
2. Any port can be used as an input or output port.
3. Output ports are latched. Input ports are not latched.
4. A maximum of four ports are available so that overall 16 I/O configurations
arepossible.
b) Mode 1: ( Strobed input/output mode ) In this mode the handshaking control the
input and output action of the specified port. Port C lines PC0-PC2, provide strobe
orhandshake lines for port B. This group which includes port B and PC0-PC2 is called
asgroup B for Strobed data input/output. Port C lines PC3-PC5 provides strobe lines for
portA.This group including port A and PC3-PC5 from group A. Thus port C is utilized
forgenerating handshake signals.
Microprocessor and Microcontrollers
1. Two groups – group A and group B are available for strobed data transfer.
2. Each group contains one 8-bit data I/O port and one 4-bit control/data port.
3. The 8-bit data port can be either used as input and output port. The inputs
andoutputs both are latched.
4. Out of 8-bit port C, PC0-PC2 are used to generate control signals for port B
andPC3-PC5 are used to generate control signals for port A. the lines PC6, PC7
may be used as independent data lines.
The control signals for both the groups in input and output modes areexplained as
follows:
• STB (Strobeinput) – If this lines falls to logic low level, the data available at 8-
bit input port is loaded into input latches.
• IBF (Input buffer full) – If this signal rises to logic 1, it indicates that data
hasbeen loaded into latches, i.e. it works as an acknowledgement. IBF is set
by a lowon STB and is reset by the rising edge of RD input.
• INTR (Interruptrequest) – This active high output signal can be used
tointerrupt the CPU whenever an input device requests the service. INTR is
set by ahigh STBpin and a high at IBF pin. INTE is an internal flag that can be
controlledby the bit set/reset mode of either PC4 (INTEA) or PC2 (INTEB) as
shown in fig.
• INTR is reset by a falling edge of RD input. Thus an external input device can
berequest the service of the processor by putting the data on the bus and
sending thestrobe signal.
• OBF (Output buffer full) – This status signal, whenever falls to low,
indicatesthat CPU has written data to the specified output port. The OBF flip-
flop will beset by a rising edge of WR signal and reset by a low going edge at
the ACKinput.
• ACK (Acknowledgeinput) – ACK signal acts as an acknowledgement to begiven
by an output device. ACK signal, whenever low, informs the CPU that thedata
transferred by the CPU to the output device through the port is received
bythe output device.
• INTR (Interruptrequest) – Thus an output signal that can be used to
interruptthe CPU when an output device acknowledges the data received
from the CPU.INTR is set when ACK, OBF and INTE are 1. It is reset by a
Microprocessor and Microcontrollers
fallingedge on WRinput. The INTEA and INTEB flags are controlled by the bit
set-reset mode ofPC6 and PC2 respectively.
Microprocessor and Microcontrollers
In this mode, 8255 is a bidirectional 8-bit port with handshake signals. The Rdand WR signals
decide whether the 8255 is going to operate as an input port oroutput port.
INTR – (Interrupt request) As in mode 1, this control signal is active high and
isused to interrupt the microprocessor to ask for transfer of the next data
byteto/from it. This signal is used for input (read) as well as output (write)
operations.
Control Signals for Output operations:
OBF (Output buffer full) – This signal, when falls to low level, indicates that
theCPU has written data to port A.
ACK (Acknowledge) This control input, when falls to logic low level,
Acknowledges that the previous data byte is received by the destination and
nextbyte may be sent by the processor. This signal enables the internal tristate
buffersto send the next data byte on port A.
INTE1 ( A flag associated with OBF ) This can be controlled by bit set/resetmode
with PC6.
STB (Strobe input)a low on this line is used to strobe in the data into the
inputLatches of 8255.
IBF (Input buffer full) when the data is loaded into input buffer, this signal risesto
logic „1‟. This can be used as an acknowledge that the data has been receivedby
the receiver.
The waveforms in fig show the operation in Mode 2 for output as well as
inputport.
Note: WR must occur before ACK and STB must be activated before RD.
Microprocessor and Microcontrollers
➢ In most of the cases, the PIO 8255 is used for interfacing the analog to digital
converters with microprocessor.
➢ We have already studied 8255 interfacing with 8086 as an I/O port, in previous section.
This section we will only emphasize the interfacing techniques of analog to digital
converters with 8255.
➢ The analog to digital converters is treated as an input device by the microprocessor
that sends an initializing signal to the ADC to start the analogy to digital data
conversation process. The start of conversation signal is a pulse of a specific duration.
➢ The analog to digital converter chips 0808 and 0809 are 8-bit CMOS,
successive approximation converters. This technique is one of the fast
techniques for analog to digital conversion. The conversion delay is 100µs at a
clock frequency of 640 KHz, which is quite low as compared to other
converters. These converters do not need any external zero or full scale
adjustments as they are already taken care of by internal circuits.
➢ These converters internally have a 3:8 analog multiplexer so that at a time
eight different analog conversion by using address lines - ADD A, ADD B,
ADD C, as shown. Using these address inputs, multichannel data acquisition
system can be designed using a single ADC. The CPU may drive these lines
using output port lines in case of multichannel applications. In case of
single input applications, these may be hardwired to select the proper
input.
➢ There are unipolar analog to digital converters, i.e. they are able to convert
only positive analog input voltage to their digital equivalent. These chips do
not contain any internal sample and hold circuit.
➢ If one needs a sample and hold circuit for the conversion of fast signal into
equivalent digital quantities, it has to be externally connected at each of the
analog inputs.
Fig (1) and Fig (2) show the block diagrams and pin diagrams for ADC 0808/0809.
Table.1
Address lines
Analog I/P selected
C B A
I/P 0 0 0 0
I/P 1 0 0 1
I/P 2 0 1 0
I/P 3 0 1 1
I/P 4 1 0 0
I/P 5 1 0 1
I/P 6 1 1 0
I/P 7 1 1 1
Microprocessor and Microcontrollers
Table.2
The digital to analog converters convert binary numbers into their analog
equivalent voltages. The DAC find applications in areas like digitally controlled gains,
motor speed controls, programmable gain amplifiers, etc.
Keyboard Interfacing
➢ In most keyboards, the key switches are connected in a matrix of Rows and Columns.
1. D e t e c t a k e y p r e s s
2. D e b o u n c e t h e k e y p r e s s .
3. Encode the keypress (produce a standard code for the pressed key).
Key Debounce:
The rows of the matrix are connected to four output Port lines, &columns are
connected to four input Port lines.
When no keys are pressed, the column lines are held high by the pull-up resistors
connected to +5v.
Pressing a key connects a row & a column.
To detect if any key is pressed is to output 0‟s to all rows & then check columns
to see it a pressed key has connected a low (zero) to a column.
Once the columns are found to be all high, the program enters another loop,
which waits until a low appears on one of the columns i.e indicating a key press.
A simple 20/10 msec delay is executed to debounce task.
After the debounce time, another check is made to see if the key is still pressed. If
the columns are now all high, then no key is pressed & the initial detection was
caused by a noise pulse.
To avoid this problem, two schemes are suggested:
1. Use of Bistablemultivibrator at the output of the key to debounce it.
2. The microprocessor has to wait for the transient period (at least for 10 ms),
so that the transient response settles down and reaches a steady state.
If any of the columns are low now, then the assumption is made that it was a valid
key press.
Microprocessor and Microcontrollers
The final task is to determine the row & column of the pressed key &convert this
information to Hex-code for the pressed key.
The 4-bit code from I/P port & the 4-bit code from O/P port (row &column) are
converted to Hex-code.
Display Interface
Microprocessor and Microcontrollers
I/O Interface
Any application of a microprocessor system requires the
transfer of data between microprocessor and external environment and also with in the microprocessor.
This is known as Input/Output. There are three different ways that the data transfer can take place. They
are
Memory interfacing
Memory is an integral part of a microcomputer system. There
are two main types of memory.
(i) Read only memory (ROM): As the name indicates this memory is available only for
reading purpose. The various types available under this category are PROM, EPROM,
EEPROM which contain system software and permanent system data.
(ii) Random Access memory (RAM): This is also known as Read Write Memory. It is a
volatile memory. RAM contains temporary data and software programs generally for
different applications.
UNIT –IV
Introdution to Microcontrollers
Microprocessor Microcontroller
Internal Circuits
Working Registers
InternalRAM ROM
ProgramCounter StackPointer StackPointer Clock
It has many instructions to move data between It has few instructions to move data between memory
memory and CPU and CPU
Less number of pins are multifunctional More number of pins are multifunctional
Single memory map for data and code Separate memory map for data and code (program)
(program)
Access time for memory and IO are more Less access time for built in memory and IO.
More flexible in the design point of view Less flexible since the additional circuits which is
residing inside the microcontroller is fixed for a
particular microcontroller
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressingmodes
Microprocessor and Microcontrollers
RISC CISC
Only load/store instructions are used to access In additions to load and store instructions, memory
memory access is possible with other instructions also.
It uses single memory space for both It has separate program memory and data memory
instructions anddata.
It is not possible to fetch instruction code and data Instruction code and data can be Fetche
simultaneously d
Execution of instruction takes more machine cycle Execution of instruction takes less machine cycle
Also known as control flow or control driven Also known as data flow or data Driven
computers computers
Simplifies the chip design because of single memory Chip design is complex due to separate memory
space space
Eg. 8085, 8086, MC6800 Eg. General purpose microcontrollers, special DSP
chips etc.
Microprocessor and Microcontrollers
COMPUTER SOFTWARE
A set of instructions written in a specific sequence for the computer to solve a specific task is called a
program and software is a collection of such programs.
The program stored in the computer memory in the form of binary numbers is called machine
instructions. The machine language program is called object code.
The assembly language program the programmer enters is called source code. The source code
(assembly language) is translated to object code (machine language) using assembler.
Programs can be written in high level languages such as C, C++ etc. High level language will be
converted to machine language using compiler or interpreter. Compiler reads the entire program and
translate into the object code and then it is executed by the processor. Interpreter takes one statement of
the high level language as input and translate it into object code and then executes.
CLASSIFICATION OF MICROCONTROLLER
Microcontrollers are divided by their bits, memory architecture, memory device and instruction set
8 bit- In this if the internal bus is 8 bit then the ALU performs arithmetic and logic operations. Examples are
8051/8031/ pic1x.
16 bit- this microcontroller performance is greater compared to 8-bit. 16 bit can use 16 bit for its operations
were as in 8-bit it is only 8-bit examples are 8051 extended, pic2x
32-bit - it uses 32 bit to perform its operations these are used in medical devices and in control systems PIC3X
is an example
Embedded memory microcontroller – This is a type of microcontroller in which all the functional blocks are
available in a chip data memory I/O ports
External memory microcontroller – in this microcontroller all functional blocks are not available 8031 doesn’t
have any program memory
CISC – complex instruction set computer it allows the user to use one instruction to do the functions of many
simple instructions
RISC- reduced instruction set computer in this instruction set is reduced each instruction can be operated on any
register or in any addressing mode
Microprocessor and Microcontrollers
Harvard memory architecture- in this microcontroller the program and data memory doesn’t have a similar
memory address space
Princeton memory architecture- in this the program and data memory have similar address space
TYPES OF MICROCONTROLLER
8051 microcontroller
It is designed by Intel in 1981 and it is an 8 bit microcontroller it has 40 pins dual inline package 128 bytes of
RAM 4k byte of ROM in 8051 there are 2 busses one for programming and other for data programming in
microcontroller is complicated basically we write a program in C language and then it is converted to machine
language understood by microcontroller two types of memory is present the program memory and data memory
program memory stores the data being executed while data memory stores the result temporarily
Renesas microcontroller
It is in the automotive microcontroller family that offers high-performance features with low power
consumption this microcontroller offers high security and embedded safety character for the automotive
applications RX microcontroller is an example which has 32 bit
AVR microcontrollers
The AVR microcontrollers have modified Harvard RISC architecture with separate memories for data and
program and speed of AVR is really high when compared to PIC and 8051
APPLICATIONS
• Industrial automation
• Communication application
• Motor control applications
• Test and measurement
• Medical applications
• Automobiles
• Cameras
• Security alarms
• Mobile phones
Microprocessor and Microcontrollers
Port 2
ROM
PC DPTR
DPH I/O
INT
DPL
Port 3 CNTR
SERIA
L
RD/W
R
EA IE
System General IP
LEPSE
Timing purpose PCON
N area SBUF
System SCON
XTAL
interrupt TCON
Bit addressible
1 timers TMOD
area
XTAL TL0
Register Bank 3
Data TH0
Register Bank 2
2RES buffers TL1
Register Bank 1
TH1
ET Memory Register Bank 0
• 8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the program
size is more than 4 K Bytes 8051 will fetch the code automatically from external memory.
• Accumulator is an 8 bit register widely used for all arithmetic and logical operations.
Microprocessor and Microcontrollers
Accumulator is also used to transfer data between external memory. B register is used along with
Accumulator for multiplication and division. A and B registers together is also called
MATHregisters.
Microprocessor and Microcontrollers
• PSW (Program Status Word). This is an 8 bit register which contains the arithmetic status of
ALU and the bank select bits of registerbanks.
CY AC F0 RS1 RS0 OV - P
CY - carryflag
AC - auxiliary carryflag
F0 - available to the user for general
purpose RS1,RS0- register bank selectbits
OV - overflow
P - parity
• Stack Pointer (SP) – it contains the address of the data item on the top of the stack. Stack may
reside anywhere on the internal RAM. On reset, SP is initialized to 07 so that the default stack
will start from address 08onwards.
• Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer lower byte). This is a
16 bit register which is used to furnish address information for internal and externalprogram
memory and for external datamemory.
• Program Counter (PC) – 16 bit PC contains the address of next instruction to be executed. On
reset PC will set to 0000. After fetching every instruction PC will increment byone.
PIN DIAGRAM
Pinout Description
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the contents of
most registers. In other words, the positive voltage on this pin resets the microcontroller. By
applying logic zero to this pin, the program starts execution from the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or output. Besides,
all of them have alternative functions
Microprocessor and Microcontrollers
Pin 31 EA. By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It means that even
there is a program written to the microcontroller, it will not be executed. Instead, the
program written to external ROM will be executed. By applying logic one to the EA pin,
the microcontroller will use both memories, first internal then external (if exists).
Pin 32-39 PORT 0. Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is
driven high (1) or as data output (Data Bus) when the ALE pin is driven low (0).
MEMORYORGANIZATION
Internal RAM organization
R7 1F
R6 1E
R5 1D
BANK3
R4 1C 2F
7F 78 7F
R3 1B 77 70 7E
2E
R2 1A
6F 68 .
R1 19 2D
R0 18 67 60 .
2C
R7 17 5F 58 .
2B
R6 16
R5 15 2A
57 50 .
BANK2
R4 14 29
4F 48 .
R3 13 47 40 .
28
R2 12
R1 11 27 3F 38 .
R0 10 26
37 30 .
R7 0F 2F 28
R6 0E 25 32
R5 0D 27 20
24 31
BANK1
R4 0C 1F 18
R3 0B
23 30
17 10
R2 0A 22
R1 09 21 0F 08
R0 08 07 00
R7 07
20 General purpose memory
R6 06
R5 05
BANK0
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0,
R1,R2, R3, R4, R5, R6, and R7). There are four such register banks. Selection of register bank
can be done through RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will beselected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to
bit variables. This is where individual memory bits in Internal RAM can be set or cleared. In all
there are 128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0
or 1. A bit variable can be set with a command such as SETB and cleared with a command such
as CLR. Example instructions are:
SETB25h;setsthebit25h(becomes1)
CLR25h;clearsbit25h(becomes0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and
2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available
Microprocessor and Microcontrollers
for general-purpose data storage, user should take care while using the memory location from 00
-2Fh
Microprocessor and Microcontrollers
since these locations are also the default register space, stack space, and bit addressable space. It
is a good practice to use general purpose memory from 30 – 7Fh. The general purpose RAM can
be accessed using direct or indirect addressingmodes.
EXTERNAL MEMORYINTERFACING
Eg. Interfacing of 16 K Byte of RAM and 32 K Byte of EPROM to
8051
Number of address lines required for 16 Kbyte memory is 14 lines and that of 32Kbytes of memory is 15
lines.
PSEN PSEN
A14 A14
A13 A13
A13
A12 A12
A12
… .. 32 Kbyte
A9 .
A8
A8
WE
. RAM
WR 16 Kbyte .
RD OE
A3
AL A7 RAM A2
LE ..
8051 A0-A7 A0-A7 A1
A1
A0
A0
LOWER BYTE
EA ADDRESS
GND [AD0 –AD7] DAT DAT
A A
O/P O/P
AD0
- DATA BUS [AD0 – AD7]
AD7
The lower order address and data bus are multiplexed. De-multiplexing is done by the latch.
Initially the address will appear in the bus and this latched at the output of latch using ALE
signal. The output of the latch is directly connected to the lower byte address lines of the
memory. Later data will be available in this bus. Still the latch output is address it self. The
higher byte of address bus is directly connected to the memory. The number of lines connected
depends on the memory size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the
memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the
program size exceeds internal memory the microcontroller will automatically switch to external
Microprocessor and Microcontrollers
memory.
Microprocessor and Microcontrollers
STACK
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The address of the
stack is contained in a register called stack pointer. Instructions PUSH and POP are used for stack operations.
When a data is to be placed on the stack, the stack pointer increments before storing the data on the stack so
that the stack grows up as data is stored (pre-increment). As the data is retrieved from the stack the byte is
read from the stack, and then SP decrements to point the next available byte of stored data (post decrement).
The stack pointer is set to 07 when the 8051 resets. So that default stack memory starts from address location
08 onwards (to avoid overwriting the default register bank ie., bank0).
I/O Ports:
8051 microcontroller have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence, total 32 I/O
pins allows the microcontroller to be connected with the peripheral devices.
1) PORT 0
P0 can be used as a bidirectional I/O port or it can be used for address/data connected for accessing external memory.
When control is 1 the port is used for address or data interfacing. When the control is 0 then the port can be used as a
bidirectional I/O port.
Microprocessor and Microcontrollers
If the control is 0 then the port is used as an input port and 1 is written to the latch. In this type of situation both the
output MOSFETs are off. Since the output pin has floats therefore, whatever data written on pin is directly read by
read pin.
If we want to write 1 on pin of P0, a '1' written to the latch which turns 'off' the lower FET while due to '0' control
signal upper FET also turns off.
Suppose we want to write '0' on pin of port 0, when '0' is written to the latch, the pin is pulled down by the lower
FET. Hence the output becomes zero.
2) PORT 1
PORT 1 is dedicated only for I/O interfacing. When used as an output port, not needed to connect additional pull-up
resistor like port 0.
To use PORT 1 as an input port '1' has to be written to the latch. In this mode 1 is written to the pin by the external
device then it read fine.
3) PORT 2
PORT 2 is used for higher external address byte or a normal I/O port. Here, the I/O operation is similar to PORT 1.
Latch of PORT 2 remains stable when Port 2 pin are used for external memory access.
4) PORT 3
P3.4 T0 Timer 0
P3.5 T1 Timer 1
It works as an I/O port same like port 2. Alternate functions of port 3 makes its architecture different than other ports.
External Memory
The system designer is not limited by the amount of internal RAM and ROM
available on chip. Two separate external memory spaces are made available by
the 16-bit PC and DPTR and by different control pins for enabling external ROM
and RAM chips. Internal control circuitry accesses the correct physical memory,
depending upon the machine cycle state and the op code being executed.
There are several reasons for adding external memory, particularly program
memory, when applying the 8051 in a system. When the project is in the
Microprocessor and Microcontrollers
prototype stage, the expense—in time and money—of having a masked internal
ROM made for each program "try" is prohibitive.To alleviate this problem, the
manufacturers make available an EPROM version, the 8751, which has 4K of on-
chip EPROM that may be programmed and erased as needed as the program is
developed. The resulting circuit board layout will be identical to one that uses a
factory-programmed 8051. The only drawbacks to the 8751 are the specialized
EPROM programmers that must be used to program the non-standard 40-pin part,
and the limit of "only" 4096 bytes of program code. The 8751 solution works
well if the program will fit into 4K bytes. Unfortunately, many times, particularly
if the program is written in a high-level language, the program size exceeds 4K
bytes, and an external program memory is needed. Again, the manufacturers
provide a version for the job, the ROMIess 8031. The EA pin is grounded when
using the 8031, and all program code is contained in an external EPROM that
may be as large as 64K bytes and that can be programmed using standard
EPROM programmers.
Microprocessor and Microcontrollers
External RAM, which is accessed by the DPTR, may also be needed when 128
bytes of internal data storage is not sufficient. External RAM, up to 64K bytes,
may also be added to any chip in the 8051 family.
8051 has about 111 instructions. These can be grouped into the following categories
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
The following nomenclatures for register, data, address and variables are used
while write instructions
A: Accumulator
B: "B" register
C: Carry bit
Direct: 8-bit internal direct address for data. The data could be in lower 128bytes of
RAM (00 - 7FH) or it could be in the special function register (80 - FFH).
@Ri: 8-bit external or internal RAM address available in register R0 or R1. This is used
for indirect addressing mode.
#data8: Immediate 8-bit data available in the instruction.
Addr11: 11-bit destination address for short absolute jump. Used by instructions AJMP
& ACALL. Jump range is 2 kbyte (one page).
Addr16: 16-bit destination address for long call or long jump.
Rel: 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all
conditional jumps.
bit: Directly addressed bit in internal RAM or SFR
Data transferinstructions.
In this group, the instructions perform data transfer operations of the following types.
Microprocessor and Microcontrollers
i. MOVXA,@R1
ii. MOVX@R0,A
h. Move the contents of program memory toA
i. MOVC A,@A+PC
ii. MOVC A,@A+DPTR
MOVXA,@DPTR
iii. MOVX@DPTR,A
Arithmeticinstructions.
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit
numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or withoutcarry.
i. ADD A,#45H
ii. ADDC A,#OB4H
ii. Add the contents of A with register Rn with or withoutcarry.
i. ADD A,R5
ii. ADDC A,R2
iii. Add the contents of A with contents of memory with or without carry using direct and
indirectaddressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A,@R1
iv. ADDC A,@R0
i. SUBB A,#45H
ii. SUBB A,#OB4H
ii. Subtract the contents of A with register Rn with or withoutcarry.
i. SUBB A,R5
ii. SUBB A,R2
iii. Subtract the contents of A with contents of memory with or without carry using direct and
indirectaddressing
i. SUBB A,51H
ii. SUBB A,75H
iii. SUBB A,@R1
iv. SUBB A,@R0
operation. Multiplication
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B
register. After multiplication the lower byte of the result will be stored in accumulator and higher
byte of result will be stored in B register.
Eg. MOVA,#45H ;[A]=45H
MOVB,#0F5H ;[B]=F5H
MULAB ;[A]x[B]=45xF5=4209
;[A]=09H, [B]=42H
Division
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned
number which is stored in B register. After division the result will be stored in accumulator and
remainder will be stored in B register.
Eg. MOVA,#45H ;[A]=0E8H
MOVB,#0F5H ;[B]=1BH
DIVAB ;[A]/[B]=E8/1B=08Hwithremainder10H
;[A] = 08H, [B]=10H
When two BCD numbers are added, the answer is a non-BCD number. To get the result in BCD, we use
DA A instruction after the addition. DA A works asfollows.
• If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lowernibble.
• If upper nibble is greater than 9 or carry is 1, 6 is added to uppernibble.
INC increments the value of source by 1. If the initial value of register is FFh, incrementing the value will
cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented. If the initial
value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value will cause it to
reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to FFh.
LogicalInstructions
Logical AND
ANLdestination,source:ANL does a bitwise "AND" operation between sourceand destination, leaving the
resulting value in destination. The value in source is not affected. "AND" instruction logically AND the
bits of source anddestination.
ANL A,#DATA ANL A, Rn
ANL A,DIRECT ANL
A,@Ri
ANL DIRECT,A ANL DIRECT, #DATA
Logical OR
ORLdestination,source:ORLdoesabitwise"OR"operationbetweensourceanddestination,
Microprocessor and Microcontrollers
leaving the resulting value in destination. The value in source is not affected. " OR " instruction
logically OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL
A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
Logical NOT
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is shifted one
location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into the carry bit in the
PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the carry bit in the
PSW, while the carry goes into bit 0.
Microprocessor and Microcontrollers
Branch (JUMP)Instructions
Jump and Call Program Range
There are 3 types of jump instructions. They are:-
1. RelativeJump
2. Short AbsoluteJump
3. Long AbsoluteJump
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than (the
address following the jump instruction by 127 or less) or less than (the address following the
jump by 128 or less) is called a relative jump. Schematically, the relative jump can be shown as
follows: -
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each. The
hexadecimal addresses of the pages are given as follows:-
00 0000-07FF
01 0800 -0FFF
02 1000-17FF
03 1800 -1FFF
.
.
1E F000 -F7FF
1F F800 -FFFF
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the
lower 11bits of the PC hold the address within that page. Thus, an absolute address is formed by
taking page numbers of the instruction (from the program counter) following the jump and
attaching the specified 11bits to it to form the 16-bit address.
LongAbsoluteJump/Call
Applications that need to access the entire program memory from 0000H to FFFFH use long
absolute jump. Since the absolute address has to be specified in the op-code, the instruction
length is 3 bytes (except for JMP @ A+DPTR). This jump is notre-locatable.
Example: -
2. Conditional Jumpinstructions.
JBC Jump if bit =1 and clearbit
JNB Jump if bit =0
JB Jump if bit =1
JNC Jump if CY =0
JC Jump if CY =1
CJNEreg,#data Jump if byte ≠ #data
CJNEA,byte Jump if A ≠byte
DJNZ Decrement and Jump if A ≠0
JNZ Jump if A ≠0
JZ Jump if A =0
JBbit, rel ; jump if the direct bit is set to the relative address specified.
JNBbit,rel ;jumpifthedirectbitiscleartotherelativeaddressspecified.
JBCbit,rel ; jump if the direct bit is set to the relative address specified and then clear thebit.
1. LCALL address(16bit)
This is long call instruction which unconditionally calls the subroutine located at the indicated 16 bit
address. This is a 3 byte instruction. The LCALL instruction works as follows.
a. DuringexecutionofLCALL,[PC]=[PC]+3;(ifaddress whereLCALLresides issay,0x3254; during
execution of this instruction [PC] = 3254h + 3h =3257h
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and[SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 57 will be stored in memory location08.
d. [SP]=[SP]+1; (SP increments again and[SP]=09)
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 32 will be stored in memory location09.
With these the address (0x3254) which was in PC is stored in stack.
f. [PC]=address(16 bit);thenewaddressofsubroutineisloadedto PC.No flagsareaffected.
2. ACALL address(11bit)
This is absolute call instruction which unconditionally calls the subroutine located at the indicated 11 bit
address. This is a 2 byte instruction. The SCALL instruction works as follows.
a. DuringexecutionofSCALL,[PC]=[PC]+2;(ifaddress whereLCALLresides issay,0x8549; during
execution of this instruction [PC] = 8549h + 2h =854Bh
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and[SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 4B will be stored in memory location08.
d. [SP]=[SP]+1; (SP increments again and[SP]=09)
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 85 will be stored in memory location09.
f. [PC10-0]= address(11bit); the new address of subroutine is loaded to PC. No flags are
affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8]=[[SP]];content of current top of the stack will be moved to higher byte ofPC.
h. [SP]=[SP]-1; (SPdecrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte ofPC.
j. [SP]=[SP]-1; (SP decrementsagain)
Bit manipulationinstructions.
8051 has 128 bit addressable memory. Bit addressable SFRs and bit addressable PORT pins. It is possible to perform
following bit wise operations for these bit addressable locations.
1. LOGICAL AND
a. ANLC,BIT(BITADDRESS) ; ‘LOGICALLY AND’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT INCARRY
b. ANLC,/BIT; ; ‘LOGICALLY AND’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT INCARRY
2. LOGICALOR
a. ORLC,BIT(BITADDRESS) ; ‘LOGICALLY OR’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT INCARRY
b. ORLC,/BIT; ; ‘LOGICALLY OR’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT INCARRY
3. CLRbit
a. CLRbit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BECLEARED.
b. CLRC ; CONTENT OF CARRY WILL BECLEARED.
4. CPLbit
a. CPLbit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BECOMPLEMENTED.
b. CPLC ; CONTENT OF CARRY WILL BECOMPLEMENTEd
ASSEMBLERDIRECTIVES
Assembler directives tell the assembler to do something other than creating the machine code for
an instruction. In assembly language programming, the assembler directives instruct the
assembler to
1. Process subsequent assembly languageinstructions
2. Define program constants
3. Reserve space forvariables
.EQU andSET
EQU and SET directives assign numerical value or register name to the specified
symbolname.
EQU is used to define a constant without storing information in the memory. The symbol
defined with EQU should not be redefined.
SET directive allows redefinition of symbols at a later stage.
DB (DEFINE BYTE)
The DB directive is used to define an 8 bit data. DB directive initializes memory with 8
bit values. The numbers can be in decimal, binary, hex or in ASCII formats. For decimal,
the 'D' after the decimal number is optional, but for binary and hexadecimal, 'B' and ‘H’
are required. For ASCII, the number is written in quotation marks (‘LIKE This).
DATA1: DB 40H ; hex
DATA2: DB 01011100B ;bina r y
DATA3: DB 48 ; decimal
DATA4: DB ' HELLO W’ ; ASCII
END
The END directive signals the end of the assembly module. It indicates the end of the
program to the assembler. Any text in the assembly file that appears after the END directive
is ignored. If the END statement is missing, the assembler will generate an error.
ADDRESSING MODES
Various methods of accessing the data are called addressin g modes. 8051 addressing modes are
classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
9. Bit inherent addressing.
10. Bit direct addressing.
1.Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself. In other words
data immediately follows the instruction.
Eg. MOVA,#30H
ADDA, #83 # Symbol indicates the data isimmediate
Microprocessor and Microcontrollers
2.Register addressing
In this addressing mode the register will hold the data. One of the eight general registers
(R0 to R7) can be used and specified as theoperand.
Eg. MOV A,R0
ADDA,R6
R0 – R7 will be selected from the current selection of register bank. The default register bank will
be bank 0.
3.Direct addressing
There are two ways to access the internal memory. Using direct address and indirect
address. Using direct addressing mode we can not only address the internal memory but SFRs
also. In direct addressing, an 8 bit internal data memory address is specified as part of the
instruction and hence, it can specify the address only in the range of 00H to FFH. In this
addressing mode, data is obtained directly from the memory.
Eg. MOV
A,60h
ADDA,30h
4.Indirect addressing
The indirect addressing mode uses a register to hold the actual address that will be used
in data movement. Registers R0 and R1 and DPTR are the only registers that can be used as data
pointers. Indirect addressing cannot be used to refer to SFR registers. Both R0 and R1 can hold 8
bit address and DPTR can hold 16 bit address.
Eg. MOV A,@R0
ADD A,@R1
MOVXA,@DP
TR
5. Indexed addressing
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)—is
used to hold the base address, and the A is used to hold the offset address. Adding the value of
the base address to the value of the offset address forms the effective address. Indexed
addressing is used with JMP or MOVC instructions. Look up tables are easily implemented with
the help of index addressing.
Eg. MOVCA,@A+DPTR // copies the contents of memory location pointed by the sum of
the accumulatorAandtheDPTRintoaccumulatorA.
MOVCA,@A+PC //copies the contents of memory location pointed by the sum of the
accumulator A and the program counter into accumulator A.
6.Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address,
(offset), is an 8 bit signed number, which is automatically added to the PC to make the address of
the next instruction. The 8 bit signed offset value gives an address range of +127 to —128
Microprocessor and Microcontrollers
locations. The jump destination is usually specified using a label and the assembler calculates the
jump offset accordingly. The advantage of relative addressing is that the program code is easy to
relocate and the address is relative to position in the memory.
Eg: SJMP LOOP1
JCBACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the lowest
11 bit of the memory. The upper 5 bit of the destination address are the upper 5 bit of the
current program counter. Hence, absolute addressing allows branching only within the current
2 Kbyte page of the program memory.
Eg. AJMP LOOP1
ACALL LOOP2
8.Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3
byte instructions. The address specifies a full 16 bit destination address so that a jump or a
call can be made to a location within a 64 Kbyte code memory space.
Eg. LJMP FINISH
LCALLDELAY
1. Write a program to add the values of locations 50H and 51H and store the result in locations in
52h and 53H.
2. Write a program to stored at a FFH into RAM memory locations 50H to 58H using direct
addressing mode
3. Write a program to subtract a 16 bit number stored at locations 51H-52H from 55H-56H and
storetheresultinlocations40Hand41H.Assumethattheleastsignificantbyteofdataorthe result is stored
in low address. If the result is positive, then store 00H, else store 01H in 42H. ORG0000H ; Set
program counter0000H
MOVA,55H ; Load the contents of memory location 55 intoA
CLRC ; Clear the borrowflag
SUBBA,51H ; Sub the contents of memory 51H from contents ofA
MOV40H,A ; Save the LSByte of the result in location40H
MOVA,56H ; Load the contents of memory location 56H intoA
SUBBA,52H ; Subtract the content of memory 52H from the contentA
MOV41H, ; Save the MSbyte of the result in location415.
MOVA, #00 ; Load 005 intoA
ADDCA,#00 ; Add the immediate data and the carry flag to A
MOV42H,A ; If result is positive, store00H, else store 0lH in42H
END
Microprocessor and Microcontrollers
4. Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H-56H and store
the result in locations 40H, 41H and 42H. Assume that the least significant byte of data and the
result is stored in low address and the most significant byte of data or the result is stored in
highaddress.
5. Write a program to store data FFH into RAM memory locations 50H to 58H using indirect
addressingmode.
ORG0000H ; Set program counter0000H
MOVA,#0FFH ; Load FFH intoA
MOVRO,#50H ; Load pointer, R0-50H
MOVR5,#08H ; Load counter,R5-08H
Start:MOV@RO,A ; Copy contents of A to RAM pointed by R0
INCRO ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H and
61H and store the result in BCD at memory locations 52H and 53H. Assume that the least
significant byte of the result is stored in lowaddress.
7. Writeaprogramtoclear10RAMlocationsstartingatRAMaddress1000H.
INCDPTR ;IncrementDPTR
DJNZR6,again ;Loop until counterR6=0
END
8. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sumat70H
ORG0000H ; Set program counter0000H
N EQU 15
MOV R0,#00 ; ClearR0
CLRA ; ClearA
again: INC R0 ; Increment R0
ADDA,R0 ; Add the contents of R0 withA
CJNE R 0,# N, again ; Loop until counter, R0,N
MOV70H,A ; Save the result in location 70HEND
9. Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the
resultatmemorylocations52Hand53H.Assumethattheleastsignificantbyteoftheresultis stored in
lowaddress.
ORG 0000H ; Set program counter 00 OH
MOVA,70H;Loadthecontentsofmemorylocation70hintoA
MOVB,71H;Loadthecontentsofmemorylocation71HintoB
MULAB ; Performmultiplication
MOV52H,A;Savetheleastsignificantbyteoftheresultinlocation52HMOV53H,B;Savethemost significant
byte of the result inlocation 53
END
10. Ten 8 bit numbers are stored in internal data memory from location 5oH. Write a
program to increment thedata.
Assume that ten 8 bit numbers are stored in internal data memory from location 50H, hence R0 or R1
must be used as a pointer.
The program is as follows.
OPT 0000H
MOV R0,#50H
MOV R3,#0AH
Loopl: INC @R0
INC RO
DJNZ R3, loopl END
END
11. Write a program to find the average of five 8 bit numbers. Store the result in H.
(Assume that after adding five 8 bit numbers, the result is 8 bitonly).
ORG 0000H
MOV 40H,#05H
MOV 41H,#55H
MOV 42H,#06H
MOV 43H,#1AH
MOV 44H,#09H
MOV R0,#40H
MOV R5,#05H
MOV B,R5
CLR A
Loop: ADD A,@RO
INC RO
Microprocessor and Microcontrollers
DJNZ R5,Loop
DIV AB
MOV55H,A
END
12. Write a program to find the cube of an 8bit number program is as follows
ORG 0000H
MOV
R1,#N
MOV A,R1
MOVB,R1
MULAB //SQUARE
ISCOMPUTED MOV R2,B
MOV B, R1
MUL AB
MOV 50,A
MOV 51,B
MOV A,R2
MOV B, R1
MUL AB
ADD A, 51H
MOV 51H,
A MOV 52H,
B
MOVA,#00H
ADDC
A,52H
MOV52H,A //CUBE IS STORED
IN52H,51H,50H END
13. Write a program to exchange the lower nibble of data present in external memory 6000H and
6001H
ORG0000H ;Setprogramcounter00h MOV
DPTR, # 6000 H ; Copy address 6000 H toDPTR
MOVXA,@DPTR ;Copycontentsof60008toA
MOVR0,#45H ;Loadpointer,R0=45H
MOV@RO,A ;CopycontofAtoRAMpointedby80
INCDPL ;Increment pointer
MOVXA,@DPTR ;Copycontentsof60018toA
XCHDA,@R0 ; E x c h a n g e l o w e r n i b b l e o f A w i t h R A M p o i n t e d b y RO
14. Write a program to count the number of and o's of 8 bit data stored in location6000H.
ORG00008 ; Set program counter 00008
MOVDPTR,#6000h ; Copy address 6000H toDPTR
MOVXA,@DPTR ; Copy num be r t o A
MOVR0,#08 ; Copy 08 inRO
MOVR2,#00 ; C o py 00 in R 2
MOVR3,#00 ; C o py 00 in R 3
Microprocessor and Microcontrollers
JCNEXT ; I f C F = 1 , b r a n c ht o n e x t
INCR2 ; I f C F = 0 , i n c r e m e n t R 2AJMP
NEXT2 NEXT: INCR3 ; I f C F = 1 , i n c r e m e n t R3
NEXT2:DJNZRO,BACK ;RepeatuntilRO iszero END
15. Write a program to shift a 24 bit number stored at 57H-55H to the left logically four places.
Assume that the least significant byte of data is stored in loweraddress.
ORG0000H ; Set program counter0000h
MOVR1,#04 ; Set up loop count to4
again: MOVA,55H ; Place the least significant byte of data inA
CLRC ; Clear tne carryflag
RLCA ; Rotate contents of A (55h) left throughcarry
MOV55H,A
MOVA,56H
RLCA ; Rotate contents of A (56H) left throughcarry
MOV56H,A
MOVA,57H
RLCA ; Rotate contentsofA (57H) left throughcarry
MOV57H,A
DJNZ R1,again ; Repeat until R1 is zero
END
16. Two 8 bit numbers are stored in location 1000h and 1001h of external data memory.
Write a program to find the GCD of the numbers and store the result in 2000h.
ALGORITHM
• Step1 :Initialize external data memory with data and DPTR with address
• Step2 :Load A and TEMP with the operands
• Step3 :Are the two operands equal? If yes,go to step 9
• Step4 :Is(A)greater than(TEMP)? If yes,go to step6
• Step5 :Exchange (A )with(TEMP)such that A contains the bigger number
• Step6 :Perform division operation(contents of A with contents of TEMP)
• Step7 :If the remainder is zero,go to step9
• Step8 :Move the remainder into A and go to step4
• Step9 :Save the contents 'of TEMP in memory and term in at the program
ORG0000H ; Set program counter 0000H
TEMP EQU70H
TEMPI EQU 71H
MOVDPTR, #1000H ; Copy address 100011 toDPTR
MOVXA,@DPTR ; Copy First number toA
MOVTEMP,A ; Copy First number to temp INCDPTR
MOVXA,@DPTR ; Copy Second number toA
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
AJMP LOOP2 ; (A) = (TEMP) branch toL00P2
LOOP1: JNC LOOP3 ; (A) > (TEMP) branch toLOOP3
NOV TEMPI, A ; (A) < (TEMP) exchange (A) with (TEMP)
MOV A, TEMP
MOV TEMP, TEMPI
LOOP3: MOV B, TEMP
DIV AB ; Divide (A) by (TEMP)
MOV A, B ; Move remainder to A
CJNE A,#00, LOOPS ; (A)/=00 branch to LOOPS
LOOP2: MOV A, TEMP
Microprocessor and Microcontrollers
UNIT-V
Timer 1 Register
The 16-bit register of Timer 1 is accessed as low- and high-byte. The low-byte register is called TL1 (Timer 1 low
byte) and the high-byte register is called TH1 (Timer 1 high byte). These registers can be accessed like any other
register. For example, the instruction MOV TL1, #4H moves the value into the low-byte of Timer 1.
Gate − When set, the timer only runs while INT(0,1) is high.
C/T − Counter/Timer select bit.
Microprocessor and Microcontrollers
M1 − Mode bit 1.
M0 − Mode bit 0.
GATE
Every timer has a means of starting and stopping. Some timers do this by software, some by hardware, and some
have both software and hardware controls. 8051 timers have both software and hardware controls. The start and stop
of a timer is controlled by software using the instruction SETB TR1 and CLR TR1 for timer 1, and SETB
TR0 and CLR TR0 for timer 0.
The SETB instruction is used to start it and it is stopped by the CLR instruction. These instructions start and stop the
timers as long as GATE = 0 in the TMOD register. Timers can be started and stopped by an external source by
making GATE = 1 in the TMOD register.
C/T (CLOCK / TIMER)
This bit in the TMOD register is used to decide whether a timer is used as a delay generator or an event manager.
If C/T = 0, it is used as a timer for timer delay generation. The clock source to create the time delay is the crystal
frequency of the 8051. If C/T = 0, the crystal frequency attached to the 8051 also decides the speed at which the
8051 timer ticks at a regular interval.
Timer frequency is always 1/12th of the frequency of the crystal attached to the 8051. Although various 8051 based
systems have an XTAL frequency of 10 MHz to 40 MHz, we normally work with the XTAL frequency of 11.0592
MHz. It is because the baud rate for serial communication of the 8051.XTAL = 11.0592 allows the 8051 system to
communicate with the PC with no errors.
M1 / M2
M1 M2 Mode
1 1 Spilt mode.
If a value, say YYXXH, is loaded into the Timer bytes, then the delay produced by the Timer will be equal to
the product :
[ ( FFFFH – YYXXH +1 ) x ( period of one timer clock ) ].
It can also be considered as follows: convert YYXXH into decimal, say NNNNN, then delay will be equal to the
product :
[ ( 65536-NNNNN ) x ( period of one timer clock ) ].
The period of one timer clock is 1.085 µs for a crystal of 11.0592 MHz frequency as discussed above.
Now to produce a desired delay, divide the required delay by the Timer clock period. Assume that the division yields
a number NNNNN. This is the number of times Timer must be updated before it stops. Subtract this number from
65536 (binary equivalent of FFFFH) and convert the difference into hex. This will be the initial value to be loaded
into the Timer to get the desired delay.
Communication Links
1. Simplex communication link:In simplex transmission,the line is dedicated for transmission. The
transmitter sends and the receiver receives the data.
Transmitter Receiver
2.Half duplex communication link:In half duplex,the communication link can be used for either transmission
or reception. Data is transmitted in only one direction at a time.
Transmitter Receiver
Receiver Transmitter
3.Full duplex communication link :If the data is transmitted in both ways at the same time,it is a full duplex i.e. transmission
and reception can proceed simultaneously. This communication link requires two wires for data, one for transmission and one
for reception.
Transmitter Receiver
Receiver Transmitter
Clock
Asynchronous Serial data transmission:In this,different clock sources are used for transmitter and receiver. In this
mode, data is transmitted with start and stop bits. A transmission begins with start bit, followed by data and then
stop bit. For error checking purpose parity bit is included just
8051 SERIALCOMMUNICATION
3.PCON register: The SMOD bit (bit 7) of PCON register controls the baud rate in asynchronous mode transmission.
CONNECTORS-232
RS-232 standards:
Microprocessor and Microcontrollers
To allow compatibility among data communication equipment made by various manufactures, an interfacing
standard called RS232 was set by the Electronics Industries Association (EIA) in 1960. Since the standard was set
long before the advent of logic family, its input and output voltage levels are not TTL compatible.
In RS232, a logic one (1) is represented by -3 to -25V and referred as MARK while logic zero
(0) is represented by +3 to +25V and referred as SPACE. For this reason to connect any RS232 to a microcontroller
system we must use voltage converters such as MAX232 to convert the TTL logic level to RS232 voltage levels
and vice-versa. MAX232 IC chips are commonly referred as line drivers.
In RS232 standard we use two types of connectors. DB9 connector or DB25 connector.
DB9MaleConnector DB25MaleConnector
The 8051 has two pins that are used specifically for transferring and receiving data serially. These two pins
are called TXD, RXD. Pin 11 of the 8051 (P3.1) assigned to TXD and pin 10 (P3.0) is designated as RXD.
These pins TTL compatible; therefore they require line driver (MAX 232) to make them RS232 compatible.
MAX 232 converts RS232 voltage levels to TTL voltage levels and vice versa. One advantage of the
MAX232 is that it uses a +5V power source which is the same as the source voltage for the 8051. The typical
connection diagram between MAX 232 and 8051 is shown below.
3.The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8-bit data is
framed with start and stopbits.
4.TR1 is set to 1 start timer1.
5.TI is cleared by the “CLR TI”instruction.
6.The character byte to be transferred serially is written into the SBUFregister.
7.The TI flag bit is monitored with the use of the instruction JNB TI, target to see if the character has
been transferredcompletely.
8.To transfer the next character, go to step5.
Example1.Write a program for the 8051 to transfer letter ‘A’ serially at 4800-baudrate,8bit data, 1 stop bit
continuously.
Example2.Write a program for the 8051to transfer the message ‘EARTH’serially at 9600 baud,8 bit data, 1
stop bit continuously.
END
Microprocessor and Microcontrollers
Interrupts:
An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate
attention. Whenever an interrupt occurs, the controller completes the execution of the current instruction and starts
the execution of an Interrupt Service Routine (ISR) or Interrupt Handler. ISR tells the processor or controller
what to do when the interrupt occurs. The interrupts can be either hardware interrupts or software interrupts.
Hardware Interrupt
A hardware interrupt is an electronic alerting signal sent to the processor from an external device, like a disk
controller or an external peripheral. For example, when we press a key on the keyboard or move the mouse, they
trigger hardware interrupts which cause the processor to read the keystroke or mouse position.
Software Interrupt
A software interrupt is caused either by an exceptional condition or a special instruction in the instruction set which
causes an interrupt when it is executed by the processor. For example, if the processor's arithmetic logic unit runs a
command to divide a number by zero, to cause a divide-by-zero exception, thus causing the computer to abandon the
calculation or display an error message. Software interrupt instructions work similar to subroutine calls.
What is Polling?
The state of continuous monitoring is known as polling. The microcontroller keeps checking the status of other
devices; and while doing so, it does no other operation and consumes all its processing time for monitoring. This
problem can be addressed by using interrupts.
In the interrupt method, the controller responds only when an interruption occurs. Thus, the controller is not required
to regularly monitor the status (flags, signals etc.) of interfaced and inbuilt devices.
Interrupts v/s Polling
Here is an analogy that differentiates an interrupt from polling −
Interrupt Polling
Reset 0000 9
• When the reset pin is activated, the 8051 jumps to the address location 0000. This is power-up reset.
• Two interrupts are set aside for the timers: one for timer 0 and one for timer 1. Memory locations are 000BH
and 001BH respectively in the interrupt vector table.
• Two interrupts are set aside for hardware external interrupts. Pin no. 12 and Pin no. 13 in Port 3 are for the
external hardware interrupts INT0 and INT1, respectively. Memory locations are 0003H and 0013H
respectively in the interrupt vector table.
• Serial communication has a single interrupt that belongs to both receive and transmit. Memory location
0023H belongs to this interrupt.
Microprocessor and Microcontrollers
If the interrupt source is still asserted Edge-triggered interrupt modules can be acted
when the firmware interrupt handler immediately, no matter how the interrupt source
handles the interrupt, the interrupt behaves.
module will regenerate the interrupt,
causing the interrupt handler to be
invoked again.
• EA − Global enable/disable.
• - − Undefined.
• ET2 − Enable Timer 2 interrupt.
• ES − Enable Serial port interrupt.
• ET1 − Enable Timer 1 interrupt.
• EX1 − Enable External 1 interrupt.
• ET0 − Enable Timer 0 interrupt.
• EX0 − Enable External 0 interrupt.
To enable an interrupt, we take the following steps −
• Bit D7 of the IE register (EA) must be high to allow the rest of register to take effect.
• If EA = 1, interrupts will be enabled and will be responded to, if their corresponding bits in IE are high. If EA
= 0, no interrupts will respond, even if their associated pins in the IE register are high.
Interrupt Priority in 8051
We can alter the interrupt priority by assigning the higher priority to any one of the interrupts. This is accomplished
by programming a register called IP (interrupt priority).
The following figure shows the bits of IP register. Upon reset, the IP register contains all 0's. To give a higher
priority to any of the interrupts, we make the corresponding bit in the IP register high.
- - - - PT1 PX1 PT0 PX0
What happens if the 8051 is executing an ISR that belongs to an interrupt and another one gets active? In such cases,
a high-priority interrupt can interrupt a low-priority interrupt. This is known as interrupt inside interrupt. In 8051,
a low-priority interrupt can be interrupted by a high-priority interrupt, but not by any another low-priority interrupt.
Triggering an Interrupt by Software
There are times when we need to test an ISR by way of simulation. This can be done with the simple
instructions to set the interrupt high and thereby cause the 8051 to jump to the interrupt vector table. For
example, set the IE bit as 1 for timer 1. An instruction SETB TF1 will interrupt the 8051 in whatever it is
doing and force it to jump to the interrupt vector table.
Light Emitting Diodes or LEDs are the mostly commonly used components in many applications. They are made of
semiconducting material. In this, It describe about basics of Interfacing LED with 8051 Microcontroller.Principle
behind Interfacing LED with 8051.The main principle of this circuit is to interface LEDs to the 8051 family micro controller.
Commonly, used LEDs will have voltage drop of 1.7v and current of 10mA to glow at full intensity. This is applied through the
output pin of the micro controller.
In this circuit, LEDs are connected to the port P0. The controller is connected with external crystal oscillator to pin 18 and 19
pins. Crystal pins are connected to the ground through capacitors of 33pf.
The Light Emitting Diode is similar to the normal PN diode but it emits energy in the form of light. The color of light depends
on the band gap of the semiconductor. The following figure shows “how an LED glows?”
Microprocessor and Microcontrollers
Thus, LED is connected to the AT89C51 microcontroller with the help of a current limiting resistor. The value of this resistor is
calculated using the following formula.
Generally, microcontrollers output a maximum voltage of 5V. Thus, the value of resistor calculated for this is 330 Ohms. This
resistor can be connected to either the cathode or the anode of the LED.
LCD display is an inevitable part in almost all embedded projects and this article is about interfacing a 16×2 LCD with 8051
microcontroller. Many guys find it hard to interface LCD module with the 8051 but the fact is that if you learn it properly, its a
very easy job and by knowing it you can easily design embedded projects like digital voltmeter / ammeter, digital clock, home
automation displays, status indicator display, digital code locks, digital speedometer/ odometer, display for music players etc
etc. Thoroughly going through this article will make you able to display any text (including the extended characters) on any part
of the 16×2 display screen. In order to understand the interfacing first you have to know about the 16×2 LCD module.
16×2 LCD module is a very common type of LCD module that is used in 8051 based embedded projects. It consists of
16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices. The module were are talking about here is type number JHD162A
which is a very popular one . It is available in a 16 pin package with back light ,contrast adjustment function and each dot matrix
has 5×8 dot resolution. The pin numbers, their name and corresponding functions are shown in the table below.
4 RS Register selection
Microprocessor and Microcontrollers
6 E Enable
7 DB0 Data
8 DB1 Data
9 DB2 Data
10 DB3 Data
11 DB4 Data
12 DB5 Data
13 DB6 Data
14 DB7 Data
VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted by varying the voltage at this
pin. This is done by connecting one end of a POT to the Vcc (5V), other end to the Ground and connecting the center terminal
(wiper) of of the POT to the VEE pin. See the circuit diagram for better understanding.
The JHD162A has two built in registers namely data register and command register. Data register is for placing the data to be
displayed , and the command register is to place the commands. The 16×2 LCD module has a set of commands each meant for
doing a particular job with the display. We will discuss in detail about the commands later. High logic at the RS pin will select
the data register and Low logic at the RS pin will select the command register. If we make the RS pin high and the put a data in
the 8 bit data line (DB0 to DB7) , the LCD module will recognize it as a data to be displayed . If we make RS pin low and put a
data on the data line, the module will recognize it as a command.
Microprocessor and Microcontrollers
R/W pin is meant for selecting between read and write modes. High level at this pin enables read mode and low level at this pin
enables write mode.
E pin is for enabling the module. A high to low transition at this pin will enable the module.
DB0 to DB7 are the data pins. The data to be displayed and the command instructions are placed on these pins.
LED+ is the anode of the back light LED and this pin must be connected to Vcc through a suitable series current limiting
resistor. LED- is the cathode of the back light LED and this pin must be connected to ground.
16×2 LCD module has a set of preset command instructions. Each command will make the module to do a particular task. The
commonly used commands and their function are given in the table below.
Command Function
01 Clear screen
02 Return home
04 Decrement cursor
06 Increment cursor
LCD initialization:
The steps that has to be done for initializing the LCD display is given below and these steps are common for almost all
applications.
• Send 38H to the 8 bit data line for initialization
• Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
• Send 06H for incrementing cursor position.
• Send 01H for clearing the display and return the cursor.
The steps for sending data to the LCD module is given below. I have already said that the LCD module has pins namely RS,
R/W and E. It is the logic state of these pins that make the module to determine whether a given data input is a command or data
to be displayed.
Circuit diagram:
The circuit diagram given above shows how to interface a 16×2 LCD module with AT89S1 microcontroller.
Capacitor C3, resistor R3 and push button switch S1 forms the reset circuitry. Ceramic capacitors C1,C2 and crystal
X1 is related to the clock circuitry which produces the system clock frequency. P1.0 to P1.7 pins of the
microcontroller is connected to the DB0 to DB7 pins of the module respectively and through this route the data goes
to the LCD module. P3.3, P3.4 and P3.5 are connected to the E, R/W, RS pins of the microcontroller and through this
route the control signals are transffered to the LCD module. Resistor R1 limits the current through the back light LED
and so do the back light intensity. POT R2 is used for adjusting the contrast of the display. Program for interfacing
LCD to 8051 microcontroller is shown below.
Program:
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#82D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#85D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#83D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#79D
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#65D
ACALL DISP
MOV A,#89D
ACALL DISP
DISP:MOV P1,A
SETB P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET
MOV P1,#0FFh
SETB P3.3
MOV A,P1
JB ACC.7,DELY
CLR P3.3
CLR P3.4
RET
END
Subroutine CMND sets the logic of the RS, R/W, E pins of the LCD module so that the module recognizes the input data ( given
to DB0 to DB7) as a command.Subroutine DISP sets the logic of the RS, R/W, E pins of the module so that the module
recognizes the input data as a data to be displayed .
Interfacing LCD Module to 8051 in 4 Bit Mode (using only 4 pins of a port)
The microcontroller like 8051 has only limited number of GPIO pins (GPIO – general purpose input output). So to design
complex projects we need sufficient number of I/O pins . An LCD module can be interfaced with a microcontroller either in 8
bit mode (as seen above) or in 4 bit mode. 8 bit mode is the conventional mode which uses 8 data lines and RS, R/W, E pins for
functioning. However 4 bit mode uses only 4 data lines along with the control pins. This will saves the number of GPIO pins
needed for other purpose.
Objectives
As shown in the circuit diagram, port 0 of the controller is used for interfacing it with LCD module. In 4 bit mode only 4 lines
D4-D7, along with RS, R/W and E pins are used. This will save us 4 pins of our controller which we might employ it for other
purpose. Here we only need to write to the LCD module. So the R/W pin can be ground it as shown in the schematic diagram. In
this way the total number of pins can be reduced to 6. In 4 Bit mode the data bytes are split into two four bits and are transferred
in the form of a nibble. The data transmission to a LCD is performed by assigning logic states to the control pins RS and E. The
reset circuit, oscillator circuit and power supply need to be provided for the proper working of the circuit.
RS EQU P0.4
EN EQU P0.5
PORT EQU P0
Microprocessor and Microcontrollers
U EQU 30H
L EQU 31H
ORG 000H
MOV DPTR,#INIT_COMMANDS
ACALL LCD_CMD
MOV DPTR,#LINE1
ACALL LCD_CMD
MOV DPTR,#TEXT1
ACALL LCD_DISP
MOV DPTR,#LINE2
ACALL LCD_CMD
MOV DPTR,#TEXT2
ACALL LCD_DISP
SJMP $
LCD_CMD: CLR A
MOVC A,@A+DPTR
JZ EXIT2
INC DPTR
CLR RS
ACALL SPLITER
MOV A,U
ACALL MOVE
MOV A,L
ACALL MOVE
SJMP LCD_CMD
EXIT2: RET
LCD_DATA: SETB RS
ACALL SPLITER
MOV A,U
ACALL MOVE
MOV A,L
ACALL MOVE
RET
LCD_DISP: CLR A
Microprocessor and Microcontrollers
MOVC A,@A+DPTR
JZ EXIT1
INC DPTR
ACALL LCD_DATA
SJMP LCD_DISP
EXIT1: RET
INIT_COMMANDS: DB 20H,28H,0CH,01H,06H,80H,0
LINE1: DB 01H,06H,06H,80H,0
LINE2: DB 0C0H,0
CLEAR: DB 01H,0
END
Interfacing a hex key pad to 8051 microcontroller. A clear knowledge on interfacing hex key pad to 8051 is very essential
while designing embedded system projects which requires character or numeric input or both. For example projects like digital
code lock, numeric calculator etc. Before going to the interfacing in detail, let’s have a look at the hex keypad.
Hex keypad
Hex key pad is essentially a collection of 16 keys arranged in the form of a 4×4 matrix. Hex key pad usually have keys
representing numerics 0 to 9 and characters A to F. The simplified diagram of a typical hex key pad is shown in the figure
below.
Hex keypad
The hex keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 and C4. R1 to R4 represents the four rows and
C1 to C4 represents the four columns. When a particular key is pressed the corresponding row and column to which the
terminals of the key are connected gets shorted. For example if key 1 is pressed row R1 and column C1 gets shorted and so on.
The program identifies which key is pressed by a method known as column scanning. In this method a particular row is kept low
(other rows are kept high) and the columns are checked for low. If a particular column is found low then that means that the key
connected between that column and the corresponding row (the row that is kept low) is been pressed. For example if row R1 is
initially kept low and column C1 is found low during scanning, that means key 1 is pressed.
The circuit diagram for demonstrating interfacing hex keypad to 8051 is shown below.Like previous 8051 projects, AT89S51 is
the microcontroller used here. The circuit will display the character/numeric pressed on a seven segment LED display. The
circuit is very simple and it uses only two ports of the microcontroller, one for the hex keypad and the other for the seven
segment LED display.
Program:
ORG 00H
MOV DPTR,#LUT // moves starting address of LUT to DPTR
MOV A,#11111111B // loads A with all 1's
MOV P0,#00000000B // initializes P0 as output port
NEXT5:JB P1.5,NEXT6
MOV A,#5D
ACALL DISPLAY
NEXT6:JB P1.6,NEXT7
MOV A,#6D
ACALL DISPLAY
NEXT7:JB P1.7,NEXT8
MOV A,#7D
ACALL DISPLAY
NEXT8:SETB P1.1
CLR P1.2
JB P1.4,NEXT9
MOV A,#8D
ACALL DISPLAY
NEXT9:JB P1.5,NEXT10
MOV A,#9D
ACALL DISPLAY
NEXT10:JB P1.6,NEXT11
MOV A,#10D
ACALL DISPLAY
NEXT11:JB P1.7,NEXT12
MOV A,#11D
ACALL DISPLAY
NEXT12:SETB P1.2
CLR P1.3
JB P1.4,NEXT13
MOV A,#12D
ACALL DISPLAY
NEXT13:JB P1.5,NEXT14
MOV A,#13D
ACALL DISPLAY
NEXT14:JB P1.6,NEXT15
MOV A,#14D
ACALL DISPLAY
NEXT15:JB P1.7,BACK
MOV A,#15D
ACALL DISPLAY
LJMP BACK
DISPLAY:MOVC A,@A+DPTR // gets digit drive pattern for the current key from LUT
MOV P0,A // puts corresponding digit drive pattern into P0
RET
DB 10011110B
DB 11111100B
DB 10001110B
DB 01111010B
END
Interface a seven segment LED display to an 8051 microcontroller. 7 segment LED display is very popular and it can display
digits from 0 to 9 and quite a few characters like A, b, C, ., H, E, e, F, n, o,t,u,y, etc. Knowledge about how to interface a seven
segment display to a micro controller is very essential in designing embedded systems. A seven segment display consists of
seven LEDs arranged in the form of a squarish ‘8’ slightly inclined to the right and a single LED as the dot character. Different
characters can be displayed by selectively glowing the required LED segments. Seven segment displays are of two
types, common cathode and common anode. In common cathode type , the cathode of all LEDs are tied together to a single
terminal which is usually labeled as ‘com‘ and the anode of all LEDs are left alone as individual pins labeled as a, b, c, d, e, f, g
& h (or dot) . In common anode type, the anode of all LEDs are tied together as a single terminal and cathodes are left alone as
individual pins. The pin out scheme and picture of a typical 7 segment LED display is shown in the image below.
Digit drive pattern of a seven segment LED display is simply the different logic combinations of its terminals ‘a’ to ‘h‘ in order
to display different digits and characters. The common digit drive patterns (0 to 9) of a seven segment display are shown in the
table below.
Microprocessor and Microcontrollers
Digit a b c d e f g
0 1 1 1 1 1 1 0
1 0 1 1 0 0 0 0
2 1 1 0 1 1 0 1
3 1 1 1 1 0 0 1
4 0 1 1 0 0 1 1
5 1 0 1 1 0 1 1
6 1 0 1 1 1 1 1
7 1 1 1 0 0 0 0
8 1 1 1 1 1 1 1
9 1 1 1 1 0 1 1
The circuit diagram shown above is of an AT89S51 microcontroller based 0 to 9 counter which has a 7 segment LED display
interfaced to it in order to display the count. This simple circuit illustrates two things. How to setup simple 0 to 9 up counter
using 8051 and more importantly how to interface a seven segment LED display to 8051 in order to display a particular result.
The common cathode seven segment display D1 is connected to the Port 1 of the microcontroller (AT89S51) as shown in the
circuit diagram. R3 to R10 are current limiting resistors. S3 is the reset switch and R2,C3 forms a debouncing circuitry. C1, C2
and X1 are related to the clock circuit. The software part of the project has to do the following tasks.
All the above said tasks are accomplished by the program given below.
Program:
Instruction MOVC A,@A+PC is the instruction that produces the required digit drive pattern for the display. Execution of this
instruction will add the value in the accumulator A with the content of the program counter(address of the next instruction) and
will move the data present in the resultant address to A. After this the program resumes from the line after MOVC A,@A+PC.
In the program, initial value in A is 00001001B. Execution of MOVC A,@A+PC will add oooo1001B to the content in PC (
address of next instruction). The result will be the address of label DB 3FH (line15) and the data present in this address ie 3FH
(digit drive pattern for 0) gets moved into the accumulator. Moving this pattern in the accumulator to Port 1 will display 0 which
is the first count.
At the next count, value in A will advance to 00001010 and after the execution of MOVC A,@+PC ,the value in A will be 06H
which is the digit drive pattern for 1 and this will display 1 which is the next count and this cycle gets repeated for subsequent
counts.
The reason why accumulator is loaded with 00001001B (9 in decimal) initially is that the instructions from line 9 to line 15
consumes 9 bytes in total.
The lines 15 to 24 in the program which starts with label DB can be called as a Look Up Table (LUT). label DB is known as
Define Byte – which defines a byte. This table defines the digit drive patterns for 7 segment display as bytes (in hex format).
MOVC operator fetches the byte from this table based on the result of adding PC and contents in the accumulator.
Register B is used as a temporary storage of the initial value of the accumulator and the subsequent increments made to
accumulator to fetch each digit drive pattern one by one from the look up table(LUT).
Note:- In line 6, Accumulator is incremented by 1 each time (each loop iteration) to select the next digit drive pattern. Since
MOVC operator uses the value in A to fetch the digit drive pattern from LUT, value in ACC has to be incremented/manipulated
accordingly. The digit drive patterns are arranged consecutively in LUT.
Register R0 is used as a counter which counts from 10 down to 0. This ensures that digits from o to 9 are continuously displayed
in the 7 segment LED. You may note lines 4, 11, 12, and 13 in the above program. Line 4 initializes R0 to 10 (OAh). When the
program counter reaches line 11 for the first time, 7 segment LED has already displayed 0. So we can reduce one count and that
is why we have written DEC Ro. We need to continuously check if R0 has reached full count (that is 0). In order to do that lines
12 and 13 are used. We move R0 to accumulator and then use the Jump if Zero (JZ) instruction to check if accumulator has
reached zero. If Acc=0, then we makes the program to jump to START (initial state) and hence we restart the 7 segment LED to
display from 0 to 9 again. If Acc not equal to zero, we continue the program to display the next digit (check line 14).
The stepper motors coil A,B,C,D is connected to the port 1 i.e. to P1.0, P1.2, P1.2 and P1.3.
The Microcontroller does not provide sufficient current to drive motor and to safeguard 8081 from loading effect and burn
out condition, a motor driver IC ULN 2003 between 8051 and stepper motor. ULN 2003 is a stepper motor driver.
Microprocessor and Microcontrollers
1. Position control
2. Direction control &
3. Speed control
Stepper motors are basically two types: Unipolar and Bipolar. Unipolar stepper motor generally has five or six wire, in which
four wires are one end of four stator coils, and other end of the all four coils is tied together which represents fifth wire, this is
called common wire (common point). Generally there are two common wire, formed by connecting one end of the two-two coils
as shown in below figure. Unipolar stepper motor is very common and popular because of its ease of use.
In Bipolar stepper motor there is just four wires coming out from two sets of coils, means there are no common wire.
Stepper motor is made up of a stator and a rotator. Stator represents the four electromagnet coils which remain stationary around
the rotator, and rotator represents permanent magnet which rotates. Whenever the coils energised by applying the current, the
electromagnetic field is created, resulting the rotation of rotator (permanent magnet). Coils should be energised in a particular
sequence to make the rotator rotate. On the basis of this “sequence” we can divide the working method of Unipolar stepper
motor in three modes: Wave drive mode, full step drive mode and half step drive mode.
In this type of functioning, the following 4 binary sequence/code are used for rotation: (Considering step angle= 1.8 degrees)
Microprocessor and Microcontrollers
In this type of functioning, the following 8 binary sequence/code are used for rotation: (Considering step angle= 0.9degrees)
Program
Microprocessor and Microcontrollers
The main purpose of DC interfacing with 8051 microcontroller is for controlling the speed of the motor. The DC motor is an
electrical machine with a rotating part termed as a rotor which has to be controlled. For example, consider the DC motor whose
Microprocessor and Microcontrollers
speed or direction of rotation of DC motor can be controlled using programming techniques which can be achieved
by interfacing with 8051 microcontroller. So, in this article let us discuss about interfacing DC motor with 8051 microcontroller.
Here, interfacing 8051 with DC motor requires a motor driver. There are various types of driver ICs among which L293D is
typically used for interfacing DC motor with 8051. L293 is an IC with 16 pins which are represented in the figure below.
This describes a bidirectional DC motor that changes its direction automatically after a preset amount of time (around 1S).
AT89S51 is the microcontroller used here and L293 forms the motor driver. Circuit diagram is shown above
In the circuit components R1, S1 and C3 forms a debouncing reset circuitry. C1, C2 and X1 are related to the oscillator.
Port pins P1.0 and P1.1 are connected to the corresponding input pins of the L293 motor driver. The motor is connected across
output pins 3 and 6 of the L293. The software is so written that the logic combinations of P1.0 and P1.1 controls the direction of
the motor. Initially when power is switched ON, P1.0 will be high and P1.1 will be low. This condition is maintained for a preset
amount of time (around 1S) and for this time the motor will be running in the clockwise direction (refer the function table of
Microprocessor and Microcontrollers
L293). Then the logic of P1.0 and P1.1 are swapped and this condition is also maintained for the same duration . This makes the
motor to run in the anti clockwise direction for the same duration and the entire cycle is repeated.
Program:
ORG 00H // initial starting address
MAIN: MOV P1,#00000001B // motor runs clockwise
ACALL DELAY // calls the 1S DELAY
MOV P1,#00000010B // motor runs anti clockwise
ACALL DELAY // calls the 1S DELAY
SJMP MAIN // jumps to label MAIN for repaeting the cycle
DELAY: MOV R4,#0FH
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END
Notes:
The maximum current capacity of L293 is 600mA/channel. So do not use a motor that consumes more than that.
The supply voltage range of L293 is between 4.5 and 36V DC. So you can use a motor falling in that range.
ADC 0808:
The ADC used in the interfacing is ADC 0808.It is successive approximation 8-bit ADC. It has 28 pins, and can
handle upto 8 analog signals using one chip. It has got an 8-bit data output. The 8 input channels are IN0-IN7, and
Vref(+)=5V; Vref(-) has been grounded. In order to select the inputs IN0-IN7; A, B and C addresses are used.
ADC receives analog signal from the source. This analog signal is received from one of the 8 input channels of
ADC0808. Then this signal is processed accordingly and converted to corresponding digital signal. This signal is then
sent to the microcontroller and the output is displayed using Light Emitting Diode (LED)
iv) If a high to low output is obtained at EOC(End of Conversion), it indicates that the data conversion is finished and
the data is ready.
v) OE(Output Enable) is activated to read output data from the ADC chip. In order to bring the digital data out of the
chip a low to high pulse is is given to the OE pin.
ASSEMBLY PROGRAM TO INTERFACE ADC WITH 8051
ORG 000AH
SJMP MAIN
ADC_DATA EQU P1 ;Give Name To Port Pins
ADC_SC BIT P3.0
ADC_EOC BIT P3.1 ADC 8051 microcontroller LED 9
ADC_ALE BIT P3.2
ADC_OE BIT P3.3
ADD_A BIT P3.4
ADD_B BIT P3.5
ADD_C BIT P3.6
MAIN: MOV ADC_DATA,#0FFH ;Port 1 is input port
SETB ADD_A ;select channel
SETB ADD_B
CLR ADD_C ;for channel 3 selection
ACALL DELAY1
ACALL ADC_COUNT
MOV P0,A ;
ADC Programming Start
ADC_COUNT: SETB ADC_EOC ;it is made as input Port
CLR ADC_ALE
CLR ADC_SC
CLR ADC_OE
BACK: SETB ADC_ALE ;High To Low Pulse is given to ALE
ACALL DELAY1
SETB ADC_SC ;High To Low Pulse is given to SC
ACALL DELAY1
CLR ADC_ALE
CLR ADC_SC
LOOP1: JB ADC_EOC,LOOP1 ;Wait for conversion to finish
LOOP2: JNB ADC_EOC,LOOP2 ;Output becomes high
SETB ADC_OE ;Set OE High to covert data on controller
ACALL DELAY1 ;For Further delay
CLR ADC_OE ;digital converted data is saved in memory
MOV B,#05H DIV AB ;amplify with gain in place of 05H for obtaining real digital data
RET ;Return To Main Routine Delay ;
App. 1.3643 Sec. Delay
DELAY: MOV R3,#3
LOOP3: MOV R1,#254
LOOP4: MOV R2,#254
LOOP5: DJNZ R2,LOOP5
DJNZ R1,LOOP4
DJNZ R3,LOOP3
RET ;Approximately 435 µsec
DELAY1: MOV R3,#1
LOOP6: MOV R1,#10
LOOP7: MOV R2,#10
Microprocessor and Microcontrollers