8051 Microcontroller Guide
8051 Microcontroller Guide
8051 Microcontroller 2
Microcontroller
• A smaller computer on a CHIP
• On-chip RAM, ROM, I/O Ports, Timer, Serial Controller…
• Example: Motorola’s 6811, Intel’s 8051, Atmel 32
8051 Microcontroller 3
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
• CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O and
ROM, I/O, timer are separate timer are all on a single chip
• Versatility • Single-purpose
4
C based Embedded Systems
• Special purpose computer system usually completely inside
the device it controls
• Has specific requirements and performs pre-defined tasks
• Cost reduction compared to general purpose processor
• Different design criteria
– Performance
– Reliability
– Availability
– Safety
8051 Microcontroller 5
Harvard Architecture
8051 Microcontroller 6
8051 CPU Operation
1. Features
2. Pin Diagram
3. Block Diagram
8051 Microcontroller 7
8051 Microcontroller
• Intel introduced 8051, referred as MCS- 51, in
1981.
8051 Microcontroller 8
Features of 8051
8 bit Processor
4KB Internal ROM
128 Bytes Internal RAM
Four 8 BIT I/O PORTS (32 I/O LINES)
Two 16 Bit Timers/Counters
On Chip Full Duplex UART for Serial Communication
5 Vector Interrupts ( 2 External, 3 Internal - Timer0,Timer1,Serial)
On Chip Clock Oscillator
16 bit Address bus
64k External Code Memory
64k External Data Memory
16-bit program counter to access external Code Memory and
16 bit Data Pointer to access external Data Memory
128 user defined flags
32 General Purpose Registers each of 8 bits
8051 Microcontroller 9
8051 Family
• The 8051 is a subset of the 8052
• The 8031 is a ROM-less 8051
– Add external ROM to it
– You lose two ports, and leave only 2 ports for I/O operations
8051 Microcontroller 10
Pin Diagram
8051 Microcontroller 11
Block Diagram of 8051
8051 Microcontroller 12
Separate read instructions for external data and code memory.
Internal code
External data memory
Memory
RAM
ROM or EPROM
64k
4k or up
0xFF
SFR(direct access)
128 bytes External code memory
ROM or EPROMext
64k
0x80
0x7F
0x30
0x2F
0x20
0x1F Register bank 0(R0-R7)
8051 Microcontroller 15
XTAL1 and XTAL2 …..
• If you use a frequency source other than a crystal
oscillator, such as a TTL oscillator:
– It will be connected to XTAL1
– XTAL2 is left unconnected
8051 Microcontroller 16
RST
• RESET pin is an input and is active high (normally low)
• Upon applying a high pulse to this pin, the microcontroller will
reset and terminate all activities
• This is often referred to as a power-on reset
• Activating a power-on reset will cause all values in the registers to
be lost
8051 Microcontroller 17
EA’
• EA’, “external access’’, is an input pin and must be
connected to Vcc or GND
8051 Microcontroller 18
PSEN’ and ALE
• PSEN, “program store enable’’, is an output pin
8051 Microcontroller 19
I/O Port Pins
• The four 8-bit I/O ports P0, P1, P2
and P3 each uses 8 pins.
8051 Microcontroller 20
Port 0
• Port 0 is also designated as AD0-AD7.
8051 Microcontroller 21
Port 1 and Port 2
• In 8051-based systems with no external
memory connection:
– Both P1 and P2 are used as simple I/O.
• In 8051-based systems with external
memory connections:
– Port 2 must be used along with P0 to provide
the 16-bit address for the external memory.
– P0 provides the lower 8 bits via A0 – A7.
– P2 is used for the upper 8 bits of the 16-bit
address, designated as A8 – A15, and it cannot
be used for I/O.
8051 Microcontroller 22
Port 3
• Port 3 can be used as input or output.
8051 Microcontroller 23
Pin Description Summary
PIN TYPE NAME AND FUNCTION
Vss I Ground: 0 V reference.
Vcc I Power Supply: This is the power supply voltage for normal,
idle, and power-down operation.
P0.0 - P0.7 I/O Port 0: Port 0 is an open-drain, bi-directional I/O port. Port
0 is also the multiplexed low-order address and data bus
during accesses to external program and data memory.
P1.0 - P1.7 I/O Port 1: Port I is an 8-bit bi-directional I/O port.
P2.0 - P2.7 I/O Port 2: Port 2 is an 8-bit bidirectional I/O. Port 2 emits the
high order address byte during fetches from external
program memory and during accesses to external data
memory that use 16 bit addresses.
P3.0 - P3.7 I/O Port 3: Port 3 is an 8 bit bidirectional I/O port. Port 3 also
serves special features as explained.
8051 Microcontroller 24
Pin Description Summary
PIN TYPE NAME AND FUNCTION
RST I Reset: A high on this pin for two machine cycles while the
oscillator is running, resets the device.
ALE O Address Latch Enable: Output pulse for latching the low byte
of the address during an access to external memory.
PSEN* O Program Store Enable: The read strobe to external program
memory. When executing code from the external program
memory, PSEN* is activated twice each machine cycle,
except that two PSEN* activations are skipped during
each access to external data memory.
EA*/VPP I External Access Enable/Programming Supply Voltage: EA*
must be externally held low to enable the device to fetch
code from external program memory locations. If EA* Is
held high, the device executes from internal program
memory. This pin also receives the programming supply
voltage Vpp during Flash programming. (applies for 89c5x
MCU's)
8051 Microcontroller 25
General Block Diagram of 8051
CPU
Bus Serial
OSC 4 I/O Ports
Control Port
TXD RXD
P0 P1 P2 P3
Detailed Block Diagram
8051 Microcontroller 27
8051
Memory Space
8051 Microcontroller 28
8051 Memory Structure
External
External
60K
64K 64K
SFR
EXT INT 4K
128
EA = 0 EA = 1 Internal
Program Memory Data Memory
8051 Microcontroller 29
Internal RAM Structure
Direct
Addressing
Only
SFR [ Special Function
Direct & Registers]
Indirect
Addressing
128 Byte Internal RAM
8051 Microcontroller 30
Special Function Registers [SFR]
8051 Microcontroller 31
Program Status Word [PSW]
C AC F0 RS1 RS0 OV F1 P
Carry Parity
Auxiliary Carry User Flag 1
User Flag 0 Register Bank Select Overflow
8051 Microcontroller 32
8051 instructions that affects flag
8051 Microcontroller 33
128 Byte RAM
• There are 128 bytes of RAM in the 8051.
– Assigned addresses 00 to 7FH General Purpose
• The 128 bytes are divided into 3 different Area
groups as follows:
1. A total of 32 bytes from locations 00 to 1F BIT Addressable
Area
hex are set aside for register banks and the 128 BYTE
stack. INTERNAL RAM
Reg Bank 3
2. A total of 16 bytes from locations 20H to 2FH
are set aside for bit-addressable read/write Reg Bank 2
Register Banks
memory. Reg Bank 1
3. A total of 80 bytes from locations 30H to 7FH
Reg Bank 0
are used for read and write storage, called
scratch pad.
8051 Microcontroller 34
8051 RAM with addresses
8051 Microcontroller 35
8051 Register Bank Structure
Bank 3 R0 R1 R2 R3 R4 R5 R6 R7
Bank 2 R0 R1 R2 R3 R4 R5 R6 R7
Bank 1 R0 R1 R2 R3 R4 R5 R6 R7
Bank 0 R0 R1 R2 R3 R4 R5 R6 R7
8051 Microcontroller 36
8051 Register Banks with address
8051 Microcontroller 37
8051 Programming Model
8051 Microcontroller 38
8051 Stack
• The stack is a section of RAM used by the CPU to store
information temporarily.
– This information could be data or an address
8051 Microcontroller 40
Bit Addressable & Byte Addressable
8051 Microcontroller 41
Single bit Instructions
8051 Microcontroller 42
Bit Addressable Programming
• Example: Find out to which by each of the following bits
belongs. Give the address of the RAM byte in hex
(a) SETB 42H, (b) CLR 67H, (c) CLR 0FH (d) SETB 28H, (e) CLR 12, (f) SETB 05
8051 Microcontroller 43
8051 Software Overview
1. Addressing Modes
2. Instruction Set
3. Programming
8051 Microcontroller 44
8051 Addressing Modes
• The CPU can access data in various ways, which are
called addressing modes
1. Immediate
2. Register
3. Direct
4. Register indirect
5. External Direct
8051 Microcontroller 45
Immediate Addressing Mode
• The source operand is a constant.
• The immediate data must be preceded by the pound sign, “#”
• Can load information into any registers, including 16-bit DPTR
register
– DPTR can also be accessed as two 8-bit registers, the high byte DPH and
low byte DPL
8051 Microcontroller 46
Register Addressing Mode
• Use registers to hold the data to be manipulated.
8051 Microcontroller 48
SFR Registers & their Addresses
MOV 0E0H,#55H ;is the same as
MOV A,#55H ;which means load 55H into A (A=55H)
49
SFR Addresses ( 1 of 2 )
50
SFR Addresses ( 2 of 2 )
51
Example
52
Stack and Direct Addressing Mode
• Only direct addressing mode is allowed for pushing or
popping the stack.
• PUSH A is invalid.
8051 Microcontroller 53
Register Indirect Addressing Mode
• A register is used as a pointer to the data.
• Only register R0 and R1 are used for this purpose.
• 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.
8051 Microcontroller 54
Register Indirect Addressing Mode
• Write a program to copy the value 55H into RAM memory locations 40H
to 41H using (a) direct addressing mode, (b) register indirect addressing
mode without a loop, and (c) with a loop.
8051 Microcontroller 55
Register Indirect Addressing Mode
• 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.
8051 Microcontroller 56
External Direct
• External Memory is accessed.
8051 Microcontroller 57
8051 Instruction Set
• 8051 instructions have 8-bit opcode
• There are 256 possible instructions of which 255 are
• implemented
8051 Microcontroller 58
Data transfer instructions
8051 Microcontroller 59
8051 Microcontroller 60
Arithmetic instructions
8051 Microcontroller 61
Logical Instructions
8051 Microcontroller 62
Unconditional branching instructions
8051 Microcontroller 63
Conditional branching instructions
8051 Microcontroller 64
Bit Manipulation
8051 Microcontroller 65
MOV Instruction
• MOV destination, source ; copy source to destination.
67
Structure of Assembly Language
ORG 0H ;start (origin) at location 0
MOV R5,#25H ;load 25H into R5
MOV R7,#34H ;load 34H into R7
MOV A,#0 ;load 0 into A
ADD A,R5 ;add contents of R5 to A
;now A = A + R5
ADD A,R7 ;add contents of R7 to A
;now A = A + R7
ADD A,#12H ;add to A value 12H
;now A = A + 12H
HERE: SJMP HERE ;stay in this loop
END ;end of asm source file
68
Data Types & Directives
ORG 500H
DATA1: DB 28 ;DECIMAL (1C in
Hex)
DATA2: DB 00110101B ;BINARY (35 in
Hex)
DATA3: DB 39H ;HEX
ORG 510H
DATA4: DB “2591” ; ASCII NUMBERS
ORG 518H
DATA6: DB “My name is Joe” ;ASCII
CHARACTERS
69
ADD Instruction and PSW
70
ADD Instruction and PSW
71
Multiplication of Unsigned Numbers
MUL AB ; A B, place 16-bit result in B
and A
MOV A,#25H ;load 25H to reg. A
MOV B,#65H ;load 65H in reg. B
MUL AB ;25H * 65H = E99 where B = 0EH and A = 99H
72
Division of Unsigned Numbers
DIV AB ; divide A by B
73
Checking an input bit
JNB (jump if no bit) ; JB (jump if bit = 1)
8051 Microcontroller 74
Switch Register Banks
75
Pushing onto Stack
76
Popping from Stack
77
Looping
78
Loop inside a Loop (Nested Loop)
79
8051 Conditional Jump Instructions
80
8051 Microcontroller 81
Conditional Jump Example
82
Conditional Jump Example
83
Unconditional Jump Instructions
• All conditional jumps are short jumps
– Target address within -128 to +127 of
PC
85
Separate read instructions for external data and code memory.
Internal code
External data memory
Memory
RAM
ROM or EPROM
64k
4k or up
0xFF
SFR(direct access)
128 bytes External code memory
ROM or EPROMext
64k
0x80
0x7F
0x30
0x2F
0x20
0x1F Register bank 0(R0-R7)
8051 Microcontroller 88
Port 0 structure
8051 Microcontroller 89
Port 1
8051 Microcontroller 90
Port 2
8051 Microcontroller 91
Port 3
8051 Microcontroller 92
Port address
Port pins Bit address
P0.0 80
P0.1 81
P0.2 82
P0.3 83
P0.4 84
P0.5 85
P0.6 86
P0.7 87
P1.0 90
P1.1 91
P1.2 92
P1.3 93
P1.4 94
P1.5 95
P1.6 96
P1.7 97
P2.0 A0
P2.1 A1
P2.2 A2
P2.3 A3
P2.4 A4
P2.5 A5
P2.6 A6
P2.7 A7
P3.0 B0 – RXD serial input data line
P3.1 B1 – TXD Transmit data line
P3.2 B2 – INT0 – External interrupt 0
P3.3 B3—INT1 – External Interrupt 1
P3.4 B4 – T0- Timer 0 external input
P3.5 B5- T1 –Timer 1 external input
P3.6 B6- WR- external data memory write strobe
8051P3.7
Microcontroller B7 – RD-External data memory read enable 93
8051
TIMERS
8051 Microcontroller 94
8051 Timer/Counter
OSC ÷12
C /T 0 TLx THx TFx
(8 Bit) (8 Bit) (1 Bit)
C /T 1
T PIN
INTERRUPT
TR
Gate
INT PIN
8051 Microcontroller 95
TMOD Register
GATE:
When set, timer/counter x is enabled, if INTx pin is high
and TRx is set.
When cleared, timer/counter x is enabled, if TRx bit set.
C/T*:
When set, counter operation (input from Tx input pin).
When cleared, timer operation (input from internal clock).
8051 Microcontroller 96
TMOD Register
8051 Microcontroller 97
TCON Register
8051 Microcontroller 98
8051 Timer Modes
8051 TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
8051 Microcontroller 99
TIMER 0
OSC ÷12
C /T 0
TL0 TH0 TF0
C /T 1
T 0 PIN
TR0 INTERRUPT
Gate
INT 0 PIN
TIMER 0 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
TF0
(5 Bit) (8 Bit)
C /T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
TF0
(8 Bit) (8 Bit)
C /T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
TF0
(8 Bit) (8 Bit)
C /T 1
T 0 PIN
TR 0
Gate Reload
INT 0 PIN
TH0
(8 Bit)
OSC ÷12
C /T 0 TL0 INTERRUPT
TF0
(8 Bit)
C /T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
TR1
TIMER 1
OSC ÷12
C /T 0
TL1 TH1 TF1
C /T 1
T1PIN
INTERRUPT
TR1
Gate
INT1 PIN
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
TF1
(5 Bit) (8 Bit)
C /T 1
T1PIN
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
TF1
(8 Bit) (8 Bit)
C /T 1
T1PIN
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
TF1
(8 Bit) (8 Bit)
C /T 1
T1PIN
TR1
Gate Reload
INT 1 PIN
TH1
(8 Bit)
are selected.
• Solution:
• The start bit is always one bit, but the stop bit can be
one or two bits
1. SBUF Register
2. SCON Register
3. PCON Register
• We can set it to high by software and thereby double the baud rate.
– Timer 0 Overflow.
– Timer 1 Overflow.
– Reception/Transmission of Serial Character.
– External Event 0.
– External Event 1.
--
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
MOV IE,#08h
• ES : Enable Serial port interrupt.
or
• ET1 : Enable Timer 1 control bit.
SETB ET1
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
Serial Port
INT 0 Pin
Timer 1 Pin
Thank you