MCS U4 Notes
MCS U4 Notes
Unit-4
Lecture Notes
8051 Microcontroller
8051 MICROCONTROLLER
1. ALU
All arithmetic and logical functions are carried out by the ALU. Addition, subtraction with carry,
and multiplication come under arithmetic operations. Logical AND, OR and exclusive OR (XOR)
come under logical operations.
lOMoAR cPSD| 9636936
3. Registers
Registers are usually known as data storage devices. 8051 microcontroller has 2 registers, namely
Register A and Register B. Register A serves as an accumulator while Register B functions as a
general purpose register. These registers are used to store the output of mathematical and logical
instructions.
The operations of addition, subtraction, multiplication and division are carried out by Register A.
Register B is usually unused and comes into picture only when multiplication and division
functions are carried out by Register A. Register A also involved in data transfers between the
microcontroller and external memory.
lOMoAR cPSD| 9636936
8051 microcontroller also has 7 Special Function Registers (SFRs). They are:
1.Serial Port Data Buffer (SBUF)
2.Timer/Counter Control (TCON)
3.Timer/Counter Mode Control (TMOD)
4.Serial Port Control (SCON)
5.Power Control (PCON)
6.Interrupt Priority (IP)
7.Interrupt Enable Control (IE)
7. Interrupt Control
An event which is used to suspend or halt the normal program execution for a temporary
period of time in order to serve the request of another program or hardware device is called an
lOMoAR cPSD| 9636936
interrupt. An interrupt can either be an internal or external event which suspends the
microcontroller for a while and thereby obstructs the sequential flow of a program.
There are two ways of giving interrupts to a microcontroller – one is by sending software
instructions and the other is by sending hardware signals. The interrupt mechanism keeps the
normal program execution in a "put on hold" mode and executes a subroutine program and after
the subroutine is executed, it gets back to its normal program execution. This subroutine program
is also called an interrupt handler. A subroutine is executed when a certain event occurs.
The SBUF register has 2 parts – one for storing the data to be transmitted and another for
receiving data from outer sources. The first function is done using TXD pin and the second
function is done using RXD pin.
Generally, the instruction of the result of a program is stored in a single bit register called a 'flag'.
The are7 flags in the PSW of 8051. Among these 7 flags, 4 are math flags and 3 are general
purpose or user flags.
The 4 Math flags are:
• Carry (c)
• Auxiliary carry (AC)
• Overflow (OV)
• Parity (P)
The 3 General purpose flags or User flags are:
i) FO, ii) GFO and iii) GF 1
lOMoAR cPSD| 9636936
● 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.
lOMoAR cPSD| 9636936
8051 Microcontroller has both Internal ROM and Internal RAM. If the internal memory is
inadequate, you can add external memory using suitable circuits.
In 8051 Microcontroller, the code or instructions to be executed are stored in the Program
Memory, which is also called as the ROM of the Microcontroller. The original 8051
Microcontroller by Intel has 4KB of internal ROM.
In case of 4KB of Internal ROM, the address space is 0000H to 0FFFH. If the address space
i.e. the program addresses exceed this value, then the CPU will automatically fetch the code from
the external Program Memory.For this, the External Access Pin (EA Pin) must be pulled HIGH
i.e. when the EA Pin is high, the CPU first fetches instructions from the Internal Program Memory
in the address range of 0000H to 0FFFFH and if the memory addresses exceed the limit, then the
instructions are fetched from the external ROM in the address range of 1000H to FFFFH.
There is another way to fetch the instructions: ignore the Internal ROM and fetch all the
instructions only from the External Program Memory (External ROM). For this scenario, the EA
Pin must be connected to GND. In this case, the memory addresses of the external ROM will be
from 0000H to FFFFH.
lOMoAR cPSD| 9636936
The Data Memory or RAM of the 8051 Microcontroller stores temporary data and intermediate
results that are generated and used during the normal operation of the microcontroller. Original
Intel’s 8051 Microcontroller had 128B of internal RAM.
But almost all modern variants of 8051 Microcontroller have 256B of RAM. In this 256B, the first
128B i.e. memory addresses from 00H to 7FH is divided in to Working Registers (organized as
Register Banks), Bit – Addressable Area and General Purpose RAM (also known as Scratchpad
area).
In the first 128B of RAM (from 00H to 7FH), the first 32B i.e. memory from addresses 00H to
1FH consists of 32 Working Registers that are organized as four banks with 8 Registers in each
Bank.
The 4 banks are named as Bank0, Bank1, Bank2 and Bank3. Each Bank consists of 8 registers
named as R0 – R7. Each Register can be addressed in two ways: either by name or by address.
To address the register by name, first the corresponding Bank must be selected. In order to select
the bank, we have to use the RS0 and RS1 bits of the Program Status Word (PSW) Register (RS0
and RS1 are 3rd and 4th bits in the PSW Register).
When addressing the Register using its address i.e. 12H for example, the corresponding Bank may
or may not be selected. (12H corresponds to R2 in Bank2).
lOMoAR cPSD| 9636936
The next 16B of the RAM i.e. from 20H to 2FH are Bit – Addressable memory locations. There
are totally 128 bits that can be addressed individually using 00H to 7FH or the entire byte can be
addressed as 20H to 2FH.
For example 32H is the bit 2 of the internal RAM location 26H.
The final 80B of the internal RAM i.e. addresses from 30H to 7FH, is the general purpose RAM
area which are byte addressable.
The upper 128B of the RAM i.e. memory addresses from 80H to FFH is allocated for Special
Function Registers (SFRs). SFRs control specific functions of the 8051 Microcontroller. Some of
the SFRs are I/O Port Registers (P0, P1, P2 and P3), PSW (Program Status Word), A
(Accumulator), IE (Interrupt Enable), PCON (Power Control), etc.
SFRs Memory addresses are only direct addressable. Even though some of the addresses between
80H and FFH are not assigned to any SFR, they cannot be used as additional RAM area.
In some microcontrollers, there is an additional 128B of RAM, which share the memory address
with SFRs i.e. 80H to FFH. But, this additional RAM block is only accessed by indirect
addressing.
lOMoAR cPSD| 9636936
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.
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.
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).
lOMoAR cPSD| 9636936
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).
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.
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.
lOMoAR cPSD| 9636936
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.
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.
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)
TxM0 TxM1 Mode Description
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)
lOMoAR cPSD| 9636936
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.
MOVA, #0AFH;
MOVR3, #45H;
MOVDPTR, #FE00H;
In these instructions, the # symbol is used for immediate data. In the last instruction, there is
DPTR. The DPTR stands for Data Pointer. Using this, it points the external data memory location.
In the first instruction, the immediate data is AFH, but one 0 is added at the beginning. So when
the data is starting with A to F, the data should be preceded by 0.
lOMoAR cPSD| 9636936
MOVA, R5;
MOVR0, A;
In 8051, there is no instruction like MOVR5, R7. But we can get the same result by using this
instruction MOV R5, 07H, or by using MOV 05H, R7. But this two instruction will work when
the selected register bank is RB0. To use another register bank and to get the same effect, we have
to add the starting address of that register bank with the register number. For an example, if the
RB2 is selected, and we want to access R5, then the address will be (10H + 05H = 15H), so the
instruction will look like this MOV 15H, R7. Here 10H is the starting address of Register Bank 2.
MOV80H, R6;
MOVR2, 45H;
MOVR0, 05H;
The first instruction will send the content of registerR6 to port P0 (Address of Port 0 is 80H). The
second one is forgetting content from 45H to R2. The third one is used to get data from Register
R5 (When register bank RB0 is selected) to register R5.
MOV0E5H, @R0;
MOV@R1, 80H
In the instructions, the @ symbol is used for register indirect addressing. In the first instruction, it
is showing that theR0 register is used. If the content of R0 is 40H, then that instruction will take
the data which is located at location 40H of the internal RAM. In the second one, if the content of
R1 is 30H, then it indicates that the content of port P0 will be stored at location 30H in the internal
RAM.
MOVXA, @R1;
MOV@DPTR, A;
In these two instructions, the X in MOVX indicates the external data memory. The external data
memory can only be accessed in register indirect mode. In the first instruction if the R0 is holding
40H, then A will get the content of external RAM location40H. And in the second one, the
content of A is overwritten in the location pointed by DPTR.
lOMoAR cPSD| 9636936
MOVCA, @A+PC;
MOVCA, @A+DPTR;
The C in MOVC instruction refers to code byte. For the first instruction, let us consider A holds
30H. And the PC value is1125H. The contents of program memory location 1155H (30H +
1125H) are moved to register A.
Implied Addressing Mode
In the implied addressing mode, there will be a single operand. These types of instruction can
work on specific registers only. These types of instructions are also known as register specific
instruction. Here are some examples of Implied Addressing Mode.
RLA;
SWAPA;
These are 1- byte instruction. The first one is used to rotate the A register content to the Left. The
second one is used to swap the nibbles in A.
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
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
lOMoAR cPSD| 9636936
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:
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
● POP
● XCH
● XCHD
The following table lists out all the possible data transfer instruction along with other details like
addressing mode, size occupied and number machine cycles it takes.
lOMoAR cPSD| 9636936
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
lOMoAR cPSD| 9636936
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
lOMoAR cPSD| 9636936
● SWAP
The following table shows all the possible Mnemonics of the Logical Instructions.
● CLR
● SETB
● MOV
● JC
● JNC
● JB
● JNB
● JBC
lOMoAR cPSD| 9636936
● ANL
● ORL
● CPL
These instructions can perform set, clear, and, or, complement etc. at bit level. All the possible
mnemonics of the Boolean Instructions are specified in the following table.
● LJMP
● AJMP
● SJMP
● JZ
● JNZ
● CJNE
● DJNZ
● NOP
lOMoAR cPSD| 9636936
● 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.
The following table shows all the mnemonics with respect to the program branching instructions.
INTERRUPTS OF 8051
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.
lOMoAR cPSD| 9636936