Upload
sunny-james
View
231
Download
0
Embed Size (px)
Citation preview
8/8/2019 Interrupts Programming
1/36
1Week 9 Vocational Training Council, Hong Kong.
Timer Programming and InterruptsTimer Programming and Interrupts
EEE3410 Microcontroller ApplicationsDepartment of Electrical Engineering
Lecture 8
8/8/2019 Interrupts Programming
2/36
2Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
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
In this LectureIn this Lecture ..
8/8/2019 Interrupts Programming
3/36
3Week 9 Vocational Training Council, Hong Kong.
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)
8/8/2019 Interrupts Programming
4/36
4Week 9 Vocational Training Council, Hong Kong.
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 valuesof the Timer 0 & Timer 1. Each timer is 16-bit register which is
split into two bytes (THx & TLx).
D0D2D4D6D8D10D12D14 D1D3D5D7D9D11D13D15
TL0TH0
Timer 0 register (16 Bit)
D0D2D4D6D8D10D12D14 D1D3D5D7D9D11D13D15
TL1TH1
Timer 1 register (16 Bit)
8/8/2019 Interrupts Programming
5/365Week 9 Vocational Training Council, Hong Kong.
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.
Timer ON/OFF control bits
IT0IE0IT1IE1TR0TF0TR1TF1
(LSB)(MSB)
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
Trigger mode of external interrupt control bit
8/8/2019 Interrupts Programming
6/366Week 9 Vocational Training Council, Hong Kong.
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.
Timer 1
M0M1C/TGATEM0M1C/TGATE
(LSB)(MSB)
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 enabledwhenever 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
Timer 0
8/8/2019 Interrupts Programming
7/367Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Timers Programming (5/10)
M1 & M0 Mode bits
Split timer mode311
8-bit auto reload timer/counter mode
THx holds a value that is to be reloaded into TLx each
time it overflows.
201
16-bit timer/counter mode
Timer value range from 0000H to FFFFH in TH - TL
110
13-bit timer/counter mode
Timer value range from 0000H to 1FFFH in TH - TL
000
Operating ModeModeM0M1
Modes 1 & 2 are used most widely.
8/8/2019 Interrupts Programming
8/368Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Example 8Example 8--11
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
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
Timers Programming (6/10)
8/8/2019 Interrupts Programming
9/369Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Example 8Example 8--22
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
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.
Timers Programming (7/10)
8/8/2019 Interrupts Programming
10/3610Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Timers Programming (8/10)
Solution:
(2)
Counts for Timer rolling over = Counts from F0ABH to FFFFHplus 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 #
Time for 1 Timer clock = 1 machine cycle = s1s12x10x12
16
=
Assume 12 MHz clock :
3410 i ll li i
8/8/2019 Interrupts Programming
11/3611Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Example 8Example 8--33
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/ 1s = 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 =D8HSETB 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 flagSJMP AGAIN
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.
Timers Programming (9/10)
EEE3410 Mic ocont olle Applications
8/8/2019 Interrupts Programming
12/3612Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Example 8Example 8--44
; Time Delay subroutine
MOV TMOD, #10H ; Timer 1, mode 1
MOV R3, #200AGAIN: MOV TL1, #08H ; TL1 =08H
MOV TH1, #01H ; TH1 =01H
SETB TR1 ; Start Timer 1
BACK: JNB TF1, BACK ; Stay until timer rolls over
CLR TR1 ; Stop Timer 1
CLR TF1 ; Clear Timer flag
DJNZ R3, AGAIN ; Loop until R3 = 0
RET
Examine the following subroutine and find the time delay for it. (12 MHz clock)
Timers Programming (10/10)
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] = 13056205Total delay time 13056205 x 1s = 13056.2 ms
Machine cycles
2
12
2
1
2
1
1
2
2
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
13/3613Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
A single microcontroller always connects to
serve several peripheral devices through itsI/O ports
There are two ways for the peripheral devicesto request service from microcontroller
Polling
Interrupt
Interrupt & PollingInterrupt & Polling
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
14/36
14Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
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 servicedThe operation described above is called polling
Programmed I/O (Polling)Programmed I/O (Polling)
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
15/36
15Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller Applications
Whenever any device needs the service, it notifies
the microcontroller by sending it an interruptrequest (IR) signal while the microcontroller isdoing other work.
The microcontroller suspends its work to service
the device at once.
Note that each IR is associated with an interruptservice routine (ISR)
Interrupt I/O (Interrupt)Interrupt I/O (Interrupt)
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
16/36
16Week 9 Vocational Training Council, Hong Kong.
pp
Comparison between Interrupt and PollingComparison between Interrupt and Polling
NoYesPriority setting
MoreLessNeed of MCUtime
SlowerFasterResponse time
MCU continuously
monitors devices to
determine whether
they need service
Devices notify MCU
by sending it an
interrupt signals
while the MCU is
doing another work
Method
PollingInterrupt
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
17/36
17Week 9 Vocational Training Council, Hong Kong.
Steps in Handling an Interrupt Request
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 RETIinstruction (return from interrupt)
Return to main program by popping the PC from the
stack
When an interrupt activates and is accepted by the MCU, the main
program is interrupted. The following actions occurs:
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
18/36
18Week 9 Vocational Training Council, Hong Kong.
Program Execution with Interrupts
Main Main Main
ISR ISR
stack
PC saved
on stack
PC popped
from 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
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
19/36
19Week 9 Vocational Training Council, Hong Kong.
There are 6 interrupts in the 8051
Types of Interrupt in the 8051Types of Interrupt 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 upexecution.
2 external interrupts Hardware external interrupts (INT0 andINT1) at pins 12 & 13 are used to receive interrupt signals fromexternal devices.
2 timer interrupts They are Timer 0 and Timer 1 which will giveout interrupt signal when the timers count to zero.
1 serial port interrupt - It is used for data reception andtransmission during serial communication.
Apart from the Reset, only the 5 interrupts are available to the user
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
20/36
20Week 9 Vocational Training Council, Hong Kong.
Interrupt Vector Table for the 8051
Serial port
Timer 1
External 1
(INT1)
Timer 0
External 0
(INT0)
Reset
Interrupt
RI or TI
TF1
IE1
TF0
IE0
RST
Flag
---0023H6
---001BH5
P3.3 (13)0013H4
---000BH3
P3.2 (12)0003H2
90000H1
PinROM locationPriority
interrupt vector table holds the addresses of ISR
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
21/36
21Week 9 Vocational Training Council, Hong Kong.
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
EX0ET0EX1ET1ESET2--EAIE.0IE.1IE.2IE.3IE.4IE.5IE.6IE.7
IE (interrupt enable) register
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
22/36
22Week 9 Vocational Training Council, Hong Kong.
Enabling and Disabling an Interrupt
Enable/disable external interrupt 0EX0IE.0
Enable/disable timer 0 interruptET0IE.1
Enable/disable external interrupt 1EX1IE.2
Enable/disable timer 1 interruptET1IE.3
Enable/disable serial port interruptESIE.4
Not use for 8051 (8052 only)ET2IE.5
Not implemented, reserved for future use--IE.6
Global Enable/disableEAIE.7
Description (1=Enable, 0=Disable)SymbolBit
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
23/36
23Week 9 Vocational Training Council, Hong Kong.
Example 8Example 8--55
Solution:
(a) MOV IE, #10010110B ; enable serial, Timer 0, EX1 interruptsor
SETB IE.7 ; EA=1, Global enable
SETB IE.4 ; enable serial interrupt
SETB IE.1 ; enable Timer 0 interruptSETB IE.2 ; enable EX1 interrupt
(b) CLR IE.1 ; disable Timer 0 interrupt
(c) CLR IE.7 ; disable all interrupts
Write instructions to
(a) Enable serial interrupt, Timer 0 interrupt and external interrupt 1, an
(b) Disable Timer 0 interrupt only, then
(c) Disable all the interrupt with a single instruction
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
24/36
24Week 9 Vocational Training Council, Hong Kong.
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 isset, the microcontroller is interrupted and jumps to the interruptvector table to service the ISR.
With timer interrupt is enabled, microcontroller can do other thingsand no need to monitor the TF for rolling over.
000BHJumps to1
Timer 0 Interrupt VectorTF0
001BHJumps to1
Timer 1 Interrupt VectorTF1
8/8/2019 Interrupts Programming
25/36
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
26/36
26Week 9 Vocational Training Council, Hong Kong.
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.
(RI+TI)Serial Communication
(TF1)Timer Interrupt 1(INT1)External Interrupt 1
(TF0)Timer Interrupt 0
(INT0)External Interrupt 0
Table 8: 8051 Interrupt Priority Upon Reset
HighestPriority
Lowest
Priority
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
27/36
27Week 9 Vocational Training Council, Hong Kong.
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.
PX0PT0PX1PT1PSPT2----
IP.0IP.1IP.2IP.3IP.4IP.5IP.6IP.7
IE (interrupt priority) register (Bit-addressable)
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
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
28/36
28Week 9 Vocational Training Council, Hong Kong.
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 thesame 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 > TF0The 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)
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
29/36
29Week 9 Vocational Training Council, Hong Kong.
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 higherpriority level compared with the rest of the interrupts.
Priority of interrupt will then beINT1 > TF1 > INT0 > TF0 > Serial
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
30/36
30Week 9 Vocational Training Council, Hong Kong.
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
Main Main Main
ISR ISR
ISR
ISR
Low ISR is
interrupted by a
higher priority ISR
higher ISRlower Lower ISR canninterrupt a higher
priority ISR
8/8/2019 Interrupts Programming
31/36
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
32/36
32Week 9 Vocational Training Council, Hong Kong.
Example 8.9 - Solution
ORG 0000HLJMP MAIN ;by-pass interrupt vector table
; ISR for INT1
ORG 0013HLJMP ISR_INT1 ; jump to ISR_INT1
; Main ProgramORG 30H
MAIN: MOV IE,#10000100B ;enable external INT 1HERE: SJMP HERE ;stay here until get interrupted
; INT1 ISR
ISR_INT1: SETB P1.3 ;turn on LEDMOV R3,#255BACK: DJNZ R3, BACK ;keep LED on for a while
CLR P1.3 ;turn off LEDRETI
END
8/8/2019 Interrupts Programming
33/36
8/8/2019 Interrupts Programming
34/36
EEE3410 Microcontroller Applications
8/8/2019 Interrupts Programming
35/36
35Week 9 Vocational Training Council, Hong Kong.
The 8051 Microcontroller and Embedded Systems -
Using Assembly and C, Mazidi
Chapter 9 P.239 P.255
Chapter 11 P.317 P.339
Read referenceRead reference
8/8/2019 Interrupts Programming
36/36
1Week 9 Vocational Training Council, Hong Kong.
EEE3410 Microcontroller ApplicationsDepartment of Electrical Engineering
END of Lecture 8 Timer Programming and InterruptsTimer Programming and Interrupts