Saltar al contenido principal
LibreTexts Español

29.2: Interrupciones externas

  • Page ID
    82330
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Consideremos primero una interrupción externa. En este caso, el cambio de estado visto en un pin externo provoca una interrupción. Utilizaremos Solicitud de Interrupción Externa 0, también conocida como el vector INT0. Esta interrupción examina los cambios de estado en PORTD.2, Uno pin 2. Nuestro ejemplo será bastante sencillo: conectamos un interruptor pasivo usando el pull-up interno al pin 2. Cuando se activa el interruptor alternará un LED conectado a Uno pin 8 (PORTB.0). El código sigue:

    /* External Interrupt Example, INT0
       External interrupt pin lights an LED
    
       Active high LED attached to PORTB.0
       Switch on Uno pin 2, falling edge triggers external pin interrupt 0,
       ISR toggles LED at PORTB.0
    */
    
    #define LEDMASK 0x01
    
    void setup()
    {
        DDRB |= LEDMASK;
        // set Uno pin 2 for input with pullup
        DDRD &= ~(0x04);
        PORTD |= 0x04;
    
        EIMSK |= (1<<INT0);  // enable external interupt INT0 (Uno pin 2)
        EICRA |= (1<<ISC01); // trigger INT0 on falling edge
    }
    
    ISR(INT0_vect)
    {
        PORTB ^= LEDMASK;    // toggle LED
    }
    
    void loop()
    {
    }
    

    Después de configurar los pines IO, el código establece el bit INT0 en el registro EIMSK para habilitar la interrupción. El Registro de Control de Interrupciones Externas (EICRA) está programado para activarse en un borde descendente (negativo). Cuando se activa el interruptor, el borde descendente activa el ISR para que funcione. El ISR simplemente alterna la broca a la que está atado el LED. Tenga en cuenta que no hay ajuste de puerto y examen de pines en el código principal. De hecho, loop () está vacío y no hace nada.

    Entre otras cosas, una interrupción externa como esta es muy útil para elementos de gran importancia como un interruptor de pánico.


    This page titled 29.2: Interrupciones externas is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.