作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用这个 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/
我是一名优秀的程序员,十分优秀!