当前位置:网站首页>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.
边栏推荐
猜你喜欢

Dynamic memory development (C language)

Dry goods!Generative Model Evaluation and Diagnosis

eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑

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

微服务 技术栈

Seata source code analysis: initialization process of TM RM client

Thinking and summary of the efficiency of IT R&D/development process specification

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

seata源码解析:事务状态及全局锁的存储

CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
随机推荐
无题九
CCVR基于分类器校准缓解异构联邦学习
Creo 9.0 基准特征:基准坐标系
无题五
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
手写柯里化 - toString 理解
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
2022-08-01 Review the basic binary tree and operations
使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
无题十二
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
Keil升级到AC6后,到底有哪些变化?
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
链表中的数字相加----链表专题
Weekly Report 2022-8-4
2022/8/4 考试总结
sphinx matches the specified field