Professional Documents
Culture Documents
4 - Interrupts
4 - Interrupts
4 - Interrupts
MODULE 4
Embedded Design – PCB Considerations
2
Atmega16 Interrupts
Interrupts
◼ Atmega16 has 3 external interrupts and 17 internal
interrupts (and RESET)
or Interrupt Handler
◼ For every interrupt there is a fixed location in memory
that holds the address of its ISR
◼ The group of memory locations set aside to hold the
L
External Interrupts
The External Interrupts are triggered by the INT0 (PD2),
INT1(PD3), and INT2 (PB2) pins
INT2
◼ It is only edge triggered
interrupt 1 is enabled
◼ The corresponding interrupt of External Interrupt
◼ For Interrupt 2
◼ Set the SREG bit-I
◼ Set the INT2 pin of GICR
◼ Select its mode by setting bit ISC2 of MCUCSR
accordingly
External Interrupts Flags Register
For interrupt vector name use the ISR names given in the
table on next slides; for example for external interrupt0
ISR(INT0_vect)
{
}
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
• Int_nested.c
More Details on Page 11 and Page 42 of Datasheet • Case 1
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 user’s preferences.
Int_nested.c
More Details on Page 11 and Page 42 of Datasheet Case 2
Nested Interrupt - Example
void main ()
{
DDRA=0xFF;
DDRC=0xFF;
PORTA=0;
PORTC=0;
while(1)
{
DDRD=0xFF;
DDRB=0xFF;
PORTB=0;
PORTD=0;
PORTD=0x04;
}
}
Nested Interrupt - Example
ISR(INT0_vect)
{
SREG|=0x80;
PORTA=0x06;
PORTD=0;
PORTD=0x08;
}
ISR(INT1_vect)
{
SREG|=0x80;
PORTC=0x05;
PORTB=0;
PORTB=0x04;
}
ISR(INT2_vect)
{
SREG|=0x80;
PORTC|=0xF0;
PORTA|=0x50;
}
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
Thank You