如何改变一个32位的寄存器而不改变其他位特定位? [英] How to change a 32bit registers specific bits without changing other bits?

查看:448
本文介绍了如何改变一个32位的寄存器而不改变其他位特定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接利用其物理地址来操纵寄存器的某些位。不过,我无法找到一个方法,使这一点。我看到了有关设置位掩码的一些职位,但我觉得他们太混乱了。

I want to manipulate some bits of a register directly using its physical address. However I couldn't find a way to make this. I saw some posts about setting bit masks but I find them too confusing.

我的寄存器的物理地址为: 0x4A10005C

My registers physical address is: 0x4A10005C

我想操纵其位,是18-16位之间。我想设置 0x3中这些位内。

I want to manipulate its bit which was between 18-16 bits. I want to set 0x3 inside those bits.

我会很高兴,如果你们能提供一个答案还是有办法做到这一点。谢谢你。

I will be really glad if you guys can provide an answer or a way to do it. Thanks.

推荐答案

您可以只定义一个指针寄存器,然后用普通的C位操作来操纵各个位:

You can just define a pointer to the register and then use normal C bitwise operations to manipulate the individual bits:

volatile uint32_t * const my_register = (uint32_t *) 0x4A10005C;
                              // set up a pointer to the register

uint32_t val = *my_register;  // read register

val &= ~(0x7 << 16);          // clear bits 16..18

val |= (0x3 << 16);           // set bits 16..18 to 0x03 (i.e. set bits 16 and 17)

*my_register = val;           // write register

(以上假设你在寄存器中谈到三个位,16位,17和18,并且要设置位18到零,16位和17比1)。

(The above assumes that you are talking about three bits within the register, bits 16, 17 and 18, and that you want to set bit 18 to zero and bits 16 and 17 to 1.)

这篇关于如何改变一个32位的寄存器而不改变其他位特定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