data=*(vu16*)addr;的理解?

本文详细解释了如何使用指针来操作内存中的数据,包括如何通过指针访问不同大小的数据类型,以及如何通过寄存器地址直接操作寄存器。此外还介绍了使用volatile关键字防止编译器优化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先addr是一个32位的整形数据(比如是0x80040000),代表的是一个地址(地址一般都是32位的),(VU16*)就是把这个整数强制转换成一个指针(这个指针还是32位的,只不过它指向的数据是16位的),这个指针指向的地址里面存了一个类型为vu16的变量x,*(vu16*)addr就是把这个x赋值给data。

 

 

再来看另一个例子(怎么样通过寄存器的地址来操作寄存器,就是给寄存器赋值,下面这句话就可以操作寄存器):

很多时候我们会看到下面的宏定义

#define   MEM_ADDR(addr)            *((volatile unsigned long  *)(addr)) 


这里的addr是一个寄存器的地址(其实地址就是一个32位的数值,比如0x80040000),要将它强制转化成一个指针(并且声明这个指针指向的内容是一个32位的long型变量)
用(unsigned long  *)(addr) 即可,这样就成了一个真正的有血有肉的地址了。
前面再加一个*号,就可以访问这个地址得到其中的变量值了。


在C语言中,unsigned char *p; 定义p为一个指向unsigned char的地址指针;而 *p=1;就是向这个指针指向的地址所存储的变量赋值为1了。
至于中间加一个volatile关键字,则指示编译器不要自作主张对此进行优化,必须每次老老实实地去直接访问这个地址!!!

转载于:https://www.cnblogs.com/jblzky/p/9342110.html

/* ----------------------------------------------------------------------------- * Copyright (c) 2014 - 2019 ARM Ltd. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from * the use of this software. Permission is granted to anyone to use this * software for any purpose, including commercial applications, and to alter * it and redistribute it freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software in * a product, an acknowledgment in the product documentation would be * appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * * 3. This notice may not be removed or altered from any source distribution. * * * $Date: 2021-7-1 * $Revision: V1.0.0 * * Project: Flash Programming Functions for Puya PY32F0xx Flash * --------------------------------------------------------------------------- */ /* History: * Version 1.0.0 * Initial release */ #include "FlashOS.h" // FlashOS Structures typedef volatile unsigned char vu8; typedef unsigned char u8; typedef volatile unsigned short vu16; typedef unsigned short u16; typedef volatile unsigned long vu32; typedef unsigned long u32; #define M8(adr) (*((vu8 *) (adr))) #define M16(adr) (*((vu16 *) (adr))) #define M32(adr) (*((vu32 *) (adr))) #define SET_BIT(REG, BIT) ((REG) |= (BIT)) #define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT)) #define READ_BIT(REG, BIT) ((REG) & (BIT)) #define CLEAR_REG(REG) ((REG) = (0x0)) #define WRITE_REG(REG, VAL) ((REG) = (VAL)) #define READ_REG(REG) ((REG)) #define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(RE
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值