[新手园地] zhaor第一帖:RTC用数码管显示成功!

[复制链接]
 楼主| zhaor 发表于 2011-9-16 15:56 | 显示全部楼层 |阅读模式
本帖最后由 zhaor 于 2011-9-17 10:50 编辑

准备最少发4个心得帖子,都在一个主题里,所以预留2、3、4楼!
注意:程序正在优化中!每优化一次都会说明的!逐渐增加按键程序(能调时间、调闹铃);增加USB程序(能和电脑的时间同步)等。准备打造新塘RTC最强时钟程序!大家多多支持啊!
程序借用lixiaoxu2men的组织结构,表示谢意!
实际效果

电路图

数码管驱动电路清晰PDF格式图

595程序
  1. int8_t   const  table[11] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//共阴
  2. int8_t   const  dig[8] ={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
  3. uint8_t  blink;    //闪烁标志位
  4. void hc595enb(void)   //数据选通
  5. {
  6. hc595_cs_clr;
  7. hc595_cs_set;
  8. hc595_cs_clr;
  9. }
  10. void hc595rst(void)   //595复位
  11. {
  12. hc595_rst_set;
  13. hc595_clk_clr;
  14. hc595_rst_clr;
  15. hc595_rst_set;
  16. hc595enb();
  17. }

  18. void hc595outbyte(int8_t  disdata)  //595输出数据  单字节
  19. {
  20. int8_t dl; //显示数据移位
  21. for(dl=0;dl<=7;dl++)
  22.    {
  23.    if(disdata&0x80) hc595_data_set;
  24.    else
  25.    hc595_data_clr;
  26.    disdata<<=1;
  27.    hc595_clk_set;
  28.    hc595_clk_clr;
  29.    }
  30. }

  31. void hc595out(int8_t  data_h,int8_t  data_l)   //数据输出  高位在前,低位在后。低位为第一通道输出
  32. {
  33. hc595outbyte(data_h);
  34. hc595outbyte(data_l);
  35. hc595enb();
  36. }
  37. void display(int8_t  buffer[8])
  38. {
  39.    
  40.   static unsigned char scan;
  41.        switch(scan)
  42.        {
  43.      case 0x00:
  44.     hc595out(dig[scan],table[buffer[scan]]);
  45.        scan++;
  46.        break;
  47.      case 0x01:
  48.         hc595out(dig[scan],table[buffer[scan]]);
  49.         scan++;
  50.         break;
  51.      case 0x02:
  52.          if(blink)
  53.    {
  54.    hc595out(dig[scan],table[10]);
  55.    }
  56.    else
  57.    {
  58.        hc595out(dig[scan],table[buffer[scan]]);
  59.        }   
  60.        scan++;
  61.        break;
  62.     case 0x03:  
  63.         hc595out(dig[scan],table[buffer[scan]]);
  64.         scan++;
  65.         break;  
  66.      case 0x04:  
  67.         hc595out(dig[scan],table[buffer[scan]]);
  68.         scan++;
  69.         break;
  70.   case 0x05:
  71.    if(blink)
  72.    {
  73.    hc595out(dig[scan],table[10]);
  74.    }
  75.    else
  76.    {
  77.   hc595out(dig[scan],table[buffer[scan]]);
  78.   }      
  79.        scan++;
  80.        break;
  81. case 0x06:
  82.             hc595out(dig[scan],table[buffer[scan]]);
  83.        scan++;
  84.        break;
  85. case 0x07:  
  86.         hc595out(dig[scan],table[buffer[scan]]);
  87.         scan=0;
  88.         break;  
  89. default:
  90. break;  
  91.   }           
  92. }
595头文件
  1. #ifndef __HC595_H__
  2. #define __HC595_H__
  3. #define  hc595_data_set  DrvGPIO_SetBit(E_GPA,9)
  4. #define  hc595_data_clr  DrvGPIO_ClrBit(E_GPA,9)
  5. #define  hc595_clk_set   DrvGPIO_SetBit(E_GPA,8)
  6. #define  hc595_clk_clr   DrvGPIO_ClrBit(E_GPA,8)
  7. #define  hc595_cs_set    DrvGPIO_SetBit(E_GPA,7)
  8. #define  hc595_cs_clr    DrvGPIO_ClrBit(E_GPA,7)   
  9. #define  hc595_rst_set   DrvGPIO_SetBit(E_GPA,9)
  10. #define  hc595_rst_clr   DrvGPIO_ClrBit(E_GPA,9)   
  11. void hc595enb(void);
  12. void hc595rst(void);
  13. void hc595outbyte(int8_t  disdata);
  14. void hc595out(int8_t  data_h,int8_t  data_l);
  15. void display(int8_t  buffer[8] );
  16. #endif

主程序调用:
  1. TimeEnable();
  2.             if(SYSTime)
  3.          {
  4.                    display(disp_buffer);   //显示
  5.               if(++SYSms==500)   
  6.                {
  7.                     SYSms=0;
  8.                            DrvRTC_Read(DRVRTC_CURRENT_TIME, &sCurTime);   // (读取当前时间,存放指针)   
  9.                  disp_buffer[6]= sCurTime.u32cHour/10;
  10.                  disp_buffer[7]= sCurTime.u32cHour%10;
  11.                  disp_buffer[4]= sCurTime.u32cMinute/10 ;
  12.                  disp_buffer[3]= sCurTime.u32cMinute%10 ;
  13.                  disp_buffer[1]= sCurTime.u32cSecond/10;
  14.                               disp_buffer[0]= sCurTime.u32cSecond%10;                                       
  15.            if (DrvGPIO_GetBit(E_GPA,5))
  16.                     DrvGPIO_ClrBit(E_GPA,5);
  17.           else
  18.                     DrvGPIO_SetBit(E_GPA,5);        
  19.                         }
  20.                 }
  21.         
  22.         }
  23. }

全部程序:


代码简要分析
int8_t  SYS1ms,SYSTime;   //
int16_t  SYSms;
#define TimeEnable() SYSTime=0; if(SYS1ms){ SYSTime=1; SYS1ms=0; }
TimeEnable()在死循环程序中使用
每一次执行的时候SYSTime都清零,但是如果SYS1ms置位的时候SYSTime也就置位了
if(SYSTime)
         {
     display(disp_buffer);   //显示
}
这个时候就调用数码管显示函数了。
SYS1ms什么时候置位呢?
是在定时器中断里:
void Timer0_Callback (void)
{
   SYS1ms=1;
}
如果我们定时器设置10毫秒中断一次,那么数码管显示函数就是10毫秒调用一次
数码管扫描的频率就是100HZ,超过人眼的视觉暂留的了,数码管显示的就很稳定,没有闪烁感了!
有的朋友可能会说为什么不在定时器中断直接调用显示函数呢?
这样做的好处在那里呢?
大家可以搜索一下以前牛人农民讲习所所长的大作。
我们后来加按键检测程序也要用到它的!

本帖子中包含更多资源

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

×
 楼主| zhaor 发表于 2011-9-16 15:56 | 显示全部楼层
预留
 楼主| zhaor 发表于 2011-9-16 15:57 | 显示全部楼层
预留
 楼主| zhaor 发表于 2011-9-16 15:58 | 显示全部楼层
预留
tao560532 发表于 2011-9-16 21:15 | 显示全部楼层
赞一个
plc_avr 发表于 2011-9-17 06:00 | 显示全部楼层
  1. 顶一下,非常不错!
john_lee 发表于 2011-9-17 08:52 | 显示全部楼层
赞一个,期待后续的心得!
hotpower 发表于 2011-9-19 08:46 | 显示全部楼层
给力!不抛弃,不放弃!

非常之好!
fskjhm 发表于 2011-9-19 08:57 | 显示全部楼层
等着,学习
strang 发表于 2011-9-19 22:19 | 显示全部楼层
不错,顶一下。我刚买的12864液晶,还没到手,等到手了也做个万年历。
毅如靳往 发表于 2011-9-19 23:20 | 显示全部楼层
期待后续作品
lixiaoxu2meng 发表于 2011-9-21 14:45 | 显示全部楼层
很好 顶楼主
wangjia2000 发表于 2011-9-29 12:33 | 显示全部楼层
可以进入深度睡眠吗
yytpy2008 发表于 2011-12-2 09:33 | 显示全部楼层
给力……
Ryanhsiung 发表于 2011-12-2 09:54 | 显示全部楼层
欠缺一个备用电池
Ryanhsiung 发表于 2011-12-2 09:54 | 显示全部楼层
不过还是不错的,顶一下
5251 发表于 2011-12-2 14:22 | 显示全部楼层
学习了
zxcscm 发表于 2011-12-2 17:29 | 显示全部楼层
强力支持
您需要登录后才可以回帖 登录 | 注册

本版积分规则

41

主题

347

帖子

0

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