MODULE-3
MODULE-3
MODULE - 2 2
FEATURES
8-bit CPU
64K bytes on-chip program memory (ROM)
128 bytes on-chip data memory (RAM)
32 I/O pins arranged as four 8-bit ports (P0 - P3)
32 general purpose registers each of 8-bit
Special Function Registers (SFRs) of 128 bytes
16-bit Program Counter
8-bit Processor Status Word (PSW) & Stack Pointer
Two 16-bit timer/counters : T0 and T1
Two external and three internal vectored interrupts
One full duplex serial I/O (UART)
MODULE - 2 3
8051 FAMILY SERIES
MODULE - 2 4
8051 FAMILY SERIES
Although the 8051 is the most popular member of the 8051
family, you will not see “8051″ in the part number.
MODULE - 2 6
8051 MAJOR MANUFACTURERS
ATMEL
ANALOG DEVICES
ST MICROELECTRONICS
DALLAS
MAXIM
SILICON LABS
TEXAS INSTRUMENTS
MICROCHIP
ZILOG
MODULE - 2 7
8051 ATMEL SERIES
Part Number ROM RAM I/O pins Timer Interrupt Vcc Packaging
AT89C51 4K 128 32 2 6 5V 40
AT89C52 8K 256 32 3 8 5V 40
AT89C1051 1K 64 15 1 3 3V 20
AT89C2051 2K 128 32 3 8 3V 20
AT89LV51 4K 128 32 2 6 3V 40
AT89LV52 8K 128 32 3 8 3V 40
MODULE - 2 8
8051 PIN DETAILS
Although 8051 family members (e.g., 8751, 89C51, 89C52,
DS89C4xO) come in different packages, such as DIP (dual in-
line package), QFP (quad flat package), and LLC (leadless chip
carrier)
MODULE - 2 10
8051 PIN DETAILS
Of these pins, six (Vcc, GND, XTAL1, XTAL2, RST, and EA)
are used by all members of the 8051 and 8031 families.
The other two pins, PSEN and ALE, are used mainly in 8031-
based systems.
MODULE - 2 11
8051 SCHEMATICS
MODULE - 2 12
8051 PIN DIAGRAM
MODULE - 2 13
8051 PIN DESCRIPTION
Pin-40 : Vcc is the main power source. Usually its +5V DC.
MODULE - 2 14
Pin-31:- ALE - Address Latch Enable. It is especially used
for 8051 IC to connect it to the external memory. It can be
used while deciding whether P0 pins will be used as Address
bus or Data bus. When ALE = 1, then the P0 pins work as
Data bus and when ALE = 0, then the P0 pins act as Address
bus.
MODULE - 2 17
Pin 9:- RESET pin is used to set the 8051 microcontroller to
its initial values, while the microcontroller is working or at
the initial start of application. The RESET pin must be set
high for 2 machine cycles.
MODULE - 2 18
8051 ARCHITECTURE (Simplified)
MODULE - 2 19
8051
ARCHITECTURE
(Detailed)
MODULE - 2 20
CENTRAL PROCESSING UNIT (CPU)
Central Processor Unit (CPU) is the brain of any processing
device of the microcontroller.
The User has no control over the work of the CPU directly.
Two external interrupts (INT0 & INT1), two timer (TF0 &
TF1) interrupts and one serial port (RI / TI) interrupt.
Two port pins are used to provide the serial interface P3.0 is
the receive pin (RXD) P3.1 is the transmit pin (TXD)
MODULE - 2 33
DATA MEMORY – REGISTER BANKS
Registers are used to store data or operands during executions.
Register banks form the lowest 32 bytes on internal RAM memory.
At a time only one register bank is selected (using RS1 & RS0 bits
in PSW register) for operations and the registers inside the
selected bank are accessed using mnemonics R0..R1.. etc.
MODULE - 2 35
DATA MEMORY – REGISTER BANKS
MODULE - 2 36
DATA MEMORY – BIT ADDRESSABLE AREA
The 8051 supports a special feature which allows access to
bit variables. This is where individual memory bits in Internal
RAM can be set or cleared.
MODULE - 2 38
DATA MEMORY – BIT ADDRESSABLE AREA
Example instructions are
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
MODULE - 2 41
DATA MEMORY – SFRs (Special Function Registers)
Each SFR has a very specific function. Note some of the SFR
registers are bit addressable.
Each SFR has an address (within the range 80h to FFh) and a
name which reflects the purpose of the SFR.
MODULE - 2 42
DATA MEMORY – SFRs
MODULE - 2 44
DATA MEMORY – SFRs
MODULE - 2 45
SFRs A- Register
MODULE - 2 46
SFRs A- Register
The most important of all special function register is
Accumulator which is also known as ACC or A.
MODULE - 2 48
SFRs B- Register
MODULE - 2 49
SFRs - PSW REGISTER
MODULE - 2 51
SFRs - PSW REGISTER
CY, the carry flag: This flag is set whenever there is a carry out from
the D7 bit. This flag bit is affected after an 8-bit addition or
subtraction.
F0, the Flag 0 : The PSW.5 and PSW.1 bits are general-purpose
status flag bits and can be used by the programmer for any purpose. In
other words, they are user definable.
MODULE - 2 52
SFRs - PSW REGISTER
RS0, RS1 - Register bank select bits. These two bits are used to
select one of four register banks of RAM. By setting and clearing these
bits, registers R0-R7 are stored in one of four banks of RAM.
MODULE - 2 53
SFRs - PSW REGISTER
OV, overflow flag:
This flag is set whenever the result of a signed number operation
is too large, causing the high-order bit to overflow into sign bit.
In general, the carry flag is used to detect errors in unsigned
arithmetic operations.
The overflow flag is only used to detect errors in signed
arithmetic operations
P, the parity flag: The parity flag reflects the number of 1 s in the A
(accumulator) register only. If the A register contains an odd number
of 1‟s, then P = 1. Therefore, P = 0 if A has an even number of 1s.
MODULE - 2 54
SFRs - P0, P1, P2, P3 - Input/Output Registers
There are 4 ports with in total of 32 input/output pins are
available for connection to peripheral environment.
So 4 Input/Output ports named P0, P1, P2 and P3 has got
four corresponding port registers P0, P1, P2 and P3. All 4
port registers are bit as well as byte addressable.
Data must be written into port registers first to send it out
to any other external device through ports.
MODULE - 2 56
SFRs - P0, P1, P2, P3 - Input/Output Registers
MODULE - 2 57
SFRs - P0, P1, P2, P3 - Input/Output Registers
Upon reset and power-on, all port bits are set (1), which
means that all appropriate pins will be configured as inputs.
MODULE - 2 58
SFRs - DPTR REGISTER
It is a 16 bit register used to hold address of external or
internal RAM where data is stored or result is to be stored.
It can be divided into two 8-bit registers, DPH-data pointer
higher order and DPL-data pointer lower order.
Each register can be used as general purpose register to
store 8 bit data and can also be used to store memory
location.
It functions as Base register in base relative addressing
mode and indirect jump.
MODULE - 2 59
SFRs - DPTR REGISTER
MODULE - 2 60
SFRs - STACK POINTER
It is 8-bit register. It is byte addressable.
MODULE - 2 62
PC - PROGRAM COUNTER (not a part of SFRs)
MODULE - 2 63
MACHINE CYCLES
The CPU takes a certain number of clock cycles to execute
an instruction.
MODULE - 2 64
MACHINE CYCLES
Pulse - One complete oscillation of the clock source; State -
Two pulses; Machine Cycle - six states.
MODULE - 2 65
MACHINE CYCLES
If an instruction takes one machine cycle to execute, it will
take 12 pulses of the crystal to execute.
The 8051 also has two really slow instructions that require a
full 4 cycles to execute-those instructions you‟d find
performance to be about 230,395 instructions per second.
MODULE - 2 67
EXAMPLE - 1
Lets find the time period of the machine cycle in each case for the
following crystal frequency of different 8051 based systems: 11.0592
MHz, 16 MHz, 20 MHz.
Answer:
11.0592 MHz:
11.0592/12 = 921.6 KHz
Machine cycle = 1/921.6 KHz = 1.085us [us=microsecond]
16 MHz:
16MHz/12 = 1.333 MHz
Machine cycle = 1/1.333 MHz = 0.75us [us=microsecond]
20MHz:
20MHz/12 = 1.66 MHz
Machine Cycle = 1/1.66 MHz = 0.60us [us=microsecond]
MODULE - 2 68
EXAMPLE - 2
Lets find how long it takes to execute each of the following
instructions, for a crystal frequency of 11.0592 MHz. The machine
cycle of a system of 11.0592.z is 1.085 us
INSTRUCTION MACHINE CYCLE TIME TO EXECUTE
MOV R2,#55H 1 1x1.085 us = 1.085 us
DEC R2 1 1x1.085 us = 1.085 us
DJNZ R2,target 2 2x1.085 us = 2.17 us
LJMP 2 2x1.085 us = 2.17 us
SJMP 2 2x1.085 us = 2.17 us
NOP 1 1x1.085 us = 1.085 us
MUL AB 4 4x1.085 us = 4.34 us
MODULE - 2 69
The process of writing program for the microcontroller
mainly consists of giving instructions (commands) in the
specific order in which they should be executed in order to
carry out a specific task.
MODULE - 3 2
All microcontrollers compatible with the 8051 have in total of
255 instructions, i.e. 255 different words available for program
writing.
For example: ADD A,R0, ADD A,R1, ... ADD A,R7 are
instructions that perform the same operation (addition of the
accumulator and register). Taking into account that all
instructions perform only 53 operations (addition, subtraction,
copy etc.)
MODULE - 3 3
8051 instructions have 8-bit opcode
MODULE - 3 4
Depending on operation they perform, all instructions are
divided in 5 groups:
1. Arithmetic Instructions
2. Logic Instructions
MODULE - 3 9
ARITHMETIC INSTRUCTIONS
MODULE - 3 10
ARITHMETIC INSTRUCTIONS
MODULE - 3 11
ARITHMETIC INSTRUCTIONS
MODULE - 3 12
ARITHMETIC INSTRUCTIONS
MODULE - 3 13
ARITHMETIC INSTRUCTIONS
MODULE - 3 14
ARITHMETIC INSTRUCTIONS
MODULE - 3 15
ARITHMETIC INSTRUCTIONS
MODULE - 3 16
LOGICAL INSTRUCTIONS
The Logical Instructions perform logical operations like AND,
OR, XOR.
ANL
ORL
XRL
MODULE - 3 17
LOGICAL INSTRUCTIONS
MODULE - 3 18
LOGICAL INSTRUCTIONS
MODULE - 3 19
BOOLEAN OR BIT-ORIENTED INSTRUCTIONS
The 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.
CLR
SETB
CPL
MOV
ANL
ORL
MODULE - 3 20
BOOLEAN OR BIT-ORIENTED INSTRUCTIONS
MODULE - 3 21
BOOLEAN OR BIT-ORIENTED INSTRUCTIONS
MODULE - 3 22
BOOLEAN OR BIT-ORIENTED INSTRUCTIONS
MODULE - 3 23
PROGRAM BRANCHING INSTRUCTIONS
These instructions control the flow of program logic.
SJMP JZ / JNZ
LJMP JC / JNC
AJMP JB / JNB
JMP JBC
LCALL CJNE
ACALL DJNZ
RET NOP
RETI
MODULE - 3 24
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 25
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 26
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 27
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 28
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 29
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 30
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 32
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 33
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 34
PROGRAM BRANCHING INSTRUCTIONS
MODULE - 3 35
DATA TRANSFER INSTRUCTIONS
The Data Transfer Instructions are associated with transfer
with data between registers or external program memory or
external data memory.
MOV
MOVC
MOVX
PUSH
POP
XCH
XCHD
MODULE - 3 36
DATA TRANSFER INSTRUCTIONS
MODULE - 3 37
DATA TRANSFER INSTRUCTIONS
MODULE - 3 38
DATA TRANSFER INSTRUCTIONS
MODULE - 3 39
DATA TRANSFER INSTRUCTIONS
MODULE - 3 40
DATA TRANSFER INSTRUCTIONS
MODULE - 3 41
DATA TRANSFER INSTRUCTIONS
MODULE - 3 42
43
Flag: It is a 1-bit register that indicates the status of the
result from an operation
All the above flags are stored in the Program Status Word(PSW)
MODULE - 3 44
INSTRUCTIONS THAT AFFECTING FLAGS
MODULE - 3 45
INSTRUCTIONS THAT AFFECTING FLAGS
MODULE - 3 46
EXAMPLE
Show how the flag register is affected by the following
instructions.
MOV A, #0F5h ; A = F5h
ADD A, #0Bh ; A = F5 + 0B = 00
Solution F5h 1111 0101
+ 0Bh + 0000 1011
100h 0000 0000
After the addition, register A (destination) contains 00 and the flags are:
CY = 1 since there is a carry out from D7
P = 0 because the number of 1s is zero
AC = 1 since there is a carry from D3 to D4
MODULE - 3 47
48
Addressing mode is a way to address an operand. Operand
means the data we are operating upon.
MODULE - 3 49
IMMEDIATE ADDRESSING MODE
This addressing mode is named as “immediate” because it
transfers an 8-bit data immediately to the accumulator
(destination operand).
MODULE - 3 51
IMMEDIATE ADDRESSING MODE
The opcode for MOV A, # data is 74H. The opcode is saved in
program memory at 0202 address. The data 6AH is saved in
program memory 0203.
When the opcode 74H is read, the next step taken would be to
transfer whatever data at the next program memory address
(here at 0203) to accumulator A (E0H is the address of
accumulator).
In the figure register 04H holds the data 1FH. So the data
1FH is moved to accumulator.
MODULE - 3 53
DIRECT ADDRESSING MODE
MODULE - 3 54
DIRECT ADDRESSING MODE
MODULE - 3 57
REGISTER ADDRESSING MODE
MODULE - 3 58
REGISTER INDIRECT ADDRESSING MODE
MODULE - 3 60
REGISTER INDIRECT ADDRESSING MODE
The opcode for MOV A, @R0 is E6H. Assuming that register
bank #0 is selected. So the R0 of register bank #0 holds the
data 20H.
Program control moves to 20H where it locates the data 2FH and
it transfers 2FH to accumulator.
The source operand is @A+DPTR and we will get the source data
(to transfer) from this location.
MODULE - 3 63
INDEXED ADDRESSING MODE
The opcode for the instruction is 93H. DPTR holds the value
01FE, where 01 is located in DPH (higher 8 bits) and FE is
located in DPL (lower 8 bits).
MODULE - 3 65