【现场直播】一个数字温控仪表的实现(能显示了)

[复制链接]
13925|83
韩秋婷 发表于 2009-12-18 16:49 | 显示全部楼层
硬件设计不合理
实现楼主的功能几毛钱就能搞定了
 楼主| 原野之狼 发表于 2009-12-18 17:07 | 显示全部楼层
7.3.2 数码管以及LED驱动
      采用动态扫描,要注意扫描的频率,为了达到比较好的观感,间隔不少于20ms。我的板子上主窗口和副窗口用的数码管是不一样的,并且74HC595位扫描信号不够,故另外使用了3个IO信号来扫描位,这给编程带来一定的麻烦。

代码片段一:

  1. /************************************************************
  2. FileName: ui.h

  3. Author: 原野之狼

  4. Version :V1.0

  5. Date: 2009.12.18

  6. Description:

  7. History:

  8. <author> <time> <version > <desc>


  9. ************************************************************/
  10. #ifndef _H_UI_H_
  11. #define _H_UI_H_

  12. #include "type.h"

  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif

  16. //define the other three bit driver signals,because hc595 has only 8 bit driver signals
  17. //this three signals are drivered by mcu directly
  18. #define B9_HIGH PORTE|=(0x01<<7)
  19. #define B9_LOW PORTE&=(~(0x01<<7))
  20. #define B10_HIGH PORTE|=(0x01<<6)
  21. #define B10_LOW PORTE&=(~(0x01<<6))
  22. #define B11_HIGH PORTB|=0x01
  23. #define B11_LOW PORTB&=(~0x01)

  24. //define some macros of operating leds
  25. //the led ON or OFF state is mapped in the array DisplayBuf[10]
  26. #define LED1ON do{cli();display_buf[10]|=0X20;sei();}while(0)
  27. #define LED1OFF do{cli();display_buf[10]&=(~0X20);sei();}while(0)
  28. #define LED2ON do{cli();display_buf[10]|=0X01;sei();}while(0)
  29. #define LED2OFF do{cli();display_buf[10]&=(~0X01);sei();}while(0)
  30. #define LED3ON do{cli();display_buf[10]|=0X40;sei();}while(0)
  31. #define LED3OFF do{cli();display_buf[10]&=(~0X40);sei();}while(0)
  32. #define LED4ON do{cli();display_buf[10]|=0X80;sei();}while(0)
  33. #define LED4OFF do{cli();display_buf[10]&=(~0X80);sei();}while(0)

  34. void display_init(void);
  35. void led_scan_run(void);
  36. void display_main(char *p);
  37. void display_vice(char *p);
  38. void display_main_const(const char *p);
  39. void display_vice_const(const char *p);
  40. void timer2_init(void);
  41. void timer2_start(void);
  42. void timer2_close(void);

  43. #ifdef __cplusplus
  44. }
  45. #endif

  46. #endif


 楼主| 原野之狼 发表于 2009-12-18 17:11 | 显示全部楼层
