0% found this document useful (0 votes)
108 views

Unit 4 - 8051 Microcontroller Basics

Uploaded by

akshatb1003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Unit 4 - 8051 Microcontroller Basics

Uploaded by

akshatb1003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

JSS MAHAVIDYAPEETHA

JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA

MICROPROCESSOR & MICROCONTROLLER


(8085, 8086 & 8051)
KEC-502
V SEMESTER
Electronics & Communication Engineering
UNIT-4
8051 MICROCONTROLLER BASICS
As per the syllabus of
Dr. A.P.J. Abdul Kalam Technical University, UTTAR PRADESH

JSS Academy of Technical Education,


NOIDA.

Dr. ARUN KUMAR. G


Professor & HOD, Dept. of ECE
Mr. GANESHA H S
Assistant Professor, Dept. of ECE
Microprocessor & Microcontroller 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.

4.1 INTRODUCTION TO MICROPROCESSOR & MICROCONTROLLERS:


MICROPROCESSOR:

Fig. 4.1: Block diagram of Microprocessor.

• 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.

Note: The microprocessor is the heart of microcomputer.

MICROCONTROLLER:

Fig. 4.2: Block diagram of 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:

Fig. 4.3: Shows applications of microcontroller

4.1.1 DIFFERENCE BETWEEN MICROPROCESSOR & MICROCONTROLLER:


Sl.
Microprocessor Microcontroller
No.
1

Fig.4.4: Block diagram of Fig.4.5: Block diagram of


microprocessor microcontroller
2 It contains only CPU. The RAM, ROM, Microcontroller has a CPU (a
Input/output devices, timers & counters microprocessor) and in addition it has
are separately interfaced. built-in RAM, ROM, Input/output devices,
Timers/Counters on a single chip.
3 Designers decides the amount of ROM, Fixed amount of on-chip ROM, RAM and
RAM and Input /output ports etc. Input /output ports etc.
4 It has many instructions to move data It has one or two instructions to move data
between memory & CPU. between memory & CPU.
5 It has one or two-bit handling instructions. It has many bit handling instructions. {ex:
CLRC, SETB P1.0 etc.,}
6 It has single memory for data & program. It has separate memory for data & program.

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.

4.1.2 RISC & CISC


Reduced Instruction Set Computer (RISC)
• The Reduced Instruction Set Computer is a type of microprocessor architecture that utilizes
a small, highly-optimized set of instructions, rather than a more specialized set of instructions
often found in other types of architectures.
• A simplified instruction set provides higher performance when combined with
microprocessor architecture capable of executing those instructions using fewer
microprocessor cycles per instruction.

Complex instruction set computing (CISC)


• The Complex instruction set computing architecture is a type of microprocessor design
contains a large set of computer instructions that range from very simple to very complex
and specialized.
• In CISC architecture, single instructions can execute several low-level operations such as
a load from memory, an arithmetic operation, and a memory store or capable of multi-
step operations or addressing modes within single instructions.

4.1.3 Difference between RISC and CISC processors:


Sl. No. RISC CISC
1 Only few instructions. Many instructions.
2 Highly pipelined. Not pipelined or Less pipelined.
Instruction interpreted by the micro
3 Instruction executed by the hardware.
program.
Complex instructions taking multiple
4 Simple instructions taking one cycle.
cycles.
5 Multiple register set. Single register set.
6 Very few instructions refer memory. Most of instructions may refer memory.
7 Fixed length instructions. Variable length instructions.
8 Complexity is in the compiler. Complexity is in the micro-program.
9 Few addressing modes. Many addressing modes.
Only Load/Store instructions access
10 Many instructions can access memory.
memory
Coding in RISC processor requires
Coding in CISC processor is simple i.e.
11 more number of lines. i.e. program size
program size is small
is large
12 It has multi-clock. It has single-clock.
Examples: PIC Microcontroller series
13 Examples: INTEL 80286, 80386 etc.
etc.

4.1.4 SELECTION OF MICROCONTROLLERS:


❖ List the points to be considered during the selection of a microcontroller for an
application.
The three criteria in choosing microcontrollers are as follows:
1. Microcontroller must perform the required task efficiently & cost effectively i.e.
• Speed
• Amount of RAM & ROM on chip
• Power consumption
• The number of input pins & the timer on the chip
• Cost per unit

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.)

2. Availability of assembler, debuggers, complier, emulator, technical support and expertise


both in-house and outside.

3. Microcontroller availability in needed quantities both now and in the future.

4.1.5 EMBEDDED MICROCONTROLLERS


A microcontroller can be considered as a system with a processor, memory and
peripherals and can be used as an embedded system.
The majority of microcontrollers in use today are embedded in other machinery, such as
automobiles, telephones, home appliances, and peripherals for computer systems.

4.2 8051 ARCHITECTURE:


Analyze the architecture of 8051 microcontroller along-with a suitable block diagram.
AKTU Question Paper 2021-22, 10 MARKS
Describe the architecture of 8051 with neat diagram.
AKTU Question Paper 2022-23, 10 MARKS

Fig. 4.6: Block diagram of 8051 Microcontroller

Central processing unit (CPU):


• The 8051 Central processing unit consists of 8-bit arithmetic & Logic unit (ALU),
Registers: A, B, PSW, SP, 16-bit program counter & “Data pointer registers” (DPTR).
• The ALU can perform arithmetic functions on 8-bit data i.e. addition, subtraction,
multiplication & division.
• Similarly, the logic unit perform logical operations such as AND, OR, NOT etc.

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.

Note: It can be accessed through its SFR address of 0F0H.


BUS:
Bus is a collection of wires which work as a communication channel or medium for transfer of
data. The 8051 has two types of buses:
1) Address Bus (16-bits) and
2) Data Bus (8-bits)

Program Counter (PC):


• PC is a16-bit register which points to the address of the next instruction to be executed.
• The PC is automatically incremented after every instruction byte is fetched.
• PC is the only register that does not have an internal address.
• When 8051 is RESET, the default value of PC is 0000 H.

Input-Output ports (I/O Ports):


• The 8051 has 4 Input/output ports i.e. PORT 1, PORT 2, PORT 3 and PORT 4 and each port
has 8 Input/output pins.
• It has total 32 Input/output pins and each pin can be configured as input or output pin.

Times & Counters

Fig. 4.7: Timer 0 & Timer 1

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.

Data pointer (DPTR):

Fig. 4.8: DPTR

• DPTR is a 16-bit register, which holds a 16-bit address.


• DPTR can be split into 2 parts:
✓ DPL: Data pointer Low byte having internal address 82h.
✓ DPH: Data pointer high byte having internal address 83h.
• DPTR is very useful for string operations and look up table operations.

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)

Program status (PSW) or Flag register:


Analyze the PSW of 8051 and also explain the relevant flag bits.
AKTU Question Paper 2021-22, 10 MARKS

Describe the different flags available in 8051 Program Status Word.


AKTU Question Paper 2020-21, 2 MARKS

CY AC F0 RS1 RS0 OV - P
D7 D6 D5 D4 D3 D2 D1 D0

Fig. 4.9: Program Status Word.

• 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.

Carry Flag (CY):


After performing arithmetic & logic operation if there is a carryout from the MSB (D7 i.e. 7th-bit)
then CY = 1, otherwise CY = 0

Auxiliary carry Flag (AC): After performing arithmetic & logic operation if a carry from D3 to D4
bit then AC = 1, otherwise AC = 0.

FO: Available for user for general purpose

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

Overflow Flag (OV):


OV flag is set to 1 if either of the following two conditions occurs:
i) There is a carry from D6 to D7, but no carry out of D7 (CY = 0).
ii) There is a carry out from D7 bit (CY = 1) but no carry from D6 to D7 bit.

Parity Flag (P):


Parity flag indicates the number of 1’s present in the accumulator.
i) If the number of 1’s in the accumulator is odd then P = 1.
ii) If the number of 1’s in the accumulator is even then P = 0.

4.2.1 APPLICATIONS OF MICROCONTROLLERS:


The applications of microcontroller are:
Home Appliances Office Automobiles
TVs, VCR, Camcorders,
Remote Controller, Video
Telephones, Computers, Engine control, ABS, Air
games, Cellular phones,
Security Systems, Fax Machines, bags, Transmission control,
Telephones, Paging,
Copiers, Laser Printer, Color Climate Control, Keyless entry,
Camera, Answering
Printer etc. Trip computer etc.
machines, Musical
Instruments etc.

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

