This is an old revision of the document!


Interruptions

Les interruptions dans AVR peuvent être réalisées par des compteurs, des interfaces de communication, le convertisseur analogique-numérique, le comparateur, des broches d'entrée-sortie spéciales et plusieurs autres fonctions, selon le contrôleur. Chaque interruption peut être activée ou désactivée par l'unité qui la réalise. Bien que l'interruption puisse être activée ou désactivée, il y a un champ de 1 bit (le signal d'interruption) dans l'unité correspondante du contrôleur, qui est marqué comme vrai quand la condition interrompant est accomplie. Si le signal d'interruption prend la valeur vrai et que l'interruption est activée, le contrôleur commence à exécuter le code indiqué pour l'interruption.

Chaque interruption dans le micro-contrôleur AVR est liée à un événement spécifique. Chaque événement a un bit signal dans le registre de statut, qui marque l'apparition de l'événement. Les événements sont aussi liés pour interrompre des registres de masque et les bits correspondants. Si le bit événement interruption est laissé unmasked et un événement arrive, le processeur met en pause l'exécution du programme actif pendant quelques cycles et commence à exécuter le programme d'interruption. Après que le programme d'interruption a été exécuté, le processeur reprend l'exécution du programme principal.

Exemple

Pour utiliser des interruptions avec la librairie LibC de l'AVR, il faut ajouter la fonction interrupt.h. La partie de code qui doit être exécuté pendant l'interruption se trouve après le mot clé “ISR”. Le texte entre parenthèses après “ISR” est le nom de l'interruption. Exemple en langage C:

#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 dealing with 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 meanwhile will get executed.

 

Exemple

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 fires 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();
fr/avr/interrupts.1268996417.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
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