代码片段二:

  1. /************************************************************
  2. FileName: ui.c
  3. Author: 原野之狼
  4. Version :V1.0      
  5. Date: 2009.12.18
  6. Description:
  7.   
  8. History:
  9.       <author>      <time>      <version >      <desc>

  10. ************************************************************/
  11. #include "includes.h"
  12. //define the 8 segments of the number led in the window.
  13. //you can modify these datas by considerating the architecture of the numer led
  14. //and the hardware connections.
  15. //here the architecture of led in the main window is different with the led in the vice window.
  16. //define the 8 segments of the number led in the main window.
  17. #define SEG_A 0X40
  18. #define SEG_B 0X80
  19. #define SEG_C 0X04
  20. #define SEG_D 0X10
  21. #define SEG_E 0X08
  22. #define SEG_F 0X01
  23. #define SEG_G 0X20
  24. #define SEG_DP 0X02
  25. //define the 8 segments of the number led in the vice window.
  26. #define SEG_A_ 0X02
  27. #define SEG_B_ 0X04
  28. #define SEG_C_ 0X08
  29. #define SEG_D_ 0X80
  30. #define SEG_E_ 0X40
  31. #define SEG_F_ 0X10
  32. #define SEG_G_ 0X20
  33. #define SEG_DP_ 0X01

  34. //define the segment code of the number in the main window
  35. static const prog_uchar  code_num[] = {
  36. SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,            // 0
  37. SEG_B | SEG_C,                                            // 1
  38. SEG_A | SEG_B | SEG_D | SEG_E | SEG_G,                    // 2
  39. SEG_A | SEG_B | SEG_C | SEG_D | SEG_G,                    // 3
  40. SEG_B | SEG_C | SEG_F | SEG_G,                            // 4
  41. SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,                    // 5
  42. SEG_A | SEG_F | SEG_E | SEG_D | SEG_C | SEG_G,            // 6
  43. SEG_A | SEG_B | SEG_C,                                    // 7
  44. SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,    // 8
  45. SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G             // 9
  46. };
  47. //define the segment code of the letters
  48. static const prog_uchar code_letter[]={
  49. SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,            // a
  50. SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,                    // b
  51. SEG_A | SEG_D | SEG_E | SEG_F,                            // c
  52. SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,                    // d
  53. SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,                    // e
  54. SEG_A | SEG_E | SEG_F | SEG_G,                            // f
  55. SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,            // g
  56. SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,                    // h
  57. SEG_E | SEG_F,                                            // i
  58. SEG_B | SEG_C | SEG_D | SEG_E,                            // j
  59. SEG_B | SEG_D | SEG_E | SEG_F | SEG_G,                    // k
  60. SEG_D | SEG_E | SEG_F,                                    // l
  61. SEG_A | SEG_C | SEG_E | SEG_G,                            // m
  62. SEG_C | SEG_E | SEG_G,                                    // n
  63. SEG_C | SEG_D | SEG_E | SEG_G,                            // o
  64. SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,                    // p
  65. SEG_A | SEG_B | SEG_C | SEG_F | SEG_G,                    // q
  66. SEG_E | SEG_G,                                            // r
  67. SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,                    // s
  68. SEG_D | SEG_E | SEG_F | SEG_G,                            // t
  69. SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,                    // u
  70. SEG_C | SEG_D | SEG_E,                                    // v
  71. SEG_B | SEG_D | SEG_F | SEG_G,                            // w
  72. SEG_B | SEG_C | SEG_E | SEG_F,                            // x
  73. SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,                    // y
  74. SEG_A | SEG_D | SEG_G                                     // z
  75. };
  76. static const prog_uchar code_null  =0x00;                   // ' '
  77. static const prog_uchar code_bar = SEG_G;                   // '-'
  78. static const prog_uchar code_dp = SEG_DP;                   // '.'

  79. //display buf
  80. //10 for number leds. 1 one for leds
  81. UINT8 display_buf[11];
  82. /***********************************************************
  83. Description:
  84.   intial the display sys
  85.   
  86. Modify Record:

  87. ***********************************************************/
  88. void display_init(void)
  89. {
  90. UINT8 i;

  91. DDRE |=0xc0; // set B9&B10 output
  92. DDRB |=0x01; // set B11 output
  93. for (i = 0; i < 11; i++)
  94. {
  95.   display_buf[i] = 0x00;
  96. }
  97. init_595();
  98. timer2_init();
  99. timer2_start();
  100. }
  101. /***********************************************************
  102. Description:
  103.   scan the led.
  104.   this function should be called by a timer at least every 20ms/11.
  105.   
  106. Modify Record:

  107. ***********************************************************/
  108. void led_scan_run(void)
  109. {
  110. /***********************************************
  111. main window:
  112.         ----    ----    ----    ----    ----
  113.         |  |    |  |    |  |    |  |    |  |
  114.         ----    ----    ----    ----    ----
  115.         |  |    |  |    |  |    |  |    |  |
  116.         ----    ----    ----    ----    ----
  117. pos:   4        3       2       1       0  
  118. bit sel:
  119.        5        4       ...
  120. buf map:
  121.        buf[0]   ...
  122. vice window:
  123.         ----    ----    ----    ----    ----
  124.         |  |    |  |    |  |    |  |    |  |
  125.         ----    ----    ----    ----    ----
  126.         |  |    |  |    |  |    |  |    |  |
  127.         ----    ----    ----    ----    ----
  128. pos:    9       8       7       6       5
  129. bit sel:
  130.         9       8       ...
  131. bif map:
  132.        buf[5]  ...
  133. led:
  134.           @       @       @      @
  135.             led4    led3    led2   led1
  136. pos:                   10
  137. bit sel:
  138.                         10
  139.               buf[10]
  140. ***********************************************/
  141. static UINT8 pos = 0;
  142. switch (pos)
  143. {
  144.   case 0:
  145.   {
  146.    B9_LOW;
  147.    B10_LOW;
  148.    B11_LOW;
  149.    driver_595(display_buf[4], 0x04);
  150.   }
  151.   break;
  152.   case 1:
  153.   {
  154.    B9_LOW;
  155.    B10_LOW;
  156.    B11_LOW;
  157.    driver_595(display_buf[3], 0x02);
  158.   }
  159.   break;
  160.   case 2:
  161.   {
  162.    B9_LOW;
  163.    B10_LOW;
  164.    B11_LOW;
  165.    driver_595(display_buf[2], 0x08);
  166.   }
  167.   break;
  168.   case 3:
  169.   {
  170.    B9_LOW;
  171.    B10_LOW;
  172.    B11_LOW;
  173.    driver_595(display_buf[1], 0x10);
  174.   }
  175.   break;
  176.   case 4:
  177.   {
  178.    B9_LOW;
  179.    B10_LOW;
  180.    B11_LOW;
  181.    driver_595(display_buf[0], 0x01);
  182.   }
  183.   break;
  184.   case 5:
  185.   {
  186.    B9_LOW;
  187.    B10_LOW;
  188.    B11_LOW;
  189.    driver_595(display_buf[9], 0x20);
  190.   }
  191.   break;
  192.   case 6:
  193.   {
  194.    B9_LOW;
  195.    B10_LOW;
  196.    B11_LOW;
  197.    driver_595(display_buf[8], 0x40);
  198.   }
  199.   break;
  200.   case 7:
  201.   {
  202.    B9_LOW;
  203.    B10_LOW;
  204.    B11_LOW;
  205.    driver_595(display_buf[7], 0x80);
  206.   }
  207.   break;
  208.   case 8:
  209.   {
  210.    B10_LOW;
  211.    B11_LOW;
  212.    driver_595(display_buf[6], 0x00);
  213.    B9_HIGH;
  214.   }
  215.   break;
  216.   case 9:
  217.   {
  218.    B9_LOW;
  219.    B11_LOW;
  220.    driver_595(display_buf[5], 0x00);
  221.    B10_HIGH;
  222.   }
  223.   break;
  224.   case 10:
  225.   {
  226.    B9_LOW;
  227.    B10_LOW;
  228.    driver_595(display_buf[10], 0x00);
  229.    B11_HIGH;
  230.   }
  231.   break;
  232.   default:
  233.    asm("nop");
  234. }
  235. pos++;
  236. pos %= 11;
  237. }

  238. /***********************************************************
  239. Description:
  240.   display contents in the main window
  241. Input:
  242.   contens in char.
  243.   for example:
  244.    99.999
  245.    abcde
  246.    sys-1
  247.    8.8.8.8.8.
  248.   
  249. Others:
  250.   for safety considerations,the input para must be checked.
  251.   but not done here.

  252. Modify Record:

  253. ***********************************************************/
  254. void display_main(char *p)
  255. {
  256. UINT8 i = 0,j = 0;
  257. do
  258. {
  259.   if (*(p + i) >= '0' && *(p + i) <= '9')
  260.   {
  261.    cli();
  262.    display_buf[j++] = pgm_read_byte(&code_num[*(p + i) - '0']);
  263.    sei();
  264.   }
  265.   if (*(p + i) >= 'a' && *(p + i) <= 'z')
  266.   {
  267.    cli();
  268.    display_buf[j++] = pgm_read_byte(&code_letter[*(p + i) - 'a']);
  269.    sei();
  270.   }
  271.   if (*(p + i) == ' ')
  272.   {
  273.    cli();
  274.    display_buf[j++] = pgm_read_byte(&code_null);
  275.    sei();
  276.   }
  277.   if (*(p + i) == '.')
  278.   {
  279.    cli();
  280.    display_buf[j -1] |= pgm_read_byte(&code_dp);
  281.    sei();
  282.   }
  283.   
  284.   if (*(p + i) == '-')
  285.   {
  286.    cli();
  287.    display_buf[j++] = pgm_read_byte(&code_bar);
  288.    sei();
  289.   }
  290.   i++;
  291. } while (*(p + i));
  292. }
 楼主| 原野之狼 发表于 2009-12-18 17:13 | 显示全部楼层
