当前位置: 首页 > news >正文

STM32 大小端与字节对齐使用记录

大小端

串口数据包解析

MDK stm32 小段模式

接收到的数据包:

DD 03 00 1B 11 59 00 00 00 00 17 70 00 00 2F 39 00 00 00 00 00 03 23 64 00 0E 02 0B 6E 0B 84 FC EA 77

其中数据内容为:

DD 03 00 1B 
11 59     //电压mV
00 00 
00 00 
17 70 
00 00 
2F 39 
00 00 
00 00 
00 03 
23 
64 
00 
0E 
02 
0B 6E 
0B 84 
FC EA 77 

我们设计的结构体

typedef struct 
{uint16_t 	total_voltage;int16_t		current;uint16_t 	RM;uint16_t 	FCC;	uint16_t 	cycle;uint16_t 	date;	uint16_t 	balanced_l;uint16_t 	balanced_h;union{struct{uint16_t Cell_OV			:1;	uint16_t Cell_UV			:1;	uint16_t Pack_OV			:1;uint16_t Pack_UV			:1;	uint16_t OTC					:1;		uint16_t UTC					:1;		uint16_t OTD					:1;	uint16_t UTD					:1;	uint16_t OCC					:1;		uint16_t OCD					:1;		uint16_t SC						:1;	uint16_t AFE_ERR			:1;	uint16_t PF						:1;		uint16_t RSVD1				:1;				uint16_t RSVD2				:1;				uint16_t RSVD3				:1;			}StatusBit;uint16_t StatusWord;}ProtectStatus;uint8_t FW_ver;uint8_t RSOC;union{struct{uint8_t CHG_FET			:1;	uint8_t DSG_FET			:1;	uint8_t RSVD1				:1;uint8_t RSVD2				:1;	uint8_t RSVD3				:1;		uint8_t RSVD4				:1;		uint8_t RSVD5				:1;	uint8_t RSVD6				:1;			}StatusBit;uint8_t StatusByte;}FETStatus;	uint8_t		number_of_series_num;uint8_t		NTC_sensor_num;uint16_t	NTC_temperature[NUMBER_NTC_SENSOR];}BMS_03_BaseInfo_statues;

使用内容复制函数

memcpy(&bms_baseinfo,buf+4,buf[3]);

想要的数据:

total_voltage = 0x1159

实际的数据:

total_voltage = 0x5911

解决方法:

CMSIS都已经给你写好的,请看core_cm3.h
Name Core Generated CPU Instruction Description
void __NOP (void) M0, M3, M4 NOP No Operation
void __WFI (void) M0, M3, M4 WFI Wait for Interrupt
void __WFE (void) M0, M3, M4 WFE Wait for Event
void __SEV (void) M0, M3, M4 SEV Set Event
void __ISB (void) M0, M3, M4 ISB Instruction Synchronization Barrier
void __DSB (void) M0, M3, M4 DSB Data Synchronization Barrier
void __DMB (void) M0, M3, M4 DMB Data Memory Barrier
uint32_t __REV (uint32_t value) M0, M3, M4 REV Reverse byte order in integer value.
uint32_t __REV16 (uint16_t value) M0, M3, M4 REV16 Reverse byte order in unsigned short value.  
sint32_t __REVSH (sint16_t value) M0, M3, M4 REVSH Reverse byte order in signed short value with sign extension to integer.
uint32_t __RBIT (uint32_t value) M3, M4 RBIT Reverse bit order of value
uint8_t __LDREXB (uint8_t *addr) M3, M4 LDREXB Load exclusive byte
uint16_t __LDREXH (uint16_t *addr) M3, M4 LDREXH Load exclusive half-word
uint32_t __LDREXW (uint32_t *addr) M3, M4 LDREXW Load exclusive word
uint8_t __STREXB (uint8_t value, uint8_t *addr) M3, M4 STREXB Store exclusive byte
uint16_t __STREXH (uint16_t value, uint16_t *addr) M3, M4 STREXH Store exclusive half-word
uint32_t __STREXW (uint32_t value, uint32_t *addr) M3, M4 STREXW Store exclusive word
void __CLREX (void) M3, M4 CLREX Remove the exclusive lock created by __LDREXB, __LDREXH, or __LDREXW
void __SSAT (void) M3, M4 SSAT saturate a signed value
void __USAT (void) M3, M4 USAT saturate an unsigned value

字节对齐

执行完这个代码后

memcpy(&bms_baseinfo,buf+4,buf[3]);

我们希望

NTC_temperature[0] = 0x6E0B;
NTC_temperature[1] = 0x840B;

实际上

NTC_temperature[0] = 0x0B6E;
NTC_temperature[1] = 0x0084;

解决方法:

#pragma pack (1)
//结构体
#pragma pack ()

http://www.lryc.cn/news/184786.html

相关文章:

  • RabbitMQ中basic**方法汇总与参数解释
  • linux之/etc/default/useradd文件
  • 3.primitive主数据类型和引用 认识变量
  • 【群智能算法改进】一种改进的光学显微镜算法 IOMA算法[1]【Matlab代码#60】
  • 第三课-软件升级-Stable Diffusion教程
  • 【C++】设计模式之——建造者
  • 【C++】基础语句(学习笔记)
  • 大厂秋招真题【DP】米哈游20230924秋招T2-米小游与魔法少女-奇运
  • LVS+Keepalived 高可用集群负载均衡
  • Qt QList类和QLinkedList类 详解
  • Mac安装GYM遇到的一些坑
  • 【高级rabbitmq】
  • 数百个下载能够传播 Rootkit 的恶意 NPM 软件包
  • SpringBoot的error用全局异常去处理
  • MyBatisPlus(十一)包含查询:in
  • Linux命令定位与查找:which、whereis和find的用法详解
  • LeetCode 面试题 17.10. Find Majority Element LCCI【摩尔投票法】简单
  • 多校联测11 模板题
  • Linux SSH连接远程服务器(免密登录、scp和sftp传输文件)
  • 从0开始python学习-30.selenium frame子页面切换
  • asp.net core 远程调试
  • Java spring boot 一次调用多个请求
  • DRM全解析 —— CRTC详解(4)
  • 六个为Rust构建的IDE
  • 25 Python的collections模块
  • JEPG Encoder IP verilog设计及实现
  • yolov5 web端部署进行图片和视频检测
  • 嵌入式养成计划-34--函数库
  • PM864AK01-eA 3BSE018161R2 工业人工智能供应链先驱
  • 参与现场问题解决总结(Kafka、Hbase)