beb*_*_lm 5 microcontroller bluetooth arduino interrupt serial-communication
我正在研究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
}
Run Code Online (Sandbox Code Playgroud)
问题是从不提供子程序.
最后我发现了我的问题.我改变了中断向量"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
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的回复!!!!
归档时间: |
|
查看次数: |
16364 次 |
最近记录: |