🛠 Sortir des sentiers : registres AVR et interruption externe

Arduino Micro · avance · Schéma : IMG-micro-avance-1-micro-registres-avr-interruption.svg — Bouton sur la broche 3 d'une Arduino Micro déclenchant une interruption externe, registre EICRA configuré sur front descendant, LED de la broche 13 allumée depuis la routine d'interruption

Objectives

Program the ATmega32U4 at the register level: configure an external interrupt on a falling edge (registers EICRA and EIMSK), write a short Interrupt Service Routine (ISR), and understand the pattern of using a volatile flag rather than performing long tasks within the ISR. On the Micro, external interrupts are available on pins 0, 1, 2, 3, and 7.

Hardware

Complete code

// Interruption externe sur front descendant, a la main (sans attachInterrupt).
// Broche 3 du Micro = INT0. On configure les registres puis on ecrit l'ISR.
volatile bool evenement = false;   // drapeau partage ISR -> boucle

void setup {
  DDRB  &= ~(1 << DDB0);          // sens entree de la broche 13 cote port B
  EIMSK &= ~(1 << INT0);          // INT0 desactive pendant la config
  EICRA = (1 << ISC01) | (0 << ISC00);  // INT0 sur front descendant
  EIMSK |= (1 << INT0);           // autoriser INT0
  // Broche 3 : entree + pull-up (le noyau Arduino gere le pull via PORT)
  pinMode(3, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  sei;                          // autorisation globale des interruptions
  Serial.begin(9600);
}

ISR(INT0_vect) {                  // executee sur chaque front descendant
  evenement = true;               // travail minimal : lever un drapeau
}

void loop {
  if (evenement) {
    evenement = false;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    Serial.println("INT0 : front descendant detecte");
  }
}

Expected verification

Each press (falling edge) toggles LED 13 and prints a line; the monitor remains responsive because only a flag toggle occurs in the ISR. By bringing your finger close to the long-wired button, you may see spurious triggers: this is real, hence the pull-up and debouncing on the loop side if necessary.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials