当前位置:网站首页>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.
边栏推荐
- Redis源码解析:Redis Cluster
- 请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
- 2022-08-01 回顾基础二叉树以及操作
- Seata source code analysis: initialization process of TM RM client
- Overall design and implementation of Kubernetes-based microservice project
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
- 无题五
- Code Audit - PHP
- express hot-reload
- 上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
猜你喜欢

19.服务器端会话技术Session

蚁剑webshell动态加密连接分析与实践

手把手教你纯c实现异常捕获try-catch组件

MySQL内部函数介绍

Concurrent CAS

Dynamic memory development (C language)

dotnet OpenXML 解析 PPT 图表 面积图入门

Overall design and implementation of Kubernetes-based microservice project

CCVR eases heterogeneous federated learning based on classifier calibration

XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
随机推荐
seata源码解析:事务状态及全局锁的存储
openpyxl操作Excel文件
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
Seata source code analysis: initialization process of TM RM client
How to realize the short press and long press detection of the button?
MySQL内部函数介绍
长达四年的减肥记录
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
ffmpeg drawtext 添加文本水印
【Excel实战】--图表联动demo_001
交换机端口的三种类型详解与hybrid端口实验
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
Creo 9.0 基准特征:基准坐标系
19.服务器端会话技术Session
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
Creo 9.0 基准特征:基准平面
Dynamic memory development (C language)
sql server中 两表查询 平均数 分组
EU | Horizon 2020 ENSEMBLE: D2.13 SOTIF Safety Concept (Part 2)