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;

使用内容复制函数

cpp 复制代码
memcpy(&bms_baseinfo,buf+4,buf[3]);

想要的数据:

cpp 复制代码
total_voltage = 0x1159

实际的数据:

cpp 复制代码
total_voltage = 0x5911

解决方法:

|----------------------------|
| CMSIS都已经给你写好的,请看core_cm3.h |

cpp 复制代码
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

字节对齐

执行完这个代码后

cpp 复制代码
memcpy(&bms_baseinfo,buf+4,buf[3]);

我们希望

cpp 复制代码
NTC_temperature[0] = 0x6E0B;
NTC_temperature[1] = 0x840B;

实际上

cpp 复制代码
NTC_temperature[0] = 0x0B6E;
NTC_temperature[1] = 0x0084;

解决方法:

cpp 复制代码
#pragma pack (1)
//结构体
#pragma pack ()
相关推荐
范纹杉想快点毕业21 小时前
ZYNQ PS 端 UART 接收数据数据帧(初学者友好版)嵌入式编程 C语言 c++ 软件开发
c语言·笔记·stm32·单片机·嵌入式硬件·mcu·51单片机
方圆工作室21 小时前
51单片机驱动数码管
单片机·嵌入式硬件·51单片机
乔宕一1 天前
stm32 链接脚本没有 .gcc_except_table 段也能支持 C++ 异常
c++·stm32·嵌入式硬件
范纹杉想快点毕业1 天前
STM32 串口接收数据包(自定义帧头帧尾)
stm32·单片机·嵌入式硬件
茯苓gao1 天前
STM32G4 电流环闭环
笔记·stm32·单片机·嵌入式硬件·学习
单片机系统设计1 天前
基于stm32的环境监测系统/智能家居/空气质量监测系统
stm32·单片机·嵌入式硬件·毕业设计·智能家居
不知所云,1 天前
5. STM32 时钟系统分配
stm32·单片机·嵌入式硬件
电子科技圈1 天前
芯科科技FG23L无线SoC现已全面供货,为Sub-GHz物联网应用提供最佳性价比
科技·嵌入式硬件·mcu·物联网·制造·智能硬件·交通物流
天天爱吃肉82181 天前
【比亚迪璇玑架构深度解析:重新定义智能电动汽车的“整车智能”】
数据库·人工智能·嵌入式硬件·架构·汽车
糖糖单片机设计1 天前
硬件开发_基于物联网的沼气池环境监测系统
stm32·单片机·嵌入式硬件·物联网·51单片机