AVR Interrupts
What are Interrupts
Interrupts are basically events that require immediate attention by the microcontroller. When an interrupt event occurs the microcontroller pause its current task and attend to the interrupt by executing an Interrupt Service Routine (ISR) at the end of the ISR, the microcontroller returns to the task it had pause and continue its normal operations.
In order to enable interrupts for a particular event, you have to complete following steps.
- Enable global interrupts.
- Enable Interrupt Enable bit of specific Interrupt.
- Implement ISR (Interrupt Service Routine) for specific Interrupt.
Following is a list of interrupt vectors available in Atmega32A microcontroller.
Vector No. | Program Address | Source | Interrupt Definition |
---|---|---|---|
1 | 0x000 | RESET | External Pin, Power-on Reset, Brown-out Reset,
Watchdog Reset, and JTAG AVR Reset |
2 | 0x002 | INT0 | External Interrupt Request 0 |
3 | 0x004 | INT1 | External Interrupt Request 1 |
4 | 0x006 | INT2 | External Interrupt Request 2 |
5 | 0x008 | TIMER2 COMP | Timer/Counter2 Compare Match |
6 | 0x00A | TIMER2 OVF | Timer/Counter2 Overflow |
7 | 0x00C | TIMER1 CAPT | Timer/Counter1 Capture Event |
8 | 0x00E | TIMER1 COMPA | Timer/Counter1 Compare Match A |
9 | 0x010 | TIMER1 COMPB | Timer/Counter1 Compare Match B |
10 | 0x012 | TIMER1 OVF | Timer/Counter1 Overflow |
11 | 0x014 | TIMER0 COMP | Timer/Counter0 Compare Match |
12 | 0x016 | TIMER0 OVF | Timer/Counter0 Overflow |
13 | 0x018 | SPI, STC | SPI Serial Transfer Complete |
14 | 0x01A | USART, RXC | USART, Rx Complete |
15 | 0x01C | USART, UDRE | USART Data Register Empty |
16 | 0x01E | USART, TXC | USART, Tx Complete |
17 | 0x020 | ADC | ADC Conversion Complete |
18 | 0x022 | EE_RDY | EEPROM Ready |
19 | 0x024 | ANA_COMP | Analog Comparator |
20 | 0x026 | TWI | Two-wire Serial Interface |
21 | 0x028 | SPM_RDY | Store Program Memory Ready |
Sample Code
Following example shows how USART Receive Complete Interrupt is handled.
#include <avr/io.h> #include <avr/interrupt.h> // import 'avr/interrupt.h' header file ISR(USART_RXC_vect){ // code to handle interrupt char c = UDR; UDR = c; } int main(void){ sei(); // enable global interrupts UBRRH |= (12 >> 8); // set UBBR value for 9600 baud rate UBRRL |= 12; UCSRB |= (1 << TXEN) | (1 << RXEN); // enable receiver and transmitter UCSRC &= ~(1 << UMSEL); // enable asynchronous mode UCSRC|= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // 8bit data format UCSRA = 1 << U2X; // double speed mode UCSRB |= (1 << RXCIE); // enable USART receive complete interrupt while(); // wait forever }
Comments
Post a Comment