made by Rk
本文由浙江大学《嵌入式系统》课程提供强力支持。
感谢翁恺老师 @翁恺BA5AG
===============================================在RPi的Linux上做一个轮盘游戏机,具有两个按钮,一个负责启动,一个负责停止。具有某个或多个输出设备,如 LED、LED矩阵、双位7段数码管、LCD、蜂鸣器、舵机等,可任选其中一个或多个的组合。按下启动按钮,输出设备上 就出现各种变化,如流水灯、递增递减的数字、舵机的旋转等,按下停止按钮,输出设备就固定在当时的状态上。程序 运行的过程,会在shell输出数据。程序必须用C/C++语言写。
成熟的库如wiringPi可以用来控制树莓派的GPIO接口。
安装过程如下:
进入系统临时文件夹/tmp
输入命令:
git clone git://git.drogon.net/wiringPi
cd wiringPi sudo ./build
输入以下命令验证wiringPi是否安装成功:
gpio -v gpio readall
wiringPi的接口映射(来自https://projects.drogon.net/raspberry-pi/wiringpi/pins/):
使用了Fritzing软件进行布线设计。
七段数码管数值设定:
根据共阴/共阳规则,需要仔细设定七段数码管的数值数组:
char digit[10][8] = //The increasing number { {0,0,0,0,0,0,1,1}, //0 {1,0,0,1,1,1,1,1}, //1 {0,0,1,0,0,1,0,1}, //2 {0,0,0,0,1,1,0,1}, //3 {1,0,0,1,1,0,0,1}, //4 {0,1,0,0,1,0,0,1}, //5 {0,1,0,0,0,0,0,1}, //6 {0,0,0,1,1,1,1,1}, //7 {0,0,0,0,0,0,0,1}, //8 {0,0,0,0,1,0,0,1} //9 }; char loop[6][8] = //the loop-running bar in the left windows { {0,1,1,1,1,1,1,1}, {1,0,1,1,1,1,1,1}, {1,1,0,1,1,1,1,1}, {1,1,1,0,1,1,1,1}, {1,1,1,1,0,1,1,1}, {1,1,1,1,1,0,1,1}, };
#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #define DIGIT0 8 #define DIGIT1 9 #define BTN0 10 #define BTN1 11 char digit[10][8] = //The increasing number { {0,0,0,0,0,0,1,1}, //0 {1,0,0,1,1,1,1,1}, //1 {0,0,1,0,0,1,0,1}, //2 {0,0,0,0,1,1,0,1}, //3 {1,0,0,1,1,0,0,1}, //4 {0,1,0,0,1,0,0,1}, //5 {0,1,0,0,0,0,0,1}, //6 {0,0,0,1,1,1,1,1}, //7 {0,0,0,0,0,0,0,1}, //8 {0,0,0,0,1,0,0,1} //9 }; char loop[6][8] = //the loop-running bar in the left windows { {0,1,1,1,1,1,1,1}, {1,0,1,1,1,1,1,1}, {1,1,0,1,1,1,1,1}, {1,1,1,0,1,1,1,1}, {1,1,1,1,0,1,1,1}, {1,1,1,1,1,0,1,1}, }; void main() { int pin; int m = 0, n = 0; int flag = 1; int run = 0; unsigned int time0 = 0, time1 = 0; if (wiringPiSetup () == -1) //test the install status of wiringPi { exit (1) ; } for (pin = 0 ; pin < 8 ; ++pin) { pinMode (pin, OUTPUT) ; digitalWrite(pin, HIGH); } pinMode(DIGIT0, OUTPUT); //The left number pinMode(DIGIT1, OUTPUT); //The right number pinMode(BTN0, INPUT); //The start button pinMode(BTN1, INPUT); //The stop button while (true) { time1 = millis(); if (digitalRead(BTN0) && run == 0) { puts("Start!"); run = 1; //change the running state m = 0; n = 0; time0 = millis(); } else if (digitalRead(BTN1) && run == 1) { puts("Stop!"); run = 0; } if (time1 - time0 >= 50 && run == 1) { time0 = time1; printf("%d\n", n); m = ++m % 6; n = ++n % 10; } for (pin = 0; pin < 8; pin++) { digitalWrite(pin, flag ? digit[n][pin] : loop[m][pin]); } digitalWrite(DIGIT0, flag); digitalWrite(DIGIT1, (flag = 1 - flag)); delay(10); } }
审核通过
http://v.youku.com/v_show/id_XNTcxMTI1MTA0.html
https://projects.drogon.net/raspberry-pi/wiringpi/pins/
https://projects.drogon.net/raspberry-pi/wiringpi/functions/