找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2746|回复: 0
收起左侧

基于时钟芯片1302的c语言程序

[复制链接]
ID:73477 发表于 2015-2-10 17:52 | 显示全部楼层 |阅读模式

  1. #include<reg52.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. //**************寄存器宏定义*******************************
  5. #define write_miao        0x80
  6. #define write_fen         0x82
  7. #define write_shi         0x84
  8. #define read_miao         0x81
  9. #define read_fen          0x83
  10. #define read_shi          0x85
  11. #define write_protect     0x8e
  12. //*********************************************************
  13. sbit ACC_7=ACC^7;      //管脚定义
  14. sbit sclk=P3^5;        //ds1302时钟信号
  15. sbit dio=P3^6;         //ds1302数据信号
  16. sbit ce=P3^7;          //ds1302片选
  17. //*********************************************************
  18. sbit wei=P2^0;
  19. sbit duan=P2^1;
  20. sbit k1=P1^0;
  21. sbit k2=P1^1;
  22. uchar table[]={0x3f,0x06,0x5b,0x4f,  //0,1,2,3
  23.                0x66,0x6d,0x7d,0x07,  //4,5,6,7
  24.       0x7f,0x6f,0x40,0x00}; //8,9,-,空格
  25. uchar wela[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};      
  26. uchar num,num1,num2,shi,fen,miao,m,s;
  27. char guo0,guo1,guo2;
  28. /********************地址、数据发送子程序*******************/
  29. void write1302(uchar addr,dat)
  30. {
  31.   uchar i,temp;
  32. ce=0;            //ce引脚为低,数据传送中止
  33. sclk=0;          //清零时钟总线
  34. ce=1;            //ce引脚为高,逻辑控制有效
  35. //发送地址
  36. for(i=8;i>0;i--) //循环8次移位
  37. {
  38.    sclk=0;
  39.   temp=addr;
  40.   dio=(bit)(temp&0x01);//每次传输低字节
  41.   addr>>=1;    //右移一位
  42.   sclk=1;
  43. }
  44. //发送数据
  45. for(i=8;i>0;i++)
  46. {
  47.    sclk=0;
  48.   temp=dat;
  49.   dio=(bit)(temp&0x01);
  50.   dat>>=1;
  51.   sclk=1;
  52. }
  53. ce=0;
  54. }
  55. /******************数据读取子程序*******************/
  56. uchar read1302(uchar addr)
  57. {
  58.   uchar i,temp,dat1;   // dat2,
  59. ce=0;
  60. sclk=0;
  61. ce=1;
  62. //发送地址
  63. for(i=8;i>0;i--)
  64. {
  65.    sclk=0;
  66.   temp=addr;
  67.   dio=(bit)(temp&0x01);//每次传输低字节
  68.   addr>>=1;
  69.   sclk=1;
  70. }
  71. //读取数据
  72. for(i=8;i>0;i--)
  73. {
  74.    ACC_7=dio;
  75.   sclk=1;
  76.   ACC>>=1;
  77.   sclk=0;
  78. }
  79. ce=0;
  80. dat1=ACC;
  81. return(dat1);  
  82. }
  83. /*****************初始化ds1302**********************/
  84. void initial()
  85. {
  86. TMOD=0x01;
  87. TH1=(65536-56074)/256; //    11.05768M.
  88. TL1=(65536-56074)%256;
  89. EA=1;      //    打开中断允许
  90. ET1=1;      //     开起计数器0
  91. EX0=1;   
  92. }
  93. void delay(uint z)
  94. {
  95.   uint i,j;
  96. for(i=z;i>0;i--)
  97.   for(j=110;j>0;j--);
  98. }
  99. void display(uchar duan_i,uchar wela_i)
  100. {
  101.   duan=1;
  102.   P0=table[duan_i];
  103. duan=0;
  104. wei=1;
  105. P0=wela[wela_i];
  106. wei=0;
  107. delay(1);
  108. P0=0;
  109. }
  110. void display_1()
  111. {
  112.   display(shi/16,0);
  113. display(shi%16,1);
  114. display(10,2);
  115. display(fen/16,3);
  116. display(fen%16,4);
  117. display(10,5);
  118. display(miao/16,6);
  119. display(miao%16,7);
  120. }
  121. void display_2()
  122. {
  123.   while(m==3&&num2%2!=0)
  124. {
  125.    display(11,0);
  126.   display(11,1);
  127.   display(10,2);
  128.   display(fen/16,3);
  129.   display(fen%16,4);
  130.   display(10,5);
  131.   display(miao/16,6);
  132.   display(miao%16,7);
  133. }
  134. while(m==1&&num2%2!=0)
  135. {
  136.    display(shi/16,0);
  137.   display(shi%16,1);
  138.   display(10,2);
  139.   display(fen/16,3);
  140.   display(fen%16,4);
  141.   display(10,5);
  142.   display(11,6);
  143.   display(11,7);
  144.   
  145. }
  146. while(m==2&&num2%2!=0)
  147. {
  148.    display(shi/16,0);
  149.   display(shi%16,1);
  150.   display(10,2);
  151.   display(11,3);
  152.   display(11,4);
  153.   display(10,5);
  154.   display(miao/16,6);
  155.   display(miao%16,7);
  156. }
  157. while(num2%2==0)display_1();
  158. }
  159. char fun_1(uchar u)
  160. {
  161.   u=u/16*10+u%16;   //将十六进制转化为十进制
  162. u++;
  163. s=0;
  164. u>=60?u=0:u;
  165. u=u/10*16+u%10;   //将十进制转化为十六进制BCD
  166. return u;
  167. }
  168. void keyboard()
  169. {
  170. if(k1==0)
  171. delay(5);
  172. if(k1==0)
  173. {
  174.    if(TR1=1,m++,m==4)
  175.    {
  176.     m=0;
  177.    TR1=0;
  178.    }
  179.    while(!k1)display_1();
  180. }
  181. if(m!=0)
  182. {  
  183.   if(k2==0)
  184.   {
  185.     if(delay(5),s=1,m==1&&s!=0)
  186.    {
  187.     miao=fun_1(miao);
  188.     write1302(write_protect,0x00);
  189.     write1302(write_miao,miao);
  190.     write1302(write_protect,0x80);   
  191.    }
  192.    if(m==2&&s!=0)
  193.    { fen=fun_1(fen);
  194.     write1302(write_protect,0x00);
  195.     write1302(write_fen,fen);
  196.     write1302(write_protect,0x80);
  197.    }
  198.    if(m==3&&s!=0)
  199.    {
  200.     shi=shi/16*10+shi%16;
  201.     shi++;
  202.     shi>=24?shi=0:shi;
  203.     shi=shi/10*16+shi%10;
  204.     write1302(write_protect,0x00);
  205.     write1302(write_shi,shi);
  206.     write1302(write_protect,0x80);  
  207.    }
  208.    while(!k2)display_1();
  209.   }
  210. }
  211. }
  212. void main()
  213. {
  214. initial();
  215.   while(1)
  216. {
  217.   shi=read1302(read_shi);
  218.   fen=read1302(read_fen);
  219.   miao=read1302(read_miao);
  220.   keyboard();
  221. /* if(m!=0)
  222.   {  
  223.    write1302(write_protect,0x00);      //禁止写保护  
  224.    write1302(write_shi,shi);               
  225.    write1302(write_fen,fen);            
  226.    write1302(write_miao,miao);      
  227.    write1302(write_protect,0x80);      //允许写保护   
  228.   }   */
  229.   m!=0?display_2():display_1();
  230. }
  231. }
  232. void time_1() interrupt 3
  233. {
  234. if(TH1=(65536-56074)/256,TL1=(65536-56074)%256,num1++,num1==20)
  235. {
  236.    num1=0;
  237.   num2++;
  238. }
  239. }
  240.     说实话,我对这个程序不是很满意,因为这个程序在设置部分还有不足,比如说在设置的时候秒还在走;如果说要在设置的时候让时候不走,那么就得每时每刻都往1302中写入秒,但是写的时间又太长,从而导致了常亮部分也出现了闪烁的现象:
  241. /* if(m!=0)
  242.   {  
  243.    write1302(write_protect,0x00);      //禁止写保护  
  244.    write1302(write_shi,shi);               
  245.    write1302(write_fen,fen);            
  246.    write1302(write_miao,miao);      
  247.    write1302(write_protect,0x80);      //允许写保护   
  248.   }   */
  249.     我不知道为什么写的时间会有那么长,以致于出现了闪烁现象,之后我为了消除这个现象,就用了这个结构:
  250. if(k2==0)
  251.   {
  252.     if(delay(5),s=1,m==1&&s!=0)
  253.    {
  254.     miao=fun_1(miao);
  255.     write1302(write_protect,0x00);
  256.     write1302(write_miao,miao);
  257.     write1302(write_protect,0x80);   
  258.    }
  259.     但这个结构有不足之处,简单的说就是只有在做加法时才能读写入,其他时间都不会读写入。正因为这样才消除了闪烁,但是这个方法却带来了新的问题,由于这个程序只有在做加法时才能读写入所以在不读写入的时间里时间一直在走。
  260.     用1302做的时钟最吸引人眼球的就是在断电的时候不影响时间的进行,不过这需要备用电源的支持!
复制代码


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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