8051 Microcontroller
8051 Microcontroller
Lecture Notes on
Microprocessor and Microcontroller
(UNIT-IV)
Branch: CSIOT Sem-3RD
Microcontroller is a single chip microcomputer which consists of CPU, Memory, I/O ports,
timers and other peripherals. The difference between microprocessor and microcontroller is
microprocessor is a single integrated CPU whereas microcontroller is single chip microcomputer.
The world leaders of manufacturing of microprocessor and microcontroller are Intel, Motorola,
IBM, Cyrix etc. Here we have to focus on microcontroller 8051.
Features of 8051
Feature Quantity
ROM 4K bytes
RAM 128bytes
Timer 2
I/O pins 32
Serial Port 1
Interrupt sources 6
1
SUBJECT- MP&MC
2
SUBJECT- MP&MC
i.e. addresses A8-A15 will appear on this port. Even though memory with capacity of 64Kb is
not used, which means that not all eight port bits are used for its addressing, the rest of them
are not available as inputs/outputs.
Pin 29: PSEN’- Program Store Enable. If external ROM is used for storing program, then a
logic zero(0) appears on it every time the microcontroller reads a byte from memory.
Pin 30: ALE – Address latch enable
1 – Address on AD 0 to AD 7
0 – Data on AD 0 to AD 7
Pin 31: EA’ – it indicates the presence of external memory
Pin 32-39: Port 0 Similar to P2.
Pin 40: VCC → +5V power supply.
2. Architecture of 8051
Fig 4.1 shows a simplified architecture for the internal Hardware. Fig 4.2 shows an overview of
the internal hardware architecture of the 8051/8031 microcontrollers.
The CPU has the controlled and sequencing logic circuits with signals as in a microprocessor.
3
SUBJECT- MP&MC
The MCU has, besides the CPU, ROM, Interrupt control circuit, internal timing devices (timers
T0, T1), serial interface (SI), RAM and special function registers (SFRs). It has four ports P0,
P1, P2 and P3 as shown in Fig. 4.1. The overview block diagram of 8051 is depicted in Fig.4.2.
4
SUBJECT- MP&MC
Description of Sub units in the hardware architecture and meaning of the symbols
5
SUBJECT- MP&MC
6
SUBJECT- MP&MC
For read and write the 128 B memory is indirectly and directly addressable in address space.
Register banks- Four set of registers
Four register banks each of 8 registers and these are also part of the internal RAM.
XTAL1 and XTAL2 – Pins to the Crystal
Pins to the crystal in the oscillator circuit, usually 12 MHz
EA - External Enable
To enable use of external memory addresses to external ROM.
RST- Reset Pin
Reset circuit input and also reset few output cycles to the external peripheral devices to let
processor reset and synchronize with devices.
INT 0 and INT 1- Interrupt pins
Active low two external interrupts.
VCC and GND- Voltage supply pi and ground pin
For 5 V supply and ground connections respectively.
PSEN - Program Store Enable
Active low when reading the external program memory bytes
RD -Read
Active low when reading the byte from external data memory.
WR - Write
Active low when writing the byte to external data memory
3. Pin Configuration
Fig 4.3 shows 40 pin signals in an 8051 series microcontroller. It shows the I/O pins, P0.0 to
P0.7, P1.0 to P1.7, P2.0 to P2.7 and P3.0 to P3.7. It shows other remaining 8 pins, VDD, VSS,
XTAL1 and XTAL2, RST, ALE, EA and PSEN .
Vcc - Pin 40 provides supply voltage to the chip. The voltage source is +5V
XTAL1 and XTAL2- 8051 has an on-chip oscillator but requires an external clock to run it.
Most upon a quartz crystal oscillator is connected to inputs XTAL1 (pin 19 and XTAL@ (pin-
18) The quartz crystal oscillator connected also needs two capacitors of 30 pF. If frequency
source other than crystal oscillator such as TTL oscillator will be connected to XTAL1 and
XTAL2 is left unconnected.
7
SUBJECT- MP&MC
RST (I/P)- Pin 9 is the RESET pin and is active high (normally low). Upon applying high pulse
to this pin the microcontroller will reset and terminate all activities. This often referred to as
power on reset. Once it is activated the contents of all registers become zero except the content
of SP which is 07H.
EA (External Access) - This pin is connected to VCC for those have on-chip ROM otherwise
it is grounded incase 8031 and 8032. Because in case of 8031 and 8032 there is no on-chip
ROM.
PSEN (o/p) (Program Store Enable)- In case of 8031 based system in which an external ROM
holds the program code . To read the code this pin is connected to OE pin of ROM chip.
AlE (o/p) (address Latch enable)- When 8051is connected to external memory, both address
and data are transferred through port 0 pins. ALE signal is active high used to demultiplex
address/data bus.
8
SUBJECT- MP&MC
4. Memory Organization
The 8051 micro controller has a total of 128 bytes of RAM. The 128 bytes of RAM inside the
8051 are assigned addresses 00H to 7FH and divided into three different groups as follows.
1. A total of 32 bytes from location s 00H to 1FH are set aside for register banks and the
stacks.
2. A total of 16 bytes from locations 20H to 2FH are set aside for bit addressable read/write
memory.
3. A total of 80 bytes from locations 30H to 7FH are used for read and write storage, or what is
normally called a scratch pad. These 80 locations of RAM are widely used for the purpose of
storing data and parameters by 8051 programmers.
As mentioned, a total of 32 bytes of RAM are set aside for the register banks. These 32
bytes are divided into 4 banks of registers in which each bank has 8 registers, R0-R7. RAM
locations from 0 to 7 are set aside for bank 0 of R0-R7 where R0 is RAM location 0 , R2 is
location 2 and so on. The second bank of registers R0-R7 start RAM location 08 and goes to
location 1FH. The third bank of R0-R7 starts at memory location 10 H and goes to location
17H. finally RAM location 18H to 1FH are set aside for the fourth bank of R0-R7. The
following shows how 32 bytes are allocated into 4 banks.
9
SUBJECT- MP&MC
Fig.4.8 shows a layout of the external code memory addresses in the classic 8051 architecture.
1. When the the EA =0 at RESET, the PC (MCU program counter ) starts from 0x0000
and accesses the external addresses from the memory. Memory addresses are between
0x0000 and 0xFFFF.
2. When the EA =1 at RESET, the PC starts from 0x0000 for banks0 and 1 and accesses
the internal addresses and the 0x1000 onwards from the external addresses from the
memory.
Fig. 4.9 shows a layout of the external data (X-DATA) memory addresses in the classic 8051
architecture. It can be accessed through the indirect addressing mode used.
10
SUBJECT- MP&MC
For a programmer, the SFRs are at the directly addressable space special registers. These can
be accessed by their names or by their addresses. The SFRs have addresses between 80H and
FFH. These addresses are above 80H, since the addresses 00 to 7FH are addresses of RAM
memory inside the 8051.Not all the address space of 80 to FF is used by the SFR. The unused
locations 80H to FFH are reserved and must not be used by the 8051 programmer. The meaning
of each symbol is enlisted in Table 4.1.
11
SUBJECT- MP&MC
6. Port Operation
The four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the ports
upon RESET are configured as output, ready to be used as output ports. To use any of these
ports as an input port , it must be programmed. The port structure is depicted in Fig. 4.10
Port 0
It can be used for input or output. It occupies total of 8 pins (pins 32-39). To use the pins of
port 0 as both input and out ports, each pin must be connected externally to a 10 K ohm pull-up
resistor. P0 is an open drain unlike P1, P2 and P3. With external pull-up resistors connected upon
reset, port0 is configured as an output port.
12
SUBJECT- MP&MC
With resistors connected to port 0 , in order to make it as input the port must be programmed by
writing 1 to all the bits. In the following code.
MOV A, #0FFH
MOV P0, A
BACK: MOVA, P0
MOV P1, A
SJMP BACK.
Port 1
Port 1 occupies a total of 8 pins (pins 1 through 8) . It can be used as input or output. In contrast
to Port 0 , this port does not need any pull-up resistors since it already has pull-up resistors
internally. Upon reset port 1 is configured as an output port. To make Port 1 an input port it must
be programmed as such by writing 1 to all its bits.
Port 2
Port 2 occupies a total of 8 pins ( pins 21 through 28). It can be used as input or output. Just like
P1, port 2 does not need any pull-up resistors since it already has pull-up resistors internally.
Upon reset, port 2 is configured as an output port. To make port 2 as input, it must programmed
as such by writing 1 to all its bits. The dual role of port 2 is also accomplished by providing
higher byte address through A8-A15 to access the external memory.
13
SUBJECT- MP&MC
Port 3
Port 3 occupies a total of 8 pins, pin 10 through 17. It can be used as input or output. P3 does not
need any pull-up resistors , the same as P1 and P2. Although Port 3 is configured as an output
port upon reset, Port 3 has additional function of providing some extremely important signals
such as interrupts. Table depicts the alternate functions of port 2
P3.0 and P3.1 are used for the RxD and TxD serial communication signals. P3.2 and P3.3 are
used for external interrupts. Bits P3.4 and P3.5 are used for timers 0 and 1. Bits P3.6 and P3.7
are used to provide WR and RD signals for external memories in 8051 based system.
For interfacing to external ROM some pins have important role that to be discussed here.
EA -When this pin is connected to Vcc, that indicates the program code is stored in the
microcontroller on-chip ROM. For external ROM access this pin is grounded.
P0 and P2 role in providing addresses- In 8051 P0 and P2 provides the 16-bit address to access
external memory. Of these ports P0 provides the lower 8 bit addresses A0-A7, and P2 provides
the upper 8 bit addresses A8-A15. More importantly, P0 is also used to provide 8 bit- data bus
D0-D7. In other words P0.0- P0.7 are used for both address and Data is called as address/data
multiplexing. The sharing of this bus is accomplished by ALE (address latch enable.) Pin.
When ALE=0, the 8051 uses P0 for the data path and when ALE=1, it is used for address path.
PSEN (program store enable)- It is an output signal must be connected to OE pin of a ROM
containing the program code. When EA pin is connected to ground the 8051 fetches opcode
from external ROM by using PSEN .
14
SUBJECT- MP&MC
7. Programmer’s Model
The CPU registers are used to store the data temporarily. The information may be data to
be processed or address pointing the data to be fetched. The majority of registers are 8 bits. The
8-bit registers are shown in the diagram from MSB (most significant bit) D7 to the LSB (least
significant bit) D0. The most widely used registers of 8051 are A (accumulator), B, R0, R1, R2,
R3, R4, R5, R6, R7, DPTR (data pointer), and PC (program counter). All these registers are 8
bits except DPTR and the program counter. The accumulator is used to hold one operand before
execution and hold the result after execution. The program counter points to the address of next
instruction to be fetched. It is a auto increment register. As the size of program counter is 16 bit.
8051 can access the program addresses from 0000H-FFFFH. When 8051 is powered-up the
program counter contents will be 0000H. This means that it expects the first opcode to be stored
at ROM address 0000H. For this reason in the 8051 system, the first opcode must be burned
memory location 0000H of program ROM since this is where it looks for the first instruction
when it is booted.
The program status word register (PSW) is an 8-bit register. It is also referred as Flag
register. Although this register is size of 8-bits, only 6bits are used by 8051. Two unused bits are
user definable flags. Other 4 bits are called as conditional flags such as CY (carry), AC
(auxiliary carry), P(parity) and OV(overflow).In this register the bits PSW.3 and PSW.4 are
designated as RS0 and RS1 and used to select the banks. PSW.5 and PSW.1 bits are general
purpose status flags and can be used by the programmer for any purpose.
15
SUBJECT- MP&MC
8. Operand addressing
1. Register
2. Immediate
3. Direct (memory related)
4. Register Indirect (memory related)
5. Index register addressing
This addressing mode involves the use of registers to hold the data to be manipulated.
Examples:
Examples:
As we know the on-chip RAM of 8051 is 128 byte, it can be accessed through memory address
from 00H to FF H. The allocations of 128 bytes are as follows.
Although the entire 128 bytes of RAM can be accessed through direct addressing mode, it is
most often used to access RAM location 30H-7FH. This is due to fact that register banks are
accessed through their names.
16
SUBJECT- MP&MC
Examples:
MOV R4, 70H ; move the contents of RAM location 70H to R4.
In this mode the address (of 8bits) is indirectly specified in the instruction by the contents
of pointer. This addressing mode so called because the source operand is from the address
specified indirectly by another register in the instruction. The limitation is that only R0 and R1
register can be used in 8051 for indirect addressing. SFRs are directly accessible.
Examples
Suppose we need to access external data RAM and external code space of on-chip ROM
16 bit address must be required. In this case we have to use DPTR. This mode is widely used in
accessing data elements of look-up table entries in the program ROM space of 8051.
Examples;
9. Instruction set
The instruction set of 8051 can be classified into following group.
17
SUBJECT- MP&MC
Three types of the data transfer can be done by move instruction. First type is transfer
within the internal RAM and SFRs, second type is transfer using code memory area (CODE) and
the third is using the external data memory X-DATA).
MOV instruction
A MOV instruction means move (copy) the bits from one source to a destination.
Table 4.4 MOV instructions within the registers, internal RAM and SFRs in 8051
Instruction Action Addressing Length cycles
(Mnemonic) in bytes
MOV A, Rn Move Rn into A Register 1 1
MOV Rn, A Move into Rn from A Register 1 1
MOV A, #data Move immediate 8-bit data into A Immediate 2 1
MOV Rn, #data Move into Rn the data. immediate 2 1
MOV A, direct Move byte at the direct address into A Direct 2 1
MOV Rn, direct Move from direct address into Rn Direct 2 2
MOV direct, A Move byte to the direct address form A Direct 2 1
MOV direct, Rn Move a byte to the direct address from Rn Direct 2 2
M OV direct, direct Move byte to the direct address from the Direct 3 2
direct address
MOV direct, #data Move immediate data byte to the direct Immediate 3 2
address
MOV a,@Ri Move into A the byte from the address Indirect 2 2
pointed by Ri
MOV @Ri, A Move A into address pointed by Ri Indirect 1 1
MOV direct, @Ri Move into direct address from address indirect 1 1
pointed by Ri
MOV @Ri, direct Move from the direct address to the address Indirect 2 2
poined by ri
MOV @Ri, #data Move data ino address pointed by Ri immediate 2 2
MOV DPTR, data16 Mov e16 bit dat immediate 3 2
MOVC-type Instruction
It moves the 8-bit code from one source at the program memory (internal and external) to the
register A destination.
Table 4.5 MOVC Instructions for transfer from the program memory area address code or
constant to accumulator in 8051
Instruction Action Addressing Length Cycles
in bytes
MOVC A, @A+DPTR Moves the code or constant into A the byte Indirect 1 2
from the program memory address pointed
18
SUBJECT- MP&MC
MOX-type Instructions
A MOVX instruction means move (copy) the 8-bit data into A and from A using the external
data memory address using DPTR or Ri as the pointer
Table 4.7 PUSH and POP instructions for using the Stack Area employing SP
Instruction Action Addressing Length in Cycles
bytes
PUSH direct Move byte from a direct Direct 2 2
internal RAM or SFR into the
stack after first incrementing
the stack pointer by 1
POP direct Move byte to a direct internal Direct 2 2
RAM or SFR into the stack
and then decrement the stack
pointer by 1.
19
SUBJECT- MP&MC
XCH-type instructions
An XCH instruction is for exchanging the A register with a source using the register (direct or
indirect addresing0 mode.
These instructions include 8 bit addition, subtraction, increment, decrement, multiply and
division instruction.
Table 4.9 Arithmetic ADD, SUB,MUL, DIV, INC and DEC instruction s in 8051
Instruction Action Addressing Flags Length Cycles
affected (bytes)
ADD A,Rn Add Rn into A Register C,AC,OV 1 1
ADD A, direct Add the byte at the direct address Direct C,AC,OV 2 1
into A
ADD A, @Ri Add the byte from the address Indirect C,AC,OV 1 1
pointed by the Ri into A
ADD A, #data Add immediate data byte to the A Immediate C,AC,OV 2 1
ADDC A, Rn Add CF(carry) bit and Rn into A Register C,AC,OV 1 1
ADDC A, direct Add CF bit and byte at the direct Direct C,AC,OV 2 1
address ito A
ADDC A @Ri Add CF bit and the byte from the Indirect C,AC,OV 1 1
address pointed by the Ri
ADDC A, #data Add CF bit and immediate data Immediate C,AC,OV 2 1
byte to the A
SBBB A,Rn Subtract borrow at CF bit and Rn Rgister C,AC,OV 1 1
into A
SBBB A, direct Subtract borrow at CF bit and byte Direct C,AC,OV 2 1
at the direct address into A
20
SUBJECT- MP&MC
by the Ri into A
SBBB A, #data Subtract borrow at CF bit and Immediate C,AC,OV 2 1
immediate data byte into A
INC A Increment Register None 1 1
INC Rn Increment Rn Register None 1 1
INC direct Increment byte at the direct address Direct None 2 1
INC @Ri Increment the byte at the address Indirect None 1 1
pointed by Ri
DEC A Decrement A Register None 1 1
DEC Rn Decrement Rn Register None 1 1
DEC direct Decrement byte at the direct Direct None 2 1
address
DEC @Ri Decrement the byte at the address Indirect None 1 1
pointed by the Ri
MUL AB Multiply A and B Result MSB in B Register OV 1 4
and LSB in A
DIV AB Divide A (Numerator) and B( Register OV 1 4
denominator) Remainder in B
Quotient in A
DAA Decimal adjust accumulator Register C 1 1
Table gives features of 8-bit AND, OR and XOR instruction. These instructions have 4
addressing modes such as register, immediate, direct and indirect.
21
SUBJECT- MP&MC
direct address
XRL A, Rn XOR Rn into A Register 1 1
XRL A, direct XOR byte at the direct address Direct 2 1
into A
XRL A, @Ri XOR the byte at the address Indirect 1 1
pointed by Ri into A
XRL A, #data XOR immediate data byte to the A immediate 2 1
XRL direct, A XOR A into byte at the direct Direct 2 1
address
XRL direct, #data XOR immediate byte into byte at Direct 3 2
the direct address
Table 4.11 MOV, CLR, CPL,SETB,ANL, and ORL Boolean Processing Instruction
Instruction Action Addressing Length Cycles
(bytes)
MOV C, bit Move bit into CF Direct bit addressing 2 1
MOV bit, C Move CF into the bit Direct bit addressing 2 2
CLR C Clear CF PSW Register CF bit 1 1
addressing
CLR bit Clear bit Direct bit addressing 2 1
CPL C Complement CF PSW Register CF bit 1 1
addressing
CPL bit Complement bit Direct bit addressing 2 1
SETB C Set CF=1 PSW Register CF bit 1 1
addressing
SETB bit Set bit =1 Direct bit addressing 2 1
ANL C,bit AND between CF and bit, place the Direct bit addressing 2 2
result in CF
ANL C, bit AND between CF and , place the Direct bit addressing 2 2
result in C
ORL C,bit OR between CF and bit, place the Direct bit addressing 2 2
result in C
ORL C, bit OR between CF and bit , place the Direct bit addressing 2 2
result in C
In the main program other sub programs may be called to perform a particular task. When a sub
program is called the processor will jump to a new address where this program is available and
22
SUBJECT- MP&MC
it has to accomplish program flow control transfer with help of JUMP and CALL instruction
when some condition met.
8051 has three jump instructions: Long- it jumps to 16-bit address, Absolute- it jumps within 2 K
bytes and Short- it jumps to address within 128 bytes above or below the present address.
23
SUBJECT- MP&MC
Table 4.15 Instruction for decrement and then jump in program-loops in 8051
Instruction Action Addressing Length Cycles
in bytes
DJNZ Rn, Rel Decrement Rn and jump if Rn is Relative (offset) 2 2
still not zero.
DJNZ direct, Rel Decrement byte at the direct and Relative (offset) 2 2
jump if byte is still not zero
Call to a Routine
24
SUBJECT- MP&MC
10. Programming
While the CPU can work only in binary, it can do so at a very high speed, however, it is quite
tedious and slow for humans to deal with 0s and 1s in order to program the computer. A program
that consists of 0s and 1s is called machine language. In the early days of the computer
programmers coded programs in machine language. Although the hexadecimal system was used
as a more efficient way to represent binary numbers, the process of working in machine code
was still cumbersome for humans. Eventually, assembly language were developed which
provided mnemonics for the machine code instructions. Plus other features which made
programming faster and less prone to error. Assembly language is referred to as low level
language because it deals directly with internal structure of CPU. Programmer needs assembler
to convert the assembly language to machine language for execution purpose. Assembly
language consists mnemonics optionally followed by one or two operands.
Programs
P1. Write an ALP (Assembly Language Program) to find the sum of values and store the result
in A ( lower byte and in R7 (higher byte). Assume that RAM locations 40-44 have the following
values.
25
SUBJECT- MP&MC
Solution:
CLR A ; A=0
P2. Assume that 5 BCD data items are stored in RAM locations starting a 40H as shown below.
Write an ALP to find the sum of all numbers. The result must be in BCD.
Solution:
CLR A ; A=0
DA A
26
SUBJECT- MP&MC
P3. Write an ALP to get hex data in the range of 00-FFH from port 1 and convert it to decimal.
Save the digits in R7, R6 and R5, where the least significant digit in R7.
MOV A, #0FFH
MOV B , #0AH ;
DIV AB ;
P4. Read and test P1 to see whether it has the value 45H. if it does send 99H to P2; otherwise, it
stays cleared.
Solution:
MOV A, P1 ; read P1
XRL A, R3 ;
27
SUBJECT- MP&MC
JNZ EXIT
EXIT: ……
Solution:
P6. Write an ALP to determine if register A contains the value 99H, if so, make R1=FFH
otherwise make R1=0.
Solution:
NEXT …..
P7. Assume that P1 is an input port connected to a temperature sensor. Write an ALP to read the
temperature and test it for the value 75. According to the rest result, place the temperature value
into the registers indicated by the following.
28
SUBJECT- MP&MC
Solution:
SJMP EXIT
EXIT ……
P8. Write an ALP that finds the number of 1s in a given byte 97H.
Solution:
MOV A, 97H
P9. Assume that register a has packed BCD 29H, write an ALP to convert packed BCD to
ASCII numbers and place them in R2 and R6.
29
SUBJECT- MP&MC
Solution:
MOV A, R6 ; save in R6
MOV A, R2 ; A=29H
RR A ; rotae right
RR A ; rotae right
RR A ; rotate right
RR A ; rotate right
P10. Write an ALP to create a square wave of 50% duty cycle on bit 0 of port 1.
Solution:
LCALL DELAY
SJMP HERE
30
SUBJECT- MP&MC
P11. Assume that the bit P2.2 is used to control the outdoor light and bit P2.5 to control the light
inside the building. Write an ALP to turn on outside light and to turn the inside one.
Solution:
SETB C ; CY=1
CLR C ; CY=0
31