Microprocessor LAB II
Microprocessor LAB II
Microcontrollers
Somnath Dey
A microprocessor incorporates most or all of the functions
of a computer's central processing unit (CPU) on a single
integrated circuit (IC, or microchip). The first
microprocessors emerged in the early 1970s and were
used for electronic calculators, using binary-coded
decimal (BCD) arithmetic on 4-bit words. Other
embedded uses of 4-bit and 8-bit microprocessors, such
as terminals, printers, various kinds of automation etc.
followed soon after. Affordable 8-bit microprocessors
with 16-bit addressing also led to the first general-
Somnath Dey purpose microcomputers from the mid-1970s on.
ETCE/D/0027 [2010-11]
C e n t r a l C a l c u tt a P o l y t e c h n i c
+919836448851
Page |1
The 8086 was the first 16-bit Microprocessor to be introduced by Intel Corporation. It is designed to be
upwardly compatible with the older 8080/8085 series of 8-bit microprocessors. The upward compatibility
allows programs written for the 8080/8085 to be easily converted to run on the 8086.
The word 16-bit means that its arithmetic logical unit, internal registers, and most of its instructions are
designed to work with 16-bit binary words. The 8086 has a 16-bit data bus, so it can read data form or
write data to memory and ports either 16-bits or 8- bits at a time. The 8086 has a 20-bit address bus, so it
can address any one of 220 or 1,048,576 memory locations. Each of the 1,048,576 memory addresses of
the 8086 represents a byte-wide location. Words will be stored in two consecutive memory locations. If the
first byte of a word is at an even address, the 8086 can read the entire word in one operation. If the first
byte of the word is at an odd address, the 8086 will read the first byte of the word in one operation, and
the second byte in another operation.
In single processor system, the 8086 operates in the minimum mode of operation. In a multiprocessor &
coprocessor configuration, the 8086 operates in the maximum mode of operation. The status of the pin
MN/MX (pin 33) decides the operating mode of 8086. When MN/MX =1, the 8086 operates in minimum
mode and when MN/MX=0, the 8086 operates in maximum mode. The 24 pin to 31 pin of 8086 have
alternate function.
Hardware Architecture:
The term architecture, as used in microprocessor circuits, describes the functional components that make
up the MPU and the interaction between them. These include the temporary storage devices known as
registers, which are used to hold data, instructions, and status information. There are also devices to
perform arithmetic and logical operations. Control devices are used to control the flow of information
through the MPU.
As shown by the block diagram in Fig.3, the 8086 MPU is divided into two independent functional parts
known as the Execution Unit (EU) and the Bus Interface Unit (BIU). The Bus Interface Unit consists of
segment registers, adder to generate 20 bit address and instruction prefetch queue.
Once this address is sent out of BIU, the instruction and data bytes are fetched from memory and they
fill a First in First out 6 byte queue.
It unit consists of scratch pad registers such as 16-bit AX, BX, CX and DX and pointers like SP
(Stack Pointer), BP (Base Pointer) and finally index registers such as source index and destination
index registers. The 16-bit scratch pad registers can be split into two 8-bit registers. For example, AX
can be split into AH and AL registers. The segment registers and their default offsets are given
below.
The Arithmetic and Logic Unit adjacent to these registers perform all the operations. The results of
these operations can affect the condition flags.
Registers:
Most of the registers contain data/instruction offsets within 64 KB memory segment. There are four
different 64 KB segments for instructions, stack, data and extra data. To specify where in 1 MB of
processor memory these 4 segments are located the 8086 microprocessor uses four segment
registers:
Code segment (CS) is a 16-bit register containing address of 64 KB segment with processor
instructions. The processor uses CS segment for all accesses to instructions referenced by instruction
pointer (IP) register. CS register cannot be changed directly. The CS register is automatically updated
during far jump, far call and far return instructions.
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.
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, and DX) and
index register (SI, DI) is located in the data segment. DS register can be changed directly using POP
and LDS instructions.
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 is possible to change default segments used by general and index registers by prefixing instructions
with a CS, SS, DS or ES prefix.
All general registers of the 8086 microprocessor can be used for arithmetic and logic operations. The
general registers are:
Accumulator register consists of 2 8-bit registers AL and AH, which can be combined together and used
as a 16-bit register AX. AL in this case contains the low-order byte of the word, and AH contains the
high-order byte. Accumulator can be used for I/O operations and string manipulation.
Base register consists of 2 8-bit registers BL and BH, which can be combined together and used as a
16-bit register BX. BL in this case contains the low-order byte of the word, and BH contains the high-
order byte. BX register usually contains a data pointer used for based, based indexed or register
indirect addressing.
Count register consists of 2 8-bit registers CL and CH, which can be combined together and used as a
16-bit register CX. When combined, CL register contains the low-order byte of the word, and CH
contains the high-order byte. Count register can be used as a counter in string manipulation and
shift/rotate instructions.
Data register consists of 2 8-bit registers DL and DH, which can be combined together and used as a
16-bit register DX. When combined, DL register contains the low-order byte of the word, and DH
contains the high-order byte. Data register can be used as a port number in I/O operations. In integer
32-bit multiply and divide instruction the DX register contains high-order word of the initial or resulting
number.
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 address 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.
Other registers:
Overflow Flag (OF) - set if the result is too large positive number, or is too small negative
number to fit into destination operand.
Direction Flag (DF) - if set then string manipulation instructions will auto-decrement index
registers. If cleared then the index registers will be auto-incremented.
Interrupt-enable Flag (IF) - setting this bit enables maskable interrupts.
Single-step Flag (TF) - if set then single-step interrupt will occur after the next instruction.
Sign Flag (SF) - set if the most significant bit of the result is set.
Zero Flag (ZF) - set if the result is zero.
Auxiliary carry Flag (AF) - set if there was a carry from or borrow to bits 0-3 in the AL register.
Parity Flag (PF) - set if parity (the number of "1" bits) in the low-order byte of the result is even.
Carry Flag (CF) - set if there was a carry from or borrow to the most significant bit during last
result calculation.
There are three internal buses, namely A bus, B bus and C bus, which interconnect the various blocks
inside 8086. The execution of instruction in 8086 is as follows:
The microprocessor unit (MPU) sends out a 20-bit physical address to the memory and fetches the first
instruction of a program from the memory. Subsequent addresses are sent
Memory
Program, data and stack memories occupy the same memory space. The total addressable memory
size is 1MB. As the most of the processor instructions use 16-bit pointers the processor can effectively
address only 64 KB of memory. To access memory outside of 64 KB the CPU uses special segment
registers to specify where the code, stack and data 64 KB segments are positioned within 1 MB of
memory (see the "Registers" section above).
Program memory - program can be located anywhere in memory. Jump and call instructions can be
used for short jumps within currently selected 64 KB code segment, as well as for far jumps anywhere
within 1 MB of memory. All conditional jump instructions can be used to jump within approximately +127
- -127 bytes from current instruction.
Data memory - the 8086 processor can access data in any one out of 4 available segments, which
limits the size of accessible memory to 256 KB (if all four segments point to different 64 KB blocks).
Accessing data from the Data, Code, Stack or Extra segments can be usually done by prefixing
instructions with the DS:, CS:, SS: or ES: (some registers and instructions by default may use the ES or
SS segments instead of DS segment).
Word data can be located at odd or even byte boundaries. The processor uses two memory accesses
to read 16-bit word located at odd byte boundaries. Reading word data from even byte boundaries
requires only one memory access.
Stack memory can be placed anywhere in memory. The stack can be located at odd memory
addresses, but it is not recommended for performance reasons (see "Data Memory" above).
Reserved locations:
0000h - 03FFh are reserved for interrupt vectors. Each interrupt vector is a 32-bit pointer in
format segment: offset.
FFFF0h - FFFFFh - after RESET the processor always starts program execution at the FFFF0h
address.
Interrupts
An interrupt is the method of accessing the MPU by a peripheral device. An interrupt is used to cause a
temporary halt in the execution of a program. The MPU responds to the interrupt with an interrupt
service routine, which is a short program or subroutine that instructs the MPU on how to handle the
interrupt.
When the 8086 is executing a program, it can get interrupted because of one of the following.
The action taken by the 8086 is similar for all the three cases, except for minor differences.
There are two basic types of interrupts, maskable and non-maskable. A non-maskable interrupt
requires an immediate response by the MPU. It is usually used for serious circumstances like power
failure. A maskable interrupt is an interrupt that the MPU can ignore depending upon some
predetermined condition defined by the status register. Interrupts are also prioritized to allow for the
case when more than one interrupts needs to be serviced at the same time.
In 8086 there are two interrupt pins. They are NMI and INTR. NMI stands for non maskable interrupt.
Whenever an external device activates this pin, the microprocessor will be interrupted. This signal
cannot be masked. NMI is a vectored
In 8086 there are two interrupt pins. They are NMI and INTR
INTR is a maskable hardware interrupt. The interrupt can be enabled/disabled using STI/CLI
instructions or using more complicated method of updating the FLAGS register with the help of
the POPF instruction. When an interrupt occurs, the processor stores FLAGS register into
stack, disables further interrupts, fetches from the bus one byte representing interrupt type, and
jumps to interrupt processing routine address of which is stored in location 4 * <interrupt type>.
Interrupt processing routine should return with the IRET instruction.
NMI is a non-maskable interrupt. Interrupt is processed in the same way as the INTR interrupt.
Interrupt type of the NMI is 2, i.e. the address of the NMI processing routine is stored in location
0008h. This interrupt has higher priority than the maskable interrupt.
INT <interrupt number> instruction - any one interrupt from available 256 interrupts.
Single-step interrupt - generated if the TF flag is set. This is a type 1 interrupt. When the CPU
processes this interrupt it clears TF flag before calling the interrupt processing routine.
Processor exceptions: divide error (type 0), unused opcode (type 6) and escape opcode (type
7).
I/O ports
65536 8-bit I/O ports. These ports can be also addressed as 32768 16-bit I/O ports.
Instruction Set
Instruction set of Intel 8086 processor consists of the following instructions:
Addressing modes:
Implied - the data value/data address is implicitly associated with the instruction.
Direct - the instruction operand specifies the memory address where data is located.
Register indirect - instruction specifies a register containing an address, where data is located. This
addressing mode works with SI, DI, BX and BP registers.
Based - 8-bit or 16-bit instruction operand is added to the contents of a base register (BX or BP), the
resulting value is a pointer to location where data resides.
Indexed - 8-bit or 16-bit instruction operand is added to the contents of an index register (SI or DI), the
resulting value is a pointer to location where data resides.
Based Indexed - the contents of a base register (BX or BP) is added to the contents of an index register
(SI or DI), the resulting value is a pointer to location where data resides.
Based Indexed with displacement - 8-bit or 16-bit instruction operand is added to the contents of a base
register (BX or BP) and index register (SI or DI), the resulting value is a pointer to location where data
resides.
There are a wide range of devices available in the 8051 family, differing in terms of memory type and
capacity, number of counter/timers, types of serial inter-face, number of input/output ports, clock rates,
frequency range, etc. However, there is a commonality among all devices in that they have been
developed from the ‘core’ 8051 device with modifications to produce the particular attributes of a different
family member. Each member of the 8051 microcontroller family has been designed with improved device
specifications in mind and to provide the customer with a device to suit particular user requirements.
The 80c51 can be considered as the core device, and functions such as I/O ports, timer/counters, serial
interfacing and interrupts will be discussed. Any variations that exist for a particular family member will be
dealt with in the relevant appendix that covers a particular device.
The 80c51 is available in three different package types and is basically a 40-pin device (some packages
have 44 pins but only 40 are internally connected) with the following architecture:
4KB 8 ROM;
128 8RAM;
full-duplex enhanced UART with framing error detection and automatic address recognition;
On-chip oscillator.
Block diagram:
The arrangement for the 80c51 device is shown in the block diagram of Fig.1. Variations exist according
to the family member, i.e. the on-chip program memory could be ROM or EPROM and the memory size
could vary (the 0C52 has 8KB ROM while the 87C52 has 8KB EPROM). Also the on-chip data memory
size could vary (both the 80C52 and 87C52 devices have 256 bytes of RAM).
The 80C51 architecture is shown in Figure 1. The basic architecture is the same for all members of the
8051 family although there are differences for devices, which may have more, or less, ports,
comparators, ADC circuits, etc. Block diagrams for other relevant devices can be seen in those
appendices that cover their specification. Reference has already been made in general terms to the
80C51 ports, timer/counters, internal RAM and ROM/EPROM (where applicable).
It is clear from the above that the 80C51 has a collection of 8-bit and 16-bit registers and 8-bit memory
locations.
Memory organization:
INTERNAL RAM:
The 80C51 has 128 bytes of on-chip RAM plus a number of SFRs. Including the SFR space gives 256
addressable locations but the addressing modes for internal RAM can accommodate 384 bytes by
splitting the memory space into three blocks viz. the lower 128, the upper 128 and the SFR space. The
lower 128 bytes use address locations 00H to 7FH and these can be accessed using direct and indirect
addressing. The upper 128 bytes use address locations 80H to FFH and may be accessed using direct
addressing only; locations in this space with addresses ending with 0H or 8H are also bit addressable.
Some members of the 80C51 family have 256 bytes of on-chip RAM and the upper 128 bytes in this
case would be accessible only using the indirect addressing mode.
For the 80C51 device, the internal RAM of 128 bytes is broken down into:
Four register banks 0 to 3, each of which contains eight registers R0 to R7. The 32 bytes
occupy addresses from 00H to 1FH. Each register can be addressed specifically when its bank
is selected or an address can identify a particular register regardless of the bank, i.e. R2 of
bank 2 can be specified if bank 2 is selected or the same location can be specified as address
12H. The register banks not selected can be used as general-purpose RAM. Bits 3 and 4 of the
PSW register determine which bank is selected when a program is running. Reset will cause
bank 0 to be selected.
Sixteen bytes that are bit addressable in the address range 20H to 2FH giving 128 addressable
bits. The bits have individual addresses ranging from 00H to 07H for byte address 20H, to 78H
to 7FH for byte address 2FH. Thus a bit may be addressed directly, say bit 78H, which is bit 7
of byte address 2F.
In addition there are SFRs (special function registers) in the address range 80H to FFH. This address
range actually gives 128 addresses but only 32 are defined for the 80C51; the number defined varies
according to device, being much larger for some devices and less for others. Details of the SFRs for the
devices referred to in the main body of the text can be found in Appendices D, E and F. For the 80C51
the SFRs of the internal RAM are described in more detail as follows:
Accumulator (ACC):
This 8-bit register, usually referred to as register A, is the major register for data operations such as
addition, subtraction, etc. and for Boolean bit manipulation. The register is also used for data transfers
between the device and external memory, where applicable. The accumulator is both bit and byte
addressable with the byte address at E0H and the bit addresses from E0H to E7H.
B Register:
This 8-bit register is used for multiplication and division operations. For other instructions it can be
considered another ‘scratch pad register. The B register is both bit and byte addressable with byte
address at F0H and bit addresses from F0H to F7H.
This 8-bit register at address D0H contains program status information as shown below:
This 8-bit register at address 81H is incremented before data is stored during PUSH and CALL
executions. The SP is initialized to RAM address 07H after a reset, which causes the stack to
commence at location 08H.
This 16-bit register is intended to contain the two bytes that make a 16-bit address, with the high byte
(DPH) at address 83H and the low byte (DPL) at address 82H. It may also be used as two independent
8-bit registers.
Ports 0 to 3:
P0, P1, P2 and P3 are the 8-bit SFR latches of ports 0, 1, 2 and 3 respectively. The addresses are
80H, 90H, A0H and B0H respectively. Writing a ‘1’ to any bit of any of the port SFRs causes the
corresponding port output pin to go high; writing a ‘0’ causes the corresponding port output pin to go
low. When used as an input, the external state of any port pin will be held in the port SFR.
This 8-bit register at address 99H is used for serial data in both transmit and receive modes. Moving
data to SBUF loads the data ready for transmission while moving data from SBUF allows access to
received data.
Timer registers:
The 80C51 contains three 16-bit timer/counters. Timer 0 has a low byte TL0 at address 8AH and a high
byte TH0 at address 8CH while timer 1 has a low byte at address 8BH and a high byte at address 8DH.
Timer 2 has a low byte at address CCH and a high byte at address CDH. Timer 2 can operate as an
event timer or event counter. An extra SFR register, the T2CON register, at address C8H, controls this
timer while a timer 2 mode control register T2MOD is at address C9H.
Control registers:
Certain control registers are required to provide control and status bits for the serial ports,
timer/counters and the interrupt system. The 8-bit control registers are:
IE at address A8H
IP at address B8H
This unit drives all the necessary timing & control signals required for the internal operation of the
circuit. It also drives control signals required for controlling the external system bus.
Oscillator:
It generates the basic timing clock signal for the operation of the circuit using crystal oscillator.
Instruction Register:
It decodes the opcode of an instruction to be executed & gives information to the timing & control unit to
generate necessary signals for the execution of the instruction.
These blocks provide an ON-chip EPROM & a mechanism to internally address it.
These blocks provide internal 128 bytes of RAM & a mechanism to address it internally.
ALU performs 8-bit arithmetic & logical operation over the operand held by the temporary registers
TMP1 & TMP2. Users cannot access these temporary registers.
The 80C51 microcontroller is available in a 40-pin dual-in-line (DIL) package; the arrangement is shown
in Fig.2. Other packages are available and although the device pin functions are the same regardless of
package configuration, pin-out numbers vary. The pin-out numbers referred to in the description that
follows are valid only for the DIL package.
The device operates from a single þ5V supply connected to pin 40 (Vcc) while pin 20 (VSS) is
grounded.
32 of the pins are arranged as four 8-bit I/O ports P0–P3. Twenty-four of these pins are dual purpose
(26 on the 80C52/80C58) with each capable of operating as a control line or part of the data/address
bus in addition to the I/O functions.
Port 0: This is a dual-purpose port occupying pins 32 to 39 of the device. The port is an open-
drain bidirectional I/O port with Schmitt trigger inputs. Pins that have 1s written to them float
and can be used as high-impedance inputs. The port may be used with external memory to
provide a multiplexed address and data bus. In this application internal pull-ups are used when
emitting 1s. The port also outputs the code bytes during EPROM programming. External pull-
ups are necessary during program verification.
Port 1: This is a dedicated I/O port occupying pins 1 to 8 of the device. The pins are connected
via internal pull-ups and Schmitt trigger input. Pins that have 1s written to them are pulled high
by the internal pull-ups and can be used as inputs; as inputs, pins that are externally pulled low
will source current via the internal pull-ups. The port also receives the low-order address byte
during program memory verification. Pins P1.0 and P1.1 could also function as external inputs
for the third timer/counter i.e.:
Port 2: This is a dual-purpose port occupying pins 21 to 28 of the device. The specification is
similar to that of port 1. The port may be used to provide the high-order byte of the address bus
for external program memory or external data memory that uses 16-bit addresses. When
accessing external data memory that uses 8-bit addresses, the port emits the contents of the
P2 register. Some port 2 pins receive the high-order address bits during EPROM programming
and verification.
Port 3: This is a dual-purpose port occupying pins 10 to 17 of the device. The specification is
similar to that of port 1. These pins, in addition to the I/O role, serve the special features of the
80C51 family; the alternate functions are summarized below:
The 80C51 is reset by holding this input high for a minimum of two machine cycles before returning it
low for normal running. An internal resistance connects to pin 20 (V SS) allowing a power-on reset using
an external capacitor connected to pin 40 (V CC). The device internal registers are loaded with selected
values prior to normal operation.
The 80C51 on-chip oscillator is driven, usually, from an external crystal. The XTAL1 input also provides
an input to the internal clock generator circuits. PSEN (program store enable) (pin 29). This pin
provides an output read strobe to external program memory. The output is active low during the fetch
stage of an instruction. The signal is not activated during a fetch from internal memory.
The ALE signal is an output pulse used to latch the low byte of an address during access to external
memory. The signal rate is 1/6 the oscillator frequency and can be used as a general-purpose
clock/timing pulse for the external circuitry. The pin also provides the program pulse input (PROG)
during EPROM programming. ALE can be disabled by setting SFR auxiliary.0. With this bit set ALE will
be active only during a MOV X instruction
This pin is either tied high or low according to circuit requirements. If tied high the device will execute
programs from internal memory provided the address is not higher than the last address in the internal
ROM/OTP. When the EA pin is tied low, thus disabling the internal ROM, program code is accessed
from external ROM. For a ROMless device the EA pin must be tied low permanently and the program
code accessed from external ROM could be as much as 64KB. EPROM versions of the device also use
this pin for the supply voltage (V PP) necessary for programming the internal EPROM. If security bit 1 is
programmed, EA will be internally latched on reset.
Instruction Set
Data transfer Instruction.
Move byte between accumulator (an SFR) and resistor at a resistor bank.
Move byte from an SFR/Internal RAM to another direct.
Move indirect.
Move immediate, mode immediate DPTR.
MOVC and MOVX indirect.
Exchange or Push or Pop direct.
Bit Manipulation:
o Set, Complement, AND or OR or Move the bit.
Logic Instructions:
o AND, XOR, OR operation instructions
Byte Manipulation:
o Clear, Complement, or swap and rotate instructions.
Arithmetic Instructions:
o 8-bit ADD, Subtract, Multiply and Divide instructions.
o Increment –Decrement Instruction.
#P3: To find Factorial of a given data stored in the #P4: To find the largest no. in a series of 8-bit
location 0300H. data.
#P5: To find the largest no. in a series of 8-bit #P6: To find the smallest no. in a series of 8-bit
data. data.
MOV R5, A;
END;
#P3: Program to add 03 by 10 times and to store #P4: Program to move a block of data (5 nos.) from the
the result in the resistor R5. location of starting address 0300H to another location of
starting address 0050H.
INC R0;
INC R1;
END;
#P5: Program to find the smallest data in a series #06. Program to find the largest data in a series (5
(5nos.) starting from the location 0040H. The result is nos.) starting from the location 0040H. The result is in
in ACC. ACC.
END; END;