CY = 0 since there is no carry beyond the D7 bit


AC = 1 since there is a carry from the D3 to the D4 bi
P = 1 since the accumulator has an odd number of 1s (it has five 1s)

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

CY = 1 since there is a carry beyond the D7 bit


AC = 1 since there is a carry from the D3 to the D4 bi
P = 0 since the accumulator has an even number of 1s (it has zero 1s)

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

CY = 1 since there is a carry beyond the D7 bit


AC = 0 since there is no carry from the D3 to the D4 bi
P = 0 since the accumulator has an even number of 1s (it has four 1s)

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.

Fig. 4.10: Special Function Register.


• The operations of 8051 are done by a group of specific internal registers; each called a
Special Function Register (SFR) and its address ranges from 80H to FFH (80 bytes).
• There are 21 Special function registers (SFR) in 8051 micro controller and these are
Register A, Register B, PSW, PCON etc. and each of these registers are of 1-byte size. Some
of these special function registers are bit addressable, while some are byte addressable.

4.2.4 FEATURES OF 8051 MICROCONTROLLER:


The Feature of 8051 is as follows
1) 8-bit CPU.
2) 4 Kbytes Internal ROM (Program Memory).
3) 128 bytes Internal RAM (Data Memory).
4) It has four 8-bit ports (Port 1, 2, 3 & 4), total 32 input/output lines.
5) Two 16-bit timers (T0 & T1).
6) One Full duplex serial communication port (data Transmitter/ Receiver).
7) Six Interrupt sources.
8) 16-bit program counter (PC) & data pointer (DPTR).
9) 8-bit Stack pointer.

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

The 8051 microcontroller's memory is divided into


1. Data Memory (RAM) and
2. Program Memory (ROM).

Data Memory (RAM)


The Data Memory is used for temporarily storing data, keeping intermediate results and
variables used during the operation of the microcontroller.

The Data memory is of two types:


1. Internal RAM and
2. External RAM.

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)

Internal data RAM

Fig. 4.11: Internal data RAM

The Internal data RAM is divided into 3 parts:


1) Register banks or General-purpose RAM
2) Bit addressable area
3) Scratch pad area

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:

Fig. 4.12: Register Banks


Bit addressable RAM
Describe the bit-addressable RAM space available in 8051.
AKTU Question Paper 2021-22, 2 MARKS

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).

Scratch pad area


• The upper 80 bytes are scratch pad area which is used for general purpose storing of data.
• The Scratch pad area is in the address range 30H to 7FH.
• The Scratch pad area can be used for stack memory.

Special function registers (SFR)


• The operations of 8051 are done by a group of specific internal registers; each called a
Special Function Register (SFR) and its address ranges from 80H to FFH (80 bytes).
• There are 21 Special function registers (SFR) in 8051 micro controller and these are
Register A, Register B, PSW, PCON etc. and each of these registers are of 1 byte size. Some
of these special function registers are bit addressable, while some are byte addressable.
• SFRs are used to control timers, counters, serial ports, I/O ports and peripherals.

Fig. 4.14: Special Function Register


External RAM

Page | 12
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Fig. 4.15: External RAM

• External data memory is 64 K Bytes read/write memory.


• The external data memory is indirectly accessed through a Data Pointer Register, it is
slower than access to internal data memory.

PROGRAM MEMORY:
The Program Memory is used for permanent saving program being executed.

Fig. 4.16: Program memory (ROM)

• 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

Fig.4.17: Pin diagram of 8051 showing I/O ports


Features of I/O ports
• An 8051 microcontroller has four I/O ports P0, P1, P2 & P3, where each port has 8 bits
i.e. total 32 I/O pins which can be configures as input or output ports.
• All the ports upon reset are configures as input, ready to use it as input port.
• To use any of these ports as an input port, it must be programmed by writing 1 to all
the bits.
• To use any of these ports as an output port, it must be programmed by writing 0 to all
the bits.
Examples: (Refer After reading instruction set)

i) Port 0 configured as output port


MOV A, #00H ; A=00H
MOV P0, A ; Make Port 0 as output port.

ii) Port 1 configured as input port


MOV A, #0FFH ; A=FFH
MOV P1, A ; Make Port 1 as input port.

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.

4.4.1 PORT 0 (Pins 32-39)


• Port 0 occupies a total of 8 pins.
• To use the pins of PORT 0 as both input and output port, each pin must be connected
externally to 10 KΩ pull-up resistors because P0 is an open drain.
• Upon reset, Port 0 is configured as input port.

Page | 14
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics

Fig.4.18: Port 0 with 10 KΩ Pull-Up Resistors

• Port 0 is multiplexed address and data to save pins.


• Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data.
• When connecting an 8051 to an external memory, port 0 provides both address and
data.
• Port 0 provides the lower 8 bits address via A0 – A7

Example 4.4: (Refer After reading instruction set)


Write an Assembly Language Program (ALP) to toggle Port 0 continuously.

Solution:
The P0 is 1st loaded with 55H = 01010101 and its complement i.e. 10101010 = AAH is
given to P0 continuously.

BACK: MOV A, #55H


MOV P0, A
ACALL DELAY ; Delay routine not shown
MOV A, #0AAH
MOV P0, A
ACALL DELAY ; Delay routine not shown
SJMP BACK

Example 4.5: (Refer After reading instruction set)


Write an ALP to configure Port 0 first as an input port and then data is received from P0
and sent to P1.

MOV A, #0FFH ; A=FF hex


MOV P0, A ; make P0 an i/p port by writing it all 1s
BACK: MOV A, P0 ; get data from P0
MOV P1, A ; send it to port 1
SJMP BACK ; keep doing it

4.4.2 PORT 1 (Pins 1-8)


• Port 1 occupies a total of 8 pins.
• Port 1 does not need any pull-up resistors since it already has pull-up resistors
internally.
• Upon reset, Port 1 is configured as input port.

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

Example 4.7: (Refer After reading instruction set)


Port 1 is configured first as an input port by writing 1s to it and then data is received from
that port and saved in R3 and R1.

MOV A, #0FFH ; A=FF hex


MOV P1, A ; make P1 an input port by writing it all 1s
MOV A, P1 ; get data from P1
MOV R3, A ; save it to in register R3
ACALL DELAY ; wait
MOV A, P1 ; another data from P1
MOV R1, A ; save it to in register R1

4.4.3 PORT 2 (Pins 21-28)


• Port 2 occupies a total of 8 pins.
• Port 2 does not need any pull-up resistors since it already has pull-up resistors
internally.
• Upon reset, Port 2 is configured as input port.
• In many 8051-based systems, P2 is used as simple I/O. Port 2 is also designated as
A8 - A15, indicating its dual function.

4.4.4 PORT 3 (Pins 10-17)


• Port 3 occupies a total of 8 pins.
• Port 3 does not need any pull-up resistors since it already has pull-up resistors
internally.
• Upon reset, Port 3 is configured as input port.
• Port 3 has the additional functions of proving some extremely important signals such as
interrupts.
Table 1 PORT 3 Alternative Functions
P3 bit Pin No. Function Description
P3.0 10 RxD Receive data for serial port
P3.1 11 TxD Transmit data for serial port
P3.2 12 ̅̅̅̅̅̅̅̅
𝐈𝐍𝐓 𝟎 External interrupt 0
P3.3 13 ̅̅̅̅̅̅̅̅
𝐈𝐍𝐓 𝟏 External interrupt 1
P3.4 14 T0 Timer/counter 0
P3.5 15 T1 Timer/counter 1
P3.6 16 ̅̅̅̅̅
𝐖𝐑 External data memory write strobe
P3.7 17 ̅̅̅̅
𝐑𝐃 External data memory read strobe

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.

4.5 8051 PIN DIAGRAM:


Illustrate the pin diagram of 8051 with the help of a suitable diagram and explain the
utility of the pins available in 8051.
AKTU Question Paper 2020-21, 10 MARKS

Fig. 4.19: Pin diagram of 8051 microcontroller

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.

Port 1 Port 1 is an 8-pin bi-directional Port. Each of these pins can


1-8
(P1.0-P1.7) be configured as either input or output pins.

• When a pulse (square wave) is applied to this pin,


9 RST (Reset) microcontroller will terminate all its activities & reset.
• Program counter is loaded with 0000.

Port 3 is an 8-pin bi-directional Port with dual function.


Port 3
10-17 Each of these pins can be configured as either input or
(P3.0-P3.7)
output pins.

• In 8051, the data is received from or transmitted to RXD &


TXD pins.
10-11 RXD & TXD
• The data is transmitted out of 8051 through the TXD line.
• The data is received by 8051 through the RXD line.

• The 8051 has two external hardware interrupts i.e.


Interrupt 0 & Interrupt 1. These two pins are used in
12-13 INT 0 & INT 1 Timers/Counter operation.
• These Pins are triggered by external circuits.

• The 8051 has two 16-bit Timers/ Counters. T0 -Timer0


register (16-bit) T1 -Timer1 register (16-bit).
• These can be used either as Timers to generate a time
14-15 T0 & T1 delay or as counters to count events happening outside
the microcontroller.
• Each 16-bit registers can be accessed as two separate 8-
bit registers.

These are active low pins.

• When RD =0, microcontroller reads the data from


16-17 RD & WR external RAM.
• When WR =0, microcontroller writes the data into
external RAM.

• The 8051 has an on-chip oscillator but requires an


external clock to run it.
• A Quartz crystal oscillator is connected to inputs XTAL1 &
XTAL2 with two capacitors having values 30PF.
18-19 XTAL2 & XTAL1
• If an external frequency (from AFO) has to be applied,
then it must be applied between XTAL1 & ground. XTAL2
must be left open.
• Oscillator frequency may vary from 10MHz to 40MHz.

20 VSS It is a ground pin i.e. VSS=0V

Port 2 • Port 2 is an 8-pin bi-directional Port.


21-28 • If external memory is not used, these pins can be used as
(P2.0-P2.7)
either input or output pins.

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.

• PSEN is an active low input to 8051.


29
PSEN (program store • PSEN is an output pin used to access the external
Enable) program memory (ROM). This pin is connected to the
OE pin of the ROM.

• ALE is an output pin. It is used for demultiplexing the


address and the data bus.
ALE/ PROG • When ALE=1, Port 0 is providing lower order address
30 (A0-A7).
(Address Latch
• When ALE=0, Port 0 is used as data lines (D0-D7).
Enable)
• This pin also has program pulse input PROG during
EEPOM programming.

• EA is an active low input to 8051.


• When EA is connected to VCC i.e. EA =1, the 8051 can
EA /VPP (External
access 4 K bytes of internal ROM i.e. 0000 H to 0FFF H and
31 Access
external ROM of 60 K bytes i.e. 1000 H to FFFF H.
Enable/Programming
supply voltage) • 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.

• Port 0 is an 8-pin bi-directional Port.


• Port 0 is also multiplexed low order address and data bus
i.e. AD0-AD7.
Port 0 • If external memory is not used, then these pins can be
(P0.0-P0.7) used as either input or output pins.
32-39
• If external memory is used then the lower address and
data lines AD0-AD7 will appear on this port.
• When ALE=1, Port 0 is providing lower order address
(A0-A7).
• When ALE=0, Port 0 is used as data lines (D0-D7).

40 VCC DC power supply +5V is connected to this pin.

Page | 19
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
4.6 EXTERNAL MEMORY (ROM & RAM) INTERFACING:
4.6.1 Interfacing External Data

Fig. 4.20: Interfacing 8051 with External Data Memory

• To address up to 64 K Bytes of external data memory then the hardware should be


configured as shown in figure 1.9.1.
• The MOVX instruction is used to access the external data memory.
• The Port 0 outputs the low address (A0 to A7) while Port 2 outputs the high address
(A8 to A15).
• The Port 0 is a multiplexed address/data bus. The LATCH is used to demultiplex address
and data bus. The LATCH will be enabled when ALE=1, so output of LATCH has lower
order address A0-A7 as shown in Fig 1.9.1.
• The RD & WR pins are used when a RAM has to be accessed.
• When RD = 0, a data byte can be read from a RAM location.
• When WR = 0, a data byte can be written into a RAM location.

4.6.2 INTERFACING EXTERNAL ROM:

Fig. 4.21: Interfacing 8051 with External Program Memory.

• To address up to 64 K Bytes of external Program memory then the hardware should be


configured as shown in figure 1.9.2.
• The Port 0 outputs the low address (A0 to A7) while Port 2 outputs the high address
(A8 to A15).
• The Port 0 is a multiplexed address/data bus. The LATCH is used to demultiplex address
and data bus. The LATCH will be enabled when ALE=1, so output of LATCH has lower
order address A0-A7 as shown in Fig 1.9.2.

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

Fig. 4.22: Interface 4K RAM to 8051 Microcontroller

Page | 21
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Example 4.9:
Interface 4K ROM to 8051 Microcontroller

Fig. 4.23: 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

Fig. 4.24: 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

Fig. 4.25: 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

Fig. 4.26 a: Interface 8K ROM to 8051 Microcontroller

Page | 24
Microprocessor & Microcontroller Unit-4: 8051 Microcontroller Basics
Interface 8K DATA ROM to 8051 Microcontroller

Fig. 4.26 b: Interface 8K DATA ROM to 8051 Microcontroller

Interface 8K of single external ROM for both CODE and DATA.

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

Fig. 4.27: 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.

Fig. 4.28: 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.

Fig. 4.29: Interfacing 8 Kbyte RAM and ROM with 8051.

Example 4.16: Interfacing 8 Kbyte RAM and 8 Kbyte ROM to 8051 microcontroller.

Fig. 4.30: Interfacing 8 Kbyte RAM and ROM with 8051.

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.

4.7 8051 ADDRESSING MODES:


Explain the various addressing modes of 8051 microcontroller.
AKTU Question Paper 2022-23, 10 MARKS

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.

Immediate addressing mode


• In Immediate addressing mode, the source operand is a constant. The immediate data
must be preceded by the pound sign, “#”.
• This addressing mode can load information into any registers, including 16-bit DPTR
register and 8051 ports.
Examples:
MOV R1, #50 ;load 50 into R1
MOV B, #50H ;load 50H into B
MOV A, #35H ;load 35H into A
MOV DPL, #66H

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

Resister addressing mode


• Resister addressing mode Use registers to hold the data to be manipulated.
• The source and destination registers must match in size.
• The movement of data between Rn registers is not allowed.
Examples:
MOV A, R1 ; copy contents of R1 into A
MOV R3, A ; copy contents of A into R3
ADD A, R2 ; add contents of R2 to A
ADD A, R0 ; add contents of R0 to A
MOV R5, A ; save accumulator in R5
MOV DPTR, #F5ABH ; 16-bit data F5ABH is moved to 16-bit register
MOV R7, DPL ; Lower byte of DPTR copied to R7
MOV R6, DPH ; Higher byte of DPTR copied to R7
MOV DPTR, A ; will give an error because A=8 bit and DPTR= 16-bit
MOV R4, R7 ; is invalid

Direct addressing mode


• The entire 128 bytes of RAM can be accessed using direct addressing mode. The RAM
locations 30-7FH are most often used.
• The register bank locations are accessed by its address or by its register names.
• In this instruction address is given as a part of the instructions.
Examples:
MOV R1, 30H ; Save content of RAM location 30H in R1
MOV 50H, A ; Save content of A in RAM location 50H
MOV A, 4 ; is same as copying R4 into A
MOV A, R4 ; copy R4 into A
NOTE:
• The “#” sign distinguishes between the immediate and direct addressing mode. The
absence of the “#” sign is the direct addressing mode.

Register indirect addressing mode


• In Register indirect addressing mode, a register is used to hold the address of the data
(as a pointer to the data).
• Only register R0 and R1 are used for this purpose. The R2 – R7 cannot be used to hold the
address of an operand located in RAM.
• When R0 and R1 hold the addresses of RAM locations, they must be preceded by the “@”
sign.
Examples:
MOV A, @R1 ; move contents of RAM whose address is held by R1 into A
MOV @R0, B ; move contents of B into RAM whose address is held by R0
MOV @R1, 04H ; move contents of 04H into RAM whose address is held by R1
MOV 30H, @R1 ; move contents of RAM whose address is held by R1 into RAM
30H
MOVC A, @A+DPTR ; the contents of A are added to the 16-bit register DPTR to form
the 16-bit address of the needed data.
Advantages
• The advantage is that it makes accessing data dynamic rather than static as in direct
addressing mode. Looping is not possible in direct addressing mode.

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.

Indexed addressing mode


• Indexed addressing mode is widely used in accessing data elements of look-up table
entries located in the program ROM space.
• Only program memory can be accessed in the index addressing. Either the DPTR or PC
can be used as an index register.
Examples:
MOVC A, @A+DPTR
MOVC A, @A+PC

4.8 SPECIAL FUNCTION REGISTERS (SFR’s):

Fig. 4.32 a: SFR Register


In 8051 microcontroller there are 21 Special function registers (SFR) and this includes
Register A, Register B, Processor Status Word (PSW), PCON etc. There are 21 unique locations for
these 21 special function registers and each of these registers is of 1-byte size.
The 21 SFR of 8051 Microcontroller are categorized into seven groups these are:
• CPU Registers: A and B Register
• Status Register: PSW (Program Status Word) Register
• Pointer Registers: DPTR (Data Pointer: DPL, DPH) and SP (Stack Pointer) Registers
• I/O Port Latches: P0 (Port 0), P1 (Port 1), P2 (Port 2) and P3 (Port 3)
• Peripheral Control Registers: PCON, SCON, TCON, TMOD, IE and IP Registers
• Peripheral Data Registers: TL0, TH0, TL1, TH1 and SBUF Registers

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.

Fig. 4.32 b: Accumulator Register


B Register:
• It is 8-bit register.
• It is bit and byte-addressable register.
• You can access 1-bit or all 8-bits by a physical address F0 H. Suppose to access a bit 1, we
have to use F1 H.
• The B register is only used for multiplication and division arithmetic operations.

Fig. 4.33: B Register

PSW (Program Status Word) Register:


The PSW (Program Status Word) Register is also called as Flag Register. It is one of the important
SFRs in 8051 microcontrollers. It is also an 8-bit register. It consists of Flag Bits or status bits that
reflect the current state of the CPU.
PSW flag register is both bit and byte addressable. The physical address of PSW starts from D0H.
The individual bits are accessed by using bit address D1, D2 … D7. The two unused bits are user-
defined flags. Four of the flags are called conditional flags, which means that they indicate a
condition which results after an instruction is executed. These four are CY (Carry), AC (auxiliary
carry), P (parity), and OV (overflow).

Fig. 4.34: PSW Register

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.

Fig. 4.35: TMOD Register


TMOD Register:
• Gate: when Gate control is set. Timer/counter is enable only while the INTx pin is high
and the TRx control pin is set. When it is cleared, the timer is enabled whenever the TRx
control bit is set.
• C/T: The Timer or counter selection. When cleared for timer operation (input from
internal system clock). Set for counter operation (input from Tx input pin).
• Mode selects bits of TMODE register: The M1 and M0 are mode select bits, which are
used to select the timer operations. There are four modes to operate the timers.

Mode Selection Bits of TMOD 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.

Fig. 4.36: TCON Register

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.

Fig. 4.37: SP Register

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

PCON (Power Control):


The PCON or Power Control register, as the name suggests is used to control the 8051
Microcontroller’s Power Modes and is located at 87H of the SFR Memory Space. Using two bits in
the PCON Register, the microcontroller can be set to Idle Mode or Power down Mode.

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.

DP (Data pointer register):


• The data pointer is a 16-bit register used to hold the 16-bit address of data memory. This
can also be used as two 8-bit register namely DPH and DPL. Data Pointer can be used as
a single 16-bit register (as DPTR) or two 8-bit registers (as DPL and DPH).
• The 8-bit data pointers are used for accessing internal RAM and SFR. The 16-bit data
pointer is used for accessing external data memory.
• The contents of data pointer are programmable using instructions. It is used by
the 8051 to access external memory using the address indicated by DPTR.

Fig. 4.38: Data Pointer Register

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.

Fig. 4.39: Input output Port Register

Page | 35

You might also like