介绍wiringPi,引脚的具体使用。
http://wiringpi.com/examples/gertboard-and-wiringpi/blink/
引脚图,二极管阴极接6号引脚(接地)
二极管阳极接11号引脚(wiringPi库,0,BCM编码17)
编码方式一、
#include <stdio.h> #include <wiringPi.h> // LED Pin - wiringPi pin 0 is BCM_GPIO 17. #define LED 0 int main (void) { printf ("Raspberry Pi - Gertboard Blink\n") ; wiringPiSetup () ; pinMode (LED, OUTPUT) ; for (;;) { digitalWrite (LED, 1) ; // On delay (500) ; // mS digitalWrite (LED, 0) ; // Off delay (500) ; } return 0 ; }
编码方式二、
#include <wiringPi.h> int main (void) { wiringPiSetup () ; pinMode (0, OUTPUT) ; for (;;) { digitalWrite (0, HIGH) ; delay (500) ; digitalWrite (0, LOW) ; delay (500) ; } return 0 ; }
The wiringPi functions we are using are:
- wiringPiSetup()
This must be called before anything else – it opens the GPIO devices and allows our program to access it.
- pinMode()
This set the mode of the pin – usually in or out, but there are some other functions too.
- digitalWrite()
Outputs a value (0 or 1) to the given pin.
- delay()
This delays for a number of milliseconds.
So there should be nothing out of the ordinary here – and some will be very familiar if you have used an Arduino in the past.
When your program is running, you can press Control-C to stop it and return to command mode.
gcc -Wall -o blink blink.c -lwiringPi sudo ./blink
-Wall
This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C++ Dialect Options and Objective-C and Objective-C++ Dialect Options.
-Wall turns on the following warning flags:
-Waddress
-Warray-bounds (only with -O2)
-Wc++11-compat
-Wchar-subscripts
-Wenum-compare (in C/Objc; this is on by default in C++)
-Wimplicit-int (C and Objective-C only)
-Wimplicit-function-declaration (C and Objective-C only)
-Wcomment
-Wformat
-Wmain (only for C/ObjC and unless -ffreestanding)
-Wmaybe-uninitialized
-Wmissing-braces
-Wnonnull
-Wparentheses
-Wpointer-sign
-Wreorder
-Wreturn-type
-Wsequence-point
-Wsign-compare (only in C++)
-Wstrict-aliasing
-Wstrict-overflow=1
-Wswitch
-Wtrigraphs
-Wuninitialized
-Wunknown-pragmas
-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wvolatile-register-var
You need to link with the wiringPi library, hence the -lwiringPi and you also need to be root to run the program, as only root can directly access the GPIO.
-l是指动态库链接 -lwiringPi是指在编译时,寻找wiringPi.so动态库文件。加上static链接 wieingPi.a静态库文件。
Linux下的库文件分为两大类分别是动态链接库(通常以.so结尾)和静态链接库(通常以.a结尾),二者的区别仅在于程序执行时所需的代码是在运行时动态加载的,还是在编译时静态加载的。
默认情况下, GCC在链接时优先使用动态链接库,只有当动态链接库不存在时才考虑使用静态链接库,如果需要的话可以在编译时加上-static选项,强制使用静态链接库。
在/usr/dev/mysql/lib目录下有链接时所需要的库文件libmysqlclient.so和libmysqlclient.a,为了让GCC在链接时只用到静态链接库,可以使用下面的命令:
gcc –L /usr/dev/mysql/lib –static –lmysqlclient test.o –o test
静态库链接时搜索路径顺序:
1. ld会去找GCC命令中的参数-L
2. 再找gcc的环境变量LIBRARY_PATH
3. 再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的
动态链接时、执行时搜索路径顺序:
1. 编译目标代码时指定的动态库搜索路径
2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径
3. 配置文件/etc/ld.so.conf中指定的动态库搜索路径
4. 默认的动态库搜索路径/lib
5. 默认的动态库搜索路径/usr/lib
有关环境变量:
LIBRARY_PATH环境变量:指定程序静态链接库文件搜索路径
LD_LIBRARY_PATH环境变量:指定程序动态链接库文件搜索路径
关于GCC更多解释和应用,可以参考 http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html
附录:gcc -I -L -l 区别
我们用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数,下面做个记录:
例:
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
上面这句表示在编译hello.c时:
-I /home/hello/include表示将/home/hello/include目录作为第一个寻找头文件的目录,寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include
-L /home/hello/lib表示将/home/hello/lib目录作为第一个寻找库文件的目录,寻找的顺序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib
-lworld表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了“-static”表示寻找libworld.a静态库文件)