当前位置:网站首页>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.
边栏推荐
- 2022-08-01 Review the basic binary tree and operations
- Weekly Report 2022-8-4
- 这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
- pytorch余弦退火学习率CosineAnnealingLR的使用
- dotnet OpenXML 解析 PPT 图表 面积图入门
- 无题一
- openpyxl to manipulate Excel files
- 基于 Kubernetes 的微服务项目整体设计与实现
- XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
- Overall design and implementation of Kubernetes-based microservice project
猜你喜欢
百行代码发射红心,程序员何愁命不中女朋友!
Thinking and summary of the efficiency of IT R&D/development process specification
C语言的高级用法
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
shell脚本实例
express hot-reload
seata源码解析:TM RM 客户端的初始化过程
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
七夕浪漫约会不加班,RPA机器人帮你搞定工作
【zeno】为zeno增加子模块/新节点的最小化的例子
随机推荐
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
PAT Level B - B1021 Single Digit Statistics (15)
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
新白娘子传奇系列
Assembly language (8) x86 inline assembly
pytorch余弦退火学习率CosineAnnealingLR的使用
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
matcher中find,matches,lookingAt匹配字符串的不同之处说明
Creo 9.0 基准特征:基准坐标系
Creo 9.0 基准特征:基准点
openpyxl to manipulate Excel files
Thinking and summary of the efficiency of IT R&D/development process specification
使用HBuilder离线本地打包ipa教程
Undefined symbols for architecture arm64解决方案
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
2.4G无线收发模块的应用
Voice conversion相关语音数据集综合汇总
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
CPU的亲缘性affinity
Oracle临时表空间作用