标签:
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接口,速度比直接调用函数慢一些,如果程序对时序要求较高,则不推荐用这种方式。
树莓派高级GPIO库,wiringpi2 for python使用笔记(三)GPIO操作
标签:
原文地址:http://www.cnblogs.com/yafengabc/p/5096720.html