树莓派-轮盘游戏机设计
浙大嵌入式课程实验。
在实验板 的 Linux 上做一个轮盘游戏机,具有两个按钮,一个负责启动,一 个负责停止。具有某个或 多个输出设备,如 LED 柱状显示器、 双位 7 段数码管(不要求输出数字)、LCD或舵机 等,可任选其中一个或多个的 组合。按下启动按钮,输出设备上就出现各种变化,如流水灯、舵机的旋转等;按下停止按 钮,输出设备就固定在当时 的状态上。程序运行的过程,会在 shell 输出数据。程序必须用 C/C++语言写。
布线方案如下:
在面包板上连线,完成外部电路:
wiringPi是一个成熟的树莓派平台的GPIO控制库函数,我们可以通过安装wiringPi来控制树莓派的GPIO:
进入目录build
查看是否安装成功
wiringPi的接口映射:
本次试验用到的LG3641BH数码管的引脚:
共阳数码管管脚定义:
数码管焊板图:
测试程序:
#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #define DIGIT1 8 #define DIGIT2 12 #define DIGIT3 13 #define DIGIT4 9 #define BTN0 10 #define BTN1 11 #define true 1 char digit[10][8] = //The increasing number { {0,1,0,1,0,0,0,0}, //0 {1,1,0,1,1,1,0,1}, //1 {0,0,0,1,0,1,0,0}, //2 {0,0,0,1,0,1,0,1}, //3 {1,0,0,1,1,0,0,1}, //4 {0,0,0,1,0,0,1,1}, //5 {0,0,0,1,1,0,1,0}, //6 {1,1,0,1,0,1,0,1}, //7 {0,0,0,1,0,0,0,0}, //8 {0,0,0,1,0,0,0,1} //9 }; char loop[10][8] = //the loop-running bar in the left windows { {1,1,1,1,0,1,1,1},//chip1 {1,1,1,1,0,1,1,1},//chip2 {1,1,1,1,0,1,1,1},//chip3 {1,1,1,1,1,1,0,1},//chip3 {1,1,0,1,1,1,1,1},//chip3 {0,1,1,1,1,1,1,1},//chip3 {0,1,1,1,1,1,1,1},//chip2 {0,1,1,1,1,1,1,1},//chip1 {1,1,1,1,1,1,1,0},//chip1 {1,1,1,1,1,0,1,1},//chip1 }; void main() { int pin; int m = 0, n = 0; int flag = 1; int chip = 0; 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(DIGIT1, OUTPUT); pinMode(DIGIT2, OUTPUT); pinMode(DIGIT3, OUTPUT); pinMode(DIGIT4, OUTPUT); 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; chip = 0; time0 = millis(); } else if (digitalRead(BTN1) && run == 1) { puts("Stop!"); run = 0; // chip = m =n = 0; } if (time1 - time0 >= 50 && run == 1) { time0 = time1; // printf("%d\n", n); m = ++m % 10; n = ++n % 10; chip = ++chip % 10; printf("%d\n", n); } for (pin = 0; pin < 8; pin++) { digitalWrite(pin, 1-flag ? digit[n][pin] : loop[m][pin]); } switch(chip){ case 7: case 8: case 9: case 0:digitalWrite(DIGIT4, flag); //digitalWrite(DIGIT3, 0); //digitalWrite(DIGIT4, 0); break; case 6: case 1:digitalWrite(DIGIT3, flag); //digitalWrite(DIGIT2, 0); //digitalWrite(DIGIT4, 0); break; case 3: case 4: case 5: case 2:digitalWrite(DIGIT2, flag); //digitalWrite(DIGIT2, 0); //digitalWrite(DIGIT3, 0); break; default:break; } //chip = (chip + 1)%10; //digitalWrite(DIGIT4, flag); digitalWrite(DIGIT1, (flag = 1 - flag)); delay(30); digitalWrite(DIGIT1,0); digitalWrite(DIGIT2,0); digitalWrite(DIGIT3,0); digitalWrite(DIGIT4,0); //delay(30); } }
程序运行,在窗口中循环输出数字0-9,两个按钮分别控制开始和暂停,四个数码管左边三个设计成为跑马灯,右边一个循环显示数字0-9。
优酷视频链接地址:
posted on 2015-06-09 00:31 Jasonzhang398 阅读(655) 评论(0) 编辑 收藏 举报