Interrupts

Interrupts in AVR can be caused by counters, communication interfaces, analog-to-digital converters, comparators, special input-output pins and several other functions, depending on the controller. Any interrupt can be allowed or disallowed by the unit that generates it. Regardless of the interrupt being allowed or disallowed, there is a 1-bit field (interrupt flag) in the corresponding unit of the controller, which is marked as true when the interrupt condition is fulfilled. If the interrupt flag is changed to true and the interrupt is allowed, the controller starts to execute the code specified for this interrupt.

Every interrupt in the AVR microcontroller is tied to a specific event. Each event has a flag bit in the status register, which marks the occurring of the event. Events are also tied to interrupt mask registers and the corresponding bits. If the event's interrupt bit is left unmasked and an event occurs, the processor halts executing the current program for a few duty-cycles and begins executing the interrupt program. After the interrupt program has been executed, the processor resumes executing the paused main program.

Example

To use interrupts with the AVR LibC library, it is necessary to include interrupt.h. The code that gets executed upon interrupt is written after the “ISR” keyword. The text between the brackets after “ISR” is the name of the interrupt. The following is a C language example:

#include <avr/interrupt.h>
 
ISR(XXX_vect)
{
	// Do something
}

Global allowing of all interrupts is configured from the control and status register SREG. The option to allow or disallow all interrupts at once is there to help protect data. Since interrupts disrupt the execution of the main program, some data used by the main program may be disturbed or corrupted in the process. Situations like this can be avoided by simply disallowing all interrupts before processing such delicate data. Disallowing interrupts globally is easy, if it can be done by changing only one register (SREG). After the critical part of the program has been executed, the interrupts can easily be allowed again and all the interrupts that would have fired in the meantime are executed.

 

Example

Let's suppose there is a 16-bit variable in use in the program, which is changed by both the main program and the interrupt program and the value of this variable is later given to another variable:

#include <avr/interrupt.h>
 
// Global 16-bit variables x and y
unsigned short x, y;
 
// A random interrupt that changes the value of x
ISR(XXX_vect)
{
	x = 0x3333;
}
 
int main()
{
	// Give a value to x
	x = 0x1111;
 
	// Allow interrupts globally
	sei();
 
	// Give the value of x to y
	y = x;	
}

The program itself is very simple - first, variable x is given a value of 0x1111 and later, its value is given to variable y. If an interrupt occurs between those two operations, x gets a value of 0x3333. By logic, variable y can have two possible values by the end of the program, but on an 8-bit AVR there is also a third option. This is because 8-bit architecture needs 2 cycles to move 16-bit data and therefore a badly-timed interrupt can corrupt the integrity of the data. Consequently, y can have a value of 0x1111 or 0x3333, but it can also have a value of 0x3311 at the end of the program. To avoid getting the third, unwanted value, all interrupts should be temporarily disallowed before performing operations that take more than 1 cycle.

In the following example, the value of x is given to y using a safe method:

	// Disallow interrupts globally
	cli();
 
	// Give the value of x to y
	y = x;
 
	// Re-allow all interrupts again
	sei();
en/avr/interrupts.txt · Last modified: 2013/03/26 23:15 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0