Orange pi GPIO output control, bare metal lighting Dafa (2)!

Orange pi GPIO output control method

foreword

The previous blog talked about bare metal lighting, this chapter will share a more comprehensive GPIO control of orangepi

This chapter mainly includes three methods, namely:
1. OPi.GPIO (the Raspberry Pi is RPi.GPIO), which is suitable for programming in the python environment
2. WiringOP-Zero-Python (the Raspberry Pi is Wiringpi), which is suitable for programming in the python environment
3. WiringOP-Zero (the Raspberry Pi is Wiringpi), which is suitable for programming in the C environment

In actual use, you can choose one of them to install according to your needs, or you can install all of them.

1. OPi.GPIO installation and use

Official Documentation Air Ticket: OPi.GPIO

Don't talk nonsense and start directly:
my armbian system is pre-installed with python3, so just use python3 directly

#明显看出来是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

In this way, the installation is complete, enter python, and the import module does not report an error, indicating that the installation is successful

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

I wrote a python script to verify, as follows: (you need to connect led or output device to the corresponding io pin to see the change of io pin)

#!/usr/bin/python  
# -*- coding:utf-8 -*-

import OPi.GPIO as GPIO
import time

IO_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_list

def 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. Pin number

In OPi.GPIO there are two ways to number the IO pins on the Orange Pi. The first is to use the BOARD numbering system. This refers to the pin number on the P1 header on the Orange Pi board. The advantage of using this numbering system is that your hardware will work regardless of the Raspberry Pi's board revision. You don't need to rewire your connectors or change your code.

The second numbering system is the BCM number. This is a lower level way of working - it refers to the channel number on the Broadcom SOC. You must always use the diagram for which pin on the Raspberry Pi board that channel number corresponds to. Your script may not work after the Orange Pi hardware revision.

Orange Pi pins have two numbering methods, BOARD and BCM (using the WiringOP library of C, there is also a wringPi numbering method), BOARD has good applicability (you don’t need to look at the interface diagram, you can wire by counting pins 1~40), no matter Orange Pi 1 2 3, you don’t need to modify the code, ho! It’s impossible to count the pins when changing the BCM numbering method to another version, you need to see the interface diagram below

The following is a hardware interface diagram of Orange Pi Zero, which is common to Orange Pi Zero and Orange Pi 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--+---+------+---------+-----+--+

insert image description here
The 40Pin OrangePi PC2 form is compatible with the 26Pin Orange Pi zero, and the Orange Pi zero is 26Pin, and its pins correspond to the first 26Pins in the above table, but the BCM codes are different. It can be seen that it is relatively common to
use 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. Installation and use of WiringOP-Zero-Python

github plane ticket: WiringOP-Zero-Python

Let’s start directly without talking too much nonsense:

git clone --recursive https://github.com/xpertsavenue/WiringOP-Zero-Python
cd WiringOP-Zero-Python

Resolve dependencies on python-dev, python-setuptools and 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

How to eat:

import wiringpi

wiringpi.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

It's quite simple, let's use a simple python script to control io, the code is as follows

vim wiring_python_io_set.py
#!/usr/bin/python  
# -*- coding:utf-8 -*-
import wiringpi

def 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

Run it to control the onboard 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

You can see that the red LED on the board is on, and it is running

root@orangepi:~# python3 wiring_python_io_set.py 
Used wPi mode,please input pinnum(0-16):30
please input pin value(1/0):0

You can see that the red LED on the board is off. works perfectly

4. Installation and use of WiringOP-Zero (C language)

github plane ticket: WiringOP-Zero

Let’s start directly without talking too much nonsense:

git clone https://github.com/xpertsavenue/WiringOP-Zero.git
cd WiringOP-Zero
chmod +x ./build
sudo ./build

All Done.The installation is complete when it appears
, check whether the installation is successful

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 -warranty

Board 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--+---+------+---------+-----+--+

Last ancestral rule, write a script townhouse:

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);
		}
	}
}

compile and run

#编译
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 readallThe status of the IO can be monitored by

Guess you like

Origin blog.csdn.net/Running_free/article/details/103063405