首页 文章

Arduino串行中断

提问于
浏览
5

我正在研究Arduino Mega 2560项目 . 在Windows 7 PC上,我使用的是Arduino1.0 IDE . 我需要 Build 一个波特率为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          
    }

问题是从不提供子程序 .

3 回答

  • 0

    最后我发现了我的问题 . 我通过 USART0_RX_vect 更改了中断向量"USART0_RXC_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
    }
    

    谢谢你的回复!!!!

  • 1

    你试过那个代码而且没用吗?我认为问题是你没有打开中断 . 您可以尝试在 setup 函数中调用 sei();interrupts(); .

  • 6

    就在 UBRR0L = 8 之后而不是:

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

    改为:

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

相关问题