Unit 4 - 8051 Microcontroller Basics
Unit 4 - 8051 Microcontroller Basics
UNIT
8051 Microcontroller Basics
SYLLABUS
8051 Microcontroller Basics: Inside the Computer, Microcontrollers and Embedded Processors,
Block Diagram of 8051, PSW and Flag Bits, 8051 Register Banks and Stack, Internal Memory
Organization of 8051, IO Port Usage in 8051, Types of Special Function Registers and their uses in
8051, Pins Of 8051. Memory Address Decoding, 8031/51 Interfacing with External ROM And RAM.
8051 Addressing Modes.
• The microprocessor mainly contains CPU and general-purpose registers. It does not have
built-in RAM, ROM, I/O ports etc. on the chip.
• The microprocessors are commonly referred to as general-purpose microprocessor.
Examples:
Intel: 8086, 80286, 80386, 80486, Pentium etc.
Motorola: 68000, 68010, 68020, 68030 etc.
MICROCONTROLLER:
Page | 1
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• A Microcontroller is a single integrated circuit (IC) that is capable of performing
specific task (e.g. washing machine, toy, Telephone, Printer, Video game etc.
• Microcontroller has a CPU (a microprocessor) and in addition it has built-in RAM, ROM,
Input/output devices, Timers/Counters on a single chip.
Examples: 8051, 8052, ARM processor etc.
Note:
7 Access time for memory & I/o devices are Less access time for built – in memory & I/o
more. devices.
8 Large number of instructions set. Limited number of instructions set.
9 Few pins are multifunctional. More number of pins are multifunctional.
10 Very few bit handling instructions Many bit handling instructions
11 Design is very flexible Design is less flexible
12 Versatile. Not versatile.
13 High cost Low cost
14 General-purpose applications. Single-purpose applications in which cost,
space & power are critical.
Page | 2
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
15 Examples: Intel: 8086, 80286, 80386, Examples:8051, 8052, ARM processor, PIC
80486, Pentium etc. Motorola: 68000, controllers etc.
68010, 68020, 68030 etc.
Page | 3
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• Easy to upgrade
• Packaging (The number of pins & the packaging format. This determines the required
space & assembly layout.)
Page | 4
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Register:
Register are used to store information temporarily, while the information could be
• a byte of data to be processed, or
• an address pointing to the data to be fetched
The majority of 8051 register are 8-bit registers. The most widely used registers are
• Accumulator (A), for all arithmetic and logic instructions.
• B, R0, R1, R2, R3, R4, R5, R6, R7.
• DPTR (data pointer), and PC (Program Counter).
A register (Accumulator):
• Accumulator is a 8 bit register & is widely used for arithmetic and data transfer
operations.
• The arithmetic operations are addition, subtraction, multiplication, division & Boolean bit
manipulating etc.
• The data transfer operation between the 8051 microcontroller and any external memory.
Note: It can be accessed through its SFR address of 0E0H.
B-register:
• The B-register is always used with the A-register to store 8-bit result of multiplication &
division operations
• It is used as temporary register where data may be stored.
The 8051 has two 16-bit timers/counters, they can be used either as
• Timers to generate a time delay or as
• Event counters to count events happening outside the microcontroller.
The two timers are
i) Timer/Counter T0 and
ii) Timer/Counter T1
• Each register can be used either for Timer or counter and can be divided into Two 8-bit
registers called Timer Low (TL) and Timer High (TH).
Page | 5
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
STACK (8-bit)
Explain the organization of stack in 8051.
AKTU Question Paper 2021-22, 2 MARKS
• The stack is a section of RAM used by the CPU to store information temporarily. This
information could be data or an address.
• The register used to access the stack is called the SP (stack pointer) register. The stack
pointer in the 8051 is only 8 bits wide
• The storing of a CPU register in the stack is called a PUSH, and loading the contents of the
stack back into a CPU register is called a POP.
Memory Organization:
The 8051 microcontroller's memory is divided into
1) Internal RAM – 128 bytes: Used for temporarily storing and keeping intermediate
results and variables.
2) ROM – 4Kbytes: Used for permanent saving program being executed
Special Function Registers (SFR):
The operations of 8051 are done by a group of specific internal registers, each called a special
function Register (SFR).
Interrupts:
The 8051 Microcontroller has 6 interrupts:
RESET, INT0, INT1, Timer0 (TF0), Timer1 (TF1), Serial Port (TI/RI)
CY AC F0 RS1 RS0 OV - P
D7 D6 D5 D4 D3 D2 D1 D0
• The program status word (PSW) register is an 8-bit register. It is also referred to as the
flag register. The only 6 bits are used by the 8051. The two unused bits are user-definable
flags.
Page | 6
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• The Carry (CY), Auxiliary carry (AC), Overflow (OV) and Parity (P) are called Conditional
flags because these flags indicate some conditions that resulted after an instruction was
executed.
Auxiliary carry Flag (AC): After performing arithmetic & logic operation if a carry from D3 to D4
bit then AC = 1, otherwise AC = 0.
RS 1 & RS 0: These two bits are used to change Register Bank and are shown below in table.
Table: Register Bank Selector
RS1 RSO Register Bank Address
0 0 Bank 0 00H-07H
0 1 Bank 1 08H-0FH
1 0 Bank 2 10H-17H
1 1 Bank 3 18H-1FH
Example 4.1
Show the status of the CY, AC and P flag after the addition of 38H and 2FH in the following
instructions.
MOV A, #38H
ADD A, #2FH ; after the addition A=67H, CY=0
Solution:
1 1 1 1
38 0 0 1 1 1 0 0 0
+ 2F + 0 0 1 0 1 1 1 1
Page | 7
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
67 1 0 1 1 0 0 1 1 1
Example 4.2
Show the status of the CY, AC and P flag after the addition of 9CH and 64H in the following
instructions.
MOV A, #9CH
ADD A, #64H ; after the addition A=00H, CY=1
Solution:
1 1 1 1 1 1
9C 1 0 0 1 1 1 0 0
+ 64 + 0 1 1 0 0 1 0 0
1 00 1 0 0 0 0 0 0 0 0
Example 4.3
Show the status of the CY, AC and P flag after the addition of 88H and 93H in the following
instructions.
MOV A, #88H
ADD A, #93H ; after the addition A=1BH, CY=1
Solution:
1
66 1 0 0 0 1 0 0 0
+ 93 + 1 0 0 1 0 0 1 1
1 1B 1 0 0 0 1 1 0 1 1
Page | 8
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.2.3 SPECIAL FUNCTION REGISTER (SFR):
The functions of any 5 SFR can be explained i.e. Accumulator, PSW, B register, Ports, Timers, DPTR
etc.
Page | 9
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.3 MEMORY ORGANIZATION:
Explain the internal memory organization of the microcontroller 8051.
AKTU Question Paper 2020-21, 10 MARKS
1. Internal RAM
The internal data memory consists of 256 bytes; these are divided into two parts:
i) Internal data RAM- 00H-FFH (128 bytes)
ii) Special function registers- 80H-FFH (128 bytes)
Page | 10
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Register banks or General-purpose RAM
State the function of RS1 and RS0 bits in the flag register of Intel 8051 microcontroller.
AKTU Question Paper 2021-22, 2 MARKS
Explain the utility of the register banks in 8051.
AKTU Question Paper 2020-21, 2 MARKS
The selection of the register banks and their addresses are given below:
RS1 RS0 REGISTER BANK ADDRESS
0 0 0 00H – 07H
0 1 1 08H – 0FH
1 0 2 10H – 17H
1 1 3 18H – 1FH
• The 8051 microcontroller consists of four register banks: Bank 0, Bank 1, Bank 2, Bank
3. Each register bank contains 8 registers of 1 byte. So total 32 register in register bank.
• To change the register bank, we have to set values of PSW register bits RS0 and RS1.
• Only one register bank is in use at a time.
• When 8051 is RESET, by default register bank 0 is selected.
Note:
Byte
Addres
Fig. 4.13: Bit addressable RAM
Page | 11
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• The area of bit addressable RAM is usually used to store bit variables.
• The address ranges from 20h to 2Fh (16 bytes) is bit-addressable RAM. Each bit can be
accessed from 00H to 7FH.
• The total bit addressable location are 16 bytes x 8 bits = 128 bits.
• Each bit can be accessed from 00H to 7FH.
• The programming using bit addressable area saves wastage of memory.
Note:
For example, Bit 0 of byte 20h has the bit address 0, and bit 7 of byte 2Fh has the bit address 7Fh).
Page | 12
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Fig. 4.15: External RAM
PROGRAM MEMORY:
The Program Memory is used for permanent saving program being executed.
• The 8051 microcontroller has an on chip internal program ROM of 4K size and if needed
can add an external memory of size 60K maximum by interfacing i.e. total 64K size
memory.
• The Program memory accessed through EA pin. The EA is an active low input.
• When EA is connected to VCC i.e. EA =1, the 8051 can access 4 K bytes of internal ROM i.e.
0000H to 0FFFH and external ROM of 60 K bytes i.e. 1000H to FFFFH.
• When EA is connected to GND i.e. EA =0, then all program fetches are directed to external
ROM i.e. 0000H to FFFFH.
Page | 13
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.4. I/O PORTS FUNCTIONS:
Discuss the significance of I/O ports along-with their dual roles in 8051.
AKTU Question Paper 2021-22, 10 MARKS
iii) Port 2: P2.0 to P0.3 configured as input & P2.4 to P2.7 configured as output port.
MOV A, #0F0H ; A=F0H
MOV P2, A ; Make P2.0 to P2.3 as output & P2.4 to P2.7 as input pins.
Page | 14
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Solution:
The P0 is 1st loaded with 55H = 01010101 and its complement i.e. 10101010 = AAH is
given to P0 continuously.
Page | 15
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.6: (Refer After reading instruction set)
Write an ALP to continuously send out to port 0 the alternating value 55H and AAH.
MOV A, #55H
BACK: MOV P1, A
ACALL DELAY
CPL A ; complement register A
SJMP BACK
Page | 16
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Note:
Table 2 RESET Values 0f 8051 ports
RESET Values
PORTS
Binary Hex
P0 11111111 FF
P1 11111111 FF
P2 11111111 FF
P3 11111111 FF
❖ Name the pins of 8051 used for external memory interfacing and list their functions.
Ans. The pins which are used for external memory interfacing are:
.
Refer pin details of 8051 to explain the functions of each pin.
The 8051 microcontroller is a dual in-line pin packages has 40 pins, out of which 32 pins are
assigned for Ports P0, P1, P2 and P4, where each port takes 8 pins.
The rest of the pins are VCC, GND, XTAL1, XTAL2, RST, EA , ALE/ PROG and PSEN .
Page | 17
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Pin
Pin Name Description
No.
Page | 18
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• If external memory is used then the higher address i.e.
A8-A15 will appear on this port.
Page | 19
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.6 EXTERNAL MEMORY (ROM & RAM) INTERFACING:
4.6.1 Interfacing External Data
Page | 20
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• The MOVC instruction is used to get data from code space.
• The EA is an active low input pin. When EA is connected to GND i.e. EA =0, then all
program fetches are directed to external ROM i.e. 0000 H to FFFF H.
• The PSEN is an active low pin used to access the external program memory (ROM),
PSEN pin is connected to the OE pin of the ROM chip.
• To access the program code, EA must be grounded then PSEN will go low to enable the
external ROM to place a byte of program code on the data bus.
Example 4.8:
Interface 4K RAM to 8051 Microcontroller
Page | 21
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.9:
Interface 4K ROM to 8051 Microcontroller
Page | 22
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.10:
Interface 4K ROM & 4K RAM to 8051 Microcontroller
Page | 23
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.11:
Interface 8K RAM to 8051 Microcontroller
Example 4.12:
Interface 8K ROM to 8051 Microcontroller
❖ Describe the method of interfacing 8K PROM to 8051 microcontroller
10-Mark
Page | 24
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Interface 8K DATA ROM to 8051 Microcontroller
Fig. 4.26 c: Interface 8K of single external ROM for both CODE and DATA
Page | 25
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.13:
Interface 8K EPROM & 4K RAM to 8051 Microcontroller
Page | 26
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.14:
Interface 16K EPROM & 8K RAM to 8051 Microcontroller.
Page | 27
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.15: Interfacing 8 Kbyte RAM and 8 Kbyte ROM to 8051 microcontroller.
Example 4.16: Interfacing 8 Kbyte RAM and 8 Kbyte ROM to 8051 microcontroller.
Page | 28
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.17: Interfacing 16 Kbyte DATA RAM, DATA ROM and PROM to 8051
microcontroller.
Fig. 4.31: Interfacing 16 Kbyte DATA RAM, DATA ROM and PROM to 8051
microcontroller.
Illustrate the addressing modes of 8051 microcontroller. Support your answer with
suitable examples.
AKTU Question Paper 2021-22, 10 MARKS
The CPU can access data in various ways. The data could be in a memory or in register or it may
be an immediate value (CONSTANT). The various ways of accessing these data are called
addressing mode.
There are 5 addressing modes in 8051
1. Immediate addressing mode
2. Resister addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode.
Page | 29
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
MOV DPH, #55H ; DPTR=5566H
MOV DPTR, #5566H ;DPTR=5566H
;This is the same as above
MOV DPTR, #69925 ;illegal Value because value is > 65535 (FFFFH)
MOV P1, #35H ;load 35H into Port 1
Page | 30
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Limitations
• R0 and R1 are the only registers that can be used for pointers in register indirect
addressing mode Since R0 and R1 are 8 bits wide, their use is limited to access any
information in the internal RAM.
• The accessing of externally connected RAM or on-chip ROM need 16-bit pointer. In such
case, the DPTR register is used.
NOTE:
• Indexed addressing mode is widely used in accessing data elements of look-up table
entries located in the program ROM.
Accumulator (A register):
• It is an 8-bit register.
Page | 31
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
• It holds a data and receives the result of the arithmetic instructions.
• ACC is usually accessed by direct addressing and its physical address is E0H. Accumulator
is both byte and bit addressable. if you want to access the second bit (i.e. bit 1), you may
use E1H and for third bit E2H and so on.
Page | 32
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
PSW Register Bits
Timer/Counter Register:
• The 8051 has two 16-bit Programmable timers / counters (Timer 0 – Timer 1).
• Which can be used either as timer to generate a time delay or as counter to count events
happening outside the microcontroller.
• The Counters and Timers in 8051 microcontrollers contain two special function
registers: TMOD (Timer Mode Register) and TCON (Timer Control Register).
TMOD Register:
• TMODE register is an 8-bit register.
Page | 33
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
TCON Register:
Timer Control or TCON is an 8-bit Register and is used to start or stop the Timers of 8051
Microcontroller. It also contains bits to indicate if the Timers has overflowed. The TCON SFR also
consists of Interrupt related bits.
SP (Stack Pointer):
• The stack is a portion of a RAM Used by the CPU to store data or memory address on
temporary basis.
• The stack pointer register is used to access the stack is known as SP register. The stack
pointer register is 8-bits wide. It can take a value of 00 to FFH. The RAM memory location
08H is the first location used for the stack. When the 8051 microcontroller is initialized,
the SP register contains the value 07H.
• If we want to store CPU register data into the stack this is known as PUSH operation and
if we getting the data from stack back into a CPU register this is known as a POP operation.
PC (program Counter):
• It is a 16-bit register. It is use to hold the address of the memory location from where the
next instruction to be fetched.
• When the 8051 initializes PC starts at 0000h and it is automatically is incremented every
time after an instruction is executed. (So, in this way PC maintain the sequence of program
execution).
• Due to this the width of the PC decides the max program length in bytes.
IE (Interrupt Enable):
The IE or Interrupt Enable Register is used to enable or disable individual interrupts. If a bit is
SET, the corresponding interrupt is enabled and if the bit is cleared, the interrupt is disabled. The
Bit7 of the IE register i.e. EA bit is used to enable or disable all the interrupts.
IP (Interrupt Priority):
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low. If
a bit is CLEARED, the corresponding interrupt is assigned low priority and if the bit is SET, the
interrupt is assigned high priority
Page | 34
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
SCON (Serial Control):
The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial Port. It is
located as an address of 98H. Using SCON, we can control the Operation Modes of the Serial Port,
Baud Rate of the Serial Port and Send or Receive Data using Serial Port.
Port Registers:
8051 microcontrollers have 4 bidirectional I/O ports. Port 0, Port 1, Port 2 and Port 3 (P0, P1, P2
and P3). Which can be work as input or output port. Each port having 8-bits. Hence, total 32
input/output pins allow the microcontroller to communicate with outside world means this port
allow to be connected with the peripheral devices.
Page | 35