本次设计的数码管计数器是基于树莓派编程,应用材料只需按键开关一个、一位共阳数码管一个、杜邦线数根。用树莓派进行C语言编程,使其计数器实现初始为零、按键+1的效果,因考虑材料有限,数据显示为0-9的循环显示。从0开始按键+1一直到9,数据显示到9之后按键继续按下又再次回到数字0,按键+1到9。以此的一个按键+1的0-9的循环。
接线表:
数码管引脚及按键开关接口 | WPi | physical | A | 25 | 37 | B | 24 | 35 | C | 23 | 33 | D | 22 | 31 | E | 21 | 29 | F | 14 | 23 | G | 13 | 21 | DP | 12 | 19 | GND | 26 | 32 | Key | 2 | 13 | Key GND | OV | 39 |
源码:
- #include<wiringPi.h>
- #include<unistd.h>
- #include<time.h>
- #define LED_A 25 //定义数码管引脚
- #define LED_B 24
- #define LED_C 23
- #define LED_D 22
- #define LED_E 21
- #define LED_F 14
- #define LED_G 13
- #define LED_DP 12
- #define DIGIT1 26
- #define btn 2 //定义按键开关引脚
- unsigned char num=0; //初始化num=0
- void showDigit( int num);
- int main (void)
- {
- wiringPiSetup (); //设置数码管引脚模式
- pinMode (LED_A, OUTPUT);
- pinMode (LED_B, OUTPUT);
- pinMode (LED_C, OUTPUT);
- pinMode (LED_D, OUTPUT);
- pinMode (LED_E, OUTPUT);
- pinMode (LED_F, OUTPUT);
- pinMode (LED_G, OUTPUT);
- pinMode (LED_DP, OUTPUT);
- pinMode (DIGIT1, OUTPUT);
- pinMode (btn, INPUT);
- pullUpDnControl (btn, PUD_UP);
- digitalWrite (DIGIT1, HIGH);
- num=0;
- while(1)
- {
- if(digitalRead(btn)==0) //按键输出为高电平时
- {
- while(digitalRead(btn)==0);
- showDigit(num);
- delay(50); //延时
- num=num+1; //num加1
- if(num<0) //当num<0时
- {
- num=0; //num=0
- }
- if(num>9) //当num>9时
- {
- num=0; //num=0
- }
- }
- }
- }
- void showDigit(int num)
- {
- if (num == 0) //显示数字0
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, LOW) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, HIGH) ;
- }
- else if (num == 1) //显示数字1
- {
- digitalWrite (LED_A, HIGH) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, HIGH) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, HIGH) ;
- digitalWrite (LED_G, HIGH) ;
- }
- else if (num == 2) //显示数字2
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, HIGH) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, LOW) ;
- digitalWrite (LED_F, HIGH) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 3) //显示数字3
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, HIGH) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 4) //显示数字4
- {
- digitalWrite (LED_A, HIGH) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, HIGH) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 5) //显示数字5
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, HIGH) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 6) //显示数字6
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, HIGH) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, LOW) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 7) //显示数字7
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, HIGH) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, HIGH) ;
- digitalWrite (LED_G, HIGH) ;
- }
- else if (num == 8) //显示数字8
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, LOW) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, LOW) ;
- }
- else if (num == 9) //显示数字9
- {
- digitalWrite (LED_A, LOW) ;
- digitalWrite (LED_B, LOW) ;
- digitalWrite (LED_C, LOW) ;
- digitalWrite (LED_D, LOW) ;
- digitalWrite (LED_E, HIGH) ;
- digitalWrite (LED_F, LOW) ;
- digitalWrite (LED_G, LOW) ;
- }
- }
复制代码
完整的Word格式文档51黑下载地址:
树莓派计数器.doc
(342 KB, 下载次数: 16)
|