我正在编写代码,以便在由Arduino作为ISP编程的ATtiny上运行。ATtiny将通过串行链路向RN42蓝牙模块发送AT命令。
由于ATtiny没有串口,所以我在引脚0和1上使用SoftwareSerial。将Tx放在"Data Out"/MISO引脚上,Rx放在"Data In"/MOSI引脚上似乎是合乎逻辑的。文档说要像SoftwareSerial mySerial(Rx,Tx)那样声明它;但我发现只有当你像SoftwareSerial mySerial(Tx,Rx)那样声明它时,它才能起作用;
我已经对我的代码和引脚进行了截图,我觉得我遗漏了一些东西,但当我这样做时,它会工作,并使蓝牙模块进入命令模式。文档是不是走错了路?
发布于 2017-08-23 23:09:57
我意识到我的方法的错误,我不必要地设置了Rx和Tx引脚的pinMode。这让我大吃一惊,因为我认为将Rx引脚设置为OUTPUT不会起作用,而实际上它确实有效,所以我在Rx线路上输出数据,并在Tx线路上接收数据!答案是不指定方向,只让SoftwareSerial处理引脚。按(Rx,Tx)顺序传递参数。
下面是我的更干净的代码,它工作得更好:
#include <SoftwareSerial.h>
const int Rx = 0; // pin 5 on ATtiny - DI/MOSI
const int Tx = 1; // pin 6 on ATtiny - DO/MISO
const int ButtonIn = 2;
const int OK_LED = 4;
int buttonState = 0;
SoftwareSerial serialBT(Rx, Tx);
void setup()
{
pinMode(ButtonIn, INPUT);
pinMode(OK_LED, OUTPUT);
serialBT.begin(9600);
}
void loop()
{
buttonState = digitalRead(ButtonIn);
if (buttonState == 0)
{
serialBT.print("$"); // $$$ enters RN42 command mode
serialBT.print("$");
serialBT.print("$");
delay(3000);
serialBT.println("R,1");
digitalWrite(OK_LED, HIGH);
delay(5000);
digitalWrite(OK_LED, LOW);
}
}https://stackoverflow.com/questions/45840720
复制相似问题