C GPIO 十六进制编号
我得到了以下代码作为示例:
使端口 0 位 0-2 输出,其他为输入。
FIO0DIR = 0x00000007;
将 P0.0、P0.1、P0.2 全部设置为低 (0)
FIO0CLR = 0x00000007;
我被告知该端口连接有 31 个 LED。我不明白为什么要启用前 3 个输出,它是 0x00000007 而不是 0x00000003?
I have been given the following bit of code as an example:
Make port 0 bits 0-2 outputs, other to be inputs.
FIO0DIR = 0x00000007;
Set P0.0, P0.1, P0.2 all low (0)
FIO0CLR = 0x00000007;
I have been told that the port has 31 LED's attached to it. I cant understand why, to enable the first 3 outputs, it is 0x00000007 not 0x00000003?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些 GPIO 配置寄存器是位图。
使用 Windows 计算器将十六进制转换为二进制:
0x00000007 = 111,或使用 32 位 - 00000000000000000000000000000111 // 三个输出
0x00000003 = 11,或使用 32 位 - 00000000000000000000000000000011 //只有两个输出
These GPIO config registers are bitmaps.
Use your Windows calculator to convert the hex to binary:
0x00000007 = 111, or with 32 bits - 00000000000000000000000000000111 // three outputs
0x00000003 = 11, or with 32 bits - 00000000000000000000000000000011 // only two outputs
因为写入寄存器的值是二进制位掩码,其中一位表示“这是一个输出”。您不写“我想要的输出数量”,而是同时设置 8 个单独的标志。
二进制中的数字 7 是
00000111
,因此它的最低三位设置为 1,这在这里似乎意味着“这是一个输出”。另一方面,十进制值 3 只是二进制的00000011
,因此只有两个位设置为 1,这显然太少了。位从右侧开始索引,从 0 开始。位号 n 的十进制值为 2n。具有多个位设置的二进制数的十进制值只是所有设置位的所有值的总和。
例如,设置了位 0、1 和 2 的数字的十进制值为 20 + 21 + 22 = 1 + 2 + 4 = 7。
这是一个很棒的 ASCII 表,显示了字节的 8 位及其各自的值:
Because the value you write to the register is a binary bit-mask, with a bit being one meaning "this is an output". You don't write the "number of outputs I'd like to have", you are setting 8 individual flags at the same time.
The number 7 in binary is
00000111
, so it has the lower-most three bits set to 1, which here seems to mean "this is an output". The decimal value 3, on the other hand, is just00000011
in binary, thus only having two bits set to 1, which clearly is one too few.Bits are indexed from the right, starting at 0. The decimal value of bit number n is 2n. The decimal value of a binary number with more than one bit set is simply the sum of all the values of all the set bits.
So, for instance, the decimal value of the number with bits 0, 1 and 2 set is 20 + 21 + 22 = 1 + 2 + 4 = 7.
Here is an awesome ASCII table showing the 8 bits of a byte and their individual values: