介紹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靜態庫文件)