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

Microcontroller 8051 - Introduction

The document summarizes the architecture and features of the 8051 microcontroller. It was designed by Intel in 1981 and has 4KB of ROM, 128 bytes of RAM, and 4 parallel 8-bit I/O ports. It uses a 40-pin DIP package and connects devices via an 8-bit data bus and 16-bit address bus. The 4 I/O ports each have 8 pins that can be configured as inputs or outputs, allowing connection to 32 peripheral devices. The document describes the functions of each pin and port, as well as features like interrupts, timers, and special function registers used to control the microcontroller's operation.

Uploaded by

E.Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Microcontroller 8051 - Introduction

The document summarizes the architecture and features of the 8051 microcontroller. It was designed by Intel in 1981 and has 4KB of ROM, 128 bytes of RAM, and 4 parallel 8-bit I/O ports. It uses a 40-pin DIP package and connects devices via an 8-bit data bus and 16-bit address bus. The 4 I/O ports each have 8 pins that can be configured as inputs or outputs, allowing connection to 32 peripheral devices. The document describes the functions of each pin and port, as well as features like interrupts, timers, and special function registers used to control the microcontroller's operation.

Uploaded by

E.Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

UNIT 4: MICROCONTROLLER
1. Architecture of 8051
8051 microcontroller is designed by Intel in 1981. It is an 8-bit microcontroller. It is built with 40 pins
DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2 16-bit timers. It
consists of are four parallel 8-bit ports, which are programmable as well as addressable as per the
requirement. An on-chip crystal oscillator is integrated in the microcontroller having crystal frequency of
12 MHz.
In the following diagram, the system bus connects all the support devices to the CPU. The system bus
consists of an 8-bit data bus, a 16-bit address bus and bus control signals. All other devices like program
memory, ports, data memory, serial interface, interrupt control, timers, and the CPU are all interfaced
together through the system bus.

The pin diagram of 8051 microcontroller looks as follows :


 Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other functions. It is
internally pulled up, bi-directional I/O port.
 Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
 Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like interrupts,
timer input, control signals, serial communication signals RxD and TxD, etc.
 Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system clock.
 Pin 20 − This pin provides the power supply to the circuit.
 Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order address bus
signals are also multiplexed using this port.
 Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a signal
from the external program memory.
 Pin 30 − This is EA pin which stands for External Access input. It is used to enable/disable the
external memory interfacing.
 Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
 Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order address and
data bus signals are multiplexed using this port.
 Pin 40 − This pin is used to provide power supply to the circuit.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 1 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence,
total 32 input/output pins allow the microcontroller to be connected with the peripheral devices.
 Pin configuration, i.e. the pin can be configured as 1 for input and 0 for output as per the logic
state.
o Input/Output (I/O) pin − All the circuits within the microcontroller must be connected
to one of its pins except P0 port because it does not have pull-up resistors built-in.
o Input pin − Logic 1 is applied to a bit of the P register. The output FE transistor is
turned off and the other pin remains connected to the power supply voltage over a pull-
up resistor of high resistance.
 Port 0 − The P0 (zero) port is characterized by two functions −
o When the external memory is used then the lower address byte (addresses A0A7) is
applied on it, else all bits of this port are configured as input/output.
o When P0 port is configured as an output then other ports consisting of pins with built-in
pull-up resistor connected by its end to 5V power supply, the pins of this port have this
resistor left out.
Input Configuration
If any pin of this port is configured as an input, then it acts as if it “floats”, i.e. the input has unlimited
input resistance and in-determined potential.
Output Configuration
When the pin is configured as an output, then it acts as an “open drain”. By applying logic 0 to a port bit,
the appropriate pin will be connected to ground (0V), and applying logic 1, the external output will keep
on “floating”.
In order to apply logic 1 (5V) on this output pin, it is necessary to build an external pullup resistor.
Port 1: P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this port can be
configured as general I/O only. It has a built-in pull-up resistor and is completely compatible with TTL
circuits.
Port 2: P2 is similar to P0 when the external memory is used. Pins of this port occupy addresses intended
for the external memory chip. This port can be used for higher address byte with addresses A8-A15.
When no memory is added then this port can be used as a general input/output port similar to Port 1.
Port 3: In this port, functions are similar to other ports except that the logic 1 must be applied to
appropriate bit of the P3 register.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 2 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
Pins Current Limitations
 When pins are configured as an output (i.e. logic 0), then the single port pins can receive a
