Anda di halaman 1dari 36

EEE3410 Microcontroller Applications

Department of Electrical Engineering

│ Lecture 8 │

Timer Programming and Interrupts

© Vocational Training Council, Hong Kong. Week 9 1


EEE3410 Microcontroller Applications

In this Lecture ….
Timer Programming
Contrast and compare interrupt versus polling
Interrupt Handling
Interrupts of the 8051
Purpose of the interrupt vector table
Enable or disable interrupts
Timers using interrupts
Interrupt priority

© Vocational Training Council, Hong Kong. Week 9 2


EEE3410 Microcontroller Applications

Timers Programming (1/10)

The 8051 has two timers: Timer 0 and Timer 1


They can be used either as timers to generate a time delay of
as counter to count events happening outside the
microcontroller
Both Timer 0 and Timer 1 are 16 bits wide
They are accessed as two separate registers, low byte and
high byte. (TL0 & TH0 for Time 0 and TL1 & TH1 for
timer 1)

© Vocational Training Council, Hong Kong. Week 9 3


EEE3410 Microcontroller Applications

Timers Programming (2/10)

There are three Special Function Registers for timer settings


1. Timer Registers (Timer 0 & Timer 1) – store the starting values
of the Timer 0 & Timer 1. Each timer is 16-bit register which is
split into two bytes (THx & TLx).
TH0 TL0

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

Timer 0 register (16 Bit)


TH1 TL1

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

Timer 1 register (16 Bit)

© Vocational Training Council, Hong Kong. Week 9 4


EEE3410 Microcontroller Applications

Timers Programming (3/10)


2. Timer Control Register (TCOD) – use to turn ON/OFF of the timers
and timer interrupt control. It is a 8-bit register and bit-addressable,
and only the upper 4-bit refers to timer control.
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Timer ON/OFF control bits Trigger mode of external interrupt control bits

TR1 –Timer 1 run control bit. Set = Timer ON and Clear = Timer OFF
TF1 – Timer flag which is set when the Timer 1 rolls over from FFFFH to 0000H.
TR0 – Timer 0 run control bit. Set = Timer ON and Clear = Timer OFF
TF0 – Timer flag which is set when Timer 0 rolls over from FFFFH to 0000H.
IE1 and IT1 – Set the trigger mode of external interrupt 1
IE0 and IT0 – Set the trigger mode of external interrupt 1

© Vocational Training Council, Hong Kong. Week 9 5


EEE3410 Microcontroller Applications

Timers Programming (4/10)

3. Timer Mode Register (TMOD) – use to set the various timer operation
modes. It is a 8-bit register and bit-addressable.
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0

Timer 1 Timer 0
GATE – Gating control when set. The timer/counter is enable only while the INTx
pin is high and the TRx control pin is set. When cleared, the timer is enabled
whenever the TRx control bit is set.
C/T – Timer or counter selected. Cleared for timer operation (input from internal
system clock) and Set for counter operation (input from Tx input pin)
M1 & M0 – Mode bits

© Vocational Training Council, Hong Kong. Week 9 6


EEE3410 Microcontroller Applications

Timers Programming (5/10)


M1 & M0 – Mode bits

M1 M0 Mode Operating Mode


0 0 0 13-bit timer/counter mode
Timer value range from 0000H to 1FFFH in TH - TL
0 1 1 16-bit timer/counter mode
Timer value range from 0000H to FFFFH in TH - TL
1 0 2 8-bit auto reload timer/counter mode
THx holds a value that is to be reloaded into TLx each
time it overflows.
1 1 3 Split timer mode

Modes 1 & 2 are used most widely.

© Vocational Training Council, Hong Kong. Week 9 7


EEE3410 Microcontroller Applications

Timers Programming (6/10)


Example 8-1
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:

Convert the values from hex to binary:

(a) TMOD = 0000 0001, mode 1 of Timer 0 is selected


(b) TMOD = 0010 0000, mode 2 of Timer 1 is selected
(c) TMOD = 0001 0010, mode 2 of Timer 0, and mode 1 of Timer 1are
selected

© Vocational Training Council, Hong Kong. Week 9 8


EEE3410 Microcontroller Applications

Timers Programming (7/10)


