Bluetooth Arduino串行中断

Bluetooth Arduino串行中断,bluetooth,arduino,microcontroller,serial-communication,interrupt,Bluetooth,Arduino,Microcontroller,Serial Communication,Interrupt,我正在进行一个Arduino Mega 2560项目。在Windows7电脑上,我使用的是Arduino1.0IDE。我需要建立一个波特率为115200的串行蓝牙通信。当RX上有可用数据时,我需要接收一个中断。我所看到的每一段代码都使用“轮询”,即在Arduino的循环中设置Serial.available的条件。我如何在Arduino的循环中为中断及其服务例程替换这种方法?attachInterrupt()似乎没有为此提供支持。我依靠一个中断将Arduino从睡眠模式唤醒 我已经开发了这个简单

我正在进行一个Arduino Mega 2560项目。在Windows7电脑上,我使用的是Arduino1.0IDE。我需要建立一个波特率为115200的串行蓝牙通信。当RX上有可用数据时,我需要接收一个中断。我所看到的每一段代码都使用“轮询”,即在Arduino的循环中设置Serial.available的条件。我如何在Arduino的循环中为中断及其服务例程替换这种方法?attachInterrupt()似乎没有为此提供支持。我依靠一个中断将Arduino从睡眠模式唤醒

我已经开发了这个简单的代码,应该是打开连接到引脚13的LED

    #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          
    }
#包括
#包括
无效设置()
{
引脚模式(13,输出);//将引脚13设置为输出
UBRR0H=0;//(波特率预刻度>>8);//将波特率值的上8位加载到UBRR寄存器的高字节中
UBRR0L=8;//将波特率值的低8位加载到UBRR寄存器的低字节中

UCSR0C |=(1你是否尝试过该代码,但它不起作用?我认为问题在于你没有打开中断。你可以尝试调用
sei();
interrupts();
在你的
设置
函数中。

最后我发现了我的问题。我更改了中断向量“USART0\u RXC\u vect”通过
USART0\u RX\u vect
。我还添加了
interrupts();
以启用全局中断,并且它工作得非常好

代码是:

#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
}
#包括
#包括
无效设置()
{
pinMode(13,输出);
UBRR0H=0;//将波特率值的高8位加载到UBRR寄存器的高字节中
UBRR0L=8;//将波特率值的低8位加载到UBRR寄存器的低字节中

UCSR0C |=(1紧跟在
UBRR0L=8
之后,而不是:

 UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01);

<代码> UCSR0C==(1)你的问题和蓝牙有什么关系?好像你只是在问如何使用有中断的普通UART?嗨,Abdelhalim,欢迎你这么做。也可以考虑解释代码背后的想法。我想这会改善你的答案。无论如何,谢谢你的贡献:什么?绝对不允许您在中断处理程序中调用
delay
。中断处理程序应该尽可能少、尽可能快地执行操作,并且绝大多数情况下都不会依赖于是否启用了其他中断以及它们之间的优先级。
 UCSR0C |= (1 << UCSZ00) | (1 << UCSZ10);