简体   繁体   English

设置 GPIO 输入/输出位

[英]Setting GPIO Input / Output Bits

Im working on an Raspberry Pi Assembly Project with some LED's and Input Button.我正在使用一些 LED 和输入按钮进行 Raspberry Pi 组装项目。 At the moment I have a blinking LED and a Button to turn on another LED.目前我有一个闪烁的 LED 和一个用于打开另一个 LED 的按钮。 Now i want to set up another LED and I'm stuck on something I'm doing wrong or don't quite understand.现在我想设置另一个 LED,但我被困在我做错或不太明白的事情上。

So if I have more then one LED's in one GPFSEL, I need to set them in one line of Code so they dont overwrite each other.因此,如果我在一个 GPFSEL 中有多个 LED,我需要在一行代码中设置它们,以免它们相互覆盖。 For example:例如:

GPIO Port 21 is FSEL21 = Bit 5 - 3 GPIO 端口 21 是 FSEL21 = 位 5 - 3

GPIO Port 27 is FSEL27 = Bit 23 - 21 GPIO 端口 27 是 FSEL27 = 位 23 - 21

To set them to Output I need to set the least significant bit to 1. These are: 0x08 and 0x200000 in Hex.要将它们设置为输出,我需要将最低有效位设置为 1。它们是:十六进制中的 0x08 和 0x200000。

If I do it in two lines of code, like:如果我用两行代码来做,比如:

ldr register,=0x08
str register,[base,#GPFSEL2]

ldr register,=0x200000 
str register,[base,#GPFSEL2]

It doesnt work.它不起作用。

So I did it in one line of Code and this worked:所以我在一行代码中做到了,这很有效:

ldr register,=0x200008
str register,[base,#GPFSEL2]

The problem I have now is to set the GPFSEL1 because it has one Output and one Input.我现在的问题是设置 GPFSEL1,因为它有一个输出和一个输入。 The Documentary says i have to set 000 for an input.纪录片说我必须为输入设置 000。

So I have:所以我有:

GPIO Port 19 = Output is FSEL19 = Bit 29 - 27 GPIO 端口 19 = 输出为 FSEL19 = 位 29 - 27

GPIO Port 17 = Input Button is FSEL17 = Bit 23-21 GPIO 端口 17 = 输入按钮为 FSEL17 = 位 23-21

GPIO 19 = 0x8000000 GPIO 19 = 0x8000000

GPIO Port 17, the documentary says i have to set the bits to 000. And a tutorial I'm reading sets the mask to 0xFF1FFFFF which is 11111111000111111111111111111111 in Binary. GPIO 端口 17,纪录片说我必须将位设置为 000。我正在阅读的教程将掩码设置为 0xFF1FFFFF,即二进制中的 111111111111111111111111。

Now i don't understand how I cant set them together.现在我不明白我怎么不能把它们放在一起。 And do I have to set the input?我必须设置输入吗? Shouldn't it be 000 anyway if I dont set anything?如果我不设置任何东西,它不应该是 000 吗? I tried to not set anything in input and it worked as input but the blinking LED's got really slow without any other change in the code.我试图在输入中不设置任何内容,它作为输入工作,但是在代码中没有任何其他更改的情况下,闪烁的 LED 变得非常慢。 I hope you can tell me what's the correct way to set this bits.我希望你能告诉我设置这些位的正确方法是什么。

Thank you for your help!感谢您的帮助!

  1. you have to make sure to touch only these bits which you are interested in. Changing other ones will affect function of unrelated pins.您必须确保只接触您感兴趣的这些位。更改其他位会影响无关引脚的功能。

  2. accordingly BCM datasheet, you want to set these bits to 0b001 (GPIO output).因此,您希望将这些位设置为 0b001(GPIO 输出)。

To set the function of the output pins, you can use要设置输出引脚的功能,您可以使用

ldr        r0, [base,#GPFSEL2]
bic        r0, #(7 << 3)
bic        r0, #(7 << 21)
orr        r0, #(1 << 3)
orr        r0, #(1 << 21)
str        r0, [base,#GPFSEL2]

Ditto for input, but you can omit the orr there.输入也是如此,但您可以省略那里的orr

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

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