gpt4 book ai didi

c - 读取引脚电平 Raspberry Pi

转载 作者:行者123 更新时间:2023-12-04 10:35:41 24 4
gpt4 key购买 nike

我一直在使用这个 example 中的代码C gpio 例子下。我能够在没有定义问题的情况下设置和写入引脚:

// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0

  INP_GPIO(4); // must use INP_GPIO before we can use OUT_GPIO
OUT_GPIO(4);
GPIO_SET = 1<<4;

这很好用。但是,如果我想从 pin 读取信息,我不确定该怎么做。我试图通过返回 gpio + thePin 来读取它,但我相信这是给我地址而不是值。我尝试返回一个指针,但这也给了我垃圾 (-232783856)。

关于如何从引脚读取值有什么想法吗?

最佳答案

#define GPIO_LEV *(gpio+13)                  // pin level
INP_GPIO(4); // pin 4 initialization for reading
unsigned int value = GPIO_LEV; // reading all 32 pins
bool pin4_value = ((value & (1 << 4)) != 0); // get pin 4 value

此代码基于 bcm2835 library 中的函数 bcm2835_gpio_lev并且仅可用于引脚 0 到 31。如果它不起作用,请查看此库 - 有双倍的读数。

关于c - 读取引脚电平 Raspberry Pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com