当前位置:网站首页>Imitation SBUS fixed with serial data conversion
Imitation SBUS fixed with serial data conversion
2022-08-05 09:30:00 【CaewarfeneggerCao】
写在前面
无论如何,Data is definitely an issue that cannot be avoided in embedded.Messy data doesn't make much sense.So we need to package the data at the source,组帧,再进行传输;Unpack after reaching the terminal,还原,Read out the raw data.
编程思路
This function is used in a simple environmental detection project,使用到了LoRa模块,It can be directly driven by the serial port of the microcontroller.但是使用的STM32F1系列单片机ADC为12位,Storage variables and only8位或16位,This requires data conversion.SBUSThe frame header in the protocol is 0x0F,帧尾为0x00,The penultimate byte is the flag byte,It is planned to be an urgent flag byte,For example, the normal situation is to report the surrounding environment of the collection node at an interval of time;But there is a fire or other data exceeds the threshold,则立即发送数据.But this time the focus is on how to convert,为求简便,Checksums are not considered.
构建结构
typedef struct _SBUSData_Struct
{
uint16_t Channel[8];
uint8_t FlagByte;
} SBUSData_Struct;
具体函数
1.Convert serial data to analogSBUS数据.
ErrorStatus Map_SCOMtoSBUS(SBUSData_Struct *SBUSdata,uint8_t *SCOMdata)
{
uint8_t ii;
if((SCOMdata == NULL) | (SCOMdata[0] != 0x0F)|(SCOMdata[13] != 0x00))
{
return ERROR;
}
SBUSdata->FlagByte = *(SCOMdata +12);
SBUSdata->Channel[0] = ((*(SCOMdata +2)<<8) | (*(SCOMdata +1))>>0)&0x07FF;
SBUSdata->Channel[1] = ((*(SCOMdata +3)<<5) | (*(SCOMdata +2))>>3)&0x07FF;
SBUSdata->Channel[2] = ((*(SCOMdata +5)<<10) | ((*(SCOMdata +4))<<2) | ((*(SCOMdata +3))>>6))&0x07FF;
SBUSdata->Channel[3] = ((*(SCOMdata +6)<<7) | (*(SCOMdata +5))>>1)&0x07FF;
SBUSdata->Channel[4] = ((*(SCOMdata +7)<<4) | (*(SCOMdata +6))>>4)&0x07FF;
SBUSdata->Channel[5] = ((*(SCOMdata +9)<<9) | ((*(SCOMdata +8))<<1) | ((*(SCOMdata +7))>>7))&0x07FF;
SBUSdata->Channel[6] = ((*(SCOMdata +10)<<6) | (*(SCOMdata +9))>>2)&0x07FF;
SBUSdata->Channel[7] = ((*(SCOMdata +11)<<3) | (*(SCOMdata +10))>>5)&0x07FF;
for(ii = 0; ii < 8; ii++)
{
if(SBUSdata->Channel[ii] > 2047)
{
SBUSdata->Channel[ii] = 2047;
}
}
return SUCCESS;
}
2.will imitateSBUS数据转换为串口数据.
ErrorStatus Map_SBUStoSCOM(uint8_t *SCOMdata,SBUSData_Struct *SBUSdata)
{
uint8_t ii;
uint16_t Tempdata[16];
if(SBUSdata == NULL)
{
return ERROR;
}
*(SCOMdata + 0) = 0x0F;
*(SCOMdata + 13) = 0x00;
*(SCOMdata + 12) = SBUSdata->FlagByte;
for(ii=0;ii<8;ii++)
{
Tempdata[ii] = SBUSdata->Channel[ii] & 0x07FF;
}
*(SCOMdata + 1) = Tempdata[0] & 0xFF;
*(SCOMdata + 2) = (Tempdata[0]>>8)|(Tempdata[1]<<3);
*(SCOMdata + 3) = (Tempdata[1]>>5)|(Tempdata[2]<<6);
*(SCOMdata + 4) = (Tempdata[2]>>2);
*(SCOMdata + 5) = (Tempdata[2]>>10)|(Tempdata[3]<<1);
*(SCOMdata + 6) = (Tempdata[3]>>7)|(Tempdata[4]<<4);
*(SCOMdata + 7) = (Tempdata[4]>>4)|(Tempdata[5]<<7);
*(SCOMdata + 8) = (Tempdata[5]>>1);
*(SCOMdata + 9) = (Tempdata[5]>>9)|(Tempdata[6]<<2);
*(SCOMdata + 10) = (Tempdata[6]>>6)|(Tempdata[7]<<5);
*(SCOMdata + 11) = (Tempdata[7]>>3);
return SUCCESS;
}
写在后面
帧头和帧尾是固定的,The flag byte is not considered for the time being,Because if only“紧急情况”与“普通情况”,The number of data bits is sufficient.本质上就是11个8bit的数据转换成8个11bit的数据.Although it is still when defining variablesuint16_t,即unsigned short int.But that's because computers are binary,I can't define11位的数据,But I can define16位的数据,但只用到11位.For example, the function also has a greater than2047便等于2047的操作.因为12位ADC,最大值应该是4095,But all in one11位,So there is this operation.
A single copy of the communication format,When I first came into contact with it, I was devastated,Let alone a more complete protocol,There are too many things to consider.还是要继续学习.
If the data requirements are only in binary,11Bits can also be transferred12位的数据,Shift right by one bit before transmission,Shift left one bit after transmission,can be restored.
边栏推荐
猜你喜欢
dotnet OpenXML 解析 PPT 图表 面积图入门
express hot-reload
Creo 9.0 基准特征:基准坐标系
sql server中 两表查询 平均数 分组
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)
mysql进阶(二十七)数据库索引原理
使用 External Secrets Operator 安全管理 Kubernetes Secrets
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
CPU的亲缘性affinity
随机推荐
茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品
CPU的亲缘性affinity
只有一台交换机,如何实现主从自动切换之nqa
使用HBuilder离线本地打包ipa教程
PAT Class B-B1019 Digital Black Hole (20)
Creo 9.0 基准特征:基准坐标系
IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
Voice conversion相关语音数据集综合汇总
长达四年的减肥记录
【ASM】字节码操作 方法的初始化 Frame
无题十二
【Excel实战】--图表联动demo_001
ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
PAT乙级-B1019 数字黑洞(20)
sql server中 两表查询 平均数 分组
Creo 9.0 基准特征:基准平面
放大器OPA855的噪声计算实例
请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
无题十四