current of 10mA.
 When these pins are configured as inputs (i.e. logic 1), then built-in pull-up resistors provide very
weak current, but can activate up to 4 TTL inputs of LS series.
 If all 8 bits of a port are active, then the total current must be limited to 15mA (port P0: 26mA).
 If all ports (32 bits) are active, then the total maximum current must be limited to 71mA.
Interrupts are the events that temporarily suspend the main program, pass the control to the external
sources and execute their task. It then passes the control to the main program where it had left off.
8051 has 5 interrupt signals, i.e. INT0, TFO, INT1, TF1, RI/TI. Each interrupt can be enabled or
disabled by setting bits of the IE register and the whole interrupt system can be disabled by clearing the
EA bit of the same register.
IE (Interrupt Enable) Register
This register is responsible for enabling and disabling the interrupt. EA register is set to one for enabling
interrupts and set to 0 for disabling the interrupts. Its bit sequence and their meanings are shown in the
following figure.

It disables all interrupts. When EA = 0 no interrupt will be acknowledged and EA = 1


EA IE.7
enables the interrupt individually.

- IE.6 Reserved for future use.

- IE.5 Reserved for future use.

ES IE.4 Enables/disables serial port interrupt.

ET1 IE.3 Enables/disables timer1 overflow interrupt.

EX1 IE.2 Enables/disables external interrupt1.

ET0 IE.1 Enables/disables timer0 overflow interrupt.

EX0 IE.0 Enables/disables external interrupt0.

IP (Interrupt Priority) Register


We can change the priority levels of the interrupts by changing the corresponding bit in the Interrupt
Priority (IP) register as shown in the following figure.
 A low priority interrupt can only be interrupted by the high priority interrupt, but not interrupted
by another low priority interrupt.
 If two interrupts of different priority levels are received simultaneously, the request of higher
priority level is served.
 If the requests of the same priority levels are received simultaneously, then the internal polling
sequence determines which request is to be serviced.

- IP.6 Reserved for future use.

- IP.5 Reserved for future use.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 3 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
PS IP.4 It defines the serial port interrupt priority level.

PT1 IP.3 It defines the timer interrupt of 1 priority.

PX1 IP.2 It defines the external interrupt priority level.

PT0 IP.1 It defines the timer0 interrupt priority level.

PX0 IP.0 It defines the external interrupt of 0 priority level.


TCON Register
TCON register specifies the type of external interrupt to the microcontroller.

2. Special Function Registers (SFRs)


The 8051 Microcontroller Special Function Registers act as a control table that monitor and control the
operation of the 8051 Microcontroller. If you observe in Internal RAM Structure, the Address Space from
80H to FFH is allocated to SFRs.

Out of these 128 Memory Locations (80H to FFH), there are only 21 locations that are actually assigned
to SFRs. Each SFR has one Byte Address and also a unique name which specifies its purpose.

Since the SFRs are a part of the Internal RAM Structure, you can access SFRs as if you access the
Internal RAM. The main difference is the address space: first 128 Bytes (00H to 7FH) is for regular
Internal RAM and next 128 Bytes (80H to FFH) is for SFRs.

TIP: As only 21 of the possible 128 SFR memory locations are assigned, it is recommended that the
remaining registers or memory locations are not accessed during programming.

List of 8051 Microcontroller Special Function Registers

 A or ACC
 B
 DPL
 DPH
 IE
 IP
 P0
 P1
 P2
 P3
 PCON
 PSW
 SCON
 SBUF
 SP
 TMOD
 TCON
 TL0
 TH0
 TL1
 TH1

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 4 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
Categories of 8051 Microcontroller Special Function Registers

