首先我们安装wiringPi 1 2 3 4 5 6
| sudo apt-get install git-core git clone git://git.drogon.net/wiringPi cd wiringPi ./build
|
这样我们就安装成功了,如果失败请更新系统: 1 2 3 4
| sudo apt-get update sudo apt-get upgrade
|
然后我们可以开始使用C语言写一个简单的闪烁程序了:
以下源码来自wiringPi的examples/blink.c: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <stdio.h> #include <wiringPi.h>
#define LED 0
int main (void) { printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ; pinMode (LED, OUTPUT) ;
for (;;) { digitalWrite (LED, HIGH) ; delay (500) ; digitalWrite (LED, LOW) ; delay (500) ; } return 0 ; }
|
按Ctrl+X,然后输入Y,回车保存. 然后,我们利用gcc来编译刚才的源码,运行:
1 2 3 4
| gcc blink.c -o blink -lwiringPi sudo ./blink
|
这里记得,控制IO的时候需要sudo,不然会提示:
1 2 3
| wiringPiSetup: Must be root. (Did you forget sudo?)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 0 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 0 | IN | CE1 | 11 | 7 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | 28 | 17 | GPIO.17 | ALT2 | 0 | 51 || 52 | 0 | ALT2 | GPIO.18 | 18 | 29 | | 30 | 19 | GPIO.19 | ALT2 | 0 | 53 || 54 | 0 | ALT2 | GPIO.20 | 20 | 31 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
|