我正在开发 Arduino Mega 2560 项目。在 Windows 7 PC 上,我使用 Arduino1.0 IDE。我需要建立波特率为 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
}
问题是子例程永远不会被服务。
终于我找到了我的问题。我更改了中断向量“USART0_RXC_vect”USART0_RX_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
}
谢谢各位的回复!!!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)