All the 21 8051 Microcontroller Special Function Registers (SFRs) along with their functions and Internal
RAM Address is given in the following table.

There are many ways to categorize these 21 Special Function Registers but I find the following way as an
appropriate one. The 21 Special Function Registers of 8051 Microcontroller are categorized in to seven
groups. They are:

Math or CPU Registers: A and B

Status Register: PSW (Program Status Word)

Pointer Registers: DPTR (Data Pointer – DPL, DPH) and SP (Stack Pointer)

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

Peripheral Data Registers: TL0, TH0, TL1, TH1 and SBUF

CPU or Math Registers

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 5 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
A or Accumulator (ACC)

The Accumulator or Register A is the most important and most used 8051 Microcontroller SFRs. The
Register A is located at the address E0H in the SFR memory space. The Accumulator is used to hold the
data for almost all the ALU Operations.

Some of the operations where the Accumulator is used are:

 Arithmetic Operations like Addition, Subtraction, Multiplication etc.


 Logical Operations like AND, OR, NOT etc.
 Data Transfer Operations (between 8051 and External Memory)

The name “Accumulator” came from the fact this register is used to accumulate (or store) the result of all
Arithmetic and most of the Logical Operations.

B (Register B)

The B Register is used along with the ACC in Multiplication and Division operations. These two
operations are performed on data that are stored only in Registers A and B. During Multiplication
Operation, one of the operand (multiplier or multiplicand) is stores in B Register and also the higher byte
of the result.

In case of Division Operation, the B Register holds the divisor and also the remainder of the result. It can
also be used as a General Purpose Register for normal operations and is often used as an Auxiliary
Register by Programmers to store temporary results.

Register B is located at the address F0H of the SFR Address Space.

Program Status Word (PSW)

The PSW or Program Status Word Register is also called as Flag Register and is one of the important
SFRs. The PSW Register consists of Flag Bits, which help the programmer in checking the condition of
the result and also make decisions.

Flags are 1-bit storage elements that store and indicate the nature of the result that is generated by
execution of certain instructions. The following image shows the contents of the PSW Register.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 6 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

The following table describes the function of each flag.

Pointer Registers
Data Pointer (DPTR – DPL and DPH)

The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data Pointer Low) and
DPH (Data Pointer High) SFRs. The Data Pointer can be used as a single 16-bit register (as DPTR) or
two 8-bit registers (as DPL and DPH).

DPTR doesn’t have a physical Memory Address but the DPL (Lower Byte of DPTR) and DPH (Higher
Byte of DPTR) have separate addresses in the SFR Memory Space. DPL = 82H and DPH = 83H.

The DPTR Register is used by the programmer addressing external memory (Program – ROM or Data –
RAM).

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 7 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

Stack Pointer (SP)

SP or Stack Pointer points out to the top of the Stack and it indicates the next data to be accessed. Stack
Pointer can be accesses using PUSH, POP, CALL and RET Instructions. The Stack Pointer is an 8-bit
register and upon reset, the Stack Pointer is initialized with 07H.

When writing a new data byte into the stack, the SP (Stack Pointer) is automatically incremented by 1 and
the new data is written at an address SP+1. When reading data from stack, the data is retrieved from the
Address in SP and after that the SP is decremented by 1 (SP-1).

I/O Port Registers (P0, P1, P2 and P3)


The 8051 Microcontroller four Ports which can be used as Input and/or Output. These four ports are P0,
P1, P2 and P3. Each Port has a corresponding register with same names (the Port Registers are also P0,
P1, P2 and P3). The addresses of the Port Registers are as follows: P0 – 80H, P1 – 90H, P2 – A0H and P2
– B0H.

Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller. All these Port
Registers are both Bit Addressable and Byte Addressable. Writing 1 or 0 on a Port Register Bit will
reflect as an appropriate voltage (5V and 0V) on the corresponding Pin.

If a Port Bit is SET (declared as 1), the corresponding Port Pin will be configured as Input and similarly if
a Port Bit is CLEARED (declared as 0), the corresponding Port Pin is configured as Output. Upon reset,
all the Port Bits are SET (1) and hence, all the Port Pins are configured as Inputs.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 8 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

Peripheral Control Registers


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 and Power Down Mode.

During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU) but it is given to
other peripherals like Timer, Serial, Interrupts, etc. In order to terminate the Idle Mode, you have to use
an Interrupt or Hardware Reset.

In the Power Down Mode, the oscillator will be stopped and the power will be reduced to 2V. To
terminate the Power Down Mode, you have to use the Hardware Reset.

Apart from these two, the PCON Register can also be used for few additional purposes. The SMOD Bit in
the PCON Register is used to control the Baud Rate of the Serial Port.

There are two general purpose Flag Bits in the PCON Register, which can be used by the programmer
during execution.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 9 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

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, you can control the Operation Modes of the Serial Port, Baud Rate of
the Serial Port and Send or Receive Data using Serial Port.

SCON Register also consists of bits that are automatically SET when a byte of data is transmitted or
received.

TCON (Timer Control)

Timer Control or TCON Register 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.

TMOD (Timer Mode)

The TMOD or Timer Mode register or SFR is used to set the Operating Modes of the Timers T0 and T1.
The lower four bits are used to configure Timer0 and the higher four bits are used to configure Timer1.

The Gatex bit is used to operate the Timerx with respect to the INTx pin or regardless of the INTx pin.

GATE1 = 1 ==> Timer1 is operated only if INT1 is SET.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 10 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
GATE1 = 0 ==> Timer1 is operates irrespective of INT1 pin.

GATE0 = 1 ==> Timer0 is operated only if INT0 is SET.

GATE0 = 0 ==> Timer0 is operates irrespective of INT0 pin.

The C/Tx bit is used selects the source of pulses for the Timer to count.

C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)

C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)

C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)

C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)

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.

Peripheral Data Registers


SBUF (Serial Data Buffer)

The Serial Buffer or SBUF register is used to hold the serial data while transmission or reception.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 11 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

TL0/TH0 (Timer 0 Low/High)

The Timer 0 consists of two SFRs: TL0 and TH0. The TL0 is the lower byte and the TH0 is the higher
byte and together they form a 16-bit Timer0 Register.

TL1/TH1 (Timer 1 Low/High)

The TL1 and TH1 are the lower and higher bytes of the Timer 0.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 12 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

3. Microcontroller Instruction Set


Writing a Program for any Microcontroller consists of giving commands to the Microcontroller in a
particular order in which they must be executed in order to perform a specific task. The commands to the
Microcontroller are known as a Microcontroller’s Instruction Set.

Just as our sentences are made of words, a Microcontroller’s (for that matter, any computer) program is
made of Instructions. Instructions written in a program tell the Microcontroller which operation to carry
out.

An Instruction Set is unique to a family of computers. This tutorial introduces the 8051 Microcontroller
Instruction Set also called as the MCS-51 Instruction Set.

As the 8051 family of Microcontrollers are 8-bit processors, the 8051 Microcontroller Instruction Set is
optimized for 8-bit control applications. As a typical 8-bit processor, the 8051 Microcontroller
instructions have 8-bit Opcodes. As a result, the 8051 Microcontroller instruction set can have up to 2 8 =
256 Instructions.

The following table shows the 8051 Instruction Groups and Instructions in each group. There are 49
Instruction Mnemonics in the 8051 Microcontroller Instruction Set and these 49 Mnemonics are divided
into five groups.

DATA PROGRAM
ARITHMETIC LOGICAL BOOLEAN
TRANSFER BRANCHING

MOV ADD ANL CLR LJMP

MOVC ADDC ORL SETB AJMP

MOVX SUBB XRL MOV SJMP

PUSH INC CLR JC JZ

POP DEC CPL JNC JNZ

XCH MUL RL JB CJNE

XCHD DIV RLC JNB DJNZ

DA A RR JBC NOP

RRC ANL LCALL

SWAP ORL ACALL

CPL RET

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 13 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
RETI

JMP

An 8051 Instruction consists of an Opcode (short of Operation – Code) followed by Operand(s) of size
Zero Byte, One Byte or Two Bytes.

The Op-Code part of the instruction contains the Mnemonic, which specifies the type of operation to be
performed. All Mnemonics or the Opcode part of the instruction are of One Byte size.

Coming to the Operand part of the instruction, it defines the data being processed by the instructions. The
operand can be any of the following:

 No Operand
 Data value
 I/O Port
 Memory Location
 CPU register

There can multiple operands and the format of instruction is as follows:

MNEMONIC DESTINATION OPERAND, SOURCE OPERAND

A simple instruction consists of just the opcode. Other instructions may include one or more operands.
Instruction can be one-byte instruction, which contains only opcode, or two-byte instructions, where the
second byte is the operand or three byte instructions, where the operand makes up the second and third
byte.

Based on the operation they perform, all the instructions in the 8051 Microcontroller Instruction Set are
divided into five groups. They are:

 Data Transfer Instructions


 Arithmetic Instructions
 Logical Instructions
 Boolean or Bit Manipulation Instructions
 Program Branching Instructions

We will now see about these instructions briefly.

Data Transfer Instructions

The Data Transfer Instructions are associated with transfer with data between registers or external
program memory or external data memory. The Mnemonics associated with Data Transfer are given
below.

 MOV
 MOVC
 MOVX
 PUSH

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 14 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
 POP
 XCH
 XCHD

Arithmetic Instructions

Using Arithmetic Instructions, you can perform addition, subtraction, multiplication and division. The
arithmetic instructions also include increment by one, decrement by one and a special instruction called
Decimal Adjust Accumulator.

The Mnemonics associated with the Arithmetic Instructions of the 8051 Microcontroller Instruction Set
are:

 ADD
 ADDC
 SUBB
 INC
 DEC
 MUL
 DIV
 DA A

The arithmetic instructions has no knowledge about the data format i.e. signed, unsigned, ASCII, BCD,
etc. Also, the operations performed by the arithmetic instructions affect flags like carry, overflow, zero,
etc. in the PSW Register.

All the possible Mnemonics associated with Arithmetic Instructions are mentioned in the following table.

Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical operations like AND,
OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on Bytes of data on a bit-by-
bit basis.

Mnemonics associated with Logical Instructions are as follows:

 ANL
 ORL
 XRL
 CLR
 CPL
 RL
 RLC
 RR
 RRC
 SWAP

Boolean or Bit Manipulation Instructions

As the name suggests, Boolean or Bit Manipulation Instructions will deal with bit variables. We know
that there is a special bit-addressable area in the RAM and some of the Special Function Registers (SFRs)
are also bit addressable.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 15 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
The Mnemonics corresponding to the Boolean or Bit Manipulation instructions are:

 CLR
 SETB
 MOV
 JC
 JNC
 JB
 JNB
 JBC
 ANL
 ORL
 CPL

These instructions can perform set, clear, and, or, complement etc. at bit level.

Program Branching Instructions

The last group of instructions in the 8051 Microcontroller Instruction Set are the Program Branching
Instructions. These instructions control the flow of program logic. The mnemonics of the Program
Branching Instructions are as follows.

 LJMP
 AJMP
 SJMP
 JZ
 JNZ
 CJNE
 DJNZ
 NOP
 LCALL
 ACALL
 RET
 RETI
 JMP

All these instructions, except the NOP (No Operation) affect the Program Counter (PC) in one way or
other. Some of these instructions has decision making capability before transferring control to other part
of the program.

In this tutorial, we have seen the introduction to the 8051 Microcontroller Instruction Set, Addressing
Modes in 8051 Microcontroller and different types of instructions in the Instruction Set of the 8051
Microcontroller.

4. 8051 Addressing Modes


An Addressing Mode is a way to locate a target Data, which is also called as Operand. The 8051 Family
of Microcontrollers allows five types of Addressing Modes for addressing the Operands. They are:

 Immediate Addressing
 Register Addressing
 Direct Addressing

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 16 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
 Register – Indirect Addressing
 Indexed Addressing

Immediate Addressing

In Immediate Addressing mode, the operand, which follows the Opcode, is a constant data of either 8 or
16 bits. The name Immediate Addressing came from the fact that the constant data to be stored in the
memory immediately follows the Opcode.

The constant value to be stored is specified in the instruction itself rather than taking from a register. The
destination register to which the constant data must be copied should be the same size as the operand
mentioned in the instruction.

Example: MOV A, #030H

Here, the Accumulator is loaded with 30 (hexadecimal). The # in the operand indicates that it is a data
and not the address of a Register.

Immediate Addressing is very fast as the data to be loaded is given in the instruction itself.

Register Addressing

In the 8051 Microcontroller Memory Organization Tutorial, we have seen the organization of RAM and
four banks of Working Registers with eight Registers in each bank.

In Register Addressing mode, one of the eight registers (R0 – R7) is specified as Operand in the
Instruction.

It is important to select the appropriate Bank with the help of PSW Register. Let us see a example of
Register Addressing assuming that Bank0 is selected.

Example: MOV A, R5

Here, the 8-bit content of the Register R5 of Bank0 is moved to the Accumulator.

Direct Addressing

In Direct Addressing Mode, the address of the data is specified as the Operand in the instruction. Using
Direct Addressing Mode, we can access any register or on-chip variable. This includes general purpose
RAM, SFRs, I/O Ports, Control registers.

Example: MOV A, 47H

Here, the data in the RAM location 47H is moved to the Accumulator.

Register Indirect Addressing

In the Indirect Addressing Mode or Register Indirect Addressing Mode, the address of the Operand is
specified as the content of a Register. This will be clearer with an example.

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 17 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
Example: MOV A, @R1

The @ symbol indicates that the addressing mode is indirect. If the contents of R1 is 56H, for example,
then the operand is in the internal RAM location 56H. If the contents of the RAM location 56H is 24H,
then 24H is moved into accumulator.

Only R0 and R1 are allowed in Indirect Addressing Mode. These register in the indirect addressing mode
are called as Pointer registers.

Indexed Addressing Mode

With Indexed Addressing Mode, the effective address of the Operand is the sum of a base register and an
offset register. The Base Register can be either Data Pointer (DPTR) or Program Counter (PC) while the
Offset register is the Accumulator (A).

In Indexed Addressing Mode, only MOVC and JMP instructions can be used. Indexed Addressing Mode
is useful when retrieving data from look-up tables.

Example: MOVC A, @A+DPTR

Here, the address for the operand is the sum of contents of DPTR and Accumulator.

5. Assembly language programming

The Elements of an Assembly Language Programming:

 Assembler Directives
 Instruction Set
 Addressing Modes
Assembler Directives:
The assembling directives give the directions to the CPU. The 8051 microcontroller consists of various
kinds of assembly directives to give the direction to the control unit. The most useful directives are 8051
programming, such as:
 ORG
 DB
 EQU
 END
ORG(origin): This directive indicates the start of the program. This is used to set the register address
during assembly. For example; ORG 0000h tells the compiler all subsequent code starting at address
0000h.
Syntax: ORG 0000h

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 18 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
DB(define byte): The define byte is used to allow a string of bytes. For example, print the “EDGEFX”
wherein each character is taken by the address and finally prints the “string” by the DB directly with
double quotes.
Syntax:
ORG 0000h

MOV a, #00h
————-
————-
DB”EDGEFX”

EQU (equivalent): The equivalent directive is used to equate address of the variable.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END:The END directive is used to indicate the end of the program.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END
Addressing Modes:

The way of accessing data is called addressing mode. The CPU can access the data in different ways by
using addressing modes. The 8051 microcontroller consists of five addressing modes such as:
 Immediate Addressing Mode
 Register Addressing Mode
 Direct Addressing Mode
 Indirect Addressing Mode
 Base Index Addressing Mode
Immediate Addressing Mode:
In this addressing mode, the source must be a value that can be followed by the ‘#’ and destination must
be SFR registers, general purpose registers and address. It is used for immediately storing the value in the
memory registers.
Syntax:
MOV A, #20h //A is an accumulator register, 20 is stored in the A//
MOV R0,#15 // R0 is a general purpose register; 15 is stored in the R0 register//
MOV P0, #07h //P0 is a SFR register;07 is stored in the P0//
MOV 20h,#05h //20h is the address of the register; 05 stored in the 20h//
Ex:

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 19 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
MOV R0, #1
MOV R0, #20 //R0 <—R0[15]+20, the final value is stored in R0//

Register Addressing Mode:


In this addressing mode, the source and destination must be a register, but not general purpose registers.
So the data is not moved within the general purpose bank registers.
Syntax:
MOV A, B; // A is a SFR register, B is a general purpose register//
MOV R0, R1 //Invalid instruction, GPR to GPR not possible//

EX:
MOV R0, #02h
MOV A, #30h
ADD R0, A //R0<—R0+A, the final value is stored in the R0 register//

Direct Addressing Mode


In this addressing mode, the source or destination (or both source and destination) must be an address, but
not value.

Syntax:
MOV A,20h // 20h is an address; A is a register//
MOV 00h, 07h // both are addressed of the GPS registers//
Ex:
MOV 07h,#01h
MOV A, #08h
ADD A,07h //A<—A+07h the final value is stored in A//

Indirect Addressing Mode:

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 20 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
In this addressing mode, the source or destination (or destination or source) must be a indirect address,
but not a value. This addressing mode supports the pointer concept. The pointer is a variable that is used
to store the address of the other variable. This pointer concept is only used for R0 and R1 registers.
Syntax:
MOVR0, #01h //01 value is stored in the R0 register, R0 address is 08h//
MOV R1, #08h//R1 is the pointer variable that stores address (08h) of R0 //
MOV 20h,@R1 //01 value is stored in the 20h address of the GP register//

The arithmetic instructions perform the basic operations such as:

 Addition
 Multiplication
 Subtraction
 Division

Addition:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
Add A, 00H // addA value with R0 value and stores the result inA//
END

Multiplication:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
MUL A, 03H // Multiplied result is stored in the Accumulator A //
END

Subtraction:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #05H // move the value 5 to accumulator A//
SUBB A, 03H // Result value is stored in the Accumulator A //
END

Division:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #15H // move the value 5 to accumulator A//
DIV A, 03H // final value is stored in the Accumulator A //
END

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 21 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023

Conditional Instructions

The CPU executes the instructions based on the condition by checking the single bit status or byte status.
The 8051 microcontroller consists of various conditional instructions such as:
 JB —>Jump below
 JNB —> Jump if not below
 JC —> Jump if Carry
 JNC —>Jump if not Carry
 JZ —>Jump if Zero
 JNZ —> Jump if not Zero

Call and Jump Instructions: The call and jump instructions are used to avoid the code replication of the
program. When some specific code used more than once in different places in the program, if we
mention specific name to code then we could use that name anywhere in the program without entering a
code for every time. This reduces the complexity of the program. The 8051 programming consists of call
and jump instructions such as LCALL, SJMP.

Shifting Operators

The shift operators are used for sending and receiving the data efficiently. The
8051 microcontroller consist four shift operators:
 RR —> Rotate Right
 RRC —>Rotate Right through carry
 RL —> Rotate Left
 RLC —>Rotate Left through carry

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 22 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
Rotate Right (RR):
In this shifting operation, the MSB becomes LSB and all bits shift towards right side bit-by-bit, serially.

Syntax:
MOV A, #25h
RR A

Rotate Left (RL):


In this shifting operation, the MSB becomes LSB and all bits shift towards Left side bit-by-bit, serially.

Syntax:
MOV A, #25h
RL A

RRC Rotate Right through Carry:


In this shifting operation, the LSB moves to carry and the carry becomes MSB, and all the bits are shift
towards right side bit by bit position.

Syntax:
MOV A, #27h
RRC A

RLC Rotate Left through Carry:


In this shifting operation, the MSB moves to carry and the carry becomes LSB and all the bits shift
towards left side in a bit-by-bit position.

Syntax:
MOV A, #27h
RLC A

Basic Embedded C Programs:


The microcontroller programming differs for each type of operating system. There are many operating
systems such as Linux, Windows, RTOS and so on. However, RTOS has several advantages for
embedded system development. Some of the Assembly level programming examples are given below.
LED blinking using with 8051 microcontroller:
 Number Displaying on 7-segment display using 8051 microcontroller

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 23 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
 Timer/Counter calculations and program using 8051 microcontroller
 Serial Communication calculations and program using 8051 microcontroller

LED programs with 8051 Microcontrller


1. WAP to toggle the PORT1 LEDs
ORG 0000H
TOGLE: MOV P1, #01 //move 00000001 to the p1 register//
CALL DELAY //execute the delay//
MOV A, P1 //move p1 value to the accumulator//
CPL A //complement A value //
MOV P1, A //move 11111110 to the port1 register//
CALL DELAY //execute the delay//
SJMP TOGLE
DELAY: MOV R5, #10H //load register R5 with 10//
TWO: MOV R6, #200 //load register R6 with 200//
ONE: MOV R7, #200 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, ONE //decrement R7 till it is zero//
DJNZ R5, TWO //decrement R7 till it is zero//
RET //go back to the main program //
END

Timer/Counter Calculations and Program using 8051 Microcontroller:


The delay is the one of the important factors in the application software development. The timers and
counters are hardware components of the microcontroller, that are used in many applications to provide
the accurate time delay with count pulses. Both the tasks are implemented by the software technique.

1. WAP to calculate the 500us time delay.


MOV TMOD, #10H //select the timer mode by the registers//
MOV TH1, #0FEH // store the delay time in higher bit//
MOV TL1, #32H // store the delay time in low bit//
JNB TF1, $ //decrement the value of the timer till it is zero//
CLR TF1 //clear the timer flag bit//
CLR TR1 //OFF the timer//

2. WAP to toggle the LEDs withthe 5 sec time delay


ORG 0000H
RETURN: MOV PO, #00H
ACALL DELAY
MOV P0, #0FFH
ACALL DELAY
SJUMP RETURN
DELAY: MOV R5, #50H //load register R5 with 50//
DELAY1: MOV R6, #200 //load register R6 with 200//
DELAY2: MOV R7, #229 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, DELAY2//decrement R6 till it is zero//
DJNZ R5, DELAY1//decrement R5 till it is zero//
RET //go back to the main program //
END

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 24 of 25
EC 1601 – Microprocessors and Microcontrollers Department of CSE 2022-2023
3. WAP to count the 250 pulses using mode0 count0
Syntax:
ORG 0000H
MOV TMOD, #50H //select the counter//
MOV TH0, #15 //move the counting pulses higher bit//
MOV TH1, #9FH //move the counting pulses, lower bit//
SET TR0 //ON the timer//
JNB $ //decrement the count value till zero//
CLR TF0 //clear the counter, flag bit//
CLR TR0 //stop the timer//
END

Serial Communication Programming Using 8051 Microcontroller:


Serial communication is commonly used for transmitting and receiving the data. The
8051 microcontroller consist of UART/USART serial communication and the signals are transmitted and
received by Tx and Rx pins. The UART communication transfers the data bit-by-bit serially. The UART
is a half-duplex protocol that transfers and receives the data, but not at the same time.

1. WAP to transmit the characters to the Hyper Terminal


MOV SCON, #50H //set the serial communication//
MOV TMOD, #20H //select the timer mode//
MOV TH1, #-3 //set the baud rate//
SET TR1 //ON the timer//
MOV SBUF, #’S’ //transmit S to the serial window //
JNB TI, $ //decrement value of the timer till it is zero//
CLR RI // clear receive interrupt //
CLR TR1 //clear timer//

St. Joseph’s College of Engineering & St. Joseph’s Institute of Technology Page 25 of 25

You might also like