Arduino 串行中断 [英] Arduino Serial Interrupts
问题描述
我正在研究 Arduino Mega 2560 项目.在 Windows 7 PC 上,我使用的是 Arduino1.0 IDE.我需要建立一个波特率为 115200 的串行蓝牙通信.我需要在 RX 有数据时接收中断.我见过的每段代码都使用轮询",即在 Arduino 的循环中放置一个 Serial.available 条件.我如何在 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.
我开发了这个简单的代码,可以打开连接到引脚 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".我还添加了 interrupts();
以启用全局中断,并且运行良好.
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.
代码是:
#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
}
感谢您的回复!!!!
这篇关于Arduino 串行中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!