Professional Documents
Culture Documents
Panakhaniya
Department of Electronics and Communication Engineering
Darshan Institute of Engineering and Technology
karshan M.Panakhaniya
Topics to be covered
Unit-1 Introduction of Microcontroller
Unit-2 8051 Hardware
Unit-3 8051 Programming
Unit-4 8051 Interfacing
Unit-5 8051 Applications
karshan M.Panakhaniya
Main References
karshan M.Panakhaniya
UNIT-I
Introduction
of
Microcontroll
ers
karshan M.Panakhaniya
karshan M.Panakhaniya
karshan M.Panakhaniya
Microprocessors
CPU for Computers
No RAM, ROM, I/O on CPU chip itself
Example: Intel's x86, Motorolas 680x0
karshan M.Panakhaniya
Microcontroller
A smaller computer
On-chip RAM, ROM, I/O ports...
Example: Motorolas 6811, Intels 8051, Zilogs
Z8 and PIC
karshan M.Panakhaniya
Microprocessor vs.
Microcontroller
Microprocessor
Microcontroller
Expansive
flexibility
General-purpose
Not Expansive
Single-purpose
karshan M.Panakhaniya
Common features of
Microcontroller
On-chip Oscillator
Interrupts,
I/O Ports
ADC
PWM
SFRs
karshan M.Panakhaniya
10
Hierarchy Of Microcontroller
karshan M.Panakhaniya
11
Commercial 8 bit
Microcontroller
karshan M.Panakhaniya
12
Office
Auto
Trip computer, engine control, air bag, ABS, instrumentation,
security system, transmission control, entertainment, climate
control, cellular phone, keyless entry
karshan M.Panakhaniya
13
Choosing a Microcontroller
8-bit microcontrollers
Motorolas 6811
Intels 8051
Zilogs Z8
Microchips PIC
14
15
number
of
Intel (original)
Atmel
Philips/Signetics
AMD
Infineon (formerly Siemens)
Matra
Dallas Semiconductor/Maxim
karshan M.Panakhaniya
16
17
8051 Microcontroller
Intel introduced 8051, referred as MCS51, in 1981.
The 8051 is an 8-bit processor
18
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
karshan M.Panakhaniya
19
8051 Features
20
21
karshan M.Panakhaniya
22
karshan M.Panakhaniya
23
karshan M.Panakhaniya
24
karshan M.Panakhaniya
25
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
karshan M.Panakhaniya
26
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.
karshan M.Panakhaniya
27
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.
karshan M.Panakhaniya
28
karshan M.Panakhaniya
29
karshan M.Panakhaniya
30
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.
31
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.
karshan M.Panakhaniya
32
Port 3
Port 3 can be used as input or
output.
Port 3 has the additional
function of providing some
extremely important signals
karshan M.Panakhaniya
33
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
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 to fetch code from 35
karshan
M.Panakhaniya
4K
ROM
128 B
RAM
CPU
OSC
Bus
Control
Serial
Port
4 I/O Ports
P0 P1
karshan M.Panakhaniya
P2
P3
TXD RXD
36
karshan M.Panakhaniya
37
8051
Memory
Space
karshan M.Panakhaniya
38
64K
External
External
60K
64K
SFR
EXT
EA = 0
INT
4K
128
EA = 1
Data Memory
Program Memory
karshan M.Panakhaniya
39
SFR [ Special
Function Registers]
128 Byte Internal
RAM
karshan M.Panakhaniya
40
karshan M.Panakhaniya
41
AC F0
RS1
RS0 OV
Carry
Parity
Auxiliary Carry
User Flag 0
F1
User Flag 1
Register Bank Select
karshan M.Panakhaniya
Overflow
42
karshan M.Panakhaniya
43
General Purpose
Area
44
karshan M.Panakhaniya
45
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
karshan M.Panakhaniya
46
karshan M.Panakhaniya
47
karshan M.Panakhaniya
48
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
49
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
50
karshan M.Panakhaniya
51
karshan M.Panakhaniya
52
karshan M.Panakhaniya
53
8051 Peripheral
Overview
1.Timers
2.Serial Port
3.Interrupts
karshan M.Panakhaniya
54
8051
TIMERS
karshan M.Panakhaniya
55
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
karshan M.Panakhaniya
56
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).
karshan M.Panakhaniya
57
TMOD Register
karshan M.Panakhaniya
58
TCON Register
karshan M.Panakhaniya
59
8051
Timer
Modes
8051 TIMERS
Timer 0
Timer 1
Mode 0
Mode 0
Mode 1
Mode 1
Mode 2
Mode 2
Mode 3
karshan M.Panakhaniya
60
TIMER 0
OSC
12
C /T 0
C /T 1
TL0 TH0
TF0
T 0 PIN
INTERRU
PT
TR 0
Gate
INT 0 PIN
karshan M.Panakhaniya
61
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
INTERRUP
T
TR 0
Gate
INT 0 PIN
62
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
INTERRUP
T
TR 0
Gate
INT 0 PIN
63
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
INTERRUP
T
TR 0
Gate
Reloa
d
INT 0 PIN
TH0
(8 Bit)
64
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
12
TR1
karshan M.Panakhaniya
65
TIMER 1
OSC
12
C /T 0
C /T 1
TL1 TH1
TF1
T 1PIN
INTERR
UPT
TR1
Gate
INT 1 PIN
karshan M.Panakhaniya
66
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
INTERRUP
T
TR1
Gate
INT 1 PIN
67
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
INTERRUP
T
TR1
Gate
INT 1 PIN
68
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
INTERRUP
T
TR1
Gate
Reloa
d
INT 1 PIN
TH1
(8 Bit)
69
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
karshan M.Panakhaniya
70
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:
71
8051
Serial
Port
karshan M.Panakhaniya
72
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.
karshan M.Panakhaniya
73
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
74
75
karshan M.Panakhaniya
76
77
0
1
2
3
78
karshan M.Panakhaniya
79
SBUF Register
SBUF is an 8-bit
communication.
register
used
solely
for
serial
80
SBUF Register
Sample Program:
karshan M.Panakhaniya
81
SCON Register
SM0 SM1 SM2 REN TB8 RB8
Set to Enable
Serial Data
reception
Enable Multiprocessor
Communication Mode
karshan M.Panakhaniya
TI
RI
82
karshan M.Panakhaniya
83
84
karshan M.Panakhaniya
85
karshan M.Panakhaniya
86
87
88
89
karshan M.Panakhaniya
90
8051
Interrup
ts
karshan M.Panakhaniya
91
INTERRUPTS
karshan M.Panakhaniya
92
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.
karshan M.Panakhaniya
93
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.
karshan M.Panakhaniya
94
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
95
96
Timer 0 Overflow.
Timer 1 Overflow.
Reception/Transmission of Serial Character.
External Event 0.
External Event 1.
97
karshan M.Panakhaniya
98
99
karshan M.Panakhaniya
100
EA
MOV IE,#08h
or
SETB ET1
: Global enable/disable.
101
SETB
SETB
SETB
SETB
IE.7
IE.4
IE.1
IE.2
102
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.
karshan M.Panakhaniya
103
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.
karshan M.Panakhaniya
104
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
karshan M.Panakhaniya
INT 0 Pin
Timer 0 Pin
105
8051 Software
Overview
1.Addressing Modes
2.Instruction Set
3.Programming
karshan M.Panakhaniya
106
karshan M.Panakhaniya
107
karshan M.Panakhaniya
108
karshan M.Panakhaniya
110
karshan M.Panakhaniya
111
SFR Addresses ( 1 of 2 )
karshan M.Panakhaniya
112
SFR Addresses ( 2 of 2 )
karshan M.Panakhaniya
113
Example
karshan M.Panakhaniya
114
karshan M.Panakhaniya
115
karshan M.Panakhaniya
116
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.
karshan M.Panakhaniya
117
karshan M.Panakhaniya
118
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.
karshan M.Panakhaniya
119
karshan M.Panakhaniya
120
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
karshan M.Panakhaniya
121
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)
karshan M.Panakhaniya
122
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
karshan M.Panakhaniya
123
;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
karshan M.Panakhaniya
124
karshan M.Panakhaniya
125
karshan M.Panakhaniya
126
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
karshan M.Panakhaniya
127
; divide A by B
Numerator
Denominator
Quotient
Remainder
byte / byte
karshan M.Panakhaniya
128
karshan M.Panakhaniya
129
karshan M.Panakhaniya
130
karshan M.Panakhaniya
131
karshan M.Panakhaniya
132
Looping
karshan M.Panakhaniya
133
karshan M.Panakhaniya
134
karshan M.Panakhaniya
135
karshan M.Panakhaniya
136
karshan M.Panakhaniya
137
Unconditional Jump
Instructions
All conditional jumps are short jumps
Target address within -128 to +127 of PC
karshan M.Panakhaniya
138
Call Instructions
LCALL (long call): 3-byte instruction
2-byte address
Target address within 64K-byte range
karshan M.Panakhaniya
139