標題: DS1302的時鐘電路(串行傳輸顯示方式) [打印本頁]

作者: 51黑ff    時間: 2016-10-10 12:59
標題: DS1302的時鐘電路(串行傳輸顯示方式)
分享一個51單片機DS1302的時鐘電路(串行傳輸顯示方式),下面是原理圖:


仿真工程文件及所有完整程序等資料下載地址:
http://www.torrancerestoration.com/bbs/dpj-56298-1.html
單片機源程序:
  1. /********************************************************************
  2.                             天馬電子
  3. *********************************************************************/
  4. #include <reg52.h>
  5. #include <stdio.h>
  6. #include<intrins.h>
  7. #define uchar unsigned char
  8. #define uint unsigned int
  9. //寫的時候,是低電平改變數據,上升沿送入數據
  10. //讀的時候,是低電平讀入數據,下降沿使得從設備把數據放到總線上
  11. //讀的時候,是低電平讀,但沒有讀出,提供下降沿使得從設備把數據放到總線上

  12. sbit P11=P1^1;        //定時和時鐘切換
  13. sbit P12=P1^2;        //時調整
  14. sbit P13=P1^3;        //分調整
  15. sbit P14=P1^4;        //秒調整

  16. sbit CLK =P3^5;
  17. sbit IO  =P3^6;
  18. sbit RST =P3^7;

  19. uchar m,m10,f,f10,s,s10;

  20. unsigned char data discode[10]={0XFC,0X60,0XDA,0XF2,0X66,0XB6,0XBE,0XE0,0XFE,0XF6};//共陰串行數組代表0~9

  21. unsigned char leddis[6]={1,2,3,4,5,6};//顯存,有幾個數碼管可以定義幾個

  22. /***************************延時函數*************************************/
  23. del()  /*延時0.2秒*/  
  24. {
  25. unsigned int i,j,k;
  26. for(i=100;i>0;i--)
  27. for(j=20;j>0;j--)
  28. for(k=248;k>0;k--);
  29. }


  30. /****************************************************************/
  31. /////////////////////DS1302////////////////////////
  32. //--------------WriteOneByteData-----------------/
  33. void WriteByte(uchar ucda)
  34.         {
  35.         uchar i;           
  36.         for(i=8;i>0;i--)
  37.                 {
  38.                 IO=(bit)(ucda&0x01);
  39.                 CLK=1;                  
  40.                 CLK=0;
  41.                 ucda>>=1;
  42.                 }
  43.         }

  44. //--------------ReadOneByteData-----------------/
  45. uchar ReadByte(void)
  46.     {
  47.         uchar i,a;
  48.         for(i=8;i>0;i--)
  49.                 {                                       
  50.                 a>>=1;
  51.                 if(IO)a|=0x80;
  52.                 CLK=1;           
  53.                 CLK=0;
  54.                 }        
  55.         return(a);
  56.         }
  57. //--------------WriteAddrFist,ThenWriteData-----------------/

  58. void write1302(uchar Addr,uchar Data)
  59.         {
  60.         RST=0;
  61.         CLK=0;
  62.         RST=1;
  63.         WriteByte(Addr);
  64.         WriteByte(Data);
  65.         CLK=1;
  66.         RST=0;
  67.         }

  68. //--------------WriteAddrFirst,ThenReadData-----------------/

  69. uchar read1302(uchar Addr)
  70.         {
  71.         uchar Data;
  72.         RST=0;
  73.         CLK=0;        
  74.         RST=1;
  75.         WriteByte(Addr);
  76.         Data=ReadByte();
  77.         CLK=1;
  78.         RST=0;
  79.         return(Data);
  80.         }


  81. /************************************************************************/
  82. ////////////////ds1302初始化//////////////////調整時間時用
  83. void csh1302(void)
  84.         {         
  85.         CLK=0;
  86.         RST=0;
  87.         write1302(0x8e,0x00);//打開寫保護
  88.         write1302(0x90,0x06);//禁止充電
  89. /////////////////////////////////////////////////
  90.         write1302(0x80,0x20);//預置秒
  91.         write1302(0x82,0x30);//預置分
  92.         write1302(0x84,0x08);//預置時
  93.         write1302(0x86,0x22);//預置日期
  94.         write1302(0x88,0x07);//預置月
  95.         //write1302(0x8a,0x02);//預置星期
  96.         write1302(0x8c,0x08);//預置年         
  97. ////////////////////////////////////////////////
  98.         write1302(0x80,0x00);//啟動時鐘
  99.         write1302(0x8e,0x80);//關閉寫保護
  100.         }
  101. /************************************************************************/
  102. //////////////////顯示函數////////////////////////////
  103. void xianshi(void)
  104. {
  105.         s10=read1302(0x85);
  106.         s=read1302(0x85)&0x0f;                //時
  107.         s10=s10>>4;
  108.         s10=s10&0x07;
  109.   ////////////////////////////////////
  110.     f10=read1302(0x83);                        //分
  111.         f=read1302(0x83)&0x0f;
  112.         f10=f10>>4;
  113.         f10=f10&0x07;
  114.   ////////////////////////////////////
  115.     m10=read1302(0x81);                         //秒
  116.         m=read1302(0x81)&0x0f;
  117.         m10=m10>>4;
  118.         m10=m10&0x07;
  119. }

  120. /**************************************************************************/
  121. void display(void)  //數碼管顯示函數
  122. {
  123.     unsigned char count;//數碼管個數
  124.         
  125.     for (count=6;count>0;count--)
  126.     {
  127.         SBUF=discode[leddis[count-1]];
  128.         while(TI==0);
  129.         TI=0;
  130.     }
  131. }   


  132. void show(void)         //顯示時間函數
  133. {
  134.         xianshi();
  135.         leddis[0]=s10;
  136.                 leddis[1]=s;
  137.                    leddis[2]=f10;
  138.                    leddis[3]=f;
  139.                 leddis[4]=m10;
  140.                    leddis[5]=m;
  141.         
  142.                 display();        //        顯示函數
  143. }
  144. /************************************************************************
  145.                                                    主函數
  146. ************************************************************************/

  147. void main()

  148. {
  149. SCON=0x00;        //設定UART的工作方式為MODEO
  150.            
  151. csh1302(); //初始化

  152. while(1)
  153. {
  154. show();
  155. del();
  156. }

  157. }

復制代碼









歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1