Example 8-2
1. Write instructions to do the followings:
a. Set Timer 0 in mode 1, use 8051 XTAL for the clock source,
instructions to start and stop the timer,
b. Set value F0ABH to Timer 0.
c. Start Timer 0
2. Determine the time for Timer 0 rolling over if XTAL = 12 MHz.

Solution:
(1)
MOV TMOD, #02H ; Timer 0 and mode 1 set, C/T = 0 to use XTAL
; clock source, Gate = 0 to use software ON/OFF
MOV TL0, #0ABH ; TL0 =ABH
MOV TH0, #0F0H ; TH0 =F0H
SETB TR0 ; Start Timer 0

© Vocational Training Council, Hong Kong. Week 9 9


EEE3410 Microcontroller Applications

Timers Programming (8/10)

Solution:
(2)
Assume 12 MHz clock :
1
Time for 1 Timer clock = 1 machine cycle = 6
x12 s = 1µs
12x10

Counts for Timer rolling over = Counts from F0ABH to FFFFH


plus rolling over to 0

Timer clock cycles = (FFFFH – F0ABH + 1) = 0F55H


= 3925 in decimal

∴ Time for Timer 0 rolls over = 3925 x 1 µs = 3925 µs #

© Vocational Training Council, Hong Kong. Week 9 10


EEE3410 Microcontroller Applications

Timers Programming (9/10)


Example 8-3
Assume XTAL = 12 MHz, write a program to generate a square wave of 50 Hz
frequency on pin P1.3 by using timer 1 as time control.
Solution:
The period of the square wave, T = 1/50 Hz = 20 ms
½ of it for the high and low portions of the pulse = 10 ms
10 ms/ 1µs = 10000 timer cycles are needed for each pulse.
Timer 1 value to be set = 65536 – 10000 = 55536 in decimal = D8F0H
i.e. TH1 = D8H and TL1 = F0H

MOV TMOD, #10H ; Timer 1, mode 1


AGAIN: MOV TL1, #0F0H ; TL1 =F0H
MOV TH1, #0D8H ; TH1 =D8H
SETB TR1 ; Start Timer 1
BACK: JNB TF1, BACK ; Stay until timer rolls over
CLR TR1 ; Stop Timer 1
CPL P1.3 ; Complement P1.3 to set Hi, Low
CLR TF1 ; Clear Timer flag
SJMP AGAIN

© Vocational Training Council, Hong Kong. Week 9 11


EEE3410 Microcontroller Applications

Timers Programming (10/10)


Example 8-4
Examine the following subroutine and find the time delay for it. (12 MHz clock)
; Time Delay subroutine Machine cycles
MOV TMOD, #10H ; Timer 1, mode 1 2
MOV R3, #200 1
AGAIN: MOV TL1, #08H ; TL1 =08H 2
MOV TH1, #01H ; TH1 =01H 2
SETB TR1 ; Start Timer 1 1
BACK: JNB TF1, BACK ; Stay until timer rolls over 2
CLR TR1 ; Stop Timer 1 1
CLR TF1 ; Clear Timer flag 1
DJNZ R3, AGAIN ; Loop until R3 = 0 2
RET 2

Solution :
Timer value = 0108H = 264 in decimal, counts to rolls over = 65536 – 264 = 65272 in decimal
Timer cycle of Timer = 65272
Total machine cycles of the subroutine = [2 + 1 + (5 + 65272 + 4) x 200 + 2] = 13056205
Total delay time 13056205 x 1µs = 13056.2 ms

© Vocational Training Council, Hong Kong. Week 9 12


EEE3410 Microcontroller Applications

Interrupt & Polling


A single microcontroller always connects to
serve several peripheral devices through its
I/O ports
There are two ways for the peripheral devices
to request service from microcontroller
Polling
Interrupt

© Vocational Training Council, Hong Kong. Week 9 13


EEE3410 Microcontroller Applications

Programmed I/O (Polling)

Microcontroller continuously monitors the status


of a certain number of devices in sequence
Services to a device if preset condition met
After the service, the microcontroller will move
on to monitor the status of another device until
all devices are serviced
The operation described above is called “polling”

© Vocational Training Council, Hong Kong. Week 9 14


EEE3410 Microcontroller Applications

Interrupt I/O (Interrupt)

Whenever any device needs the service, it notifies


the microcontroller by sending it an interrupt
request (IR) signal while the microcontroller is
doing other work.
The microcontroller suspends its work to service
the device at once.
Note that each IR is associated with an interrupt
service routine (ISR)

© Vocational Training Council, Hong Kong. Week 9 15


EEE3410 Microcontroller Applications

Comparison between Interrupt and Polling

Interrupt Polling
Method Devices notify MCU MCU continuously
by sending it an monitors devices to
interrupt signals determine whether
while the MCU is they need service
doing another work
Response time Faster Slower
Need of MCU Less More
time

Priority setting Yes No

© Vocational Training Council, Hong Kong. Week 9 16


EEE3410 Microcontroller Applications

Steps in Handling an Interrupt Request


When an interrupt activates and is accepted by the MCU, the main
program is interrupted. The following actions occurs:

‹ The current instruction will be finished


‹ The PC is saved on the stack
‹ The current interrupt status is saved internally
‹ The PC is loaded with the vector address of the ISR
from the interrupt vector table (Jump to execute ISR)
‹ The ISR is executed and will be finished with a RETI
instruction (return from interrupt)
‹ Return to main program by popping the PC from the
stack
© Vocational Training Council, Hong Kong. Week 9 17
EEE3410 Microcontroller Applications

Program Execution with Interrupts


ISR ISR

Main Main Main

PC saved PC popped
on stack from stack
stack

The program that deals with an interrupt is called an interrupt


service routine (ISR)
ISR executes in response to the interrupt and generally performs I/O
operation to a device
© Vocational Training Council, Hong Kong. Week 9 18
EEE3410 Microcontroller Applications

Types of Interrupt in the 8051


There are 6 interrupts in the 8051
Reset – when the reset pin is activated, the 8051 will reset all
registers and ports and jumps to address location 0000H starting up
execution.
2 external interrupts – Hardware external interrupts (INT0 and
INT1) at pins 12 & 13 are used to receive interrupt signals from
external devices.
2 timer interrupts – They are Timer 0 and Timer 1 which will give
out interrupt signal when the timers count to zero.
1 serial port interrupt - It is used for data reception and
transmission during serial communication.
Apart from the Reset, only the 5 interrupts are available to the user

© Vocational Training Council, Hong Kong. Week 9 19


EEE3410 Microcontroller Applications

Interrupt Vector Table for the 8051


¾ interrupt vector table holds the addresses of ISR
Priority Interrupt Flag ROM location Pin

1 Reset RST 0000H 9


External 0
2 IE0 0003H P3.2 (12)
(INT0)
3 Timer 0 TF0 000BH ---
External 1
4 IE1 0013H P3.3 (13)
(INT1)
5 Timer 1 TF1 001BH ---

6 Serial port RI or TI 0023H ---


© Vocational Training Council, Hong Kong. Week 9 20
EEE3410 Microcontroller Applications

Enabling and Disabling an Interrupt


Upon reset, all interrupts are disabled
The interrupts must be enabled by software
A register called IE (interrupt enable) register, which is bit-
addressable, is responsible for enabling and disabling the interrupts
Bit IE. 7 must be set high to allow the rest of register to take effect
¾ EA = 1 ; Global enable interrupt
¾ EA = 0 ; Global disable interrupt

IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0


EA -- ET2 ES ET1 EX1 ET0 EX0

IE (interrupt enable) register

© Vocational Training Council, Hong Kong. Week 9 21


EEE3410 Microcontroller Applications

Enabling and Disabling an Interrupt

Bit Symbol Description (1=Enable, 0=Disable)


IE.7 EA Global Enable/disable
IE.6 -- Not implemented, reserved for future use
IE.5 ET2 Not use for 8051 (8052 only)
IE.4 ES Enable/disable serial port interrupt
IE.3 ET1 Enable/disable timer 1 interrupt
IE.2 EX1 Enable/disable external interrupt 1
IE.1 ET0 Enable/disable timer 0 interrupt
IE.0 EX0 Enable/disable external interrupt 0

© Vocational Training Council, Hong Kong. Week 9 22


EEE3410 Microcontroller Applications

Example 8-5
Write instructions to
(a) Enable serial interrupt, Timer 0 interrupt and external interrupt 1, and
(b) Disable Timer 0 interrupt only, then
(c) Disable all the interrupt with a single instruction

Solution:
(a) MOV IE, #10010110B ; enable serial, Timer 0, EX1 interrupts
or
SETB IE.7 ; EA=1, Global enable
SETB IE.4 ; enable serial interrupt
SETB IE.1 ; enable Timer 0 interrupt
SETB IE.2 ; enable EX1 interrupt
(b) CLR IE.1 ; disable Timer 0 interrupt
(c) CLR IE.7 ; disable all interrupts

© Vocational Training Council, Hong Kong. Week 9 23


EEE3410 Microcontroller Applications

Timer Interrupt
When timer rolls over, its timer flag (TF) is set.
If the timer interrupt in the IE register is enable, whenever the TF is
set, the microcontroller is interrupted and jumps to the interrupt
vector table to service the ISR.
With timer interrupt is enabled, microcontroller can do other things
and no need to monitor the TF for rolling over.

TF0 Timer 0 Interrupt Vector


1 Jumps to 000BH

TF1 Timer 1 Interrupt Vector


1 Jumps to 001BH

© Vocational Training Council, Hong Kong. Week 9 24


EEE3410 Microcontroller Applications

Example 8-6
Write a program to generate a square wave of 50 Hz on pin P1.3. This is similar
to Example 8.3 except that it uses an interrupt for Timer 1.
Solution:
ORG 0000H
LJMP MAIN ;by-pass interrupt vector table
; ISR for Timer 1 to generate square wave
ORG 001BH
LJMP ISR_T1 ; jump to ISR_T1
; Main Program for initialization
MAIN: ORG 30H
MOV TMOD, #10H ; Timer 1, mode 1
MOV TL1, #0F0H ; TL1 =F0H
MOV TH1, #0D8H ; TH1 =D8H
MOV IE, #88H ; enable Timer 1 interrupt
SETB TR1 ; Start Timer 1
HERE: SJMP HERE
; Timer 1 ISR
ISR_T1: CPL P1.3 ; Complement P1.3 to set Hi, Low
MOV TL1, #0F0H ; reload timer value
MOV TH1, #0D8H ;
SETB TR1 ; Start Timer 1
RETI
END
© Vocational Training Council, Hong Kong. Week 9 25
EEE3410 Microcontroller Applications

Interrupt Priority
Upon reset, the priorities of interrupt source are assigned from
top to bottom as in the following Table 8 , i.e. if INT0 and
INT1 are activated at the same time, INT0 is first responded.

Table 8: 8051 Interrupt Priority Upon Reset


External Interrupt 0 (INT0) Highest
Priority
Timer Interrupt 0 (TF0)
External Interrupt 1 (INT1)
Timer Interrupt 1 (TF1)
Lowest
Serial Communication (RI+TI) Priority

© Vocational Training Council, Hong Kong. Week 9 26


EEE3410 Microcontroller Applications

Setting Interrupt Priority with the IP register


The sequence of Table 8 can be changed by assigning a
higher priority to any one of the interrupts.
It is done by setting high at the corresponding bit in the IP
(interrupt priority) register.

Priority bit = 1 (assign high priority) Priority bit = 0 (assign low priority)

IP.7 IP.6 IP.5 IP.4 IP.3 IP.2 IP.1 IP.0


-- -- PT2 PS PT1 PX1 PT0 PX0
IE (interrupt priority) register (Bit-addressable)

PT2, PT1 & PT0 – Timer 2 (8052 only), Timer 1 & Timer 0 interrupts
PX1 & PX0– External interrupts 1 & 0, PS – Serial port interrupt
© Vocational Training Council, Hong Kong. Week 9 27
EEE3410 Microcontroller Applications

Setting Interrupt Priority with the IP register

Example 8.7 :
(a) Program the IP register to assign the highest priority to INT1,
(b) Discuss what happens if INT0, INT1 and TF0 are activated at the
same time.

Solution :
(a) MOV IP, #00000100B ; set IP.2=1 INT1 has the highest priority
or SETB IP.2
(b) Priority of interrupt will be changed to INT1 > INT0 > TF0
The 8051 will services INT1 first and then INT0 and TF0.
(As the INT0 and TF0 bits in IP register are 0, their priorities follow the
sequence in Table 8.1)

© Vocational Training Council, Hong Kong. Week 9 28


EEE3410 Microcontroller Applications

Setting Interrupt Priority with the IP register


