UNIT III Introduction To Microcontroller
UNIT III Introduction To Microcontroller
UNIT III Introduction To Microcontroller
to
Microcontrollers
8051 Microcontroller 2
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
• General-purpose • Single-purpose
8051 Microcontroller 3
8051 CPU Operation
1. Features
2. Pin Diagram
3. Block Diagram
8051 Microcontroller 4
8051 Microcontroller
• Intel introduced 8051, referred as MCS- 51, in
1981.
8051 Microcontroller 5
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 6
8051 Features
• 64KB Program Memory address space
• 64KB Data Memory address space
• 4K bytes of on-chip Program Memory
• 128 bytes of on-chip Data RAM
• 32 bidirectional and individually addressable I/0 lines
• Two 16-bit timer/counters
• Full duplex UART
• 6-source/5-vector interrupt structure with two priority
levels
• On-chip clock oscillator
8051 Microcontroller 7
Pin Description of the 8051
• 8051 family members (e.g., 8751, 89C51, 89C52,
DS89C4x0)
– Have 40 pins dedicated for various functions such as I/O, RD,
WR, address, data, and interrupts.
– Come in different packages, such as
• DIP(dual in-line package),
• QFP(quad flat package), and
• LLC(leadless chip carrier)
8051 Microcontroller 9
XTAL1 and XTAL2
• The 8051 has an on-chip oscillator but requires an
external clock to run it
– A quartz crystal oscillator is connected to inputs XTAL1 (pin19)
and XTAL2 (pin18)
– The quartz crystal oscillator also needs two capacitors of 30 pF
value
8051 Microcontroller 10
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 11
XTAL1 and XTAL2 …..
• The speed of 8051 refers to the maximum oscillator
frequency connected to XTAL.
8051 Microcontroller 12
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 13
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 Microcontroller 14
EA’
• EA’, “external access’’, is an input pin and
must be connected to Vcc or GND
8051 Microcontroller 16
I/O Port Pins
• The four 8-bit I/O ports P0, P1, P2
and P3 each uses 8 pins.
8051 Microcontroller 17
Port 0
• Port 0 is also designated as AD0-AD7.
8051 Microcontroller 18
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 19
Port 3
• Port 3 can be used as input or output.
8051 Microcontroller 20
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 21
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 22
General Block Diagram of 8051
Interrupt 4K Timer 0
128 B
Control ROM RAM Timer 1
CPU
Bus Serial
OSC 4 I/O Ports
Control Port
TXD RXD
P0 P1 P2 P3
Detailed Block Diagram
8051 Microcontroller 24
8051
Memory Space
8051 Microcontroller 25
8051 Memory Structure
External
External
60K
64K 64K
SFR
EXT INT 4K
128
EA = 0 EA = 1
Direct
Addressing
Only
SFR [ Special Function
Direct & Registers]
Indirect
Addressing
128 Byte Internal RAM
8051 Microcontroller 27
Special Function Registers [SFR]
8051 Microcontroller 28
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 29
8051 instructions that affects flag
8051 Microcontroller 30
128 Byte RAM
• There are 128 bytes of RAM in the 8051.
– Assigned addresses 00 to 7FH General Purpose
Area
• The 128 bytes are divided into 3 different
groups as follows:
BIT Addressable
1. A total of 32 bytes from locations 00 to 1F 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
Reg Bank 2
are set aside for bit-addressable read/write 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 31
8051 RAM with addresses
8051 Microcontroller 32
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 33
8051 Register Banks with address
8051 Microcontroller 34
8051 Programming Model
8051 Microcontroller 35
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 37
Bit Addressable & Byte Addressable
8051 Microcontroller 38
Single bit Instructions
8051 Microcontroller 39
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 40
8051 Peripheral Overview
1. Timers
2. Serial Port
3. Interrupts
8051 Microcontroller 41
8051
TIMERS
8051 Microcontroller 42
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 43
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 44
TMOD Register
8051 Microcontroller 45
TCON Register
8051 Microcontroller 46
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 47
TIMER 0
OSC ÷12
C /T 0
TL0 TH0 TF0
C /T 1
T 0 PIN
TR 0 INTERRUPT
Gate
INT 0 PIN
TIMER 0 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN
(5 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN
(8 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN
(8 Bit) (8 Bit)
TR 0
Gate Reload
INT 0 PIN
TH0
(8 Bit)
OSC ÷12
C /T 0 TL0 INTERRUPT
C /T 1 TF0
T 0 PIN
(8 Bit)
TR 0
Gate
INT 0 PIN
TR1
TIMER 1
OSC ÷12
C /T 0
TL1 TH1 TF1
C /T 1
T 1PIN
INTERRUPT
TR1
Gate
INT1 PIN
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN
(5 Bit) (8 Bit)
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN
(8 Bit) (8 Bit)
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN
(8 Bit) (8 Bit)
TR1
Gate Reload
INT 1 PIN
TH1
(8 Bit)
8051 Microcontroller 57
Programming Timers
• Find the timer’s 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:
8051 Microcontroller 58
8051
Serial
8051 Microcontroller
Port 59
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.
8051 Microcontroller 60
Basics of Serial Communication
• Serial data communication uses two methods
– Synchronous method transfers a block of data at a time
8051 Microcontroller 61
Asynchronous – Start & Stop Bit
• Asynchronous serial data communication is widely used
for character-oriented transmissions
– Each character is placed in between start and stop bits, this is
called framing.
– Block-oriented data transfers use the synchronous method.
• The start bit is always one bit, but the stop bit can be
one or two bits
8051 Microcontroller 63
Data Transfer Rate
• The rate of data transfer in serial data communication is
stated in bps (bits per second).
8051 Microcontroller 64
8051 Serial Port
• Synchronous and Asynchronous
• SCON Register is used to Control
• Data Transfer through TXd & RXd pins
• Some time - Clock through TXd Pin
• Four Modes of Operation:
8051 Microcontroller 65
Registers related to Serial
Communication
1. SBUF Register
2. SCON Register
3. PCON Register
8051 Microcontroller 66
SBUF Register
• SBUF is an 8-bit register used solely for serial communication.
• For a byte data to be transferred via the TxD line, it must be
placed in the SBUF register.
• The moment a byte is written into SBUF, it is framed with the
start and stop bits and transferred serially via the TxD line.
• SBUF holds the byte of data when it is received by 8051 RxD
line.
• When the bits are received serially via RxD, the 8051 deframes
it by eliminating the stop and start bits, making a byte out of
the data received, and then placing it in SBUF.
8051 Microcontroller 67
SBUF Register
• Sample Program:
8051 Microcontroller 68
SCON Register
8051 Microcontroller 69
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following features:
8051 Microcontroller 70
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following features:
8051 Microcontroller 71
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the following features:
8051 Microcontroller 72
8051 Serial Port – Mode 3
The Serial Port in Mode-3 has the following features:
8051 Microcontroller 73
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the use of timer
1 in mode 2 (8-bit auto-reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode
1, where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF
register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx to see
if the character has been transferred completely.
8. To transfer the next byte, go to step 5
8051 Microcontroller 74
Programming Serial Data Reception
1. TMOD register is loaded with the value 20H, indicating the use of timer 1
in mode 2 (8-bit auto-reload) to set baud rate.
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 50H, indicating serial mode 1,
where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx to see if
an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved into a safe
place.
8. To receive the next character, go to step 5.
8051 Microcontroller 75
Doubling Baud Rate
• There are two ways to increase the baud rate of data
transfer
1. By using a higher frequency crystal
2. By changing a bit in the PCON register
•We can set it to high by software and thereby double the baud rate.
8051 Microcontroller 76
Doubling Baud Rate (cont…)
8051 Microcontroller 77
8051
Interrupts
8051 Microcontroller 78
INTERRUPTS
• An interrupt is an external or internal event that
interrupts the microcontroller to inform it that a device
needs its service
8051 Microcontroller 79
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 Microcontroller 80
Interrupt Vs Polling
• The polling method is not efficient, since it wastes much of
the microcontroller’s 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.
– Timer 0 Overflow.
– Timer 1 Overflow.
– Reception/Transmission of Serial Character.
– External Event 0.
– External Event 1.
8051 Microcontroller 85
8051 Interrupt related Registers
• The various registers associated with the use of
interrupts are:
8051 Microcontroller 86
Enabling and Disabling an Interrupt
• Upon reset, all interrupts are disabled (masked),
meaning that none will be responded to by the
microcontroller if they are activated.
8051 Microcontroller 87
Interrupt Enable (IE) Register
--
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
8051 Microcontroller 90
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 Microcontroller 91
Interrupt Priority (IP) Register
Serial Port
INT 0 Pin
Timer 1 Pin
RMKCET/EEE/VAC/ES
Read latch Vcc
TB2
Load(L1)
Internal D Q P1.X
CPU bus P1.X pin
Write to Clk Q M
latch 1
TB1
Read pin P0.x
8051 IC
RMKCET/EEE/VAC/ES
Each pin of I/O ports
Internal CPU bus:communicate with CPU
A D latch store the value of this pin
D latch is controlled by “Write to latch”
Write to latch=1:write data into the D latch
2 Tri-state buffer:
TB1: controlled by “Read pin”
Read pin=1:really read the data present at the pin
TB2: controlled by “Read latch”
Read latch=1:read value from internal latch
A transistor M1 gate
Gate=0: open
Gate=1: close
RMKCET/EEE/VAC/ES
Output Input
Tri-state control
(active high)
L L H H Low
H H Highimpedance
(open-circuit)
RMKCET/EEE/VAC/ES
Read latch Vcc
TB2
2. output pin
1. write a 1 to the pin Load(L1) is Vcc
D Q
1 P1.X
Internal
CPU bus P1.X pin
0 output 1
Write to Clk Q M
latch 1
TB1
Read pin
8051 IC
RMKCET/EEE/VAC/ES
Read latch Vcc
TB2
2. output pin
1. write a 0 to the pin Load(L1) is ground
D Q
0 P1.X
Internal
CPU bus P1.X pin
1 output 0
Write to Clk Q M
latch 1
TB1
Read pin
8051 IC
RMKCET/EEE/VAC/ES
Send data to Port 1:
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
Let P1 toggle.
You can write to P1 directly.
RMKCET/EEE/VAC/ES
When reading ports, there are two possibilities:
Read the status of the input pin. (from external pin value)
MOV A, PX
JNB P2.1, TARGET ; jump if P2.1 is not set
JB P2.1, TARGET ; jump if P2.1 is set
Figures C-11, C-12
Read the internal latch of the output port.
ANL P1, A ; P1 ← P1 AND A
ORL P1, A ; P1 ← P1 OR A
INC P1 ; increase P1
Figure C-17
Table C-6 Read-Modify-Write Instruction (or Table 8-5)
See Section 8.3
RMKCET/EEE/VAC/ES
Read latch Vcc 2. MOV A,P1
TB2 external pin=High
1. write a 1 to the pin MOV Load(L1)
P1,#0FFH
1 1 P1.X pin
Internal CPU D Q
bus
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read
latch=0 Write to latch=1
8051 IC
RMKCET/EEE/VAC/ES
Read latch Vcc 2. MOV A,P1
TB2
1. write a 1 to the pin Load(L1) external pin=Low
MOV P1,#0FFH
1 0 P1.X pin
Internal CPU D Q
bus
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read
latch=0 Write to latch=1
8051 IC
RMKCET/EEE/VAC/ES
In order to make P1 an input, the port must be programmed by writing 1
to all the bit.
RMKCET/EEE/VAC/ES
Following are instructions for reading external pins of ports:
RMKCET/EEE/VAC/ES
1. Read pin=0 Read latch=1 Write to
latch=0 (Assume P1.X=0 initially)
0 1 P1.X pin
Internal CPU D Q
bus
1 P1.X
0
Write to latch Clk Q M1
3. write result to latch Read
pin=0 Read latch=0
Write to latch=1
TB1
Read pin
8051 IC
RMKCET/EEE/VAC/ES
Read-modify-write Instructions
Table C-6
This features combines 3 actions in a single instruction:
1. CPU reads the latch of the port
2. CPU perform the operation
3. Modifying the latch
4. Writing to the pin
Note that 8 pins of P1 work independently.
RMKCET/EEE/VAC/ES
Exclusive-or the Port 1:
MOV P1,#55H ;P1=01010101
AGAIN: XOR P1,#0FFH ;complement
ACALL DELAY
SJMP AGAIN
Note that the XOR of 55H and FFH gives AAH.
XOR of AAH and FFH gives 55H.
The instruction read the data in the latch (not from the pin).
The instruction result will put into the latch and the pin.
RMKCET/EEE/VAC/ES
Mnemonics Example
RMKCET/EEE/VAC/ES
How to write the data to a pin?
How to read the data from the pin?
Read the value present at the external pin.
Why we need to set the pin first?
Read the value come from the latch(not from the external
pin).
Why the instruction is called read-modify write?
RMKCET/EEE/VAC/ES
P1, P2, and P3 have internal pull-up resisters.
P1, P2, and P3 are not open drain.
P0 has no internal pull-up resistors and does not connects to
Vcc inside the 8051.
P0 is open drain.
Compare the figures of P1.X and P0.X.
However, for a programmer, it is the same to program P0, P1,
P2 and P3.
All the ports upon RESET are configured as output.
RMKCET/EEE/VAC/ES
Read latch
TB2
Internal D Q P0.X
CPU bus P1.X pin
Write to Clk Q M
latch 1
TB1
Read pin
P1.x
8051 IC
RMKCET/EEE/VAC/ES
P0 is an open drain.
Open drain is a term used for MOS chips in the same way
that open collector is used for TTL chips.
When P0 is used for simple data I/O we must connect it to
external pull-up resistors.
Each pin of P0 must be connected externally to a 10K
ohm pull-up resistor.
With external pull-up resistors connected upon reset, port
0 is configured as an output port.
RMKCET/EEE/VAC/ES
Vcc
10 K
P0.0
Port
DS5000 P0.1
P0.2
8751 P0.3
8951 P0.4 0
P0.5
P0.6
P0.7
RMKCET/EEE/VAC/ES
When connecting an 8051/8031 to an external memory, the 8051
uses ports to send addresses and read instructions.
8031 is capable of accessing 64K bytes of external memory.
16-bit address:P0 provides both address A0-A7, P2 provides
address A8-A15.
Also, P0 provides data lines D0-D7.
When P0 is used for address/data multiplexing, it is connected to
the 74LS373 to latch the address.
There is no need for external pull-up resistors as shown in
Chapter 14.
RMKCET/EEE/VAC/ES
PSEN O
ALE G 74LS373 E
O
P0.0 C
A0
D
P0.7 A7
D0
D7
EA
P2.0 A8
P2.7 A15
8051 ROM
RMKCET/EEE/VAC/ES
2. 74373 latches
1. Send address
PSEN the address and O
to ROM
ALE send to ROM E
G 74LS373 O
P0.0 C
A0
D
P0.7 A7
Address
D0
D7
EA
P2.0 A8
P2.7 A12
8051 ROM
RMKCET/EEE/VAC/ES
2. 74373 latches
the address and
PSEN send to ROM O
ALE G 74LS373 E
O
P0.0 C
A0
D
P0.7 Address A7
D0
D7
EA 3. ROM send the
instruction back
P2.0 A8
P2.7 A12
8051 ROM
RMKCET/EEE/VAC/ES