CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 5600|回复: 0

也发个74HC595 8段数码管的DIY:显示系统时间

[复制链接]
发表于 2013-11-18 19:11:24 | 显示全部楼层 |阅读模式
在TLL的帖子基础上写的,感谢他!
http://forum.cubietech.com/forum ... =896&extra=page%3D1

发个图片先
P31117-084007-small.png

然后是代码


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "lib/gpio_lib.h"

  6. #define LSBFIRST 0
  7. #define MSBFIRST 1

  8. typedef unsigned char byte;

  9. // Cubieboard port-D pin connected to ST_CP of 74HC595 (RCK)
  10. unsigned int latchPin = 2;

  11. // Cubieboard port-D pin connected to SH_CP of 74HC595 (SCK)
  12. unsigned int clockPin = 3;

  13. // Cubieboard port-D pin connected to DS of 74HC595 (DIN)
  14. unsigned int dataPin = 4;

  15. // Digits: 0,1,2,....,9,'dot','clear' for negative-shared 8-segment-LED
  16. unsigned int Tab[] =
  17.     { 0xc0, 0xcf, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff };

  18. /**
  19. * Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
  20. */
  21. void pinMode(unsigned int pin, unsigned int io_mode)
  22. {
  23.     if (SETUP_OK != sunxi_gpio_set_cfgpin(SUNXI_GPD(pin), io_mode))
  24.     {
  25.         printf("Failed to config GPIO pin\n");
  26.     }
  27. }

  28. /**
  29. * Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
  30. */
  31. void digitalWrite(int pin, int hl)
  32. {
  33.     if (sunxi_gpio_output(SUNXI_GPD(pin), hl))
  34.     {
  35.         printf("Failed to set GPIO pin value\n");
  36.     }
  37. }

  38. /**
  39. * Arduino shiftOut:
  40. * https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/wiring_shift.c
  41. */
  42. void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
  43. {
  44.     byte i;
  45.     for (i = 0; i < 8; i++)
  46.     {
  47.         if (bitOrder == LSBFIRST)
  48.             digitalWrite(dataPin, ! !(val & (1 << i)));
  49.         else
  50.             digitalWrite(dataPin, ! !(val & (1 << (7 - i))));

  51.         digitalWrite(clockPin, HIGH);
  52.         digitalWrite(clockPin, LOW);
  53.     }
  54. }

  55. /**
  56. * Initialize the GPIO & relative pins
  57. */
  58. void init_gpio()
  59. {
  60.     if (SETUP_OK != sunxi_gpio_init())
  61.     {
  62.         printf("Failed to initialize GPIO\n");
  63.     }
  64.     pinMode(latchPin, OUTPUT);
  65.     pinMode(clockPin, OUTPUT);
  66.     pinMode(dataPin, OUTPUT);
  67. }

  68. int main(int argc, char **argv)
  69. {
  70.     time_t timep;
  71.     struct tm *p;
  72.     char buf[6];

  73.     // init GPIO & pins
  74.     init_gpio();
  75.   
  76.     while (1)
  77.     {
  78.         // get localtime
  79.         time(&timep);
  80.         p = localtime(&timep);
  81.         sprintf(buf, "%02d%02d%02d", p->tm_hour, p->tm_min, p->tm_sec);
  82.         int i;
  83.         for (i = 0; i < sizeof(buf); i++)
  84.         {
  85.             digitalWrite(latchPin, 0);
  86.             shiftOut(dataPin, clockPin, MSBFIRST, 1 << i);
  87.             shiftOut(dataPin, clockPin, MSBFIRST, Tab[buf[i] - '0']);
  88.             digitalWrite(latchPin, 1);
  89.             usleep(1000);
  90.         }

  91.     }

  92.     return 0;
  93. }
复制代码
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-4-25 11:48 , Processed in 0.023128 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部