上次用Python做GPIO控制,個人認為用於簡單電路測試或控制足夠。但是,未來有一些計劃會用到interrupt,用C/C++開發應該會是主要做法。
WiringBP是由LeMaker改寫Drogon開發的Wiring Pi:
root@bpro:/home/regis# git clone https://github.com/LeMaker/WiringBP -b bananapro
root@bpro:/home/regis# chmod +x ./build
root@bpro:/home/regis# sudo ./build
安裝好之後,查看GPIO的status:
root@bpro:/home/regis/WiringBP/examples# gpio readall
root@bpro:/home/regis/WiringBP/examples# gpio readall
照慣例,找個簡單的program來試試,用上次Python的LED電路來做實驗:
root@bpro:/home/regis/WiringBP/examples# ls
blink.sh isr.c PiGlow softPwm.c
blink12.c BProTest isr-osc.c pwm.c softTone.c
blink12drcs.c clock.c lcd-adafruit.c q2w speed.c
blink6drcs.c COPYING.LESSER lcd.c README.TXT wfi.c
blink8.c delayTest.c Makefile rht03.c
blink.c ds1302.c nes.c serialRead.c
blink.o Gertboard okLed.c serialTest.c
blink.rtb header.h PiFace servo.c
root@bpro:/home/regis/WiringBP/examples# nano blink.c
#include <stdio.h> #include <wiringPi.h>
// LED Pin - wiringPi pin 0 is BCM_GPIO 17.
#define LED 7
int main (void)
{
printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ;
{
printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ;
pinMode (LED, OUTPUT) ;
for (;;) {
for (;;) {
digitalWrite (LED, HIGH) ; // On
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ;
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ;
}
return 0 ;
}
return 0 ;
}
要注意pinMode(pin, mode)的pin設定,現在LED電路是接在板上pin7,查GPIO表,是相對“wPi”的號碼7,所以LED現在define為7。
如果想接在板上pin11,那LED要define為0,以此類推。
改好後,重新compile:
root@bpro:/home/regis/WiringBP/examples# make blink
[CC] blink.c
[link]
執行blink:
root@bpro:/home/regis/WiringBP/examples# ./blink
Raspberry Pi blink
如果看到LED交互閃爍,表示成功!
沒有留言:
張貼留言