2013-04-10 59 views
3

我一直在使用這個代碼從這個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; 

這工作得很好。不過,我不確定如果我想從一個別針讀取,該怎麼辦。我試圖通過返回gpio + thePin來閱讀它,但我相信這是給我的地址,而不是價值。我試圖返回一個指針,但也給了我垃圾(-232783856)。

任何想法如何讀取銷釘的價值?

回答

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_gpio_lev在bcm2835 library,僅用於引腳0到31。如果它不工作,看看這個庫可用 - 有加倍的價值讀數。