0
本实验的目的是在TTY界面实现GPIO的控制。为此需要安装WiringNP的C工程,并且编译成功才可以。
这次采用putty虚拟终端控制台,打开putty,需要设置登录用户和密码,这个界面与登录Tear Term虚拟超级终端软件相似,此处不罗嗦了。登录成功后输入如下代码:
root@NanoPi-NEO2:~# cd WiringNP/
root@NanoPi-NEO2:~/WiringNP# chmod 755 build
root@NanoPi-NEO2:~/WiringNP# ./build
wiringPi Build script
=====================
WiringPi Library
[UnInstall]
make: Nothing to be done for 'all'.
[Install Headers]
[Install Dynamic Lib]
WiringPi Devices Library
[UnInstall]
make: Nothing to be done for 'all'.
[Install Headers]
[Install Dynamic Lib]
GPIO Utility
make: Nothing to be done for 'all'.
[Install]
All Done.
NOTE: To compile programs with wiringNP, you need to add:
-lwiringPi
to your compile line(s).
root@NanoPi-NEO2:~/WiringNP# git clone https://github.com/friendlyarm/WiringNP
Cloning into 'WiringNP'...
remote: Counting objects: 215, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 215 (delta 2), reused 6 (delta 2), pack-reused 209
Receiving objects: 100% (215/215), 192.15 KiB | 284.00 KiB/s, done.
Resolving deltas: 100% (114/114), done.
Checking connectivity... done.
root@NanoPi-NEO2:~/WiringNP# dit chechout nanopi-mi
-bash: dit: command not found
root@NanoPi-NEO2:~/WiringNP# git chechout nanopi-mi
git: 'chechout' is not a git command. See 'git --help'.
Did you mean this?
checkout
root@NanoPi-NEO2:~/WiringNP# cd WiringNP
root@NanoPi-NEO2:~/WiringNP/WiringNP# git chechout nanopi-mi
git: 'chechout' is not a git command. See 'git --help'.
Did you mean this?
checkout
root@NanoPi-NEO2:~/WiringNP/WiringNP# chmod 755 build
root@NanoPi-NEO2:~/WiringNP/WiringNP# ./build
wiringPi Build script
=====================
WiringPi Library
[UnInstall]
[Compile] wiringPi.c
wiringPi.c: In function ‘wiringPiSetup’:
wiringPi.c:1940:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
if ((int32_t) gpio == -1)
^
wiringPi.c:1946:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
if ((int32_t) pwm == -1)
^
wiringPi.c:1952:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
if ((int32_t) clk == -1)
^
wiringPi.c:1958:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
if ((int32_t) pads == -1)
^
wiringPi.c: In function ‘digitalRead’:
wiringPi.c:1431:7: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read(sysFds [pin], &c, 1);
^
wiringPi.c: In function ‘digitalWrite’:
wiringPi.c:1502:21: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write(sysFds [pin], "0n", 2);
^
wiringPi.c:1504:21: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write(sysFds [pin], "1n", 2);
^
wiringPi.c: In function ‘waitForInterrupt’:
wiringPi.c:1727:5: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
(void) read(fd, &c, 1);
^
[Compile] wiringSerial.c
wiringSerial.c: In function ‘serialPutchar’:
wiringSerial.c:146:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write (fd, &c, 1) ;
^
wiringSerial.c: In function ‘serialPuts’:
wiringSerial.c:158:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write (fd, s, strlen (s)) ;
^
[Compile] wiringShift.c
[Compile] piHiPri.c
[Compile] piThread.c
[Compile] wiringPiSPI.c
[Compile] wiringPiI2C.c
[Compile] softPwm.c
[Compile] softTone.c
[Compile] softServo.c
[Compile] mcp23008.c
[Compile] mcp23016.c
[Compile] mcp23017.c
[Compile] mcp23s08.c
[Compile] mcp23s17.c
[Compile] sr595.c
[Compile] pcf8574.c
[Compile] pcf8591.c
pcf8591.c: In function ‘myAnalogWrite’:
pcf8591.c:44:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write (node->fd, b, 2) ;
^
[Compile] mcp3002.c
[Compile] mcp3004.c
[Compile] mcp4802.c
[Compile] mcp3422.c
mcp3422.c: In function ‘myAnalogRead’:
mcp3422.c:62:7: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read (node->fd, buffer, 4) ;
^
mcp3422.c:68:7: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read (node->fd, buffer, 3) ;
^
mcp3422.c:74:7: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read (node->fd, buffer, 3) ;
^
mcp3422.c:80:7: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read (node->fd, buffer, 3) ;
^
[Compile] max31855.c
[Compile] max5322.c
[Compile] sn3218.c
[Compile] drcSerial.c
[Link (Dynamic)]
[Install Headers]
[Install Dynamic Lib]
WiringPi Devices Library
[UnInstall]
[Compile] ds1302.c
[Compile] maxdetect.c
[Compile] piNes.c
[Compile] gertboard.c
[Compile] piFace.c
[Compile] lcd128x64.c
[Compile] lcd.c
[Compile] piGlow.c
[Link (Dynamic)]
[Install Headers]
[Install Dynamic Lib]
GPIO Utility
[Compile] gpio.c
gpio.c: In function ‘doLoad’:
gpio.c:228:6: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
system (cmd) ;
^
gpio.c:235:6: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
system (cmd) ;
^
gpio.c:274:7: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
system (cmd) ;
^
gpio.c:280:7: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
system (cmd) ;
^
[Compile] extensions.c
[Compile] readall.c
[Compile] pins.c
[Link]
[Install]
All Done.
NOTE: To compile programs with wiringNP, you need to add:
-lwiringPi
to your compile line(s).
root@NanoPi-NEO2:~/WiringNP/WiringNP# gpio readall
+-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| | | 3.3V | | | 1 || 2 | | | 5V | | |
| 12 | 8 | GPIOA12 | ALT5 | 0 | 3 || 4 | | | 5V | | |
| 11 | 9 | GPIOA11 | ALT5 | 0 | 5 || 6 | | | 0v | | |
| 203 | 7 | GPIOG11 | OFF | 0 | 7 || 8 | 0 | ALT5 | GPIOG6 | 15 | 198 |
| | | 0v | | | 9 || 10 | 0 | ALT5 | GPIOG7 | 16 | 199 |
| 0 | 0 | GPIOA0 | ALT2 | 0 | 11 || 12 | 0 | OFF | GPIOA6 | 1 | 6 |
| 2 | 2 | GPIOA2 | ALT2 | 0 | 13 || 14 | | | 0v | | |
| 3 | 3 | GPIOA3 | ALT2 | 0 | 15 || 16 | 0 | OFF | GPIOG8 | 4 | 200 |
| | | 3.3v | | | 17 || 18 | 0 | OFF | GPIOG9 | 5 | 201 |
| 64 | 12 | GPIOC0 | ALT4 | 0 | 19 || 20 | | | 0v | | |
| 65 | 13 | GPIOC1 | ALT4 | 0 | 21 || 22 | 0 | OFF | GPIOA1 | 6 | 1 |
| 66 | 14 | GPIOC2 | ALT4 | 0 | 23 || 24 | 0 | ALT4 | GPIOC3 | 10 | 67 |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
+-----+----NanoPi NEO/NEO2 Debug UART-+----+
| BCM | wPi | Name | Mode | V | Ph |
+-----+-----+----------+------+---+----+
| 4 | 17 | GPIOA4 | ALT5 | 0 | 37 |
| 5 | 18 | GPIOA5 | ALT5 | 0 | 38 |
+-----+-----+----------+------+---+----+
到此WiringNP就安装编译完成了,可以看到上表中的GPIO口号,下边可以继续GPIO的测试。注意,现在的控制台属于Linux的,因此所有命令符合Linux,这一点还算熟悉,曾经玩过Linux系统。此案例创建一个文档:led1.c,在TTY中输入命令:vi led1.c,在vi界面按i键,输入以下代码:
#include
int main(void){
wiringPiSetup();
pinMode(14,OUTPUT);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
for(;;){
digitalWrite(12,HIGH);
digitalWrite(14,LOW);
delay(500);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
digitalWrite(14,HIGH);
delay(500);
}
}
输入完成,按键盘esc键,输入 :wq,退出vi编辑器。
而后输入:gcc -Wall -o led1 led1.c -lwiringPi -lpthread,完成编译后,就可以测试了。
我们在双排插针靠RJ45座的最边上的三个插针上接入3个LED管,而后输入: .led1回车,就可以看到PC0,PC1,PC2三个引脚的LED红灯轮流闪亮以及硬件连接见照片31。
下边测试PWM和其它内容。创建文件:vi pwmTest.c,在vi界面输入i,进入编辑状态,输入如下代码:
#include
#include
#include
#include
int main (void){
int i;
int pin=18;
if(wiringPiSetup()==-1)
exit(1);
pinMode(pin,PWM_OUTPUT);
for(;;){
for(i=0;i<1024;++i){
pwmWrite(pin,i);
delay(1);
}
for(i=1023;i>=0;--i){
pwmWrite(pin,i);
delay(1);
}
}
return 0;
}
完成后按键盘esc键退出编辑,输入gcc -Wall -o pwmTest pwmTest.c -lwiringPi -lpthread,编译通过后,输入: ./pwmTest 回车,结果UART的RX引脚连接的LED渐变亮暗,照片32是PWM示波器采样波形,图33是硬件连接和LED渐亮暗。最后是测试OLED,因为下载的是支持OLED的OS,因此直接在I2C0口上连接一片I2C的OLED,启动后就看到OLED显示了,见照片34。
至此GPIO的基本测试做完了。
|
|