微控制器位域
我正在学习微控制器,但我很难理解如何使用联合来设置 GPIO 端口上的各个位。
typedef union _BYTE
{
byte _byte;
struct
{
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
}Bit;
} BYTE;
我使用上面的方法来访问字节的各个位,但是如何使用它以以下方式分配 io 端口值?
MCF_GPIO_PORTDD.Bit.b0 = 1;
我宁愿不分配 _BYTE 类型,然后将端口分配给它。
#define MCF_GPIO_PORTDD (*(vuint8 *)(&__IPSBAR[0x100009]))
MCF_GPIO_PORTDD 只是一个内存地址。
I am learning about microcontrollers and I am having difficulty understanding how one would use a union to set individual bits on a gpio port.
typedef union _BYTE
{
byte _byte;
struct
{
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
}Bit;
} BYTE;
I am using the above to gain access to the individual bits of a byte, but how do I use this to assign a io port values in the following manner?
MCF_GPIO_PORTDD.Bit.b0 = 1;
I would rather not allocate a type of _BYTE and then assign the port to that.
#define MCF_GPIO_PORTDD (*(vuint8 *)(&__IPSBAR[0x100009]))
MCF_GPIO_PORTDD is just a memory address.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将端口指针转换为
易失性无符号字符
(8 位整数)。您可以为您的类型创建一个类似的宏:然后使用
PORTDD.Bit.b0
访问它。请注意,在某些情况下,如果您需要确保在单个寄存器写入中设置(或清除)多个位,则需要直接写入._byte
。如果您不命名Bit
(只是将其设为匿名),您的 C 编译器可能会让您将其缩短为PORTDD.b0
。It's casting the port pointer to a
volatile unsigned char
(8 bit int). You can make a similar macro for your type:and then access it with
PORTDD.Bit.b0
. Note that in some instances you will need to write._byte
directly if you need to ensure multiple bits are set (or cleared) in a single register write. If you don't nameBit
(just make it anonymous) your C compiler may let you shorten that toPORTDD.b0
.