找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3833|回复: 0
打印 上一主题 下一主题
收起左侧

基于树莓派C语言编程的数码管计数器制作

[复制链接]
跳转到指定楼层
楼主
本次设计的数码管计数器是基于树莓派编程,应用材料只需按键开关一个、一位共阳数码管一个、杜邦线数根。用树莓派进行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


源码:
  1. #include<wiringPi.h>
  2. #include<unistd.h>
  3. #include<time.h>
  4. #define LED_A 25  //定义数码管引脚
  5. #define LED_B 24
  6. #define LED_C 23
  7. #define LED_D 22
  8. #define LED_E 21
  9. #define LED_F 14
  10. #define LED_G 13
  11. #define LED_DP 12
  12. #define DIGIT1 26
  13. #define btn 2   //定义按键开关引脚
  14. unsigned char num=0;  //初始化num=0
  15. void showDigit( int num);
  16. int main (void)
  17. {
  18.   wiringPiSetup ();  //设置数码管引脚模式
  19. pinMode (LED_A, OUTPUT);
  20. pinMode (LED_B, OUTPUT);
  21.   pinMode (LED_C, OUTPUT);
  22.   pinMode (LED_D, OUTPUT);
  23.   pinMode (LED_E, OUTPUT);
  24.   pinMode (LED_F, OUTPUT);
  25.   pinMode (LED_G, OUTPUT);
  26.   pinMode (LED_DP, OUTPUT);
  27.   pinMode (DIGIT1, OUTPUT);
  28.   pinMode (btn, INPUT);
  29.   pullUpDnControl (btn, PUD_UP);
  30.   digitalWrite (DIGIT1, HIGH);
  31. num=0;
  32. while(1)
  33. {
  34.         if(digitalRead(btn)==0)  //按键输出为高电平时
  35. {
  36. while(digitalRead(btn)==0);
  37. showDigit(num);
  38. delay(50);  //延时
  39. num=num+1;  //num加1
  40. if(num<0)  //当num<0时
  41. {
  42. num=0;  //num=0
  43. }
  44. if(num>9)  //当num>9时
  45. {
  46. num=0;  //num=0
  47. }
  48. }
  49. }
  50. }

  51. void showDigit(int num)
  52. {
  53.   if (num == 0)  //显示数字0
  54. {
  55.     digitalWrite (LED_A, LOW) ;
  56.     digitalWrite (LED_B, LOW) ;
  57.     digitalWrite (LED_C, LOW) ;
  58.     digitalWrite (LED_D, LOW) ;
  59.     digitalWrite (LED_E, LOW) ;
  60.     digitalWrite (LED_F, LOW) ;
  61.     digitalWrite (LED_G, HIGH) ;
  62.   }
  63. else if (num == 1)  //显示数字1
  64. {
  65.     digitalWrite (LED_A, HIGH) ;
  66.     digitalWrite (LED_B, LOW) ;
  67.     digitalWrite (LED_C, LOW) ;
  68.     digitalWrite (LED_D, HIGH) ;
  69.     digitalWrite (LED_E, HIGH) ;
  70.     digitalWrite (LED_F, HIGH) ;
  71.     digitalWrite (LED_G, HIGH) ;
  72.   }
  73. else if (num == 2)  //显示数字2
  74. {
  75.     digitalWrite (LED_A, LOW) ;
  76.     digitalWrite (LED_B, LOW) ;
  77. digitalWrite (LED_C, HIGH) ;
  78. digitalWrite (LED_D, LOW) ;
  79. digitalWrite (LED_E, LOW) ;
  80. digitalWrite (LED_F, HIGH) ;
  81. digitalWrite (LED_G, LOW) ;
  82.   }
  83. else if (num == 3) //显示数字3
  84. {
  85.     digitalWrite (LED_A, LOW) ;
  86.     digitalWrite (LED_B, LOW) ;
  87.     digitalWrite (LED_C, LOW) ;
  88.     digitalWrite (LED_D, LOW) ;
  89.     digitalWrite (LED_E, HIGH) ;
  90.     digitalWrite (LED_F, HIGH) ;
  91.     digitalWrite (LED_G, LOW) ;
  92.   }
  93. else if (num == 4)  //显示数字4
  94. {
  95.     digitalWrite (LED_A, HIGH) ;
  96.     digitalWrite (LED_B, LOW) ;
  97.     digitalWrite (LED_C, LOW) ;
  98.     digitalWrite (LED_D, HIGH) ;
  99.     digitalWrite (LED_E, HIGH) ;
  100.     digitalWrite (LED_F, LOW) ;
  101.     digitalWrite (LED_G, LOW) ;
  102.   }
  103. else if (num == 5)  //显示数字5
  104. {
  105.     digitalWrite (LED_A, LOW) ;
  106.     digitalWrite (LED_B, HIGH) ;
  107.     digitalWrite (LED_C, LOW) ;
  108.     digitalWrite (LED_D, LOW) ;
  109.     digitalWrite (LED_E, HIGH) ;
  110.     digitalWrite (LED_F, LOW) ;
  111.     digitalWrite (LED_G, LOW) ;
  112.   }
  113. else if (num == 6)  //显示数字6
  114. {
  115.     digitalWrite (LED_A, LOW) ;
  116.     digitalWrite (LED_B, HIGH) ;
  117.     digitalWrite (LED_C, LOW) ;
  118.     digitalWrite (LED_D, LOW) ;
  119.     digitalWrite (LED_E, LOW) ;
  120.     digitalWrite (LED_F, LOW) ;
  121.     digitalWrite (LED_G, LOW) ;
  122.   }
  123. else if (num == 7)  //显示数字7
  124. {
  125.     digitalWrite (LED_A, LOW) ;
  126.     digitalWrite (LED_B, LOW) ;
  127.     digitalWrite (LED_C, LOW) ;
  128.     digitalWrite (LED_D, HIGH) ;
  129.     digitalWrite (LED_E, HIGH) ;
  130.     digitalWrite (LED_F, HIGH) ;
  131.     digitalWrite (LED_G, HIGH) ;
  132.   }
  133. else if (num == 8) //显示数字8
  134. {
  135.     digitalWrite (LED_A, LOW) ;
  136.     digitalWrite (LED_B, LOW) ;
  137.     digitalWrite (LED_C, LOW) ;
  138.     digitalWrite (LED_D, LOW) ;
  139.     digitalWrite (LED_E, LOW) ;
  140.     digitalWrite (LED_F, LOW) ;
  141.     digitalWrite (LED_G, LOW) ;
  142.   }
  143. else if (num == 9)  //显示数字9
  144. {
  145.     digitalWrite (LED_A, LOW) ;
  146.     digitalWrite (LED_B, LOW) ;
  147.     digitalWrite (LED_C, LOW) ;
  148.     digitalWrite (LED_D, LOW) ;
  149.     digitalWrite (LED_E, HIGH) ;
  150.     digitalWrite (LED_F, LOW) ;
  151.     digitalWrite (LED_G, LOW) ;
  152.   }
  153. }
复制代码

完整的Word格式文档51黑下载地址:
树莓派计数器.doc (342 KB, 下载次数: 16)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表