香橙派是国产SBC ,对标树莓派。不过国内的开发环境确实挺惨的,没多少帖子讨论。楼主决定从今天起,不定期更新香橙派的教程。
今天的教程是如何使用香橙派下载wiringOP 并使用CPP操作GPIO
操作GPIO
下载wiringPi
- 检查git 版本
- 克隆wiringPi 仓库
- 最后检查wiringOP 是否在仓库中
orangepi@orangepi3b:~$ git --version
git version 2.34.1
orangepi@orangepi3b:~$ git clone https://github.com/orangepi-xunlong/wiringOP
正克隆到 'wiringOP'...
remote: Enumerating objects: 1099, done.
remote: Counting objects: 100% (721/721), done.
remote: Compressing objects: 100% (114/114), done.
remote: Total 1099 (delta 666), reused 622 (delta 607), pack-reused 378 (from 1)
接收对象中: 100% (1099/1099), 459.35 KiB | 1007.00 KiB/s, 完成.
处理 delta 中: 100% (800/800), 完成.
orangepi@orangepi3b:~$ tree -L 1
.
├── bop_code
├── Desktop
├── Documents
├── Downloads
├── Music
├── opencvc
├── Pictures
├── Public
├── Templates
├── Videos
└── wiringOP
11 directories, 0 files
- 切换到仓库
- 查看目录
- 编译
./build clean
/build
orangepi@orangepi3b:~$ cd wiringOP/
orangepi@orangepi3b:~/wiringOP$ tree -L 1
.
├── build
├── COPYING.LESSER
├── debian
├── debian-template
├── devLib
├── examples
├── gpio
├── INSTALL
├── newVersion
├── People
├── pins
├── README.md
├── sh.sh
├── update
├── VERSION
├── version.h
├── wiringPi
└── wiringPiD
8 directories, 10 files
orangepi@orangepi3b:~/wiringOP$ ./build clean
wiringPi: [Clean]
DevLib: [Clean]
gpio: [Clean]
Examples: [Clean]
Gertboard: [Clean]
PiFace: [Clean]
Quick2Wire: [Clean]
PiGlow: [Clean]
scrollPhat: [Clean]
Deb:
orangepi@orangepi3b:~/wiringOP$ ./build
wiringPi Build script
=====================
WiringPi Library
[sudo] orangepi 的密码:
[UnInstall]
[Compile] wiringPi.c
[Compile] wiringSerial.c
[Compile] wiringShift.c
[Compile] piHiPri.c
[Compile] piThread.c
[Compile] wiringPiSPI.c
[Compile] wiringPiI2C.c
[Compile] softPwm.c
.....
[Link]
[Install]
All Done.
NOTE: To compile programs with wiringPi, you need to add:
-lwiringPi
to your compile line(s) To use the Gertboard, MaxDetect, etc.
code (the devLib), you need to also add:
-lwiringPiDev
to your compile line(s).
结果解读
NOTE: To compile programs with wiringPi, you need to add:
-lwiringPi
to your compile line(s) To use the Gertboard, MaxDetect, etc.
code (the devLib), you need to also add:
-lwiringPiDev
to your compile line(s).
翻译:
注意: 使用wiringPi编译程序,你需要在编译命令中添加
-lwiringPi
若要使用 Gertboard, MaxDetect 这些库,还需要在代码的编译命令中添加
-lwiringPiDev
CPP 操作GPIO
#include <wiringPi.h>
#include <stdio.h>
#define LED_PIN 0
int main(){
if (wiringPiSetup()==-1)
{
printf("wiring setup error\n");
return 1;
}
pinMode(LED_PIN ,OUTPUT);
while(1)
{
digitalWrite(LED_PIN ,HIGH);
delay(500);
digitalWrite(LED_PIN ,LOW);
delay(500);
printf("循环一次\n");
}
return 0;
}
orangepi@orangepi3b:~/bop_code$ gcc test_gpio.c -o test_gpio -lwiringPi
orangepi@orangepi3b:~/bop_code$ chmod +x test_gpio
orangepi@orangepi3b:~/bop_code$ sudo ./test_gpio
[sudo] orangepi 的密码:
循环一次
循环一次
如何链接线路?
使用 gpio readall
就可以查看GPIO 的编码
表格的方向是USB 贴近身体观察GPIO的顺序,通过颜色也可以观察
红色是5V 黑色是地线 橙色是3.3V 蓝色是可控的GPIO
我们这里 #define LED_PIN 0
实际上使用的是GPIO 140
也就是说 <wiringPi.h>
使用的GPIO的编码是wPi编码,这样我们可以方便的把代码中的编号和物理世界中的连接对应
orangepi@orangepi3b:~$ gpio readall
+------+-----+----------+--------+---+ PI3B +---+--------+----------+-----+------+
| GPIO | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | GPIO |
+------+-----+----------+--------+---+----++----+---+--------+----------+-----+------+
| | | 3.3V | | | 1 || 2 | | | 5V | | |
| 140 | 0 | SDA.2 | IN | 1 | 3 || 4 | | | 5V | | |
| 141 | 1 | SCL.2 | IN | 1 | 5 || 6 | | | GND | | |
| 147 | 2 | PWM15 | IN | 0 | 7 || 8 | 1 | ALT1 | RXD.2 | 3 | 25 |
| | | GND | | | 9 || 10 | 1 | ALT1 | TXD.2 | 4 | 24 |
| 118 | 5 | GPIO3_C6 | IN | 0 | 11 || 12 | 0 | IN | GPIO3_C7 | 6 | 119 |
| 128 | 7 | GPIO4_A0 | IN | 0 | 13 || 14 | | | GND | | |
| 130 | 8 | TXD.7 | IN | 0 | 15 || 16 | 0 | IN | RXD.7 | 9 | 131 |
| | | 3.3V | | | 17 || 18 | 0 | IN | GPIO4_A1 | 10 | 129 |
| 138 | 11 | SPI3_TXD | IN | 0 | 19 || 20 | | | GND | | |
| 136 | 12 | SPI3_RXD | IN | 0 | 21 || 22 | 0 | IN | TXD.9 | 13 | 132 |
| 139 | 14 | SPI3_CLK | IN | 0 | 23 || 24 | 0 | IN | SPI3_CS1 | 15 | 134 |
| | | GND | | | 25 || 26 | 0 | IN | GPIO3_D6 | 16 | 126 |
| 32 | 17 | SDA.3 | IN | 1 | 27 || 28 | 1 | IN | SCL.3 | 18 | 33 |
| 133 | 19 | RXD.9 | IN | 0 | 29 || 30 | | | GND | | |
| 124 | 20 | GPIO3_D4 | IN | 0 | 31 || 32 | 0 | IN | PWM11 | 21 | 144 |
| 127 | 22 | GPIO3_D7 | IN | 0 | 33 || 34 | | | GND | | |
| 120 | 23 | GPIO3_D0 | IN | 0 | 35 || 36 | 0 | IN | GPIO3_D5 | 24 | 125 |
| 123 | 25 | GPIO3_D3 | IN | 0 | 37 || 38 | 0 | IN | GPIO3_D2 | 26 | 122 |
| | | GND | | | 39 || 40 | 0 | IN | GPIO3_D1 | 27 | 121 |
+------+-----+----------+--------+---+----++----+---+--------+----------+-----+------+
| GPIO | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | GPIO |
+------+-----+----------+--------+---+ PI3B +---+--------+----------+-----+------+
线路连接如下。LED连接的事项就不多说了,记得连1Kohm电阻,否则会烧坏。楼主的是香橙派3B-4G版 外面是散热外壳