当前位置:网站首页>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.
边栏推荐
- selectPage 动态改变参数方法
- 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
- 嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
- 2022.8.3
- 交换机端口的三种类型详解与hybrid端口实验
- 请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
- ts/js function pass parameter with function writing
- 歌词整理
- 轩辕实验室丨欧盟EVITA项目预研 第一章(四)
- C语言-数组
猜你喜欢

【zeno】为zeno增加子模块/新节点的最小化的例子

pytorch余弦退火学习率CosineAnnealingLR的使用

CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类

【ASM】字节码操作 方法的初始化 Frame

XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法

Concurrent CAS

Creo 9.0 基准特征:基准点

js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)

Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need

Undefined symbols for architecture arm64解决方案
随机推荐
Voice conversion相关语音数据集综合汇总
明天去订票,准备回家咯~~
无题十三
无题六
【Excel实战】--图表联动demo_001
如何实现按键的短按、长按检测?
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
mysql进阶(二十七)数据库索引原理
Two-table query average grouping in sql server
express hot-reload
随时牵手 不要随意分手[转帖]
Creo 9.0 基准特征:基准坐标系
使用 External Secrets Operator 安全管理 Kubernetes Secrets
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
Creo 9.0 基准特征:基准平面
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
施一公:科学需要想象,想象来自阅读
今天是元宵节~~