我买了一个mpu6050模块,用作角度传感器,将tx和rx分别插入rx0和tx0,可以从串口得到反馈角度,但是我该用serial1、serial2和serial3都无法使用,求问到底是为什么,谢谢!
unsigned char Re_buf[11],counter=0;
unsigned char sign=0;
float a[3],w[3],angle[3],T;
void setup() {
// initialize serial:
Serial2.begin(9600);
}
void loop() {
if(sign)
{
sign=0;
if(Re_buf[0]==0x55) //检查帧头
{
switch(Re_buf [1])
{
case 0x53:
angle[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*180;
angle[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*180;
angle[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*180;
T = (short(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;
Serial2.print("a:");
Serial2.print(a[0]);Serial2.print(" ");
Serial2.print(a[1]);Serial2.print(" ");
Serial2.print(a[2]);Serial2.print(" ");
Serial2.println(T);
break;
}
}
}
}
void serialEvent2() {
while (Serial2.available()) {
//char inChar = (char)Serial.read(); Serial.print(inChar); //Output Original Data, use this code
Re_buf[counter]=(unsigned char)Serial2.read();
if(counter==0&&Re_buf[0]!=0x55) return; //第0号数据不是帧头
counter++;
if(counter==11) //接收到11个数据
{
counter=0; //重新赋值,准备下一帧数据的接收
sign=1;
}
}
}
|