文章目录
-
- Orange pi GPIO输出控制方式
-
- 前言
- 1、OPi.GPIO安装及使用
- 2、引脚编号
- 3、WiringOP-Zero-Python安装及使用
- 4、WiringOP-Zero 安装及使用(C语言)
Orange pi GPIO输出控制方式
前言
上一篇博客讲了裸机点灯,这一章将会分享一个更全面的orangepi的GPIO控制
本章主要包含三种方式,分别是:
1、OPi.GPIO(树莓派是RPi.GPIO),适用于python环境编程
2、WiringOP-Zero-Python(树莓派是Wiringpi),适用于python环境编程
3、WiringOP-Zero(树莓派是Wiringpi),适用于C环境编程
实际使用根据需要任选其一安装即可,也可全部安装
1、OPi.GPIO安装及使用
官方文档飞机票:OPi.GPIO
废话不多说直接开始:
我的armbian系统预装python3,那就直接用python3好了
#明显看出来是python3.6
root@orangepi:~# whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /usr/local/lib/python3.6
#先安装python3.6对应的pip3
root@orangepi:~# sudo apt-get install python3-pip
#根据官方文档安装:
root@orangepi:~# sudo pip3 install --upgrade OPi.GPIO
这样就安装好了,进入python,导入模块没有报错就说明安装成功
root@orangepi:~# python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import OPi.GPIO as GPIO
写了一个python脚本来验证,如下:(需要在相应的io引脚接上led或者输出设备才能看到io引脚的变化)
#!/usr/bin/python
# -*- coding:utf-8 -*-import OPi.GPIO as GPIO
import timeIO_set_list = []
Unavailable_IO_num = [1,2,4,6,9,14,17,20,25]def Io_init():GPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD) #板载编码#GPIO.setmode(GPIO.BCM) #BCM编码#GPIO.setmode(GPIO.SUNXI) #WPI编码def Get_user_input():pinnum = int(input("Used BOARD mode,please input pinnum(0-26):"))while pinnum < 0 or pinnum > 26 or pinnum in Unavailable_IO_num:print('Incorrect input or Unavailable IO,try again')pinnum = int(input("Used BOARD mode,please input pinnum(0-26):"))IO_set_list.append(pinnum)pinval = int(input("please input pin value(1/0):"))while pinval not in [0,1]:print('Incorrect input,try again')pinval = int(input("please input pin value(1/0):"))IO_set_list.append(pinval)return IO_set_listdef IO_out():print("welcom")Io_init()Get_user_input()GPIO.setup(IO_set_list[0], GPIO.OUT)GPIO.output(IO_set_list[0],IO_set_list[1])def main():#os.system(`gpio-readall`)IO_out()GPIO.cleanup()if __name__ == '__main__':try:main()#except KeyboardInterrupt:except:GPIO.cleanup()finally:GPIO.cleanup()
2、引脚编号
在OPi.GPIO中,有两种方法可以对Orange Pi上的IO引脚进行编号。第一种是使用BOARD编号系统。这是指Orange Pi板上P1接头上的引脚号。使用这种编号系统的优点是,无论树莓派的电路板版本如何,您的硬件都能正常工作。你不需要重新连接你的连接器或更改你的代码。
第二个编号系统是BCM号码。这是一种较低级别的工作方式 - 它指的是Broadcom SOC上的通道号码。您必须始终使用那个通道编号所对应的树莓派板上哪个引脚的图表。您的脚本程序可能会在Orange Pi板的硬件修订后而不能使用。
香橙派引脚有BOARD和BCM两种编号方式( 使用C的WiringOP库还有一种wringPi编号方式 ), BOARD具有很好的适用性( 不用看接口图,数引脚1~40就可以接线 ), 不论香橙派1 2 3, 都不用修改代码, 吼啊! BCM编号方式换个版本再接线时数引脚是不行的, 需要看下下面的接口图
下面给出一张香橙派 zero的硬件接口图,香橙派zero和香橙派R1通用
+-----+-----+----------+------+--Orange Pi Zero--+---+------+---------+-----+--+| H2+ | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | H2+ |+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+| | | 3.3v | | | 1 || 2 | | | 5v | | || 12 | 8 | SDA.0 | ALT5 | 0 | 3 || 4 | | | 5V | | || 11 | 9 | SCL.0 | ALT5 | 0 | 5 || 6 | | | 0v | | || 6 | 7 | GPIO.7 | ALT3 | 0 | 7 || 8 | 0 | ALT5 | TxD3 | 15 | 198 || | | 0v | | | 9 || 10 | 0 | ALT5 | RxD3 | 16 | 199 || 1 | 0 | RxD2 | ALT5 | 0 | 11 || 12 | 0 | ALT3 | GPIO.1 | 1 | 7 || 0 | 2 | TxD2 | ALT5 | 0 | 13 || 14 | | | 0v | | || 3 | 3 | CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT4 | GPIO.4 | 4 | 19 || | | 3.3v | | | 17 || 18 | 0 | ALT4 | GPIO.5 | 5 | 18 || 15 | 12 | MOSI | ALT5 | 0 | 19 || 20 | | | 0v | | || 16 | 13 | MISO | ALT5 | 0 | 21 || 22 | 0 | ALT3 | RTS2 | 6 | 2 || 14 | 14 | SCLK | ALT5 | 0 | 23 || 24 | 0 | ALT5 | CE0 | 10 | 13 || | | 0v | | | 25 || 26 | 0 | ALT3 | GPIO.11 | 11 | 10 |+-----+-----+----------+------+---+---LEDs---+---+------+----------+-----+-----+| 17 | 30 | STAT-LED | OUT | 0 | 27 || 28 | | | PWR-LED | | |+-----+-----+----------+------+---+-----+----+---+------+----------+-----+-----+| H2+ | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | H2+ |+-----+-----+----------+------+--Orange Pi Zero--+---+------+---------+-----+--+
40Pin的OrangePi PC2表格兼容26Pin的香橙派zero,香橙派zero为26Pin,其引脚对应于上表的前26Pin,但是BCM编码不同
由此可见用BOARD还是比较通用的
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+| | | 3.3v | | | 1 || 2 | | | 5v | | || 12 | 8 | SDA.0 | ALT5 | 0 | 3 || 4 | | | 5V | | || 11 | 9 | SCL.0 | ALT5 | 0 | 5 || 6 | | | 0v | | || 6 | 7 | GPIO.7 | ALT3 | 0 | 7 || 8 | 0 | ALT5 | TxD3 | 15 | 13 || | | 0v | | | 9 || 10 | 0 | ALT5 | RxD3 | 16 | 14 || 1 | 0 | RxD2 | ALT5 | 0 | 11 || 12 | 0 | ALT3 | GPIO.1 | 1 | 110 || 0 | 2 | TxD2 | ALT5 | 0 | 13 || 14 | | | 0v | | || 3 | 3 | CTS2 | ALT5 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4 | 4 | 68 || | | 3.3v | | | 17 || 18 | 0 | ALT3 | GPIO.5 | 5 | 71 || 64 | 12 | MOSI | ALT4 | 0 | 19 || 20 | | | 0v | | || 65 | 13 | MISO | ALT0 | 0 | 21 || 22 | 0 | ALT5 | RTS2 | 6 | 2 || 66 | 14 | SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0 | 10 | 67 || | | 0v | | | 25 || 26 | 0 | ALT3 | GPIO.11 | 11 | 21 || 19 | 30 | SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1 | 31 | 18 || 7 | 21 | GPIO.21 | ALT3 | 0 | 29 || 30 | | | 0v | | || 8 | 22 | GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT5 | RTS1 | 26 | 200 || 9 | 23 | GPIO.23 | ALT3 | 0 | 33 || 34 | | | 0v | | || 10 | 24 | GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT5 | CTS1 | 27 | 201 || 20 | 25 | GPIO.25 | OUT | 1 | 37 || 38 | 0 | ALT5 | TxD1 | 28 | 198 || | | 0v | | | 39 || 40 | 0 | ALT5 | RxD1 | 29 | 199 |+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+
3、WiringOP-Zero-Python安装及使用
github飞机票:WiringOP-Zero-Python
一样废话不多说直接开始:
git clone --recursive https://github.com/xpertsavenue/WiringOP-Zero-Python
cd WiringOP-Zero-Python
解决依赖 python-dev, python-setuptools 和 swig
sudo apt-get install python3-dev python3-setuptools swig
swig3.0 -python wiringpi.i
sudo python3 setup.py install
sudo python3 tests/test.py
食用方法:
import wiringpiwiringpi.wiringPiSetup() # For sequential pin numbering, one of these MUST be called before using IO functions
# OR
wiringpi.wiringPiSetupSys() # For /sys/class/gpio with GPIO pin numbering
# OR
wiringpi.wiringPiSetupGpio() # For GPIO pin numbering
wiringpi.pinMode(6,1) # Set pin 6 to 1 ( OUTPUT )
wiringpi.digitalWrite(6,1) # Write 1 ( HIGH ) to pin 6
wiringpi.digitalRead(6) # Read pin 6
挺简单吧,还是来一个简单的python脚本控制io,代码如下
vim wiring_python_io_set.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import wiringpidef main():#os.system(`gpio-readall`)wiringpi.wiringPiSetup() #设置GPIO编号为wPi方式,当使用这个函数初始化树莓派引脚时,程序使用的是wiringPi 引脚编号表。引脚的编号为 0~16#wiringpi.wiringPiSetupSys() #设置GPIO编号为#wiringpi.wiringPiSetupPhys() #设置GPIO编号为#wiringpi.wiringPiSetupGpio() #设置GPIO编号为BCM方式 ,当使用这个函数初始化树莓派引脚时,程序中使用的是BCM GPIO 引脚编号表。pinnum = int(input("Used wPi mode,please input pinnum:"))pinval = int(input("please input pin value(1/0):"))wiringpi.pinMode(pinnum,1)wiringpi.digitalWrite(pinnum,pinval)if __name__ == '__main__':try:main()#except KeyboardInterrupt:except:print("set failed ,please chek you input and try again!")finally:pass
运行一下控制板载 status led
root@orangepi:~# python3 wiring_python_io_set.py
Used wPi mode,please input pinnum(0-16):30
please input pin value(1/0):1
可以看到板上的红色led亮起来了,在运行
root@orangepi:~# python3 wiring_python_io_set.py
Used wPi mode,please input pinnum(0-16):30
please input pin value(1/0):0
可以看到板上的红色led灭了。完美运行
4、WiringOP-Zero 安装及使用(C语言)
github飞机票:WiringOP-Zero
一样废话不多说直接开始:
git clone https://github.com/xpertsavenue/WiringOP-Zero.git
cd WiringOP-Zero
chmod +x ./build
sudo ./build
出现All Done.
就安装完成了
检测是否安装成功
root@orangepi:~/WiringOP-Zero# gpio -v
gpio version: 2.20
Copyright (c) 2012-2014 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warrantyBoard details:Type: Orange Pi Zero, Revision: Unknown, Memory: 512MB, Maker: Unknown
root@orangepi:~/WiringOP-Zero# gpio readall
+-----+-----+----------+------+--Orange Pi Zero--+------+----------+-----+-----+
| H2+ | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | H2+ |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 12 | 8 | SDA.0 | OUT | 0 | 3 || 4 | | | 5V | | |
| 11 | 9 | SCL.0 | OUT | 0 | 5 || 6 | | | 0v | | |
| 6 | 7 | GPIO.7 | ALT3 | 0 | 7 || 8 | 0 | ALT3 | TxD3 | 15 | 198 |
| | | 0v | | | 9 || 10 | 0 | ALT3 | RxD3 | 16 | 199 |
| 1 | 0 | RxD2 | ALT3 | 0 | 11 || 12 | 0 | ALT3 | GPIO.1 | 1 | 7 |
| 0 | 2 | TxD2 | ALT3 | 0 | 13 || 14 | | | 0v | | |
| 3 | 3 | CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4 | 4 | 19 |
| | | 3.3v | | | 17 || 18 | 0 | ALT3 | GPIO.5 | 5 | 18 |
| 15 | 12 | MOSI | ALT3 | 0 | 19 || 20 | | | 0v | | |
| 16 | 13 | MISO | ALT3 | 0 | 21 || 22 | 0 | ALT3 | RTS2 | 6 | 2 |
| 14 | 14 | SCLK | ALT3 | 0 | 23 || 24 | 0 | ALT3 | CE0 | 10 | 13 |
| | | 0v | | | 25 || 26 | 0 | ALT3 | GPIO.11 | 11 | 10 |
+-----+-----+----------+------+---+---LEDs---+---+------+----------+-----+-----+
| 17 | 30 | STAT-LED | OUT | 0 | 27 || 28 | | | PWR-LED | | |
+-----+-----+----------+------+---+-----+----+---+------+----------+-----+-----+
| H2+ | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | H2+ |
+-----+-----+----------+------+--Orange Pi Zero--+---+------+---------+-----+--+
最后祖传的规矩,写一个脚本镇楼:
vim wiring_c_io_set.c
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
/*
运行:sudo ./wiring_c_io_set [引脚 值]
编译:gcc wiring_c_io_set.c -o wiring_c_io_set -L lib -l wiringPi -l pthread (需已安装wiringPi)
by:Fang
*/#define Set_io_pin 30 //定义需要控制哪个引脚int pin,value,count=5;int main(int args, char *argv[])
{
if((argv[1])&&(argv[2])){
wiringPiSetup();pin=atoi(argv[1]);value=atoi(argv[2]);pinMode(pin,OUTPUT);digitalWrite(pin,value);}else{
int i=0;wiringPiSetup(); //选择WPI编码方式pinMode(Set_io_pin, OUTPUT); //设置为输出模式for (i=0;i<count;i++){
digitalWrite(Set_io_pin,1); //控制引脚高电平delay(500);digitalWrite(Set_io_pin,0); //控制引脚低电平delay(500);}}
}
编译并运行
#编译
gcc wiring_c_io_set.c -o wiring_c_io_set -L lib -l wiringPi -l pthread
#不传参数运行,可以看到板载status led 闪烁5下
./wiring_c_io_set
#传参数运行,可以看到板载status led 亮起
./wiring_c_io_set 30 1
#传参数运行,可以看到板载status led 熄灭
./wiring_c_io_set 30 0
通过 watch -n 1 gpio readall
可以监控IO的状态