MEAM.Design - ATmega32 Programming - Interrupts


To support interrupts on the ATmega32, you must include <avr/interrupt.h>, which is included by default in m_general.h or teensy_general.h file.

To then enable global interrupts, call the function sei();. To disable global interrupts, call the function cli();.

To service an interrupt, you must write a separate function within your C file according to the following syntax:

ISR(SOURCE_vect){
...
}

where SOURCE corresponds to one of the sources listed below:

INT0_vect External Interrupt Request 0
INT1_vect External Interrupt Request 1
INT2_vect External Interrupt Request 2
INT3_vect External Interrupt Request 3
INT6_vect External Interrupt Request 6
PCINT0_vect Pin Change Interrupt Request 0 (from PCINT0 through PCINT7)
TIMER1_CAPT_vect Timer/Counter1 Capture Event
TIMER1_COMPA_vect Timer/Counter1 Compare Match A
TIMER1_COMPB_vect Timer/Counter1 Compare Match B
TIMER1_COMPC_vect Timer/Counter1 Compare Match C
TIMER1_OVF_vect Timer/Counter1 Overflow
TIMER0_COMPA_vect Timer/Counter0 Compare Match A
TIMER0_COMPB_vect Timer/Counter0 Compare Match B
TIMER0_OVF_vect Timer/Counter0 Overflow
SPI_STC_vect SPI Serial Transfer Complete
USART1_RX_vect USART1, Rx Complete
USART1_UDRE_vect USART1 Data register Empty
USART1_TX_vect USART1, Tx Complete
ANALOG_COMP_vect Analog Comparator
ADC_vect ADC Conversion Complete
EE_READY_vect EEPROM Ready
TIMER3_CAPT_vect Timer/Counter3 Capture Event
TIMER3_COMPA_vect Timer/Counter3 Compare Match A
TIMER3_COMPB_vect Timer/Counter3 Compare Match B
TIMER3_COMPC_vect Timer/Counter3 Compare Match C
TIMER3_OVF_vect Timer/Counter3 Overflow
TWI_vect 2-wire Serial Interface
SPM_READY_vect Store Program Memory Read
TIMER4_COMPA_vect Timer/Counter4 Compare Match A
TIMER4_COMPB_vect Timer/Counter4 Compare Match B
TIMER4_COMPD_vect Timer/Counter4 Compare Match D
TIMER4_OVF_vect Timer/Counter4 Overflow
TIMER4_FPF_vect Timer/Counter4 Fault Protection Interrupt




Important Note from the AVR-LIBC FAQ.

My program doesn't recognize a variable updated within an interrupt routine?

When using the optimizer, in a loop like the following one:

uint8_t flag;
...
ISR(SOME_vect) {

flag = 1;

}
...

while (flag == 0) {
...
}

the compiler will typically access flag only once, and optimize further accesses completely away, since its code path analysis shows that nothing inside the loop could change the value of flag anyway. To tell the compiler that this variable could be changed outside the scope of its code path analysis (e. g. from within an interrupt routine), the variable needs to be declared like:

volatile uint8_t flag;

In other words, sometimes the compile optimizer will "outsmart" you and hard code a variable that it doesn't realize is used elsewhere. So if you want to use a global variable outside of your interrupt handler (e.g. in your main function), you will probably have to declare the variable as volatile.