Lecture 7 - AVR Interrupt Programming
Lecture 7 - AVR Interrupt Programming
MICRO-PROCESSING
SYSTEM
Lecture 7:
AVR Interupt Programming
in C and Assembly
These bits, along with the I bit, must be set high for an interrupt to be responded to. Upon activation
of the interrupt, the I bit is cleared by the AVR itself to make sure another interrupt can not interrupt
the microcontroller while it is servicing the current one. At the end of the ISR, the RETI instruction will
make I = 1 to allow another interrupt to come in.
int main ()
{
DDRB |= 0x20; // make DDRB.5 output
OCR0 = 40;
TCCR0 = 0x09; // CTC mode, internal elk, no
prescaler
TIMSK = (1<<OCIE0); //enable TimerO compare match int.
sei(); // enable interrupts
DDRC = 0x00; // make PORTC input
DDRD = 0xFF; // make PORTD output
while (1) // wait here
PORTD = PINC;
}
#include "avr/io.h"
#include "avr/interrupt.h"
main:
LDI R16,0xFF
OUT DDRA,R16 ; DDRA = 0xFF; // PA as an output
LDI R16,0b00001011; MCUCR = 0b00001011;
OUT MCUCR,R16
LDI R16,0x00;
OUT MCUCSR,R16 ; MCUCSR = 0; // make INT2 rising edge triggered
LDI R16,(1<<INT0)|(1<<INT1)|(1<<INT2) ;
OUT GICR,R16 ; GICR = (1<<INT0)|(1<<INT1)|
(1<<INT2) ;
EXT_INT1:
IN R16,PORTA ; PORTA ^= (1<<0) ; // toggle PORTA.0
LDI R17,(1<<1)
EOR R16,R17
OUT PORTA,R16;
RETI
EXT_INT2:
IN R16,PORTA
LDI R17,(1<<2)
EOR R16,R17
OUT PORTA,R16;
RETI
Using INT0, INT1 and INT2
Interrupts Priority
If two interrupts are activated at the same time, the interrupt
with the higher priority is served first.
The priority of each interrupt is related to the address of that
interrupt in the interrupt vector.
The interrupt that has a lower address, has a higher priority. See
Table 10-1.
For example, the address of external interrupt 0 is 2, while the
address of external interrupt 2 is 6; thus, external interrupt 0
has a higher priority, and if both of these interrupts are
activated at the same time, external interrupt 0 is served first.
Interrupt latency
The time from the moment an interrupt is activated to the
moment the CPU starts to execute the task is called the
interrupt latency. This latency is 4 machine cycle times.
During this time the PC register is pushed on the stack and the I
bit of the SREG register clears, causing all the interrupts to be
disabled.
The duration of an interrupt latency can be affected by the type
of instruction that the CPU is executing when the interrupt
comes in,
since the CPU finishes the execution of the current instruction
before it serves the interrupt. It takes slightly longer in cases
where the instruction being executed lasts for two (or more)
machine cycles (e.g., MUL) compared to the instructions that
last for only one instruction cycle (e.g., ADD).