Talk is cheap, show you code!
编译平台:Arduino IDE
/**
* 功能:软件串口读取S-BUS数据;解析;串口打印
*/
#include <SoftwareSerial.h>
#define None 5//D1(空)
#define DataPort 14//D5
uint8_t Data_packet[26];
uint16_t CH[16]; // 通道值
//声明一个软件串口
SoftwareSerial swSerial;
void setup()
{
Serial.begin(115200);
//设置软件串口波特率
//begin(uint32_t baud, SoftwareSerialConfig config,int8_t rxPin, int8_t txPin, bool invert,int Data_packetCapacity = 64, int isrData_packetCapacity = 0);
swSerial.begin(100000,SWSERIAL_8E2,DataPort,None,true);
}
void loop()
{
readSerial();
Sbus_Data_Count();
DebugPrint();
}
void DebugPrint()
{
//Serial.printf("%4d ",Data_packet[1]);//0x0F
for(int i=0;i<6;i++)//mc6c遥控器只有6个通道
{
Serial.printf("%4d ",CH[i]);
}
Serial.println();
//Serial.printf("%4d\n",Data_packet[25]);//0x00
/*for(int i=1;i<=25;i++)
{
Serial.printf("%4d ",Data_packet[i]);
}
Serial.println("");*/
}
void readSerial()
{
for(int i=1;i<=25;i++)
{
if(swSerial.available())
{
Data_packet[i] = swSerial.read();
//Serial.printf("Byte%d:%d\n",i,Data_packet[i]);
}
}
}
void Sbus_Data_Count()
{
CH[ 0] = ((int16_t)Data_packet[ 2] >> 0 | ((int16_t)Data_packet[ 3] << 8 )) & 0x07FF;
CH[ 1] = ((int16_t)Data_packet[ 3] >> 3 | ((int16_t)Data_packet[ 4] << 5 )) & 0x07FF;
CH[ 2] = ((int16_t)Data_packet[ 4] >> 6 | ((int16_t)Data_packet[ 5] << 2 ) | (int16_t)Data_packet[ 6] << 10 ) & 0x07FF;
CH[ 3] = ((int16_t)Data_packet[ 6] >> 1 | ((int16_t)Data_packet[ 7] << 7 )) & 0x07FF;
CH[ 4] = ((int16_t)Data_packet[ 7] >> 4 | ((int16_t)Data_packet[ 8] << 4 )) & 0x07FF;
CH[ 5] = ((int16_t)Data_packet[ 8] >> 7 | ((int16_t)Data_packet[ 9] << 1 ) | (int16_t)Data_packet[10] << 9 ) & 0x07FF;
CH[ 6] = ((int16_t)Data_packet[10] >> 2 | ((int16_t)Data_packet[11] << 6 )) & 0x07FF;
CH[ 7] = ((int16_t)Data_packet[11] >> 5 | ((int16_t)Data_packet[12] << 3 )) & 0x07FF;
CH[ 8] = ((int16_t)Data_packet[13] << 0 | ((int16_t)Data_packet[14] << 8 )) & 0x07FF;
CH[ 9] = ((int16_t)Data_packet[14] >> 3 | ((int16_t)Data_packet[15] << 5 )) & 0x07FF;
CH[10] = ((int16_t)Data_packet[15] >> 6 | ((int16_t)Data_packet[16] << 2 ) | (int16_t)Data_packet[17] << 10 ) & 0x07FF;
CH[11] = ((int16_t)Data_packet[17] >> 1 | ((int16_t)Data_packet[18] << 7 )) & 0x07FF;
CH[12] = ((int16_t)Data_packet[18] >> 4 | ((int16_t)Data_packet[19] << 4 )) & 0x07FF;
CH[13] = ((int16_t)Data_packet[19] >> 7 | ((int16_t)Data_packet[20] << 1 ) | (int16_t)Data_packet[21] << 9 ) & 0x07FF;
CH[14] = ((int16_t)Data_packet[21] >> 2 | ((int16_t)Data_packet[22] << 6 )) & 0x07FF;
CH[15] = ((int16_t)Data_packet[22] >> 5 | ((int16_t)Data_packet[23] << 3 )) & 0x07FF;
}
版权声明:本文为CSDN博主「feng锋~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_49597570/article/details/122749041
暂无评论