噢啦,显示部分基本完成了,只贴了部分代码,完整代码打包上传:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
xlsbz 发表于 2009-12-18 19:05 | 显示全部楼层
:victory:
爱学小生 发表于 2009-12-18 20:12 | 显示全部楼层
先mark 以后看。。
天雨粟 发表于 2009-12-19 09:55 | 显示全部楼层
知道595可以驱动点阵之类,没想到尚可用之驱动LED数码管,想请教狼兄一个问题:
图中用的共阴管,9013用于位控,高电平有效
MCU可用模拟串口的办法驱动595,6个I/O口可扩得十六个输出口
据说595内部有小限流电阻,显示部分图看不清,想请教,串在笔划(字段)上的限流电阻为多大?谢谢!
 楼主| 原野之狼 发表于 2009-12-19 11:16 | 显示全部楼层
回复48楼:
我是共用了595的移位时钟和锁存时钟,也就是4个IO扩展了16个输出。
限流电阻用的是330欧姆。
xwj 发表于 2009-12-19 11:27 | 显示全部楼层
呵呵,这个要顶,
这个也可以顶~


只是,你呀也太会浪费了吧?
大手大脚的可不好哦~:)
天雨粟 发表于 2009-12-19 13:30 | 显示全部楼层
谢谢楼主!
 楼主| 原野之狼 发表于 2009-12-19 23:03 | 显示全部楼层
新做的板子到了 需要忙了 所以更新会比较慢
很不幸 今天新焊的板子 被我弄坏了 4块芯片全崩了
xwj 发表于 2009-12-19 23:09 | 显示全部楼层
牛叉!


咋崩的?
 楼主| 原野之狼 发表于 2009-12-20 00:08 | 显示全部楼层
一念之间 把12V加到了5V上 然后芯片就工作不正常了 所以不得不把和5V有关的芯片全卸下来
aihe 发表于 2009-12-20 17:44 | 显示全部楼层
造门大**打蚊子
不过还是赞一个
huangqi412 发表于 2009-12-21 08:54 | 显示全部楼层
牛.12V...
comelon 发表于 2009-12-21 11:01 | 显示全部楼层
1# 原野之狼
LZ的代码风格不错,学习!
icecut 发表于 2009-12-21 13:08 | 显示全部楼层
代码不错.
yangjichun 发表于 2009-12-21 17:26 | 显示全部楼层
顶下,原理图发下更好,初次介入嵌入式行业,需要用AD转换+数码管显示做个模拟信号显示输出设备。
my1693 发表于 2009-12-22 10:50 | 显示全部楼层
:D不错,
加个电压采样,改成电压表+温度显示 另外在搞个模拟量输出
andy2003hu 发表于 2009-12-22 13:06 | 显示全部楼层
代码比较规范。顶一个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部