C8 AVR Interrupts
C8 AVR Interrupts
Outline
ä Learn how to write interrupt service routine for timers and external devices.
References
1
1 Introduction
ä There are two ways to determine whether peripheral devices require attention or
services of the microcontroller: polling and interrupt.
ä This method is useful when the processor has to do only one task (or a few) and
response time is not an issue.
ä Interrupt is an event that causes the processor to suspend its present task, and
transfer the control to another program called Interrupt Service Routine (ISR) or
interrupt handler. After completing the executing of ISR, the control is transferred
back again to the main program.
2
2 AVR interrupts
ä The term interrupt vector refers to the starting address of the ISR.
ä The supported interrupts and their starting address of ATmega328P is shown below:
3
SEI ; enable interrupt
CLI ; disable interrupt
ä Each interrupt can be enabled/disabled individually using mask register of the in-
terrupt source.
2. PC =⇒ STACK
3. Address(ISR) =⇒ PC
5. STACK =⇒ PC
ä When any interrupt is generated (or asserted), it forces the microcontroller to jump
to a fixed address in the vector table.
ä There are only two bytes reserved for ISR of each interrupt.
ä When an interrupt is used, RJMP is written at the address of ISR as shown below:
.ORG 0x0000
JMP MAIN
T0ISR : ...
...
RETI
4
2.3 External Interrupts
ä AVRs have two or three external interrupts depending on the family member.
Reg. D7 D6 D5 D4 D3 D2 D1 D0
EICRA - - - - ISC11 ISC10 ISC01 ISC00
EIMSK – – – – – – INT1 INT0
EIFR – – – – – – INTF1 INTF0
ä The flag INTFn is cleared when the interrupt routine is executed. Alternatively, the
flag can be cleared by writing ’1’ to it. This flag is always cleared when INTn is
configured as a level interrupt.
Example 1 - Assume that the INT1 pin is connected to a switch that is normally
high. Whenever it goes low, it should turn on a LED. The LED is connected to PA.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 low, the LED should stay on.
.ORG 0x0000
JMP MAIN
5
ISR1: SBI PORTA, 3 ; PA.3 = 1 , turn LED on
LDI R24, 0XFF delay loop
LDI R25, 0XFF delay loop
BACK: SBIW R25:R24, 0x01
BREQ BACK ;
CBI PORTA, 3 ; PA.3 = 0 , turn LED off
RETI
6
0x0026 USART, Data Register Empty
0x0028 USART Tx Complete
0x002A ADC Conversion Complete
0x002C EEPROM Ready
0x002E Analog Comparator
0x0030 Two-wire Serial Interface
0x0032 Store Program Memory Read