0% found this document useful (0 votes)
21 views45 pages

Chapter 3

The document provides a comprehensive overview of the 8051 microcontroller, detailing its architecture, special function registers, instruction set, addressing modes, and assembly language programming. It covers key features such as I/O ports, timers/counters, interrupts, and serial communication, along with examples of programming and usage. Additionally, it explains the operation of timers and the serial port, including baud rate settings and interrupt handling.

Uploaded by

praveenvh.in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views45 pages

Chapter 3

The document provides a comprehensive overview of the 8051 microcontroller, detailing its architecture, special function registers, instruction set, addressing modes, and assembly language programming. It covers key features such as I/O ports, timers/counters, interrupts, and serial communication, along with examples of programming and usage. Additionally, it explains the operation of timers and the serial port, including baud rate settings and interrupt handling.

Uploaded by

praveenvh.in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

8051 MICROCONTROLLER

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

•PCON (Power Control Register)


7
•TMOD (Timer Mode Register)
8
8051 Addressing modes

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-

MOV A,88H;Moves content of SFR TCON (address is 88H) to A

4. Register Indirect Addressing:


• The address of data is available in the R0 or R1 registers as specified in the
instruction.
• For example-MOV A, @R0; moves content of memory location whose address is
pointed by R0 to A.

5. External Data Addressing:


• Pointer used for external data addressing can be either R0/R1 (256 byte access) or
DPTR (64kbyte access). For example-
• MOVX A,@R0;Moves content of external RAM memory location whose address in R0
to A
• MOVX A,@DPTR; Moves content of external RAM memory location whose address is10
6. External Code Addressing:
• Sometimes we may want to store non-volatile data in to
the ROM e.g.look-uptables. Such data may require reading
the code memory. This may be done as follows:

• MOVC A,@A+DPTR; Moves content of External ROM


memory location whose address is pointed by A+DPTR to A

• MOVC A,@A+PC; Moves content of External ROM memory


location whose address pointed by A+PC to A

11
8051 Pin details

12
13
Instruction Set of 8051

8051 Instructions are grouped into:

• 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.

1 1 Spilt timer mode. 25


Mode 0 (13-Bit Timer Mode)
• Timer register is configured as a 13-bit register consisting of all the 8 bits of
TH1 and the lower 5 bits of TL1. The upper 3 bits of TL1 should be ignored.
The timer interrupt flag TF1 is set when the count rolls over from all 1s to all
0s.
Mode 1 (16-Bit Timer Mode)
• Timer mode "1" is a 16-bit timer and is a commonly used mode. It functions
in the same way as 13-bit mode except that all 16 bits are used.
Mode 2 (8 Bit Auto Reload)
• Both the timer registers are configured as 8-bit counters (TL1 and TL0) with
automatic reload. Overflow from TL1 (TL0) sets TF1 (TF0) and also reloads
TL1 (TL0) with the contents of TH1 (TH0), which is preset by software. The
reload leaves TH1 (TH0) unchanged.
Mode 3 (Split Timer Mode)
• When Timer 0 is placed in mode 3, it becomes two separate 8-bit timers.
Timer 0 is TL0 and TH0. Both the timers count from 0 to 255 and in case of
overflow, reset back to 0.
• When Timer 1 is in split mode, TH1 and TL1 can be set in modes 0, 1 or 2,
but it cannot be started/stopped as the bits that do that are now linked to
TH0. The real timer 1 will be incremented with every machine cycle. 26
Mode 0

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:

• When 8051 is powered, SMOD bit =0.


• By setting SMOD, the baud rate can be doubled.
MOV A, PCON
SETB ACC.7
MOV PCON,A
• Idle mode (IDL): Clock to CPU is disabled; peripheral clock is active,
CPU status is protected & the power drawn in this mode is 15% of full
power.
• Power down mode (PD): Onchip oscillator is stopped (both CPU &
peripheral clocks), device draws only 10microA current, reduces power
consumption
• GF0, GF1: General purpose flags
37
Examples for Serial port programming
1. Write a program for the 8051 to transfer letter “A” serially at 4800
baud rate continuously.

MOV TMOD, #20H Timer 1, mode 2 (auto reload); 0010 0000


MOV TH1, #-06 4800 baud rate; FAH
MOV SCON, #50H 8 bit, 1 start, 1 stop, REN enabled; 0101 0000
SETB TR1 start timet 1
AGAIN: MOV SBUF, #”A” letter “A” to be transmitted
HERE: JNB TI, HERE wait for last bit
CLR TI clear TI for next character
SJMP AGAIN keep sending “A”
38
2. Write a program for the 8051 to receive bytes of data serially, and put
them in P1. Set the baud rate at 4800, 8 bit data and 1 start and 1 stop bit.

MOV TMOD, #20H Timer 1, mode 2 (auto reload); 0010 0000


MOV TH1, #-6 4800 baud rate
MOV SCON, #50H 8 bit, 1 start, 1 stop, REN enabled; 0101 0000
SETB TR1 start timet 1
HERE: JNB RI, HERE wait for character to come in
MOV A, SBUF save incoming byte in A
MOV P1, A sent to port 1
CLR RI get ready to receive next byte
SJMP HERE keep getting data

39
8051 Interrupts

40
41
42
43
44
45

You might also like