Chapter 3
Chapter 3
1. 8051 – Architecture
2. Special Function Registers (SFRs)
3. Instruction set
4. Addressing modes
5. Assembly language programming
6. I/O Ports
7. Timers/counters
8. Interrupts and
9. Serial communication
1
Block diagram of 8051
2
Features of 8051
• 8 bit Microcontroller
• 8 bit CPU
• 128 bytes on-chip data memory (RAM)
• 4KB bytes on-chip program memory (ROM)
• Four register banks (Bank 0,1,2 & 3)
• 32 general purpose registers each of 8-bit.
• 21 SFRs
• 128 user defined software flags (1 bit register).
• 8-bit bidirectional data bus.
• 16-bit unidirectional address bus.
• 16 bit Timers (usually 2, but may have more or less)
• Clock frequency: 11.0592MH
3
4
Processor Status Word (PSW)
5
Architecture of 8051
6
Special Function Registers
1. Immediate Addressing:
• Data is immediately available in the instruction.
For example-
• ADD A,#77H; Adds 77(decimal) to A and stores in A
• ADD A,#4DH; Adds 4D(hexadecimal) to A and stores in A
MOV DPTR,#1000H; Moves1000(hexadecimal) to data pointer
2. Register Addressing:
• Data is available in the register specified in the instruction. The
register bank is decided by 2 bits of Processor Status Word.
For example-
• ADD A,R0; Adds content of R0 to A and stores in A
9
3. Direct Addressing:
• The address of the data is available in the instruction. For example-
11
8051 Pin details
12
13
Instruction Set of 8051
• Arithmetic Instructions
• Logical Instructions
• Data Transfer instructions
• Boolean Variable (Bit manipulation)Instructions
• Program Branching Instructions
14
Assembly language programming
Program to add 79, F5, E2 & store the result in R0 & R5
MOV A, #00
MOV R5,A
ADD A, #79H
JNC LOOP1
INC R5
LOOP1: ADD A, #F5H
JNC LOOP2
INC R5
LOOP2: ADD A, #E2H
JNC OVER
INC R5
OVER: MOV R0, A
15
• Program to add 2 numbers:
MOV DPTR, #4500
MOVX A, @DPTR
MOV R0,A
MOV R1, #00
INC DPTR
MOVX A, @DPTR
ADD A, R0
JNC LOOP1
INC R1
LOOP1: INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,R1
MOVX@DPTR,A
16
• Program to subtract 2 numbers:
MOV A, #Data1
SUBB A, #Data2
MOV DPTR, #4500
MOVX @DPTR, A
17
• Program to MULTIPLY 2 numbers:
MOV A, #Data1
MOV B, #Data2
MUL AB
MOV DPTR, #4500
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
18
• Program to DIVIDE 2 numbers:
MOV A, #Data1
MOV B, #Data2
DIV AB
MOV DPTR, #4500
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
19
I/O Ports
20
• The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins.
All the ports upon RESET are configured as output, ready to
be used as input ports.
• Port 0 is also designated as AD0-AD7, allowing it to be used
for both address and data. When connecting an 8051 to an
external memory, port 0 provides both address and data.
The 8051 multiplexes address and data through port 0 to
save pins.
• ALE indicates if P0 has address or data.
When ALE=0, it provides data D0-D7
When ALE=1, it has address A0-A7
21
• 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
• Port 3 can be used as input or output.
• Port 3 does not need any pull-up resistors. Port 3 has the
additional function of providing some extremely important
signals.
22
23
8051 Timer/Counter
• In 8051, there are two 16-bit timer registers. These
registers are known as Timer0 and Timer1.
• The timer registers can be used as Timer or Counter.
TMOD (Timer Mode) Register
• Both Timer 0 and Timer 1 use the same register to set
the various timer operation modes. It is an 8-bit
register in which the lower 4 bits are set aside for
Timer 0 and the upper four bits for Timer 1. In each
case, the lower 2 bits are used to set the timer mode.
24
•GATE: Every timer has a means of starting and stopping. 8051 timers have both
software and hardware controls. The SETB instruction is used to start it and it is
stopped by the CLR instruction. These instructions start and stop the timers as long
as GATE = 0 in the TMOD register.
•Timers can be started and stopped by an external source by making GATE = 1 in
the TMOD register.
•C/T!: This bit in the TMOD register is used to decide whether a timer is used as
a delay generator or an event manager/counter.
M1 M2 Mode
0 0 13-bit timer mode.
0 1 16-bit timer mode.
1 0 8-bit auto reload
mode.
Mode 1
Mode 2
Mode 3
27
TCON Register:
Set/Cleared by SW
Set/Cleared by SW
Set/Cleared by HW
Set/Cleared by HW
28
29
8051 Serial Port
• Serial port in 8051 transfers/receives bits serially at different baud rate
(9600, 4800, 2400 & 1200) & is programmable using T1.
• T1 is programmed in mode2 (8bit auto reload mode) to generate different
baud rate
• TxD, RxD: Transmits/Receives data serially
• XTAL OSCR frequency=11.0592MH
• Timer clock=XTAL OSCR frequency/12
• Registers related to Serial port communication
• SBUF Register: 8 bit register used in serial communication; When a byte is
written in SBUF, framing is done & transmitted serially over TxD. When serial
bits are received over RxD line, deframing is done & stores the byte in SBUF.
• SCON Register
• PCON Register
30
31
32
33
34
35
Writing to the Serial port:
• MOV SBUF, #A: 8051 transmits character A through TxD; After
transmitting A, TI is set. After that next character will be
transmitted.
CLR TI
MOV SBUF, #A
$:JNB TI, $; wait until TI is set
The above 3 instructions will successfully transmit a character A &
wait for TI bit to be set to continue.
Reading the Serial port:
$:JNB RI, $; wait for 8051 to set RI
MOV A, SBUF; Read the character from the serial port
36
PCON Register:
39
8051 Interrupts
40
41
42
43
44
45