Atmel AVR » Historique » Version 31
Patrice Nadeau, 2025-08-10 11:38
| 1 | 1 | Patrice Nadeau | # Atmel AVR |
|---|---|---|---|
| 2 | |||
| 3 | Particularités pour les microcontrôleurs 8 bits AVR d’Atmel. |
||
| 4 | |||
| 5 | 2 | Patrice Nadeau | ## Règles |
| 6 | 14 | Patrice Nadeau | 1. Macros définis dans le fichier `config.h` |
| 7 | 12 | Patrice Nadeau | 1. `F_CPU` : La fréquence utilisée par l’horloge (interne ou externe) du microcontrôleur, en Hz |
| 8 | 2 | Patrice Nadeau | |
| 9 | > Les « fuses » doivent correspondent à la bonne source de l’horloge. |
||
| 10 | 12 | Patrice Nadeau | 1. De nouveau type d’entier sont fournis avec la librairie `stdint.h`. L’utilisation de ces types DOIT être utilisé afin d’exprimer le nombre de bit d’un objet. |
| 11 | 2 | Patrice Nadeau | 1. Progmem : Pour mettre des variables en lecture seule dans la section FLASH au lieu de SRAM. |
| 12 | 15 | Patrice Nadeau | 1. Inclue la librairie `avr/pgmspace.h` |
| 13 | 12 | Patrice Nadeau | 1. Le nom de la variable DOIT être suivie de **_P** |
| 14 | 2 | Patrice Nadeau | 1. Utiliser les macros de la librairie pour accéder à ces variables |
| 15 | 17 | Patrice Nadeau | 1. Fonction `main` |
| 16 | 27 | Patrice Nadeau | > * [AVR035 : Efficient C Coding for AVR6](https://ww1.microchip.com/downloads/en/AppNotes/doc1497.pdf) |
| 17 | 26 | Patrice Nadeau | > <https://gcc.gnu.org/onlinedocs/gcc/AVR-Function-Attributes.html> |
| 18 | 27 | Patrice Nadeau | * Déclarer la fonction avec le type `void` et l’attribut `OS_main` |
| 19 | * Utiliser une boucle sans fin `for (;;)` |
||
| 20 | 12 | Patrice Nadeau | 1. Opérations atomiques (opérations ne devant pas être interrompus) |
| 21 | 2 | Patrice Nadeau | 1. Inclure la librairie `util/atomic.h` |
| 22 | 1 | Patrice Nadeau | 1. Insérer les instructions dans une section `ATOMIC_BLOCK` |
| 23 | 6. Interruptions |
||
| 24 | 12 | Patrice Nadeau | 1. TODO |
| 25 | 1 | Patrice Nadeau | |
| 26 | 12 | Patrice Nadeau | ## Exemple |
| 27 | 1 | Patrice Nadeau | ```c |
| 28 | 21 | Patrice Nadeau | #if !defined (__AVR_ATmega48__) || (__AVR_ATmega48P__) |
| 29 | #warning "Cette librairie n'as pas été testée sur cette famille de microcontrôleur." |
||
| 30 | 1 | Patrice Nadeau | #endif |
| 31 | |||
| 32 | 7 | Patrice Nadeau | #include <avr/io.h> |
| 33 | 1 | Patrice Nadeau | #include <avr/pgmspace.h> |
| 34 | 8 | Patrice Nadeau | #include <util/atomic.h> |
| 35 | |||
| 36 | 1 | Patrice Nadeau | /** @brief Variable en FLASH */ |
| 37 | 23 | Patrice Nadeau | char my_string[] PROGMEM = "POWER"; |
| 38 | 8 | Patrice Nadeau | |
| 39 | 1 | Patrice Nadeau | /** |
| 40 | 29 | Patrice Nadeau | * @brief La fonction principale |
| 41 | 1 | Patrice Nadeau | */ |
| 42 | void main(void) __attribute__ ((OS_main)); |
||
| 43 | |||
| 44 | void main(void) |
||
| 45 | { |
||
| 46 | 25 | Patrice Nadeau | ... |
| 47 | 30 | Patrice Nadeau | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
| 48 | /* Initialisations ne devant pas être interrompues */ |
||
| 49 | ... |
||
| 50 | } |
||
| 51 | 19 | Patrice Nadeau | /* Boucle sans fin */ |
| 52 | 31 | Patrice Nadeau | for (;;) |
| 53 | { |
||
| 54 | 1 | Patrice Nadeau | }; |
| 55 | }; |
||
| 56 | ``` |
||
| 57 | 11 | Patrice Nadeau | |
| 58 | ## Justifications |
||
| 59 | 22 | Patrice Nadeau | * [Atmel AVR4027 : Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers](https://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf) |
| 60 | 27 | Patrice Nadeau | |
| 61 | 11 | Patrice Nadeau | * [Vérification du modèle de microcontrôleur via l’option -m de gcc](https://github.com/embecosm/avr-gcc/blob/avr-gcc-mainline/gcc/config/avr/avr-mcus.def) |