8051
TIMERS
1
8051 Timer Modes
8051 TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
2
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(1), counter operation (input from Tx input
pin).
When clear(0), timer operation (input from internal 3
TMOD Register
The TMOD byte is not bit addressable.
00 MODE 0
01 MODE 1
10 MODE 2
11 MODE 3
4
TCON Register
TF 1-Timer 1 overflow flag
TF0-
TR1- Timer 1 Run control bit
TR0-
IE1- Interrupt 1
IE0-
IT1- Timer 1 interrupt
IT0-
5
8051 Timer/Counter
OSC ÷12
C/T0 TLx THx TFx
(8 Bit) (8 Bit) (1 Bit)
C/T
1
T PIN
INTERRUPT
TR
Gate
INT PIN
6
TIMER 0
OSC ÷12
C/T0
TL0 TH0 TF0
C/T
1
T
0PIN INTERRUPT
TR0
Gate
INT 0
PIN
7
TIMER 0 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C/T 0 TL0 TH0 INTERRUPT
TF0
T 0PIN
C/T
1 (5 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
Maximum Count = 1FFFh (0001111111111111)
8
TIMER 0 – Mode 1
16 Bit Timer / Counter
OSC ÷12
C/T 0 TL0 TH0 INTERRUPT
TF0
T 0PIN
C/T
1 (8 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
Maximum Count = FFFFh (1111111111111111)
9
TIMER 0 – Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC ÷12
C/T 0 TL0 TH0 INTERRUPT
TF0
T 0PIN
C/T
1 (8 Bit) (8 Bit)
TR 0
Gate Reload
INT 0 PIN
TH0
(8 Bit)
Maximum Count = FFh (11111111)
10
TIMER 0 – Mode 3
Two - 8 Bit Timer / Counter
OSC ÷12
C/T 0 TL0 INTERRUPT
TF0
T 0PIN
C/T
1 (8 Bit)
TR 0
Gate
INT 0 PIN
OSC ÷12 TH0 INTERRUPT
TF1
(8 Bit)
TR
1
11
TIMER 1
OSC ÷12
C/T0
TL1 TH1 TF1
C/T
1
T1PIN
INTERRUPT
TR1
Gate
INT1PIN
12
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C/T 0 TL1 TH1 INTERRUPT
TF1
T 1PIN
C/T
1 (5 Bit) (8 Bit)
TR1
Gate
INT1PIN
Maximum Count = 1FFFh (0001111111111111)
13
TIMER 1 – Mode 1
16 Bit Timer / Counter
OSC ÷12
C/T 0 TL1 TH1 INTERRUPT
TF1
T 1PIN
C/T
1 (8 Bit) (8 Bit)
TR1
Gate
INT1PIN
Maximum Count = FFFFh (1111111111111111)
14
TIMER 1 – Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC ÷12
C/T 0 TL1 TH1 INTERRUPT
TF1
T 1PIN
C/T
1 (8 Bit) (8 Bit)
TR1
Gate Reload
INT1PIN
TH1
(8 Bit)
Maximum Count = FFh (11111111)
15
Timer modes
TCON Register (1/2)
• Timer control register: TMOD
– Upper nibble for timer/counter, lower nibble for
interrupts
• TR (run control bit)
– TR0 for Timer/counter 0; TR1 for
Timer/counter 1.
– TR is set by programmer to turn timer/counter
on/off.
• TR=0: off (stop) TR=1: on (start)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
TCON Register (2/2)
• TF (timer flag, control flag)
– TF0 for timer/counter 0; TF1 for timer/counter 1.
– TF is like a carry. Originally, TF=0. When TH-TL roll
over to 0000 from FFFFH, the TF is set to 1.
• TF=0 : not reach
• TF=1: reach
• If we enable interrupt, TF=1 will trigger ISR.
(MSB)
TF1 TR1
(LSB) TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
Equivalent Instructions for the Timer Control
Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
SETB TF0 = SETB TCON.5
CLR TF0 = CLR TCON.5
For timer 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6
SETB TF1 = SETB TCON.7
CLR TF1 = CLR TCON.7
TCON: Timer/Counter Control Register
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
8051
Serial
Port
21
22
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
• There are special IC’s made by many manufacturers for
serial communications.
– UART (universal asynchronous Receiver transmitter)
– USART (universal synchronous-asynchronous Receiver-
transmitter)
23
614
61 5
Asynchronous – Start & Stop Bit
• Asynchronous serial data communication is widely used
for character-oriented transmissions
• The start bit is always a 0 (low) and the stop bit(s) is 1
(high)
26
Asynchronous – Start & Stop Bit
27
Data Transfer Rate
• The rate of data transfer in serial data communication is
stated in bps (bits per second).
• Another widely used terminology for bps is baud rate.
– It is modem terminology and is defined as the number of
signal changes per second
28
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:
Mode 0 :Synchronous Serial Communication
Mode 1 :8-Bit UART with Timer Data Rate
Mode 2 :9-Bit UART with Set Data Rate
Mode 3 :9-Bit UART with Timer Data Rate
29
Registers related to Serial
Communication
1. SBUF Register
2. SCON Register
3. PCON Register
30
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.
• SBUF holds the byte of data when it is received by 8051 RxD
line.
31
SBUF Register
• Sample Program:
32
SCON Register
SM0 SM1 SM2 REN TB8 RB8 TI RI
Set when a Cha-
ractor received
Set to Enable
Serial Data
reception Set when Stop bit Txed
9th Data Bit
Enable Multiprocessor 9th Data Bit
Transmitted
Communication Received in Mode 2,3
in Mode 2,3
Mode
33
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following features:
1. Serial data enters and exits through RXD
2. TXD outputs the clock
3. 8 bits are transmitted / received
4. The baud rate is fixed at (1/12) of the oscillator frequency
34
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. On receive, the stop bit goes into RB8 in SCON
4. 10 bits are transmitted / received
1. Start bit (0)
2. Data bits (8)
3. Stop Bit (1)
5. Baud rate is determined by the Timer 1 over flow rate.
35
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1. Start bit (0)
2. Data bits (9)
3. Stop Bit (1)
6. Baud rate is programmable
36
8051 Serial Port – Mode 3
The Serial Port in Mode-3 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1. Start bit (0)
2. Data bits (9)
3. Stop Bit (1)
6. Baud rate is determined by Timer 1 overflow rate.
37
8051
Interrupts
38
INTERRUPTS
• An interrupt is an external or internal event that
interrupts the microcontroller to inform it that a device
needs its service
• A single microcontroller can serve several devices by two
ways:
1. Interrupt
2. Polling
39
Interrupt
– 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) .
40
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.
Steps in executing an interrupt
• Finish current instruction and saves the PC on
stack.
• Jumps to a fixed location in memory depend on type
of interrupt
• Starts to execute the interrupt service routine until
RETI (return from interrupt)
• Upon executing the RETI the microcontroller returns
to the place where it was interrupted. Get pop PC
from stack
Interrupt Sources
• Original 8051 has 6 sources of interrupts
– Reset (RST)
– Timer 0 overflow (TF0)
– Timer 1 overflow (TF1)
– External Interrupt 0 (INT0)
– External Interrupt 1 (INT1)
– Serial Port events (RI+TI)
{Reception/Transmission of Serial Character}
8051 Interrupt Vectors
44
8051 Interrupt related Registers
• The various registers associated with the use
of interrupts are:
– TCON - Edge and Type bits for External Interrupts 0/1
– SCON - RI and TI interrupt flags for RS232
{SERIAL COMMUNICATION}
– IE - interrupt Enable
– IP - Interrupts priority
45
Enabling and Disabling an Interrupt
• The register called IE (interrupt enable) that is
responsible for enabling (unmasking) and disabling
(masking) the interrupts.
46
Interrupt Enable (IE) Register
--
• EA: Global enable/disable.
• --- : Reserved for additional interrupt hardware.
• ES : Enable Serial port interrupt.
MOV IE,#08h
or • ET1 : Enable Timer 1 control bit.
SETB ET1
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
47
Interrupt Priority
48
Interrupt Priority (IP) Register
Reserved PS PT1 PX1 PT0 PX0
Serial Port
Timer 1 Pin INT 0 Pin
INT 1 Pin Timer 0 Pin
Priority bit=1 assigns high priority
Priority bit=0 assigns low priority
49
64 8