AVR microcontroller interrupts using WINAVR
I can’t imagine microcontroller without interrupts. Without interrupts you would have to do loops in order to check if one or another event occurred. Polling has many disadvantages like program have do loops what takes valuable microcontroller resources. These resources may be used for other data processing tasks. This is why microcontroller comes with built in interrupt support. Instead of checking events microcontroller has ability to interrupt normal program flow on event and jump to interrupt service subroutine.
During interrupt microcontroller stops the task at current point, saves necessary information to stack and gives resources to interrupt subroutine. When interrupt subroutine finishes, and then normal program flow continues.
In assembler your program would start with interrupt table:
After table goes the main program (RESET) and other interrupt subroutines where program jumps after interrupt occurs.
C Compiler creates this table while compiling, but how about interrupt handling routines.
First of all to make compiler understand interrupt macro command include desired library;
#include “avr\interrupt.h”
All interrupts then can be described using macro command: ISR(), for instance:
ISR(ADC_vect)
{
//Your code here
}
This routine is for ADC complete conversion handler. This macro command is convenient for handling all unexpected interrupts. Just create routine like this;
ISR(_vector_dfault)
{
//your code here;
}
If you want to describe an empty interrupt (puts reti in interrupt table) just use:
EMPTY_INTERRUPT(ADC_vect)
Example:

Simple program:
#include “avr\io.h”
#include “avr\iom8.h”
#include “avr\interrupt.h”
#define outp(a, b) b = a
#define inp(a) a
uint8_t led;
ISR(INT0_vect) { /* signal handler for external interrupt int0 */
led = 0×01;
}
ISR(INT1_vect) { /* signal handler for external interrupt int1 */
led = 0×00;
}
int main(void) {
outp(0×01, DDRB); /* use PortB for output (LED) */
outp(0×00, DDRD); /* use PortD for input (switches) */
outp((1<<
sei(); /* enable interrupts */
led = 0×01;
for (;;) {
outp(led, PORTB);
} /* loop forever */
}
Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post | TrackBack URI for this post
New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …Programming AVR ADC module with WinAVR,Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut … |
New on WinARM Tutorial
What are differences between WinARM and WinAVR,Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …LPC2000 watchdog timer,As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount … |
