EEE3410 Microcontroller Applications Department of Electrical Engineering
EEE3410 Microcontroller Applications Department of Electrical Engineering
│ Lecture 8 │
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
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
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
Solution:
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
Solution:
(2)
Assume 12 MHz clock :
1
Time for 1 Timer clock = 1 machine cycle = 6
x12 s = 1µs
12x10
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
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
PC saved PC popped
on stack from stack
stack
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
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.
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.
Priority bit = 1 (assign high priority) Priority bit = 0 (assign low priority)
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
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)
Solution :
MOV IP, #00001100B instruction sets INT1 & TF1 to a higher
priority level compared with the rest of the interrupts.
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
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
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
│ END of Lecture 8 │