树莓派轮盘游戏机设计实现

made by Rk

本文由浙江大学《嵌入式系统》课程提供强力支持。

感谢翁恺老师 @翁恺BA5AG

===============================================

0、实验要求:

在RPi的Linux上做一个轮盘游戏机,具有两个按钮,一个负责启动,一个负责停止。具有某个或多个输出设备,如 LED、LED矩阵、双位7段数码管、LCD、蜂鸣器、舵机等,可任选其中一个或多个的组合。按下启动按钮,输出设备上 就出现各种变化,如流水灯、递增递减的数字、舵机的旋转等,按下停止按钮,输出设备就固定在当时的状态上。程序 运行的过程,会在shell输出数据。程序必须用C/C++语言写。


1、安装wiringPi

成熟的库如wiringPi可以用来控制树莓派的GPIO接口。

安装过程如下:

进入系统临时文件夹/tmp

输入命令:

git clone git://git.drogon.net/wiringPi

树莓派轮盘游戏机设计实现_第1张图片


cd wiringPi
sudo ./build
树莓派轮盘游戏机设计实现_第2张图片

输入以下命令验证wiringPi是否安装成功:

gpio -v
gpio readall

树莓派轮盘游戏机设计实现_第3张图片



wiringPi的接口映射(来自https://projects.drogon.net/raspberry-pi/wiringpi/pins/):

树莓派轮盘游戏机设计实现_第4张图片


2、具体连线

使用了Fritzing软件进行布线设计。

树莓派轮盘游戏机设计实现_第5张图片


七段数码管数值设定:

树莓派轮盘游戏机设计实现_第6张图片


根据共阴/共阳规则,需要仔细设定七段数码管的数值数组:

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},
    };




3、本实验所需代码

#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);
    }

}

4、实验结果视频:

审核通过

http://v.youku.com/v_show/id_XNTcxMTI1MTA0.html


5、参考资料:

https://projects.drogon.net/raspberry-pi/wiringpi/pins/

https://projects.drogon.net/raspberry-pi/wiringpi/functions/

你可能感兴趣的:(树莓派轮盘游戏机设计实现)