Professional Documents
Culture Documents
Evolution of Microprocessors:
Intel introduced its first 4-bit microprocessor 4004 in 1971 and its 8-bit microprocessor
8008 in 1972. These microprocessors could not survive as general purpose microprocessors
due to their design and performance limitations.
The first general purpose 8-bit microprocessor 8080 was launched in 1974 and later in
1977 an updated version of 8080, the 8085 microprocessor was introduced with more added
features which result in a functionally complete microprocessor.
low speed,
low memory addressing capability,
limited number of general purpose registers and
less powerful instruction set.
In the family of 16-bit microprocessors, Intel’s 8086 was the first one to be launched in 1978.
No. of Bits 4 8 8 8 16
It is a 40 pin DIP based on N-channel, depletion mode silicon gate technology (HCMOS).
The term 16-bit means that it supports a 16-bit ALU, its internal registers and most of the
instructions are designed to work with 16 bit binary words.
8086 is available at different clock speeds Viz, 5 MHz (8086); 8MHz (8086-2) and 10MHz
(8086-1).
8086 microprocessor has a 16-bit data bus and 20-bit address bus. So, it can address any
one of 220 = 1048576 = 1 Megabyte (1MB) memory locations.
The 8086 microprocessor can work in two modes of operations. They are Minimum mode
and Maximum mode. The minimum mode of operation means single processor system, but
in the maximum mode the 8086 can work in multi-processor or co-processor
configuration. These minimum or maximum operations are decided by the pin MN/ MX.
When this pin is high 8086 operates in minimum mode otherwise it operates in maximum
mode.
Execution Unit:
The 8086 16-bit flag register contents indicate the results of computations in the ALU. It
also contains some flag bits to control the CPU operations.
A flag is a flip-flop that indicates some condition produced by the execution of an
instruction or controls certain operations of the EU.
General Purpose Registers:
Bus Interface Unit: The BIU consists of a 6-byte long prefetch instruction Queue and four
stack segment registers (ES, CS, SS, DS), one Instruction Pointer (IP) and an adder circuit to
calculate the 20bit physical address of a location. This bus interface unit will perform all
external bus operations. They are fetching the instructions from the memory, read/write data
from/into memory or port and also supporting the instruction Queue etc. The BIU fetches up
to six instruction bytes from the memory and stores these pre-fetched bytes in a first –in first
out register set called Queue.
REGISTER ORGANISATION:
The 14 sixteen bit registers of 8086 microprocessor are categorized into four groups. They are
general purpose or data registers, Pointer & Index registers, Segment registers and Flag
register.
Segment Registers: There are four 16-bit segment registers namely code segment register
(CS), Stack segment register (SS), Data segment register (DS) and Extra segment register (ES).
The code segment register is used for addressing the 64KB memory location in the code
segment of the memory ,where the code of the executable program is stored. Similarly the DS
register points to the data segment of the 64KB memory where the data is stored. The Extra
segment register also refers to essentially another data segment of the memory space. The SS
register is useful for addressing stack segment of memory.
Instruction Pointer Register: It is a 16-bit register which always points to the next instruction
to be executed within the currently executing code segment. So, this register contains the 16-
bit offset address pointing to the next instruction code within the 64KB of the code segment
area. Its content is automatically incremented as the execution of the next instruction takes
place.
Flag Register: This register is also called status register. It is a 16 bit register which contains
six status flags and three control flags. So, only nine bits of the 16 bit register are defined and
the remaining seven bits are undefined. Normally this status flag bits indicate the status of the
ALU after the arithmetic or logical operations.
CF- Carry Flag: This flag is set, when there is a carry out of MSB in case of addition or a
borrow in case of subtraction.
PF - Parity Flag: This flag is set to 1, if the lower byte of the result contains even number of
1’s else (for odd number of 1s ) set to zero.
AF- Auxiliary Carry Flag: This is set, if there is a carry out of lowest nibble.
ZF- Zero Flag: This flag is set, if the result of the computation or comparison performed by
the previous instruction is zero.
SF- Sign Flag: This flag is set, when the result of any computation is negative.
TF - Tarp Flag: If this flag is set, the processor enters the single step execution mode.
IF- Interrupt Flag: If this flag is set, the maskable interrupt INTR of 8086 is enabled and if it
is zero, the interrupt is disabled. It can be set by using the STI instruction and can be cleared
by executing CLI instruction.
DF- Direction Flag: This is used by string manipulation instructions. If this flag bit is ‘0’, the
string is processed beginning from the lowest address to the highest address, i.e., auto
incrementing mode. Otherwise, the string is processed from the highest address towards
the lowest address, i.e., auto decrementing mode.
OF- Over flow Flag: This flag is set, if an overflow occurs, i.e, if the result of a signed operation
is large enough to accommodate in a destination register. The result is of more than 7-bits
in size in case of 8-bit signed operation and more than 15-bits in size in case of 16-bit sign
operations, and then the overflow will be set.
A16/S3, A17/S4, A18/S5, A19/S6: The specified address lines are multiplexed with corresponding
status signals.
BHE (Active Low)/S7 (Output): Bus High Enable/Status. During T1 it is low. It is used to
enable data onto the most significant half of data bus, D8-D15. 8-bit device connected to upper
half of the data bus use BHE (Active Low) signal. It is multiplexed with status signal S7.
RD (Read) (Active Low): The signal is used for read operation. It is an output signal. It is
active when low.
READY: This is the acknowledgement from the slow device 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.
INTR-Interrupt Request: This is a level triggered interrupt input. This is sampled during
the last clock cycles of each instruction to determine the availability of the request. If any
interrupt request is pending, the processor enters in to the interrupt acknowledge cycle. This
can be internally masked by resulting the interrupt enable flag.
NMI (Input) NON-MASKABLE INTERRUPT: It is an edge triggered input which causes a
type 2 interrupt. A subroutine is vectored to via an interrupt vector lookup table located in
system memory. NMI is not maskable internally by software. A transition from LOW to HIGH
initiates the interrupt at the end of the current instruction.
RQ/GT RQ/GT0: REQUEST/GRANT: These pins are used by other local bus masters to
force the processor to release the local bus at the end of the processor's current bus cycle. Each
pin is bidirectional with RQ/GT having higher priority than RQ /GT1.
LOCK: It indicates that other system bus masters are not to allow to gain control of the system
bus while LOCK is active LOW. The LOCK signal remains active until the completion of the
next instruction.
TEST: This input is examined by a ‘WAIT’ instruction. If the TEST pin goes low, execution
will continue, else the processor remains in an idle state.
CLK- Clock Input: The clock input provides the basic timing for processor operation and
bus control activity and an asymmetric square wave with 33% duty cycle.
RESET (Input) RESET: causes the processor to immediately terminate its present activity.
The signal must be active HIGH for at least four clock cycles.
GND – Ground
QS1, QS0 (Queue Status) These signals indicate the status of the internal 8086 instruction queue
according to the table shown below
0 0 No Operation
0 1 First Byte of Op Code from
Queue
DEN: DATA ENABLE this pin is is active LOW during each memory and I/O access and for
INTA cycles.
HOLD/HLDA: HOLD indicates that another master is requesting a local bus .This is an active
HIGH. The processor receiving the ``hold'' request will issue HLDA (HIGH) as an
acknowledgement in the middle of a T 4 or T 1 clock cycle.
MEMORY ORGANIZATION:
The 8086 processor provides a 20-bit address to access any location of the 1 MB memory
space. The memory is organized as a linear array of 1 million bytes, addressed as 00000(H) to
FFFFF (H). The memory is logically divided into code, data, extra and stack segments of up to
64K bytes each. Physically, the memory is organized as a high bank (D15 - D8) and a low
bank (D7–D0) of 512 K 8-bitbytes addressed in parallel by the processor's address lines A19 -
A1. Byte data with even addresses is transferred on the D7 – D0 bus lines while odd addressed
byte data (A0 HIGH) is transferred on the D15-D8 bus lines. The processor provides two enable
signals, BHE and A0, to selectively allow reading from or writing into either an odd byte
location, even byte location, or both. The instruction stream is fetched from memory as words
and is addressed internally by the processor to the byte level as necessary.
Fig. 1.4. Memory Organization
Transreceivers are the bidirectional buffers and they are called as data amplifiers. They
are required to separate the valid data from the time multiplexed address/data signals.
They are controlled by two signals namely, DEN and DT/R.
The DEN signal indicates the direction of data, i.e. from or to the
processor.
The opcode fetch and read cycles are similar. Hence the timing diagram can be
categorized in two parts, the first is the timing diagram for read cycle and the second is
the timing diagram for write cycle.
The read cycle timing diagram is shown in figure 1.6.
The read cycle begins inT1with the assertion of 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 T1 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 address device to enable its data 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 and is
shown in figure 1.7.
The M/IO signal is again asserted to indicate a memory or I/O operation. In T2,
after sending the address in T1, the processor sends the data to be written to the
addressed location.
The data remains on the bus until middle of T4state.TheWR becomes active at the
beginning of T2
The BHE and A0 signals are used to select the proper byte or bytes of
memory or I/O word to be read or write.
The M/IO, RD and WR signals indicate the type of data transfer.
S0, S1, S2 are set at the beginning of bus cycle. 8288 bus controller will output a
pulse as on the ALE and apply a required signal to its DT/ R pin during T1.
In T2, 8288 will set DEN=1 thus enabling transceivers, and for an input it will
activate MRDC or IORC. These signals are activated until T4.
For an output, the AMWC or AIOWC is activated from T2 to T4 and MWTC or
IOWC is activated from T3 to T4.
The status bit S0 to S2 remains active until T3 and become passive during T3
and T4.
If reader input is not activated before T3, wait state will be inserted between
T3 and T4.
Instruction formats:
Instruction Definition: A command to the Microprocessor to perform some task.
Need of instruction: A MP is a multi purpose programmable device. It can perform the
required operation by giving commands to it. Without giving any command the microprocessor
is not a useful device. It is a machine, so the user will give commands with the help of
understandable language (Assembly language).
The representation and size of an instruction is called instruction format.
This is an operation without any operand, which clear the carry flag bit.
10010 reg
b) Register to Register:
This format is 2 bytes long. The first byte of the code specifies the operation code and
the width of the operand specifies by w bit. The second byte of the opcode shows the register
operands and R/M field.
D7 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
The register represented by the REG field is one of the operands. The R/M field specifies
another register or memory location, ie., the other operand. The register specified by REG is a
source operand if D 0 , else it is a destination operand.
For example:
MOV : data transfer operation from Register to Register.
Op-code is 100010
100010 d w 1 1 R E G R/M
The MOD field shows the MOD of addressing. In case of no displacement MOD = 00
For example :
MOV: Data transfer Register/memory to/from register.
This format is similar to register to register transfer. The difference is in mod field.
For register to register, mod = 11
For register to/from memory with no displacement, mod = 00.
When mod = 0 0, the r/m fields indicates the address to memory location.
As for example r/m = 1 1 1 indicates (Bx)
The instruction
1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 indicates the instruction MOV AX, [BX]
In hexadecimal, the instruction is 8 AH O 7 H
Here the data is present in a memory location in DS whose offset address is in BX. The effective
address of the data is given as 10H DS BX
There d 1 indicates AX is a destination register so it moves the data from memory to register.
d) Register to/from Memory with Displacement:
This type of instruction format contains one or two additional bytes for displacement
along with 2-byte the format of the register to/from memory without displacement.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0 D7 D0 D7 D0
When w 0 , the size of immediate data is 8 bits and the size of instruction is 3 bytes.
When w 1 , the size of immediate data is 16 bits and the size of instruction is 4 bytes.
f) Immediate operand to memory with 16-bit displacement:
This type of instruction format requires 5 to 6 bytes for coding. The first two bytes contain the
information regarding OPCODE, MOD and R/M fields. The remaining 4 bytes contain 2 bytes
of displacement and 2 bytes of data.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0
D7 D0 D7 D0 D7 D0
Higher byte of Lower byte of data Higher byte of
data data
The different addressing modes of the 8086 instructions along with corresponding MOD, REG
and R/M field are given in the table.
operands Memory operands Register operands
No Displacement Displacement
Displacement 8 bits 16 bits
MOD 00 01 10 11
R/M w0 w 1
000 BX SI BX SI D8 BX SI D16 AL AX
001 BX DI BX DI D8 BX DI D16 CL CX
010 BP SI BP SI D8 BP SI D16 DL DX
011 BP DI BP DI D8 BP DI D16 BL BX
100 SI SI D8 SI D16 AH SP
101 DI DI D8 DI D16 CH BP
5. Indexed:
In this addressing mode offset of the operand is stored in one of the index register. DS
and ES are the default segments for index registers SI and DI respectively
e.g. MOV AX, SI
6. Register Relative:
In this addressing mode the data is available at an effective address formed by adding
an 8-bit or 16-bit displacement with the content of any one of the registers BX, BP, SI and DI
in the default either DS or ES segment.
e.g. MOV AX, 50H BX
7. Based Indexed:
In this addressing mode the effective address of the data is formed by adding the content
of a base register (any one of BX or BP) to the content of an index register (any one of SI or
DI). The default segment register may be ES or DS.
e.g MOV , BX SI
The 8086 instructions are categorized into eight different groups. Since various instructions are
available to program a microprocessor.
a) Data Transfer Instructions:
This type of instructions are used to transfer data from source operand to destination
operand. All the store, load, move, exchange, input and output operations belong to this category.
All the instructions performing arithmetic, logical, increment, decrement, compare and scan
instructions belong to this category.
c) Branch Instructions:
These instructions transfer control of execution to the specified address. All the call, jump,
interrupt and return instructions belong to this category.
d) Loop Instructions:
The LOOP, LOOPNZ and LOOPZ instructions belong to this category. These are useful to
implement different loop structures.
These instructions control the machine status. NOP, HLT, WAIT and LOCK instructions
belongs to this category.
All instructions which directly affect the flag register belong to this category. The instructions
CLD, STD, CLI, STI etc. belong to this category.
These instructions involve the bitwise shifting or rotation in either direction with or without
a count in CX.
h) String Instructions:
These instructions involve string manipulation operations like load, move, scan, compare,
store etc. These instructions are only to be operated upon the string data.
DB: Define Byte: The DB directive is used to reserve byte or bytes of memory
locations in the available memory.
DW: Define Word: The DW directive serves the same purposes as the DB directive,
but it makes the assembler reserves the number of memory words (16bit) instead of
bytes.
DQ: Define Quad word: This directive is used to direct the assembler to reserve 4
words (8 bytes) of memory for the specified variable and may initialize it with the
specified values.
DT: Define Ten Bytes: The DT directive directs the assembler to define the specified
variable requiring 10-bytes for its storage and initialize the 10-bytes with the specified
values.
ASSUME: Assume Logical Segment Name: The ASSUME directive is used to
inform the assembler, the names of the logical segments to be assumed for different
segments used in the program.
END: END of Program: The END directive marks the end of an assembly language
program.
ENDP: END of Procedure: The ENDP directive is used to indicate the end of a
procedure.
PROCEDURE STAR
:
.
STAR ENDP //indicates the end of procedure STAR
ENDS: END of Segment: This directive marks the end of a logical segment.
DATA SEGMENT
EQU: Equate: The directive EQU is used to assign a label with a value or symbol. The
use of this directive is just to reduce the recurrence of the numerical values or constants
in the program code.
EXTERN: External and PUBLIC: Public: The directive EXTERN informs the
assembler that the names, procedures and labels declared after this directive have
already been defined in some other assembly language module.
GROUP: Group the Related Segments: This directive is used to form logical groups
of segments with similar purpose or type.
LABEL: The LABEL directive is used to assign a name to the current content of the
location counter. A LABEL directive can be used to make a FAR jump. The label
directive can be used to refer to the data segment along with the data type, byte or word.
DATA SEGMENT
DATAS DB 50H DUP (?)
DATA-LAST LABEL BYTE FAR
DATA ENDS
After reserving 50H locations for DATAS, the next location will be assigned a label
DATA-LAST and its type will be byte and far.
LENGTH: Byte Length of A Label: This directive is used to refer to the length of a
data array or a string. Not available in MASM.
LOCAL: The label, variables, constants or procedures declared LOCAL in a module
are to be used only by that particular module.
NAME: Logical Name of A Module: The NAME directive is used to assign a name
to an assembly language program module.
OFFSET: Offset of A Label: When the assembler comes across the OFFSET operator
along with a label, it first computes the 16-bit displacement of the particular label, and
replaces the string ‘OFFSET LABEL’ by the computed displacement.
ORG: Origin: The ORG directive directs the assembler to start the memory allotment
for the particular segment, block or code from the declared address in the ORG
statement.
PROC: Procedure: The PROC directive marks the start of a named procedure in the
statement. Also the types FAR and NEAR specifies the type of the procedure.
PTR: Pointer: The POINTER operator is used to declare the type of a label, variable
or memory operand. The operator PTR is prefixed by either BYTE (8-bit quantity)
or WORD (16-bit quantity).
SEGMENT: Logical Segment: The SEGMENT directive marks the starting of a
logical segment. The started segment is also assigned a name, i.e. label, by this
statement.
SHORT: The SHORT operator indicates the assembler that only one byte is required
to code the displacement for a jump. This method of specifying jump address saves
memory.
TYPE: The TYPE operator directs the assembler to decide the data type of the
specified label and replaces the TYPE label by the decided data type.
FAR PTR: This directive indicates the assembler that the label following FARPTR is
not available within the same segment and the address of the bit is of 32 bits i.e. 2 bytes
offset followed by 2 bytes.
NEAR PTR: This directive indicates that the label following NEAR PTR is in the
same segment and need only 16 bit i.e. 2 byte offset to address it. A NEAR PTR label
is considered as default if a label is not preceded by NEAR PTR or FAR PTR.
MACRO and ENDM directives: They are used to define macros in assembly language
programs. MACROS are a set of instructions which are intended to do a particular task.
Where ever assembler finds the name of the macro in the main program, it replaces the
set of instructions present in the macro at that place.
Oscillator
All the 8051 family members use an external crystal for oscillator function. The
frequency of operation can be depending upon the individual device.
Data sheets of the device can be referred to see the operating frequencies supported
by a typical device. For example, 80C51 operates at 12 MHz frequency.
Actually, most of the time, the common frequency is 11.0592 MHz because many
devices actually run at frequencies below or up to 12 MHZ.
The slight lower frequency allows one of the timers to generate clock frequency for
the baud rate of 9600 baud for the operation of serial port. It must be noted that only
it is necessary to connect the quartz crystal externally and all the other oscillator
circuit is on-chip.
B-register 8-bit
Bank 3: 18 -1FH
Fig 1.3 Four register banks and their locations in the On-Chip RAM
B* B-register 0F0H
Registers marked with ‘*’ are bit addressable as well as byte addressable.
Program Status Word: Program status word or simply PSW, is an 8-bit register,
it consists of carry, auxiliary carry, overflow, and parity flags, also RS1 and RS0
for register bank selection. The PSW is a bit addressable register, each bit of the
PSW is referred as PSW.X. Thus PSW.0 is the LSB, which is a parity flab, and the
MSB PSW.7 is the carry flag. Figure 1.4 shows the program status word.
MSB LSB
C AC F0 RS1 RS0 OV ---- P
0 0 Bank 0 00-07 H
0 1 Bank 1 08-0F H
1 0 Bank 2 10-17 H
1 1 Bank 3 18-1F H
Bit 2 (Over flow flag): OV flag is used to detect errors in signed arithmetic
operations. when two signed numbers are added, if the result exceeds the
destination, overflow flag is set, else it is reset. OV is set, if there is a carry from
D6 to D7 but no carry D7 or if there is a carry from D7 but no carry from D6 to
D7.
Bit 0 (Parity flag): Parity flag indicates the number of 1’s in accumulator.
Parity flag is set, if the result contains an even number of 1 bit, else it is reset,
if the result contains an odd number of 1 bit.
Timer Register: Registers pairs (TH0, TL0), (TH1, TL1), (TH2, TL2) form 16-bit
timer/counter registers 0, 1,2 respectively. There are instructions for reading and
writing these registers byte-wise. Timer/counter 2 is only available in 8052. The
operation may be timing or counting. Further there are various modes in which
timers can be configured. For this purpose there are Timer control (TCON) and
timer mode registers (TMOD).
Control and status registers: All the special function registers (like IP, IE, TMOD,
TCON, PCON, SCON etc) that are used for controlling the internal resources or to
see the status of these registers. These registers contain the control and status bits
of the interrupt systems, timers, and serial port operations.
Serial data Buffer (SBUF): This register holds the data that has to be
transmitted through the serial port and holds the data that is received. This register
is interconnected to two 8-bit shift registers. When data is written into the SBUF, it
is loaded in the transmit shift register and hence the process of moving a data byte
into the SBUF starts the transmission process. During the reception, the data coming
to the 8051 is clocked into the receive shift register and once all the 8-bits of data
of a frame is received, it is transferred to the SBUF.
Capture Registers: Register pair (RCAP2H-RCAP2L) are the capture registers for
the timer 2. These are available only in 8052, for timer 2 capture mode operation.
In capture mode, a transition at the 8052 T2EX pin causes TH2 and TL2 to be
copied into RCAP2H and RCAP2L. Timer 2 is also has a 16-bit auto reload mode
and RCAP2H and RCAP2L hold the reload value for this mode.
RST/ VPD (Pin 9): The microcontroller provides a reset mechanism to establish initial
conditions. The special function registers and few CPU registers must be initialized
before the microcontroller can operate properly. If the reset pin is high for more than
24 oscillator cycles (2 Machine cycles), will reset the chip. VPD may be used to supply
power to the internal RAM during power failure or power down modes.
𝐄𝐀 /VPP (Pin 31): External access (EA) pin, when held high, executes instruction from
the internal program memory till address 0FFFH, beyond this address, the instructions
are fetched from external program memory. If this pin is low, all the instructions are
fetched from the external memory, during normal operation, this pin should not be
floated.
ALE (Pin 30): Address latch enable (ALE) output is used for latching the low address
byte during external memory access, ALE is activated periodically with a constant rate
of 1/6 the oscillator frequency. However, during the external data memory access, one
ALE pulse is skipped.
8051 CONNECTIONS
Before discussing the programming of microcontrollers, let us see the 8051 connection
diagram and the minimum hardware environment. This is shown in Fig. 1.6. The
oscillator connections, reset circuitry, power supply connections are basically needed
to design any 8051 based board, for any application in general.
Oscillator Circuit
8051 has on-chip oscillator. Only the frequency determining components (crystal)
are to be connected externally.
30 pF disc capacitors are recommended when a quartz crystal is used.8051 can work
up to 12 MHz.
For low-cost designs, one may go for 3-pin ceramic resonators. For ceramic
resonators, the capacitor values are typically 47 pF.
Recommendations by the manufacturer of ceramic resonators must be considered
for the values of these capacitors. Normally, a quartz crystal is preferred, because
resonators are not available for higher frequencies of the order of 12 MHz.
The reduction in cost due to the use of resonators may be negligible as compared
with the total cost of a microcontroller board. Further, resonators are not as stable
as the quartz crystals. MCS-51 also supports an external clock. This is shown in
Fig. 1.6.
Fig. 1.7 External Clock source for: (a) CHMOS MCS-51 Parts, (b) HMOS Parts
When external clock is used, pin XTAL2 is left floating in case of CHMOS MCS-
51 parts. There is one important difference between HMOS and CHMOS parts of
MCS-51, as far as the external clock source is concerned. For CHMOS parts
(80C51), the internal clocking circuitry is driven by the signal at XTAL 1. In case
of HMOS versions, the external clock source needs to be connected to XTAL 2 and
XTAL 1 is to be grounded.
The write to latch signal from the CPU. The Q output from the flip-flop can be read on
to the internal data bus in response to a Read Latch signal from the CPU. Read pin is a
different operation from reading a latch. The port pin status can be read onto the internal
data bus when CPU gives a ‘read pin’ command.
Ports 1, 2 and 3 : Ports 1, 2 and 3 are quasi-bi-directional ports and have fixed internal
pull-up resistors. Figure 1.8 shows a quasi-bi-directional port. Ports 1, 2 and 3 when
configured as input, they are pulled high and source current if externally pulled low.
For configuring a port pin as input, a ‘1’ must be written to a port latch. This turns 0FF
the FET and the pin is simply pulled high by the pull-up resistor, the external device
may pull it low.
The pin status can now be read onto the internal data bus. On reset, 8051 port latches
have 1’s written to them and configured as inputs. One can drive the pin as output at
any time; however, for input the FET must be 0FF. If the pin is to be used as output
writing a ‘0’ on to a pin requires that the FET should be ON. Similarly, to write a ‘1’
onto the pin, the FET should be 0FF, outputting a’1’ because of the pull-up resistor.
One important thing that must be observed here is that the 8051 ports can sink more
current than it can source. Port pins can sink around 0.5 mA but can source only tens
of µA.
Port 0: Port 0 does not have internal pull-up resistors. When configured as an input, it
floats and is therefore a true bi-directional port. Port 0 structure can be seen from Fig.
1.9. It has two output FETs. For normal operation, the upper pull-up FET is OFF,
providing ‘open drain’ output pin and external pull-up resistor is required. If a ‘1’ is
written to a port 0 latch, either FETs go OFF and the pin floats and can be used as high-
impedance input.
The pull-up FET only operates when there is an access to external memory. Port 0
output buffers can drive 8 LS TTL inputs. The output drivers of Port 0 (and also Port
2) can be switched to an internal ADDR (and ADDR/DATA) bus by using an internal
control signal while accessing the external memory. When used as ADDR and
ADDR/DATA bus, Port 0 and port 2 pins, respectively, cannot be used as general-
purpose I/Os.
Memory Organization
The MCS-51 has 64K external data memory, 64K program memory and 256 bytes
of internal data memory. The program memory map of 8051 is shown in Fig. 1.10.
The 64K program memory space of 8051 is divided into internal and external
memory.
If the EA pin is high, then 8051 executes from the internal program memory until
the address exceeds 0FFFH.
After that, locations 1000H through 0FFFFH are executed from the external
memory portion. If EA pin is held low, then 8051 executes instructions from
external memory only.
Table 1.6 shows this. The external 64 K data memory can be accessed using MOVX
instruction. Figure 1.11 shows the internal and external data memory of 8051.
The internal data memory of 8051 is 256 bytes, which is divided into two parts
again. The lower 128 bytes (00H through 7FH) called as internal data RAM and the
upper 128 bytes (80 through FFH) consists of special function registers (SFRs).
In case of 8032/52, the upper 128 bytes of internal data memory are also
addressable. Even though the SFRs and upper 128 bytes of RAM have the same
address space, they are different and accessed through different addressing modes.
The CPU can access data in various ways. The data could be in a register, or in a
memory, or be provided as an immediate value. These various ways of accessing data are called
addressing modes. The 8051 provides total of five distinct addressing modes.
(i) Immediate addressing mode
(ii) Register addressing mode
(iii) Direct addressing mode
(iv) Register Indirect addressing mode
(v) Indexed addressing mode
Data transfer instructions of 8051 are MOV, MOVX, MOVC, PUSH, POP and
exchange XCHG, XCH instructions. Data transfer instructions do not affect any of the PSW
flags. However, if there is a MOV or POP directly to PSW, it can affect the PSW. Signed and
unsigned addition and subtraction by using OV flag. BCD arithmetic is also possible on packed
BCD. There are unsigned multiplication and division operations directly supported by the
instructions. Table 6.8 lists the arithmetic instructions.
Logical Instructions
Bitwise logical AND, OR, EXCLUSIVE-OR operations are possible in MCS-51. These
instructions accept two 8-bit operands and the result is stored at the destination, no flags are
affected by ANL, OR and XOR instructions. There are single-operand instructions lick CLR,
SETB and CPL. Rotate instructions RR, RRC, RL, RLC, and swap instruction SWAP. It is
important to note that the CPL instruction complements the accumulator without affecting any
of the flags. Rotate instructions RL and RR do not affect any flag. However, RLC and RRC
instructions modify CY flag. RLC instruction moves bit-7 of the accumulator into CY position.
Similarly RRC instruction moves bit-0 of the accumulator into CY-flag. SWAP A instruction
simply interchange the lower and higher nibbles of the accumulator and no flags are affected.
Logical instructions are listed in Table 1.9
Boolean Variable Manipulation Instructions
Operands for Boolean variable manipulations are defined in these instructions. In case
of two operand instructions, there is a destination bit and source bit. For example, MOV
instruction has two single-bit operands. OR, AND operations with two single-bit operands are
possible. Single-operand instructions CLR, SETB, CPL modifies the bit location specified in
the instruction. Table 6.10 shows the Boolean Manipulation instructions.
There are instructions that test a specified bit and transfer control to the desired location.
For example, JC instruction transfers control to the location specified by the relative address,
if the Cary bit is set, 8-bit relative address is in 2’s complement form and it is possible to
transfer the control within a range from -128 to 127 bytes, relative to the first byte of the
following instructions. Similarly, it is possible to test any direct bit and transfer the control.
In case of JC, JNC, JB, JNB instructions, the bit under test is not modified and no flags
are affected. However, it is important to note that JBC instruction transfers the control to the
relative address specified, if the bit is set, and clears the bit specified in the instruction.
Both conditional branch and unconditional branch operations are possible in MCS-51.
There are jump, call and return instructions. Jump instructions are of three types: short jumps
(SJMP), long jumps (LJMP) and absolute jumps (AJMP). A short jump transfers control within
a 256 byte range, which is from -128 to +127 bytes relative to the first byte of the following
instruction. Absolute jump allows 11-bit address to be specified in the instruction. The
destination must be within 2K block of the program memory from the next instruction followed
by AJMP instruction. In case of LJMP instruction, a 16-bit address is specified in the
instruction and a jump to anywhere within the 64K block of program memory is possible.
Similar to absolute and long jumps, there are absolute calls (LCALL) instruction, is possible.
LCALL uses a 16-bit address in the instruction and subroutine anywhere within 64K-program
memory block can be called. There are conditional jumps JZ, CJNE, DJNZ, JZ rel instruction
tests the accumulator for zero and then transfers control to the address in the program memory
given by the relative address. CJNE instruction compares the first operand with the second
operand and then performs branch operation if the operands are not equal. DJNZ instruction
decrements the source operand and the result is stored in the source operand. A jump is made
to the relative address if the result is not zero.. DJNZ can be used to implement the software
delay routines. Apart from these, there are RET and RETI instructions. RETI and RET both
transfer control to the return address stored on the stack; the only difference is that in addition
to the return function, RETI instruction enables interrupts of the current priority level, All the
program branching instructions are listed in Table 1.11.
Arithmetic Operations
MUL AB Multiply A and B Result: A <‐ low byte, B <‐ high byte. 1 4
DIV AB Divide A by B Result: A <‐ whole part, B <‐ remainder. 1 4
DA A Decimal adjust ACC. 1 1
Logical Operations
Data Transfer
Other Instructions
NOP No operation. 1 1
8051 Interrupts
The 8051 architecture can handle interrupts from 5 sources. These are: the two external interrupt
lines, two timers and the serial interface. Each one of these is assigned an interrupt vector address.
This is quite similar to the RST interrupt vectors in the case of 8085.
External Interrupts
Port P3 of 8051 is a multi‐function port. Different lines of this port carry out functions which are
additional to data input‐output on the port.
Lines P3.2 and P3.3 can be used as interrupt inputs. Interrupts will be caused by a ‘LOW’ level,
or a negative edge on these lines. Half of the special function register TCON is used for setting
the conditions for causing interrupts from external sources. This register is bit addressable.
IT1 and IT0 are the “Interrupt Type” flags for external sources 1 and 0 respectively. These
decide whether a negative going edge or a ‘LOW’ level will cause an interrupt. If the bit is set,
the corresponding interrupt is edge sensitive. If it is cleared, the interrupt is level sensitive. IE1
and IE0 are the status flags for the two external interrupt lines. If the flag is 1, the selected type
of event (edge or level) has occurred on the corresponding interrupt line.
Internal Interrupts
Internally generated interrupts can be from either timer, or from the serial interface. The serial
interface causes interrupts due to a receive event (RI) or due to a transmit event (TI). The
receive event occurs when the input buffer of the serial line (SBUF in) is full and a byte needs
to be read from it. The transmit event indicates that a byte has been sent a new byte can be
written to output buffer of the serial line (SBUF out).
8051 timers always count up. When their count rolls over from the maximum count to 0000,
they set the corresponding timer flag TF1 or TF0 in TCON. Counters run only while their run
flag (TR1 or TR0) is set by the user program. When the run flag is cleared, the count stops
incrementing. The 8051 can be setup so that an interrupt occurs whenever TF1 or TF0 is set.
Enabling Interrupts
At power-up, all interrupts are disabled. Suppose Timer 0 is started. When it times out, TF0 in
the special function register TCON will be set. However, this will not cause an interrupt. To
enable interrupts, a number of steps need to be taken.
Interrupts are enabled in a manner which is quite similar to the 8085. There is an interrupt
enable special function register IE at byte address A8H. This register is bit addressable.
The most significant bit of the register is a global interrupt enable flag. This bit must be set in
order to enable any interrupt. Bits 6 and 5 are undefined for 8051. (Bit 5 is used by 8052 for
the third timer available in 8052). Bit 4, when set, enables interrupts from the serial port. Bit 3
should be set to enable interrupts from Timer 1 overflow. Bit 2 is set to enable interrupts from
external interrupt 1 (pin P3.3 on Port 3). Bit 1 enables interrupts from Timer 0 when it
overflows.
Bit 0, when set, will enable interrupts from external interrupt 0 (pin P3.2 on Port 3).
Interrupt Vectors
When an interrupt occurs, the updated PC is pushed on the stack and is loaded with the vector
address corresponding to the interrupt. The following table gives the vector addresses. The
order of entries in the table is also the order in which the 8051 will poll these in case of multiple
interrupts.
8051 starts executing from address 0000H at power-up or reset. The first 3 bytes are typically
used for placing a long jump instruction to start of the code area. The interrupt vectors start
from 0003 and are separated by 8 bytes from each other. Many simple interrupt handlers can
be accommodated in this space. Otherwise, jump instructions (to handler locations) need to be
placed at the vector addresses. This is quite similar to the RST handlers for 8085.
After this, whenever T0 overflows, TF0 will be set (in SFR TCON), the currently running
program will be interrupted, its PC value will be put on the stack (PC-L first, PC-H after –
because the stack grows upwards in 8051), and PC will be loaded with 000B H. The interrupt
handler for T0 should be placed here, and it should end with the instruction: RETI
Interrupt Priorities
8051 has two levels of interrupt priorities: high or low. By assigning priorities, we can control
the order in which multiple interrupts will be serviced. Priorities are set by bits in a special
function register called IP, which is at the byte address B8H. This register is also bit
addressable. The assembler defines special names for bits of this register.
Notice that the bits are in the polling order of interrupts. A 1 in a bit position assigns a high
priority to the corresponding source of interrupts – a 0 gives it a low priority. In case of multiple
interrupts, the following rules apply:
While a low priority interrupt handler is running, if a high priority interrupt arrives,
the handler will be interrupted and the high priority handler will run. When the high
priority handler does ‘RETI’, the low priority handler will resume. When this
handler does ‘RETI’, control is passed back to the main program.
If a high priority interrupt is running, it cannot be interrupted by any other source –
even if it is a high priority interrupt which is higher in polling order.
A low-priority interrupt handler will be invoked only if no other interrupt is already
executing. Again, the low priority interrupt cannot preempt another low priority
interrupt, even if the later one is higher in polling order.
If two interrupts occur at the same time, the interrupt with higher priority will
execute first. If both interrupts are of the same priority, the interrupt which is higher
in polling sequence will be executed first. This is the only context in which the
polling sequence matters.
Serial Interrupts
There are independent interrupt flags for reception and transmission of serial data,
called RI and TI.
RI indicates that a byte has been received and is available for reading in the input
buffer.
TI indicates that the previous byte has been sent serially and a new byte can be
written to the serial port.
A serial interrupt occurs if either of these flags is set. (Of course the serial interrupt
must be enabled for this to occur).
The interrupt service routine should check which of these events caused the
interrupt. This can be done by examining the flags. Either or both of the flag might
be set, requiring a read from or write to the serial buffer SBUF (or both).
Recall that the input and output buffers are distinct but are located at the same
address. A read from this address reads the input buffer while a write to the same
address writes to the output buffer.
The RI and TI flags are not automatically cleared when an interrupt is serviced.
Therefore, the interrupt service routine must clear them before returning.
The 8051 has two timer/counters T0 and T1, which may be configured and used
individually. The 8052 has an additional Timer T2.
All these counters count up on negative going edges at their inputs.
These can be used as event counters (where they count the number of negative
transitions on a pin connected to some external source), or as Timers, where they count
up once every twelfth clock cycle.
A special use of timers is for generating baud rates for the serial port.
8051 timers always count up. Each counter has a 16 bit count register in the SFR area.
The low and high bytes can be accessed as separate bytes. When their count rolls over
from the maximum count to 0000, they set the corresponding timer flag (TF1 or TF0)
in TCON.
The 8051 can be set up so that an interrupt occurs whenever TF1 or TF0 is set. When
8051 branches to the interrupt vector, it automatically clears the TF flag.
Timer Functions
When used as timers, the 8051 timers count up every 12th clock cycle. This is selected by
clearing the corresponding C/T flags in the TMOD special function register, placed at the
address 89H.
External Event Counting
Port P3 of 8051 is a multi-function port. Different lines of this port carry out functions which
are additional to data input-output on the port.
Lines P3.5 and P3.4 can be used as inputs to Timers T1 and T0 respectively. If the C/T flag of
a timer is set, the corresponding line is sampled once every machine cycle. The count is
advanced when a negative step is noticed on the line: this involves sampling a high level in one
cycle and a low one on the next. Since each machine cycle takes 12 clock cycles, the fastest
event counting rate is clock frequency/24.
Special Function Registers
The functioning of these timers is controlled through several special function registers.
The TR0 and TR1 flags in the TCON register (at address 88H) enable a timer to run, when set.
The C/T flags in the TMOD register (at address 89H) decide whether event counter
operation (flag set) or timer operation (flag cleared) will be used. (TMOD is not bit
addressable).
The Gate flags in TMOD decide whether counting will be gated by the corresponding
external interrupt pin in P3. If the Gate Flag is cleared, the counter is enabled by the TR
flag alone. If the Gate flag is set, counting also requires the corresponding external interrupt
pin in P3 to be HIGH. This is useful for measuring pulse widths.
Various control registers for timers are placed in the SFR area as shown below:
Timer modes
The timers may operate in one of four modes:
Mode 0 :In this mode, the timers act as 13 bit counters. This mode is largely meant for
providing compatibility with an older microcontroller from intel (8048). This mode is
practically never used in fresh designs. Except for the counter size, this mode is
identical to mode 1.
Mode 1: In this mode, the timers are 16 bits in size. This is a commonly used mode. It
is common to configure the timer to cause an interrupt when it overflows. The interrupt
routine then reloads the timer.
Mode 2 : This mode provides an 8 bit counter with auto-reload. It uses the high byte of
the count register to store the count value and the low byte as the actual counter. The
counter is automatically re-loaded from TH when it overflows. Thus, there is no
software overhead for re-loading the registers. This is convenient for generating baud
rates etc. The timing resolution is much lower in this mode (only 8 bits). Therefore
crystal frequencies have to be carefully chosen to generate accurate baud rates. Crystals
of 11.059 MHz are often used rather than 12 MHz for this reason.
Mode 3 : In this mode Timers T0 and T1 behave quite differently. T0 acts as two
independent 8 bit counters. Count register TL0 uses the resources (such as the RUN
flag, overflow flag) in TCON, TMOD etc. meant for T0. Similarly, TH0 uses the
resources meant for T1. Thus, TR1 will enable running the 8 bit counter made up of
TH0. TF1 will be set whenever TH0 overflows.
8051-SERIAL COMMUNICATION :
Serial communication uses only one or two data lines to transfer data and is generally used for
long distance communication. In serial communication the data is sent as one bit at a time in
a timed sequence on a single wire. Serial Communication takes place in two methods,
Asynchronous data Transfer and Synchronous data Transfer.
Asynchronous data transfer allows data to be transmitted without the sender having to send
a clock signal to the receiver. Instead, special bits will be added to each word in order to
synchronize the sending and receiving of the data. When a word is given to the UART for
Asynchronous transmissions, a bit called the "Start Bit" is added to the beginning of each word
that is to be transmitted. The Start Bit is used to alert the receiver that a word of data is about
to be sent, and to force the clock in the receiver into synchronization with the clock in the
transmitter.
Serial data transmission
After the Start Bit, the individual bits of the word of data are sent .Here each bit in the word is
transmitted for exactly the same amount of time as all of the other bits. When the entire data
word has been sent, the transmitter may add a Parity Bit that the transmitter generates. The
Parity bit may be used by the receiver to perform simple error checking. Then at least one Stop
Bit is sent by the transmitter. If the Stop Bit does not appear when it is supposed to, the UART
considers the entire word to be corrupted and will report a Framing Error.
In the Synchronous data transfer method the receiver knows when to “read” the next bit
coming from the sender. This is achieved by sharing a clock between sender and receiver. In
most forms of serial Synchronous communication, if there is no data available at a given time
to transmit, a fill character will be sent instead so that data is always being transmitted.
Synchronous communication is usually more efficient because only data bits are transmitted
between sender and receiver, however it will be more costly because extra wiring and control
circuits are required to share a clock signal between the sender and receiver.
Devices that use serial cables for their communication are split into two categories.
1. DTE (Data Terminal Equipment). Examples of DTE are computers, printers & terminals.
2. DCE (Data Communication Equipment). Example of DCE is modems.
Parallel communication uses multiple wires (bus) running parallel to each other, and can
transmit data on all the wires simultaneously. i.e all the bits of the byte are transmitted at a
time. So, speed of the parallel data transfer is extremely high compared to serial data transfer.
An 8-bit parallel data transfer is 8-times faster than serial data transfer. Hence with in the
computer all data transfer is mainly based on Parallel data transfer. But only limitation is due
to the high cost ,this method is limited to only short distance communications.
10 Serial communication work effectively Parallel buses are hard to run at high
even at high frequencies. frequencies.
The 8051 has two pins for transferring and receiving data by serial communication. These two
pins are part of the Port3(P3.0 &P3.1) .These pins are TTL compatible and hence they require
a line driver to make them RS232 compatible .Max232 chip is one such line driver in use.
Serial communication is controlled by an 8-bit register called SCON register,it is a bit
addressable register.
RB8 SCON.2 9th data bit received in modes 2 and 3.it is not
used in mode 0 & mode 1.If SM2 = 0 RB8 is
the stop bit .
M0 , SM1 : These two bits of SCON register determine the framing of data by
specifying the number of bits per character and start bit and stop bits. There are 4 serial
modes.
SM0 SM1
0 0 : Serial Mode 0
1 0 : Serial Mode 2
1 1 : Serial Mode 3
REN (Receive Enable) also referred as SCON.4. When it is high,it allows the 8051 to
receive data on the RxD pin. So to receive and transfer data REN must be set to 1.When
REN=0,the receiver is disabled. This is achieved as below
SETB SCON.4
TI (Transmit interrupt) is the D1 bit of SCON register. When 8051 finishes the transfer
of 8-bit character, it raises the TI flag to indicate that it is ready to transfer another byte.
The TI bit is raised at the beginning of the stop bit.
RI (Receive interrupt) is the D0 bit of the SCON register. When the 8051 receives data
serially ,via RxD, it gets rid of the start and stop bits and places the byte in the SBUF
register. Then it raises the RI flag bit to indicate that a byte has been received and should
be picked up before it is lost. RI is raised halfway through the stop bit.