Arduino的串行中断 [英] Arduino Serial Interrupts

查看:495
本文介绍了Arduino的串行中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Arduino兆2560项目。在Windows 7 PC,我使用的IDE Arduino1.0。我需要建立的115200波特率,我需要的数据时,可在RX接收中断串行蓝牙通信。每件code我见过用投票,这是将Serial.available的条件的Arduino的循环中。我怎样才能在替代的Arduino的回路中断其服务程序这种做法?似乎attachInterrupt()不提供用于此目的。我靠中断从休眠模式唤醒Arduino的。

I am working on an Arduino Mega 2560 project. At a Windows 7 PC I am using the Arduino1.0 IDE. I need to establish a serial Bluetooth communication with a baud rate of 115200. I need to receive an interrupt when data is available at RX. Every piece of code I have seen use "polling", which is placing a condition of Serial.available inside Arduino’s loop. How can I replace this approach at Arduino’s loop for an Interrupt and its Service Routine? It seems that attachInterrupt() does not provides for this purpose. I depend on an Interrupt to awake the Arduino from sleep mode.

我已经开发了这个简单的code是应该打开连接到销13一个LED

I have developed this simple code that is supposed to turn on a LED connected to the 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          
    }

的问题是,该子程序被从未供应

The problem is that the subroutine is never served.

推荐答案

最后,我发现我的问题。我通过 USART0_RX_vect 改变中断向量USART0_RXC_vect。此外,我加入中断(); 来启用全局中断,它是工作得很好。

Finally I have found my problem. I changed the interruption vector "USART0_RXC_vect" by USART0_RX_vect. Also I added interrupts(); to enable the global interrupt and it is working very well.

在code是:

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

感谢您的答复!!!!

Thanks for the replies!!!!

这篇关于Arduino的串行中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