测试程序:
代码:
/*==================================================
*功能:按键按下,对应LED点亮同时屏幕打印对应颜色LED亮
*思路: 根据51单片机的控制思路
*方法:通过树莓派GPIO库wiringPi实现
*过程:在win7下编写代码,再传入树莓派。
*编译:用 cd ~/代码所在文件夹 打开
* 然后 gcc -Wall -o ** **.c -lwiringPi 命令编译
* ** 代表你的 .C 文件名称, 用 sudo ./** 命令运行
*以下接线GPIO引脚序号是wiringP库对应引脚序号*
*以下IO分布可通过树莓派终端输入 gpio readall 命令得到*
+-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+
| 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 | OUT | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | OUT | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | OUT | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | ALT0 | 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 | 1 | OUT | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 1 | OUT | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 1 | OUT | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+
------------------------接线-------------------------
+LED共阳接法
+红LED接 GPIO 29
+黄LED接 GPIO 28
+绿LED接 GPIO 27
+
+按键共阳接法
+按键A接 GPIO 1
+按键B接 GPIO 4
+按键C接 GPIO 5
+GPIO 输入引脚 通过 10K电阻接地
——#include<刘云祥>
————————
===================================================*/
// 头文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
//主函数
int main()
{
wiringPiSetup() ; //初始化GPIO
pinMode (1, INPUT) ; //设置GPIO 1 引脚 为输入模式
pinMode (4, INPUT) ;
pinMode (5, INPUT) ;
pinMode (29, OUTPUT) ; //设置GPIO 29 引脚 为输出模式
pinMode (28, OUTPUT) ;
pinMode (27, OUTPUT) ;
while(1)
{
//以下为按键处理以及LED点亮;
if(digitalRead (1) == 1){delay (100) ;//延时去抖动
if(digitalRead (1) == 1) { digitalWrite(27, LOW); printf("绿色LED亮
");//如果GPIO 1 引脚为高电平,则 GPIO 27 引脚 点亮,同时打印;
while(digitalRead (1) == 1) ; }}//等待按键放开;
else digitalWrite(27, HIGH);// 如果 GPIO 1 不为高 那么 熄灭LED;
if(digitalRead (4) == 1){delay (100) ;
if(digitalRead (4) == 1) { digitalWrite(28, LOW); printf("黄色LED亮
");
while(digitalRead (4) == 1) ; } }
else digitalWrite(28, HIGH);
if(digitalRead (5) == 1){delay (100) ;
if(digitalRead (5) == 1) { digitalWrite(29, LOW); printf("红色LED亮
");
while(digitalRead (5) == 1) ; } }
else digitalWrite(29, HIGH);
}
return 0;
}
测试运行一下: