3

我一直在 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)。

任何想法如何从引脚读取值?

4

1 回答 1

4
#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 库中的函数 bcm2835_gpio_lev并且仅可用于引脚 0 到 31。如果它不起作用,请查看此库 - 读取值加倍。

于 2013-05-05T21:52:04.443 回答