Module 3 Final
Module 3 Final
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)
3
8051 FAMILY SERIES
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.
6
8051 MAJOR MANUFACTURERS
➢ ATMEL
➢ ANALOG DEVICES
➢ ST MICROELECTRONICS
➢ DALLAS
➢ MAXIM
➢ SILICON LABS
➢ TEXAS INSTRUMENTS
➢ MICROCHIP
➢ ZILOG
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
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)
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.
11
8051 SCHEMATICS
12
8051 PIN DIAGRAM
13
8051 PIN DESCRIPTION
▪ Pin-40 : Vcc is the main power source. Usually its +5V DC.
14
▪ Pin-31:- ALE - Address Latch Enable. It is especially used
for 8031 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.
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.
18
8051 ARCHITECTURE (Simplified)
19
8051
ARCHITECTURE
(Detailed)
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)
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.
35
DATA MEMORY – REGISTER BANKS
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.
38
DATA MEMORY – BIT ADDRESSABLE AREA
❑ Example instructions are
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
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.
42
DATA MEMORY – SFRs
44
DATA MEMORY – SFRs
45
SFRs A- Register
46
SFRs A- Register
❑ The most important of all special function register is
Accumulator which is also known as ACC or A.
48
SFRs B- Register
49
SFRs - PSW REGISTER
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.
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.
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.
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.
56
SFRs - P0, P1, P2, P3 - Input/Output Registers
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.
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.
59
SFRs - DPTR REGISTER
60
SFRs - STACK POINTER
❑ It is 8-bit register. It is byte addressable.
62
PC - PROGRAM COUNTER (not a part of SFRs)
63
MACHINE CYCLES
❑ The CPU takes a certain number of clock cycles to execute
an instruction.
64
MACHINE CYCLES
❑ Pulse - One complete oscillation of the clock source; State -
Two pulses; Machine Cycle - six states.
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.
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]
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
69
1
❑ 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.
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.)
3
❑ 8051 instructions have 8-bit opcode
• There are 256 possible instructions of which 255 are
implemented
➢ Arithmetic Instructions
➢ Branch Instructions
➢ Data Transfer Instructions
➢ Logic Instructions
➢ Bit-oriented Instructions
http://www.mikroe.com/chapters/view/66/chapter-3-the-8051-instruction-set/
5
DA A - Decimal adjust accumulator
Syntax: DA A;
EXAMPLE:
7
8
❑ Addressing mode is a way to address an operand. Operand
means the data we are operating upon (in most cases source
data).
9
❑ There are eight addressing modes available in the 8051:
– Register
– Direct
– Indirect
– Immediate
– Relative
– Absolute
– Long
– Indexed
10
IMMEDIATE ADDRESSING MODE
❑ This addressing mode is named as “immediate” because it
transfers an 8-bit data immediately to the accumulator
(destination operand).
12
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.
ECE3003–Microcontroller and Application 14
DIRECT ADDRESSING MODE
15
REGISTER ADDRESSING MODE
❑ In this addressing mode we use the register name directly
(as source operand). At a time registers can take value from
R0,R1…to R7.An example is shown below.
MOV A, R4
❑ In register direct addressing mode, data is transferred to
accumulator from the register (based on which register bank
is selected).
18
REGISTER ADDRESSING MODE
19
REGISTER INDIRECT ADDRESSING MODE
21
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.
24
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).
26
RELATIVE ADDRESSING MODE
29
ABSOLUTE ADDRESSING MODE
❑ This addressing mode used only with ACALL and AJUMP instructions.
❑ This two byte instruction allow within the current 2K page of the
code memory by providing the 11 least significant bits of the
destination address in the opcode (A10-A8) and byte-2 of the
instruction (A7-A0).
❑ The upper five bits of the destination address are the current upper
five bits in the program counter, so the instruction following the
branch instruction and the destination for the branch instruction
must be with in the same 2K page, since A15-A11 do not change.
30
ABSOLUTE ADDRESSING MODE
❑ The underline bit are the lower order 11 bits of the destination
address 0F46 H = 0000111101000110 B
31
ABSOLUTE ADDRESSING MODE
32
ABSOLUTE ADDRESSING MODE
33
LONG ADDRESSING MODE
❑ Long addressing mode is used only with LCALL and LJMP instruction ,
the advantage includes a full 16-bit destination address as bytes 2 or
3 of the instruction
❑ The LJMP instruction would still jump to 2040 H which is not the
correct location after the program has been moved.
34
35
36
37
38
39
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)
43
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
44
BOOLEAN PROCESSING INSTRUCTIONS
8051 ALP program to find number of negative numbers in an array
ORG 000h
MOV DPTR,#0AH ; an external address
MOV R0,#0AH ;array counter
MOV R1,#00H ;to count number neg numbers
MOV A,#00H
Loop: MOVC A,@A+DPTR
INR DPTR
RLC A
JNC Loop1
INC R1 ;Counter
DJNZ R0,loop
SJMP Exit
Loop1: DJNZ R0, Loop
Exit: NOP
END