Example 8.8 :
The IP register is set by the instruction “MOV IP, #00001100B”
after reset. Discuss the sequence in which the interrupts are serviced.

Solution :
MOV IP, #00001100B instruction sets INT1 & TF1 to a higher
priority level compared with the rest of the interrupts.

Priority of interrupt will then be


INT1 > TF1 > INT0 > TF0 > Serial

© Vocational Training Council, Hong Kong. Week 9 29


EEE3410 Microcontroller Applications

Interrupt inside an interrupt


Higher-priority interrupt can interrupt a low-priority interrupt.
An interrupt cannot be interrupted by a low-priority interrupt
No low priority interrupt can get the immediate attention of the
CPU until the 8051 has finished servicing the high-priority
interrupts

Low ISR is higher ISR lower ISR Lower ISR cannot


interrupted by a interrupt a higher
higher priority ISR priority ISR
ISR ISR ISR

Main Main Main

© Vocational Training Council, Hong Kong. Week 9 30


EEE3410 Microcontroller Applications

Example 8.9
Assume that the INT1 pin is connected to a switch that
is normally high. Whenever it goes low, it should turn on
an LED. The LED is connected to P1.3 and is normally off.
When it is turned on it should stay on for a fraction of a
second. As long as the switch is pressed, the LED should
stay on.
5v 8051
P1.3 to LED
INT1

0v

© Vocational Training Council, Hong Kong. Week 9 31


EEE3410 Microcontroller Applications

Example 8.9 - Solution


ORG 0000H
LJMP MAIN ;by-pass interrupt vector table
; ISR for INT1
ORG 0013H
LJMP ISR_INT1 ; jump to ISR_INT1
; Main Program
ORG 30H
MAIN: MOV IE,#10000100B ;enable external INT 1
HERE: SJMP HERE ;stay here until get interrupted
; INT1 ISR
ISR_INT1: SETB P1.3 ;turn on LED
MOV R3,#255
BACK: DJNZ R3, BACK ;keep LED on for a while
CLR P1.3 ;turn off LED
RETI
END

© Vocational Training Council, Hong Kong. Week 9 32


EEE3410 Microcontroller Applications

Review Questions
1. How many timers do we have in the 8051_____ ?(8052 _____?)
2. The timers of the 8051 are ___-bit and are designated as ____
and _______.
3. The registers of Timer 0 are accessed as ______ and ______.
4. The registers of Timer 1 are accessed as ______ and ______.
5. The TMOD register is ____-bit register.
6. Find the TMOD value for both Timer 0 and Timer 1, mode 2,
software start/stop (gate=0), with driving by internal clock.
7. Find the frequency and period used by the timer if the crystal
attached to the 8051 has the following values.
(a) XTAL = 11.0592 MHz (b) XTAL = 12 MHz
(c) XTAL = 16 MHz (d) XTAL = 20 MHz
(e) XTAL = 24 MHz (f) XTAL = 30 MHz

© Vocational Training Council, Hong Kong. Week 9 33


EEE3410 Microcontroller Applications

Review Questions
8. Find the value (in hex) loaded into TH in each of the following.
(a) MOV TH1, #-120 (b) MOV TH1, #-67
(c) MOV TH1, #-222 (d) MOV TH0, #-104
9. What address in the interrupt vector table is assigned to Time
0?
10. To put the entire interrupt service routine in the interrupt
vector table, it must be no more than _____ bytes in size.
11. Why do we put an LJMP instruction at address 0?
12. What register keeps track of interrupt priority in the 8051? Is it
a bit-addressable register?
13. If the value of IP register is 09H, arrange the priority of the 5
interrupts (exclude Reset) of 8051 in descending order.
14. Assume that the IP register contains all 0s. Explain what
happens if both INT0 and INT1 are activated at the same time.
© Vocational Training Council, Hong Kong. Week 9 34
EEE3410 Microcontroller Applications

Read reference
The 8051 Microcontroller and Embedded Systems -
Using Assembly and C, Mazidi
¾ Chapter 9 P.239 – P.255
¾ Chapter 11 P.317 – P.339

© Vocational Training Council, Hong Kong. Week 9 35


EEE3410 Microcontroller Applications
Department of Electrical Engineering

│ END of Lecture 8 │

Timer Programming and Interrupts

© Vocational Training Council, Hong Kong. Week 9 1

Anda mungkin juga menyukai