5

Estoy trabajando en un proyecto Arduino Mega 2560. En una PC con Windows 7, estoy usando el IDE de Arduino1.0. Necesito establecer una comunicación serial de Bluetooth con una velocidad en baudios de 115200. Necesito recibir una interrupción cuando haya datos disponibles en RX. Cada pieza de código que he visto usa "polling", que está colocando una condición de Serial.available dentro del ciclo de Arduino. ¿Cómo puedo reemplazar este enfoque en el ciclo de Arduino para una Interrupción y su Rutina de Servicio? Parece que attachInterrupt() no proporciona para este propósito. Depende de una Interrupción para despertar al Arduino del modo de suspensión.Arduino Interrupciones en serie

he desarrollado este código simple que se supone para encender un LED conectado al pin 13.

#include <avr/interrupt.h> 
    #include <avr/io.h> 
    void setup() 
    { 
     pinMode(13, OUTPUT);  //Set pin 13 as output 

     UBRR0H = 0;//(BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
     UBRR0L = 8; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
     UCSR0C |= (1 << UCSZ00) | (1 << UCSZ10); // Use 8-bit character sizes 
     UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // Turn on the transmission, reception, and Receive interrupt  
    } 

    void loop() 
    { 
     //Do nothing 
    } 

    ISR(USART0_RXC_vect) 
    {  
     digitalWrite(13, HIGH); // Turn the LED on   
    } 

El problema es que la subrutina no se sirve.

+0

¿Qué tiene que ver tu pregunta con bluetooth? Parece que solo estás preguntando cómo usar un UART regular con interrupciones. – TJD

Respuesta

6

Finalmente he encontrado mi problema. Cambié el vector de interrupción "USART0_RXC_vect" por USART0_RX_vect. También agregué interrupts(); para habilitar la interrupción global y está funcionando muy bien.

El código es:

#include <avr/interrupt.h> 
#include <avr/io.h> 
void setup() 
{ 
    pinMode(13, OUTPUT); 

    UBRR0H = 0; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
    UBRR0L = 8; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
    UCSR0C |= (1 << UCSZ00) | (1 << UCSZ10); // Use 8-bit character sizes 
    UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // Turn on the transmission, reception, and Receive interrupt  
    interrupts(); 
} 

void loop() 
{ 

} 

ISR(USART0_RX_vect) 
{ 
    digitalWrite(13, HIGH); // set the LED on 
    delay(1000);    // wait for a second 
} 

Gracias por las respuestas !!!!

1

¿Probaste ese código y no funcionó? Creo que el problema es que no has activado las interrupciones. Puede intentar llamar al sei(); o interrupts(); en su función setup.