嵌入式系统中union的用法
来源:互联网 发布:苹方字体ttf windows 编辑:程序博客网 时间:2024/05/22 10:38
在嵌入式实际应用中,大多数的协议结构定义中,都会使用union类型来定义。
使用联合来将较大的对象分解成组成这个对象的各个字节。
typedef union{
char bytes[4];
uint udata;
}data;
可以使用data.udata数组来访问data.bytes[4]字段的各个字节。
注意:
char 占 1 字节
short 占 2 字节,
int 、float 占 4 字节,
double 占8 字节
使用用法:在利用 flash中保存数据信息时
#include "stm32f0xx_flash.h"
#define FLASH_PAGE_SIZE
((uint32_t)0x00000400)
#define FLASH_USER_START_ADDR((uint32_t)0x0800FC00)
#define FLASH_USER_END_ADDR((uint32_t)0x08010000)
//定义需保存用户设置数据的结构
typedef struct //构造结构体{unsigned char name[16];float height;float weight;}Personal_Information_TypeDef;//为了方便变量的使用和在Flash里面的存贮,定义一联合(union)
typedef union
{
uint32_t DataAll[6]; //因为变量结构总共为24bytes,所以为6个32位变量
Personal_Information_TypeDef,inforDataI; } settings_Information;
//申明变量,注意一定要四字节对齐
volatile settings_Information user_seting;
void User_infor_read(void)
{
int i;
uint32_t address_t;
address_t = FLASH_USER_START_ADDR; //flash数据存储起始地址
for(i=0;i
{
user_seting.DataAll[i] = *(__IO uint32_t *)address_t; // 读出所以数据 //*(__IO uint32_t *)address_t =
user_seting.DataAll[i]; //写入数据
address_t += 4;
}
}
由于 user_seting.DataAll[i] ,与 user_seting.inforDataI 共用一块内存,所以可以通过:
user_seting.inforDataI .name[];
user_seting.inforDataI .height;
user_seting.inforDataI .weight;访问数据结构的数据
- 嵌入式系统中union的用法
- sql中UNION的用法
- C中union的用法
- 数据库中Union的用法
- Oracle 中union的用法
- Oracle 中union的用法
- Oracle中union的用法
- Oracle 中union的用法
- mysql 中 union 的用法
- mysql中union的用法
- 九.Sql server中union, union all 的用法
- SQL中Union和Union All的用法
- sql中union 和union all的用法和区别
- MySQL中UNION和UNION ALL的用法区别
- SQL中union 与union all的用法
- .Sql server中union, union all 的用法
- SQL中UNION和UNION ALL的详细用法
- mysql中union、union all的用法实例
- Java双向链表实现
- 利用wireshake暴力破解密码
- java 构造函数浅谈
- 内部赛题目 学学格式控制
- 变量在shell脚本间的传递
- 嵌入式系统中union的用法
- python list 去重
- 编写高质量java代码一
- union和union all 的区别
- 如何访问VirtualBox虚拟机中的服务器
- oracle 存储过程 如何获得 捕获异常的内容
- [转] 网卡超时实现机制 watchdog_timeo/ndo_tx_timeout
- 王学岗ListView滑动删除
- AVplayer实现播放本地和网络视频(Swift3.0)