3 AVR Interrupts
3 AVR Interrupts
AVR Lecture 3
Interrupts
Atmega16 has 3 external interrupts and 17 internal
interrupts
12 / 50
External Interrupts
The External Interrupts are triggered by the INT0 (PD2),
INT1(PD3), and INT2 (PB2) pins
INT2
It is only edge triggered
For interrupt vector name use the ISR names given in the
table on next slides; for example for external interrupt0
ISR(INT0_vect)
{
}
@ Dr Javaid, M.H Fidai 28 / 50
Vector Names for WinAVR(AVR GCC)
External interrupt 0 INT0_vect
External interrupt 1 INT1_vect
External interrupt 2 INT2_vect
ADC Conversion Complete ADC_vect
Analog Comparator ANA_COMP_vect
Serial Transfer Complete SPI_STC_vect
Store Program Memory Ready SPM_RDY_vect
Timer/Counter0 Compare Match TIMER0_COMP_vect
Timer/Counter0 Overflow TIMER0_OVF_vect
Timer/Counter Capture Event TIMER1_CAPT_vect
Timer/Counter1 Compare Match A TIMER1_COMPA_vect
Timer/Counter1 Compare MatchB TIMER1_COMPB_vect
Timer/Counter1 Overflow TIMER1_OVF_vect
ISR(INT0_vect)
{ PORTC ^=1<<3;
}
AVR_ex_10.9.c
Int. isis
@ Dr Javaid, M.H Fidai 32 / 50
Interrupt within Interrupt
When an interrupt occurs, the Global Interrupt Enable
I-bit is cleared hence no other interrupt will interrupt this
ISR.
The I-bit is automatically set when a Return from
Interrupt instruction is executed
While executing an ISR if any interrupt occurs it will be
latched since I-bit is disabled and will be served after
completion of current ISR
If more than one interrupt occurs during an ISR, they will
be served according to the Polling sequence.
Int_nested.c
More Details on Page 11 and Page 42 of Datasheet Case 1
@ Dr Javaid, M.H Fidai 33 / 50
Nested Interrupts
What will happen if we set I bit of SREG in ISR ?
This will allow all enabled interrupts to interrupt this ISR.
If the I bit of SREG is set in an ISR and another interrupt
occurs, the controller will finish the currently executing
instruction and then serve the interrupt that occurs. After
serving that interrupt it will return to this ISR.
Since this ISR can be interrupted by all other enabled
interrupts, this interrupt has lower priority in comparison
to the other interrupts. But this priority is user defined and
depends on the users preferences.
Int_nested.c
More Details on Page 11 and Page 42 of Datasheet Case 2
@ Dr Javaid, M.H Fidai 34 / 50
Can interrupts occur while I bit of SREG
is disabled?
Interrupts events (that is, noticing the event) can occur at
any time, and most are remembered by setting an "interrupt
event" flag (GICR, local int enable) inside the processor. If
interrupts are disabled (SREG, global int enable), then that
interrupt will be handled when they are enabled again, in
priority order (polling sequence).
while(1)
{ PORTB=0;
PORTD=0;
cli(); // Disables Interrupt
PORTD=0x04; // calling INT 0
x++;
x++;
sei(); // Enables Interrupt
x++;
Int_nested.c
} Case 3
@ Dr Javaid, M.H Fidai 38 / 50
Arduino Interrupt Functions
External Interrupts
attachInterrupt()
detachInterrupt()
Interrupts
interrupts() -> sei() [SEt Interrupt ]
noInterrupts() -> cli() [CLear Interrupt]
Mode Defines when the interrupt should be triggered. Four constants are
predefined as valid values:
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.
void setup()
{ pinMode ( pin, OUTPUT);
attachInterrupt (0, blink, CHANGE);
}
void loop()
{ digitalWrite (pin, state);
}
void blink()
{ state = !state;
}
msb = *handle->baseAddr;
Between reads, the bytes are changed in the latch.
Without the volatile, the compiler optimizes this to a
single assignment:
The above is an example where the user waits for the status register to
become non-zero. To the right is the code, that is produced by the
compiler with optimization turned on. The logic of the optimizer is simple:
having already read the variable's value into the accumulator (on the
second line of assembly), there is no need to reread it, since the value
will always be the same. Thus, in the third line, we end up with an infinite
loop. To force the compiler to do what we want, we modify the
declaration as follows:
Syntax
detachInterrupt (interrupt)
Parameters
Interrupt:- The number of the interrupt to disable
void setup() {}
void loop()
{ noInterrupts();
// critical, time-sensitive code here
interrupts();
// other code here
}