5

Sto lavorando su un progetto Arduino Mega 2560. Su un PC Windows 7 sto usando l'IDE Arduino1.0. Devo stabilire una comunicazione Bluetooth seriale con una velocità di trasmissione di 115200. Devo ricevere un interrupt quando i dati sono disponibili su RX. Ogni pezzo di codice che ho visto usa "polling", che sta mettendo una condizione di Serial.available all'interno del loop di Arduino. Come posso sostituire questo approccio al loop di Arduino per un interrupt e la sua routine di servizio? Sembra che attachInterrupt() non preveda questo scopo. Dipendo da un Interrupt per risvegliare l'Arduino dalla modalità di sospensione.Arduino Serial Interrupts

Ho sviluppato questo semplice codice che dovrebbe girare su un LED collegato 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   
    } 

Il problema è che la subroutine non viene mai servito.

+0

Che cosa ha a che fare la tua domanda con il bluetooth? Sembra che tu stia solo chiedendo come utilizzare un UART regolare con interrupt? – TJD

risposta

6

Finalmente ho trovato il mio problema. Ho modificato il vettore di interruzione "USART0_RXC_vect" di USART0_RX_vect. Inoltre ho aggiunto interrupts(); per abilitare l'interrupt globale e sta funzionando molto bene.

Il codice è:

#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 
} 

Grazie per le risposte !!!!

1

Hai provato quel codice e non ha funzionato? Penso che il problema è che non hai attivato gli interrupt. Potresti provare a chiamare sei(); o interrupts(); nella tua funzione setup.