查看: 4204|回复: 1
收起左侧

[交流] 【分享】做个4位数码管显示系统时间的小实验

2015-11-27 11:18:48 | 显示全部楼层 |阅读模式
材料:
树莓派2 b
面包板
跳线
4位共阳数码管

4位共阳数码管的引脚图:
(主要关注数字编号1-12的顺序,以及数字编号对应的字母编号的位置,即哪段led亮哪段灭搞清楚,DP就是小数点啦);
11.png

派2的40pin引脚图:
(使用 gpio readall 命令获得,依赖wiringPi库,安装方法查百度,主要看的是wPi的引脚编号)
22.png


软件方面我采用的是wiringPi C库(下文有完整源码);
根据以上的两张截图就可以连接硬件了:
数码管和树莓派的连线对应关系如下:

数码管段数字编号 -》数码管段字母编号 -》小派wPi引脚编号
1 ——》E——》21
2——》D——》22
3——》DP——》23
4——》C——》24
5——》G——》25
7——》B——》0
10——》F——》2
11——》A——》3

(共阳端口的链接)
数码管数字编号 -》wPi引脚编号
6——》26
8——》27
9——》28
12——》29

连接完上图一张,凑合着看吧:
33.PNG


连完所有的线,启动树莓派,写程序:
首先你要安装wiringPi开发库环境,百度一搜就会了,这里略过。

下面是源码:
居然不能回复可见,还是算了 (⊙o⊙)…
  1. // 4位共阳数码管驱动程序
  2. // 文件:led4display.c
  3. // 作者:Jason
  4. // Q1165175299 欢迎交流学习
  5. #include <wiringPi.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <time.h>

  9. // 数值0-9对应的7段控制信号
  10. // 由于共阳,1表示熄灭该段,0表示点亮该段
  11. // 数组第二维0-6分别表示7段的A-G
  12. const int value[10][7] = {
  13.     { 0,0,0,0,0,0,1 }, //0
  14.     { 1,0,0,1,1,1,1 }, //1
  15.     { 0,0,1,0,0,1,0 }, //2
  16.     { 0,0,0,0,1,1,0 }, //3
  17.     { 1,0,0,1,1,0,0 }, //4
  18.     { 0,1,0,0,1,0,0 }, //5
  19.     { 0,1,0,0,0,0,0 }, //6
  20.     { 0,0,0,1,1,1,1 }, //7
  21.     { 0,0,0,0,0,0,0 }, //8
  22.     { 0,0,0,0,1,0,0 }  //9
  23. };

  24. // 7个段的wPi GPIO引脚映射
  25. // 下标0-6表示段A-G,元素分别表示A-G的wPi引脚编号
  26. // 实际硬件连线就按这个关系连接
  27. const int pin[7] = { 3,0,24,22,21,2,25 };

  28. // 4个共阳端的引脚映射
  29. // 0-3分别表示从左往右4个数字位
  30. // 数码管引脚6,8,9,12分别对应wPi引脚26,27,28,29
  31. const int com[4] = { 29,28,27,26 };

  32. // 数码管的小数点wPi引脚号
  33. const int pin_dot = 23;

  34. // 函数头定义
  35. char *get_system_time(void);

  36. // usage: led4display [number] [dots]
  37. // example: led4display 1234 1011
  38. // 主函数无错误检测,参数1、2必须都是4位数字
  39. int main(int argc, char *argv[])
  40. {
  41.     char *num = NULL, dots[] = "1011";
  42.     int i = 0, init = 0, seg = 0; // seg为7个段
  43.     int count = 0, delay_time = 5;

  44.     /*
  45.     if (argc != 3) {
  46.     printf("usage: led4display [4 nums] [4 dots]\n");
  47.     printf("4 nums like 1234 to display number 1234\n");
  48.     printf("4 dots like 1011, dot form left to right means off-on-off-off\n");
  49.     exit(1);
  50.     }
  51.     */

  52.     if (wiringPiSetup() == -1) {
  53.     printf("wPi init fail\n");
  54.     exit(2);
  55.     }

  56.     /*
  57.     num = argv[1];
  58.     dots = argv[2];
  59.     */
  60.     num = get_system_time();
  61.     //dots = "1011"

  62.     // initalize for all pins OUTPUT
  63.     for (init = 0; init < 7; init++) {
  64.     pinMode(pin[init], OUTPUT);
  65.     }
  66.     for (init = 0; init < 4; init++) {
  67.     pinMode(com[init], OUTPUT);
  68.     }
  69.     pinMode(pin_dot, OUTPUT);

  70.     while (1){
  71.     // display
  72.         for (i = 0; i < 4; i++) {
  73.         // every 20s to get sys time
  74.         if (count % (20000/delay_time) == 0){
  75.         num = get_system_time();
  76.         count = 0;
  77.         }
  78.         // firstly, empty 4 numbers
  79.         for (seg = 0; seg < 7; seg++) {
  80.         digitalWrite(pin[seg], HIGH);
  81.         }
  82.         digitalWrite(pin_dot, dots[i] - 48);
  83.         // 打开第i个数码管的共阳端
  84.         digitalWrite(com[i], HIGH);
  85.         for (seg = 0; seg < 7; seg++) {
  86.         // 打开数码管的段, 减48,把字符转换成数值(ASCII编码)
  87.         digitalWrite(pin[seg], value[num[i] - 48][seg]);
  88.         }
  89.         // 打开数码管的小数点
  90.         digitalWrite(pin_dot, dots[i] - 48);
  91.         // 显示完一个数字后关闭显示
  92.         delay(delay_time);
  93.         count++;
  94.         digitalWrite(com[i], LOW);
  95.     }
  96.     }

  97.     return 0;
  98. }

  99. // get time
  100. char *get_system_time(void)
  101. {
  102.     static char timestr[20];
  103.     time_t t;
  104.     struct tm *nowtime;

  105.     time(&t);
  106.     nowtime = localtime(&t);
  107.     strftime(timestr, sizeof(timestr), "%H%M", nowtime);

  108.     return timestr;
  109. }
复制代码

源码写好保存,开始编译,一条命令搞定了:

gcc -Wall -I/usr/local/include -L/usr/local/lib -lwiringPi -o led4display led4display.c

// 编译参数你可以 man 去查看,简单说下,-Wall显示所有警告,-I、-L分别指定了包含文件和库,-lwiringPi 这个很重要一定要加





运行啦:

sudo ./
led4display



是不是看到数码管显示当前系统时间了?

截图呢?额,在上文已经放过了。。。。

看了下运行起来不会闪,程序cpu负载小于1%,应该可以了。




回复

使用道具 举报

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

本版积分规则

热点推荐

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部