Week 7 Micro
Week 7 Micro
2
Basics
1. Polling
2. Interrupt
5
Polling
JNB TF0,$
6
Interrupt
7
Polling Vs Interrupt
A phone with a ringer and a phone without a ringer
8
Polling Vs Interrupt
Polling
Microcontroller can serve many devices
Time wasted in waiting for certain conditions
While waiting, no other task can be performed
Interrupt
Microcontroller can serve many devices
Time not wasted in waiting for certain conditions
Microcontroller can perform other tasks and anytime a device
can interrupt it.
9
Interrupts in 8051
6 Interrupts
1. Reset
2. Timer 0 overflow
3. Timer 1 overflow
4. Reception / Transmission of serial character
5. External Event 0
6. External Event 1
10
Interrupt SFRs
11
Interrupt SFRs
IE
Interrupt Enable
Bit Addressable
IP
Interrupt Priority
Bit addressable
12
Interrupt SFRs
Interrupt Enable – IE
13
Interrupt SFRs
Interrupt Enable – IE
14
Interrupt SFRs
Interrupt Priority – IP
2 Levels of interrupt in 8051
0 (Low priority)
1 (High priority)
15
Interrupt SFRs
Interrupt Priority – IP
16
Interrupt SFRs
Interrupt Priority – IP
Nothing can interrupt a high priority interrupt – not even
another high priority interrupt
A high priority interrupt may interrupt low priority
interrupt
A low priority interrupt may only occur if no other
interrupt is already being executed
If 2 interrupts occur at same time, interrupt of higher
priority will execute first
If both interrupts have same priority, Polling Sequence
(Next Slide) will determine which interrupt will be
executed first
17
Polling Sequence
Priority
External 0 interrupt
Highest
Timer 0 Interrupt
External 1 Interrupt
Timer 1 Interrupt
Lowest
Serial Interrupt
18
How to Set Interrupt Priority
19
IVT
Interrupt Vector Table
For every interrupt, there is a fixed location in memory
that holds address of its ISR
ISR is Interrupt Service Routine
When an interrupt is generated, its ISR is automatically
executed.
20
Interrupt Vector Table (IVT) of 8051
22
Interrupt Flags
Timer Control – TCON SFR
23
Interrupt Flags
Serial Control – SCON SFR
24
Interrupt Flags - Summary
25
Clearing Interrupt Flags
Serial Interrupt.
Serial Interrupt flags are to be manually reset
26
What Happens when an Interrupt Occurs?
When interrupt is generated, a microcontroller will take the
following sequence of steps:
It completes execution of current instruction.
Saves (pushes) the return address (PC) on to the stack; low byte first.
The current status of all the Interrupt Enable bits (in IE register) are
stored internally, and interrupts of the same or lower priority are
disabled (blocked).
It jumps to memory location called IVT that keeps addresses of ISRs
PC is loaded with address of ISR
ISR is executed until “RETI= Return from Interrupt” is reached
At the end of the ISR, the RETI instruction will retrieve the saved status of
interrupt enable bits, thus, enabling the same and lower priority
interrupts again, and retrieves the return address from the stack into PC
and continue to execute from the place it was interrupted.
27
What happens when an interrupt ends?
RETI ends the execution of ISR
28
What Happens when an Interrupt Occurs?
29
Important Consideration
Take care of registers
ISR may change value of useful registers and flags
A
B
PSW
Carry
Auxiliary Carry
30
Short Interrupt Routine
ORG 00h
JMP MAIN
ORG 30h
MAIN:
.
.
END
31
Long Interrupt Routine
ORG 00h
JMP MAIN
ORG 30h
MAIN:
.
.
JMP MAIN
TOISR: .
.
RETI
END
32
Timer Interrupt
Generate 10 Khz Square Wave using Timer 0 Interrupt
Using
Polling
Interrupt
33
Timer – Polling
Generate 10 Khz Square Wave using Timer 0 Without Interrupt
ORG 00h
MOV TH0, #-50 ; Load the high byte of the initial value for Timer 0
MOV TL0, #-50 ; Load the low byte of the initial value for Timer 0
END
34
Timer – Interrupt
Generate 10 Khz Square Wave using Timer 0 Interrupt
ORG 00h
JMP MAIN ; Jump to the MAIN label
ORG 30h
MAIN:
MOV TMOD, #02h ; Set Timer 0 to operate in 8-bit auto-reload mode
MOV TH0, #-50 ; Load the high byte of the initial value for Timer 0
MOV TL0, #-50 ; Load the low byte of the initial value for Timer 0
SETB TR0 ; Set Timer 0 run bit, starts the timer
MOV IE, #82h ; Enable Timer 0 interrupt (ET0) and global interrupts (EA)
SJMP $ ; Infinite loop
35
External Interrupts
Two External Interrupts
INT0
INT1
Pins
P3.2
P3.3
Two activation levels
Level Triggered
Edge Triggered
If IT0 or IT1 (in TCON) is set, it will make INT0 or INT1
Edge Triggered. Else they will be level triggered
Default mode is level trigger
36
External Interrupts
TCON SFR
37
External Interrupts
Level Triggered
Default mode
38
External Interrupts
Edge Triggered
By making IT0 and IT1, external interrupts will be edge
triggered
40
Nested and Multiple Interrupts
What will be the response of the 8051 when the ISR of
one interrupt is in progress and another interrupt is
generated?
A low-priority interrupt can be interrupted by high-
priority interrupt but not by another low or same-priority
interrupt.
They will be serviced only after completion of high-
priority interrupts.
Assume that Timer 0 (TF0) ISR is being executed and external
interrupt 0 (INT0) is asserted. Consider IP = 0000 0001.
41
Nested and Multiple Interrupts
What will be the response of the 8051 when the ISR of one
interrupt is in progress and another interrupt is generated?
A low-priority interrupt can be interrupted by high-priority
interrupt but not by another low or same-priority interrupt.
They will be serviced only after completion of high-priority
interrupts.
Assume that Timer 0 (TF0) ISR is being executed and external
interrupt 0 (INT0) is asserted. Consider IP = 0000 0001.
ISR of Timer 0 is interrupted and the controller will service the ISR of
external interrupt 0 because it is assigned higher priority (1) using IP
register. Upon completion of ISR of external interrupt 0, the
execution of ISR of Timer 0 is resumed.
42