MPS W7-L3 Programming AVR Interrupts II
MPS W7-L3 Programming AVR Interrupts II
If TOV0=1 NO
YES
EOR R17,R16
Timer 0 roll over
OUT PORTx,R17 TCNT0 TOV0
House-keeping
Create A Square with Timer
Interrupt
CPU Interrupt Control
Enable Interrupt
YES
Regular Work
ISR An Interrupt Event
EOR R17,R16
Timer 0 roll over
OUT
PORTx,R17 TCNT0 TOV0
RETI
Interrupt Example
• Assume that PORTC is connected to 8
switches and PORTD to 8 LEDS:
▫ Use Timer0 to generate a square wave on
PORTB.5
▫ While at the same time transfer data from
PORTC to PORTD
5
6
Example 10-2
What is the difference between the RET and RETI instructions?
Explain why we cannot use RET instead of RETI as the last
instruction of an ISR.
Solution:
Both perform the same actions of popping off the top bytes of
the
stack into the program counter, and making the AVR return to
where it left. However, RETI also performs the additional
task of setting the I flag, indicating that the servicing of the
interrupt is
over and the AVR now can accept a new interrupt on that pin.
If you use RET instead of RETI as that last instruction of the
interrupt service routine, you simply block any new interrupt
on that pin after the first interrupt, since the pin status
would indicate that the interrupt is still being serviced.
Interrupt Priority in AVR
Interrupt Priority
• If two interrupts are activated at the same time,
▫ Then the one with he higher priority is served
first.
15
Programming External Hardware
Interrupts
Review: Pin-out ATmega16As
http://ww1.microchip.com/downloads/en/devicedoc/atmel-8154-8-bit-avr-
External Interrupts
• External interrupts are triggered by the:
▫ INT0
▫ INT1
▫ INT2 [only edge triggered interrupt]
Conclusion
• Programming AVR External Interrupts
▫ INT0 & INT1
▫ INT2
▫ GIFR
25
Reading Material
• Textbook:
▫ Chapter 10
26
Questions?