简体   繁体   English

计算GPIO地址

[英]Calculate GPIO address

GPIO_MODER 端口模式寄存器

Each pin uses two bits.每个引脚使用两位。

00 = input
01 = output

In an example they configure pins 15-8 as inputs and pins 7-0 as outputs.在一个示例中,他们将引脚 15-8 配置为输入,将引脚 7-0 配置为输出。

@ arm assembly
LDR R0,=0x40020C00
LDR R1,=0x00005555
STR R1,[R0]
/* c */
*( (unsigned long *)0x40020C00) = 0x5555;

This is the first example in the chapter "General Purpose IO" and I am completely lost.这是“通用 IO”一章中的第一个示例,我完全迷失了。 I thought I had figured out how they got 0x00005555 but now I do not know.我以为我已经弄清楚他们是如何得到 0x00005555 的,但现在我不知道。 If each pin uses two bits and 01 is output I get 01 01 01 01 which is 0x55 and not 0x5555.如果每个引脚使用两位并且 01 是 output 我得到 01 01 01 01 是 0x55 而不是 0x5555。 They use 16 bits to get 0x5555 = 0101 0101 0101 0101 and I do not understand why.他们使用 16 位来获得 0x5555 = 0101 0101 0101 0101,我不明白为什么。

Now to pins 15-8.现在到引脚 15-8。 Here I do not understand anything sadly.在这里,我可悲的是什么都不懂。 00 is for input so I get 00 00 00 00. Then there is an offset 0x01 which I do not understand how it should be used. 00 用于输入,所以我得到 00 00 00 00。然后有一个偏移量 0x01,我不明白它应该如何使用。

Any help is greatly appreciated, I am in need of a more simpler example to understand I guess.非常感谢任何帮助,我想我需要一个更简单的例子来理解。


EDIT: So I mixed up the bits and pins, thanks NoTengoBattery.编辑:所以我混淆了位和引脚,感谢 NoTengoBattery。 I understand 0101 0101 0101 0101 = 0x5555.我理解 0101 0101 0101 0101 = 0x5555。 But I still do not understand how I get to 0x40020C00 from 0000 0000 0000 0000.但我仍然不明白我是如何从 0000 0000 0000 0000 到 0x40020C00 的。

I think that your confusion comes from understanding the text, not the concept.我认为您的困惑来自对文本的理解,而不是概念。 You said:你说:

... and pins 7-0 as outputs... ...和引脚7-0作为输出...

That's 8 pins.那是8针。 After that, you said:之后,你说:

... and 01 is output I get 01 01 01 01 which is 0x55... ...和 01 是 output 我得到 01 01 01 01 这是 0x55 ...

If you see how you separated it yourself, you are only considering 4 pins, not 8. If you consider 4 pins you are right, it's 0x55.如果您自己查看如何将其分开,那么您只考虑 4 个引脚,而不是 8 个。如果您考虑 4 个引脚,那么您是对的,它是 0x55。 If you consider 8 pins, its 0b0101010101010101 which is, indeed, 0x5555.如果你考虑 8 个引脚,它的 0b0101010101010101 实际上是 0x5555。

It looks like you're getting lost converting binary to hex.看起来你迷失了将二进制转换为十六进制。 Remember, every four bits can be expressed as a single hex character.请记住,每四位可以表示为一个十六进制字符。 The following is a breakdown of where they derive 0x5555 based on what you've described (I've split the whole register into two parts for my own sanity dealing with word wrap in this answer box, but imagine they're mashed together):以下是根据您所描述的内容对它们派生 0x5555 的位置的细分(我已将整个寄存器分成两部分,以便我自己在此答案框中处理自动换行,但想象它们被混合在一起):

       |pin15|pin14|pin13|pin12|pin11|pin10|pin09|pin08|
bit    |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|
bin 0b |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |
hex 0x |     0     |     0     |     0     |     0     |

       |pin07|pin06|pin05|pin04|pin03|pin02|pin01|pin00|
bit    |15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00|
bin 0b |0 |1 |0 |1 |0 |1 |0 |1 |0 |1 |0 |1 |0 |1 |0 |1 |
hex 0x |     5     |     5     |     5     |     5     |

Put is all together and you get 0x00005555.将所有内容放在一起,您将得到 0x00005555。 0x40020C00 is apparently the address of this register. 0x40020C00 显然是这个寄存器的地址。 *( (unsigned long *)0x40020C00) casts 0x40020C00 to an unsigned long pointer, then dereferences that pointer to write 0x00005555 to that address with = 0x5555; *( (unsigned long *)0x40020C00)将 0x40020C00 转换为unsigned long指针,然后取消引用该指针以使用= 0x5555; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM