GPIO庫的核心功能,當然就是操作GPIO了,GPIO就是“通用輸入/輸出”介面,比如點亮一個LED、繼電器等,或者通過iic spi 1-wire等協議,讀取、寫入數據,這都是GPIO的用處,可以說沒有GPIO,樹莓派只能當小電腦用,有了GPIO,就升級成一個控制器了。先來說說怎麼操作一個數字量(...
GPIO庫的核心功能,當然就是操作GPIO了,GPIO就是“通用輸入/輸出”介面,比如點亮一個LED、繼電器等,或者通過iic spi 1-wire等協議,讀取、寫入數據,這都是GPIO的用處,可以說沒有GPIO,樹莓派只能當小電腦用,有了GPIO,就升級成一個控制器了。先來說說怎麼操作一個數字量(高低電平)。
先看代碼:
import wiringpi2 as gpio from wiringpi2 import GPIO gpio.wiringPiSetup() #初始化 gpio.pinMode(25,GPIO.OUTPUT) # 把pin25設置為輸出模式 gpio.digitalWrite(25,GPIO.HIGH) #pin25輸出為高電平 print(gpio.digitalRead(25)) #列印pin25的狀態
值的註意的是,GPIO在輸出模式時,也可以讀取GPIO狀態。
wiringpi對樹莓派2的GPIO定義如下:
[root@RasPi ~/testcode]# gpio readall +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | IN | 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 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
我們看到,wringpi對針腳有三種定義方式,BCM代表GPIO模式,wPi代表pin模式,Physical代表物理針腳模式。
比如我們上邊操作的Pin25,按照物理針腳就是37,GPIO模式則為26
下邊代碼訪問的同一個針腳:
view plaincopy to clipboardprint? # GPIO port numbers import wiringpi2 as wiringpi wiringpi.wiringPiSetupGpio() wiringpi.pinMode(26, 0) # sets GPIO 26 to input # wiringpi numbers import wiringpi2 as wiringpi wiringpi.wiringPiSetup() wiringpi.pinMode(25, 0) # sets WP pin 25 to input # Physical P1 header pin numbers import wiringpi2 as wiringpi wiringPiSetupPhys() wiringpi.pinMode(37, 0) # sets P1 pin 37 to input
更換初始化函數,可以在各種模式下切換,我們用的wiringPiSetup() 函數,所以是pin模式,pin25在插針的左下角,跟0v相鄰,接線如下:
另外,還有另一種操作GPIO的方法:
from wiringpi2 import GPIO gpio=GPIO() #新建GPIO對象,預設為pin模式,可以加參數切換為別的模式 gpio.pinMode(25,GPIO.OUTPUT) #把GPIO25設為OUTPUT gpio.digitalWrite(25,1) #把GPIO25設為高電平 print(gpio.digitalRead(25))
這種方法是用類封裝的wiringpi介面,速度比直接調用函數慢一些,如果程式對時序要求較高,則不推薦用這種方式。