Microcontroller 8051
Microcontroller 8051
Microcontroller 8051
CPU operation
Memory space
Software overview
Peripheral overview
Interrupt
Timers
Parallel port inputs and
outputs
Serial port
Main References
8051
Introduction
to
Microcontroll
ers
8051
8051
8051
Microprocessors
CPU for Computers
No RAM, ROM, I/O on CPU chip itself
Example: Intel's x86, Motorolas 680x0
8051
Microcontroller
A smaller computer
On-chip RAM, ROM, I/O ports...
Example: Motorolas 6811, Intels 8051, Zilogs
Z8 and PIC
8051
Microprocessor vs.
Microcontroller
Microprocessor
Microcontroller
Expansive
Versatility
General-purpose
8051
Office
Auto
Trip computer, engine control, air bag, ABS, instrumentation,
security system, transmission control, entertainment, climate
control, cellular phone, keyless entry
8051
10
Choosing a Microcontroller
8-bit microcontrollers
Motorolas 6811
Intels 8051
Zilogs Z8
Microchips PIC
11
12
8051
number
of
Intel (original)
Atmel
Philips/Signetics
AMD
Infineon (formerly Siemens)
Matra
Dallas Semiconductor/Maxim
Suresh.P.Nair , HOD - ECE , RCET
13
14
8051 Microcontroller
Intel introduced 8051, referred as MCS51, in 1981.
The 8051 is an 8-bit processor
15
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
16
8051 Features
8051
17
18
8051
19
8051
20
8051
21
8051
22
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
23
RST
In order for the RESET input to be effective, it must
have a minimum duration of 2 machine cycles.
In other words, the high pulse must be high for a
minimum of 2 machine cycles before it is allowed
to go low.
8051
24
EA
EA, external access, is an input pin
and must be connected to Vcc or GND
The 8051 family members all come with
on-chip ROM to store programs and also
have an external code and data
memory.
Normally EA pin is connected to Vcc
EA pin must be connected to GND to
indicate that the code or data is stored
externally.
8051
25
8051
26
8051
27
Port 0
Port 0 is also designated as AD0-AD7.
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.
8051
28
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 16bit address, designated as A8 A15,
and it cannot be used for I/O.
8051
29
Port 3
Port 3 can be used as input or
output.
Port 3 has the additional
function of providing some
extremely important signals
8051
30
TYP
E
Vss
Ground: 0 V reference.
Vcc
P0.0
- I/O
P0.7
P1.0
- I/O
P1.7
P2.0
- I/O
P2.7
8051
TY
P
E
RST
ALE
PSEN*
EA*/VP
P
External
Access
Enable/Programming
Supply Voltage: EA* must be externally held
low
to enable
the
device
Suresh.P.Nair
, HOD
- ECE
, RCET to fetch code from 32
8051
4K
ROM
128 B
RAM
CPU
OSC
Bus
Control
Serial
Port
4 I/O Ports
P0
P1
P2
P3
TXD RXD
8051
34
8051
Memory
Space
8051
35
64K
External
External
60K
64K
SFR
EXT
EA = 0
INT
4K
128
EA = 1
Program Memory
8051
Data Memory
36
8051
SFR [ Special
Function Registers]
128 Byte Internal
RAM
37
8051
38
AC F0
RS1
RS0 OV
Carry
Parity
Auxiliary Carry
User Flag 0
8051
F1
User Flag 1
Register Bank Select
Overflow
39
8051
40
General Purpose
Area
8051
41
8051
42
8051
R0 R1 R2 R3 R4 R5 R6 R7
R0 R1 R2 R3 R4 R5 R6 R7
R0 R1 R2 R3 R4 R5 R6 R7
R0 R1 R2 R3 R4 R5 R6 R7
43
8051
44
8051
45
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
46
8051 Stack
The storing of a CPU register in the stack is
called a PUSH
SP is pointing to the last used location of the stack
As we push data onto the stack, the SP is
incremented by one
This is different from many microprocessors
47
8051
48
8051
49
8051
50
8051 Peripheral
Overview
1.Timers
2.Serial Port
3.Interrupts
8051
51
8051
TIMERS
8051
52
8051 Timer/Counter
OSC
12
C /T 0
C /T 1
TLx THx
TFx
(8 Bit)
(8 Bit) (1 Bit)
T PIN
INTERRUPT
TR
Gate
INT PIN
8051
53
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
54
TMOD Register
8051
55
TCON Register
8051
56
8051
Timer
Modes
8051 TIMERS
Timer 0
Timer 1
Mode 0
Mode 0
Mode 1
Mode 1
Mode 2
Mode 2
Mode 3
8051
57
TIMER 0
OSC
12
C /T 0
C /T 1
TL0 TH0
TF0
T 0 PIN
TR 0
Gate
INT 0 PIN
INTERRU
PT
TIMER 0 Mode 0
13 Bit Timer /
Counter
OSC
12
C /T 0
C /T 1
T 0 PIN
TL0
TH0
(5 Bit) (8 Bit)
TF0
TR 0
Gate
INT 0 PIN
INTERRUP
T
TIMER 0 Mode 1
16 Bit Timer /
Counter
OSC
12
C /T 0
C /T 1
T 0 PIN
TL0
TH0
(8 Bit) (8 Bit)
TF0
TR 0
Gate
INT 0 PIN
INTERRUP
T
TIMER 0 Mode 2
8 Bit Timer / Counter with
AUTORELOAD
OSC
12
C /T 0
C /T 1
T 0 PIN
TL0
TH0
(8 Bit) (8 Bit)
TF0
TR 0
Gate
Reloa
d
INT 0 PIN
TH0
(8 Bit)
INTERRUP
T
TIMER 0 Mode 3
Two - 8 Bit Timer /
Counter
OSC
12
C /T 0
C /T 1
T 0 PIN
TL0
(8 Bit)
TF0
INTERRUP
T
TH0
(8 Bit)
TF1
INTERRUP
T
TR 0
Gate
INT 0 PIN
OSC
TR1
12
TIMER 1
OSC
12
C /T 0
C /T 1
TL1 TH1
TF1
T 1PIN
TR1
Gate
INT 1 PIN
INTERR
UPT
TIMER 1 Mode 0
13 Bit Timer /
Counter
OSC
12
C /T 0
C /T 1
T 1PIN
TL1
TH1
(5 Bit) (8 Bit)
TF1
TR1
Gate
INT 1 PIN
INTERRUP
T
TIMER 1 Mode 1
16 Bit Timer /
Counter
OSC
12
C /T 0
C /T 1
T 1PIN
TL1
TH1
(8 Bit) (8 Bit)
TF1
TR1
Gate
INT 1 PIN
INTERRUP
T
TIMER 1 Mode 2
8 Bit Timer / Counter with
AUTORELOAD
OSC
12
C /T 0
C /T 1
T 1PIN
TL1
TH1
(8 Bit) (8 Bit)
TF1
TR1
Gate
Reloa
d
INT 1 PIN
TH1
(8 Bit)
INTERRUP
T
Programming Timers
Example: Indicate which mode and which timer
are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H
(c) MOV TMOD, #12H
Solution: We convert the value from hex to binary.
(a) TMOD =
(b) TMOD =
(c) TMOD =
1 of timer
8051
67
Programming Timers
Find the timers clock frequency and its period for
various 8051-based system, with the crystal
frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
68
8051
Serial
Port
8051
69
Basics of Serial
Communication
Computers transfer data in two ways:
Parallel: Often 8 or more lines (wire conductors) are used
to transfer data to a device that is only a few feet away.
Serial: To transfer to a device located many meters away,
the serial method is used. The data is sent one bit at a
time.
8051
70
Basics of Serial
Communication
Serial data communication uses two methods
Synchronous method transfers a block of data at a
time
Asynchronous method transfers a single byte at a time
71
72
8051
73
74
8051
0
1
2
3
75
8051
76
SBUF Register
SBUF is an 8-bit
communication.
register
used
solely
for
serial
77
SBUF Register
Sample Program:
8051
78
SCON Register
SM0 SM1 SM2 REN TB8 RB8
Set to Enable
Serial Data
reception
Enable Multiprocessor
Communication Mode
8051
TI
RI
79
8051
80
81
8051
82
8051
83
84
85
86
8051
87
8051
Interrup
ts
8051
88
INTERRUPTS
8051
89
Interrupt Vs Polling
1. Interrupts
Whenever any device needs its service, the device notifies
the microcontroller by sending it an interrupt signal.
Upon receiving an interrupt signal, the microcontroller
interrupts whatever it is doing and serves the device.
The program which is associated with the interrupt is called
the interrupt service routine (ISR) or interrupt handler.
2. Polling
The microcontroller continuously monitors the status of
a given device.
When the conditions met, it performs the service.
After that, it moves on to monitor the next device until
every one is serviced.
8051
90
Interrupt Vs Polling
The polling method is not efficient, since it wastes
much of the microcontrollers time by polling devices that
do not need service.
The advantage of interrupts is that the microcontroller
can serve many devices (not all at the same time).
Each devices can get the attention of the microcontroller
based on the assigned priority.
For the polling method, it is not possible to assign
priority since it checks all devices in a round-robin fashion.
The microcontroller can also ignore (mask) a device
request for service in Interrupt.
8051
91
Steps in Executing an
Interrupt
finishes the instruction it is executing and saves
1. It
address of the next instruction (PC) on the stack.
the
92
93
Timer 0 Overflow.
Timer 1 Overflow.
Reception/Transmission of Serial Character.
External Event 0.
External Event 1.
94
8051
95
96
8051
97
EA
MOV IE,#08h
or
SETB ET1
: Global enable/disable.
8051
98
SETB
SETB
SETB
SETB
IE.7
IE.4
IE.1
IE.2
99
Interrupt Priority
When the 8051 is powered up, the priorities are assigned
according to the following.
In reality, the priority scheme is nothing but an internal
polling sequence in which the 8051 polls the interrupts in
the sequence listed and responds accordingly.
8051
100
Interrupt Priority
We can alter the sequence of interrupt priority by
assigning a higher priority to any one of the interrupts by
programming a register called IP (interrupt priority).
To give a higher priority to any of the interrupts, we make
the corresponding bit in the IP register high.
8051
101
Reserved
PS
PT1 PX1
PT0
PX0
Serial Port
Timer 1 Pin
INT 1 Pin
Priority bit=1 assigns high
priority
Priority bit=0 assigns low
8051
INT 0 Pin
Timer 0 Pin
102
8051 Software
Overview
1.Addressing Modes
2.Instruction Set
3.Programming
8051
103
8051
104
8051
105
8051
107
Mr. P. Suresh
108
SFR Addresses ( 1 of 2 )
Mr. P. Suresh
109
SFR Addresses ( 2 of 2 )
Mr. P. Suresh
110
Example
Mr. P. Suresh
111
8051
112
8051
113
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
114
8051
115
External Direct
External Memory is accessed.
There are only two commands that use
External Direct addressing mode:
MOVX A, @DPTR
MOVX @DPTR, A
DPTR must first be loaded with the address of
external memory.
8051
116
8051
117
MOV Instruction
MOV destination, source ; copy source to
destination.
MOV A,#55H ;load value 55H into reg. A
MOV R0,A
;copy contents of A into R0
;(now A=R0=55H)
MOV R1,A
;copy contents of A into R1
;(now A=R0=R1=55H)
MOV R2,A
;copy contents of A into R2
;(now A=R0=R1=R2=55H)
MOV R3,#95H ;load value 95H into R3
;(now R3=95H)
MOV A,R3
;copy contents of R3 into A
;now A=R3=95H
Mr. P. Suresh
118
ADD Instruction
ADD A, source ;ADD the source operand
to the
accumulator
MOV A, #25H
MOV R2,#34H
ADD A,R2
;add R2 to accumulator
;(A = A + R2)
Mr. P. Suresh
119
Structure of Assembly
Language
ORG
MOV
MOV
MOV
ADD
0H
R5,#25H
R7,#34H
A,#0
A,R5
ADD A,R7
ADD A,#12H
HERE: SJMP HERE
END
Mr. P. Suresh
120
;DECIMAL (1C
in Hex)
DATA2: DB
00110101B
;BINARY (35 in
Hex)
DATA3: DB
39H
ORG 510H
DATA4: DB
2591
;HEX
; ASCII
NUMBERS
ORG 518H
DATA6: DB
My name is Joe ;ASCII CHARACTERS
Mr. P. Suresh
121
Mr. P. Suresh
122
Mr. P. Suresh
123
Multiplication of Unsigned
Numbers
MUL AB
MOV A,#25H
MOV B,#65H
MUL AB
and A = 99H
Operand 1
Operand 2
Result
byte byte
A=low byte,
B=high byte
Mr. P. Suresh
124
; divide A by B
Numerator
Denominator
Quotient
Remainder
byte / byte
8051
125
8051
126
Mr. P. Suresh
127
Mr. P. Suresh
128
Mr. P. Suresh
129
Looping
Mr. P. Suresh
130
Mr. P. Suresh
131
Mr. P. Suresh
132
Mr. P. Suresh
133
Mr. P. Suresh
134
Unconditional Jump
Instructions
All conditional jumps are short jumps
Target address within -128 to +127 of PC
Mr. P. Suresh
135
Call Instructions
LCALL (long call): 3-byte instruction
2-byte address
Target address within 64K-byte range
Mr. P. Suresh
136