找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2269|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

為什么我的ds1302年月日只有年不正確,我用的是GT107D,看附件

[復(fù)制鏈接]
回帖獎(jiǎng)勵(lì) 10 黑幣 回復(fù)本帖可獲得 2 黑幣獎(jiǎng)勵(lì)! 每人限 1 次(中獎(jiǎng)概率 80%)
跳轉(zhuǎn)到指定樓層
樓主

  1. /**************main.c**************/
  2. 這是主函數(shù)數(shù)碼管顯示部分代碼
  3. /*  shijian[]={50, 59,23, 1, 1, 1,2018}  
  4.                        0    1   2   3  4  5    6
  5.                       秒  分  時(shí) 日  月  周   年
  6.                       應(yīng)顯示:
  7.                       2018 01  01  23 59  50
  8.                            6   4    3   2     1   0

  9. */
  10.                 if(xianshi==1) //年月日
  11.                 {
  12.                         yi=shijian[6]/1000;er=shijian[6]%1000/100;san=shijian[6]/10;si=shijian[6]%10;
  13.                         wu=shijian[4]/10;liu=shijian[4]%10;qi=shijian[3]/10;ba=shijian[3]%10;
  14.                 }
  15. /*************ds1302.c*******************/
  16. ds1302代碼我把所有的數(shù)據(jù)全改成了uint
  17. #include<stc15f2k60s2.h>
  18. #include <ds1302.h>
  19. #include <intrins.h>
  20. #define uchar unsigned char
  21. #define  uint unsigned int
  22. uint shijian[]={50,59,23,1, 1, 1, 2018};
  23. /*                     秒 分 時(shí) 日 月 周   年  */
  24. sbit SCK=P1^7;               
  25. sbit SDA=P2^3;           //  I/O口        
  26. sbit RST = P1^3;   // DS1302復(fù)位                                                                                                

  27. void Write_Ds1302_Byte(uint temp) //寫入
  28. {
  29.         uint i;
  30.         for (i=0;i<8;i++) //開始傳送八位地址命令            
  31.         {
  32.                 SCK=0;
  33.                 SDA=temp&0x01;  //數(shù)據(jù)從低位開始傳送
  34.                 temp>>=1;
  35.                 SCK=1;                                        //數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
  36.         }
  37. }   

  38. void Write_Ds1302( uint address,uint dat )     
  39. {
  40.          RST=0;   //將RST(CE)置低電平。
  41.         _nop_();
  42.          SCK=0;
  43.         _nop_();
  44.          RST=1;        
  45.   _nop_();  
  46.          Write_Ds1302_Byte(address);        //調(diào)用上面
  47.          Write_Ds1302_Byte(dat/10<<4|(dat%10));
  48.          RST=0;
  49. }

  50. unsigned int Read_Ds1302 ( uint address )
  51. {
  52.          uint i,temp=0x00,dat1,dat2;
  53.          RST=0;
  54.         _nop_();
  55.          SCK=0;
  56.         _nop_();
  57.          RST=1;
  58.         _nop_();
  59.          Write_Ds1302_Byte(address);
  60.          for (i=0;i<8;i++)         
  61.          {               
  62.                 SCK=0;
  63.                 temp>>=1;        
  64.                  if(SDA)
  65.                  temp|=0x80;        
  66.                  SCK=1;
  67.         }
  68.          RST=0; //讀取穩(wěn)定
  69.         _nop_();
  70.          RST=0;
  71.         SCK=0;
  72.         _nop_();
  73.         SCK=1;
  74.         _nop_();
  75.         SDA=0;
  76.         _nop_();
  77.         SDA=1;
  78.         _nop_();
  79.         
  80.         dat1=temp/16;   
  81.         dat2=temp%16;
  82.         temp=dat1*10+dat2;
  83.         return (temp);                        
  84. }
  85. void ds1302_init()
  86. {
  87.         uint add,i;
  88.         add=0x80;
  89.         Write_Ds1302(0x8e,0x00); //禁止寫保護(hù),關(guān)閉寫保護(hù) wp=1000  111 0
  90.   for(i=0;i<7;i++)         //寫 0x80 0x82 0x84 0x86 0x88 0x8a 0x8c
  91.         {
  92.                 Write_Ds1302(add,shijian[i]);
  93.                 add+=2;
  94.         }
  95.         Write_Ds1302(0x8e,0x80);  //打開寫保護(hù),使其不受外界影響
  96. }
  97. void ds1302_get()
  98. {
  99.         uint add,i;
  100.         add=0x81;               
  101.         Write_Ds1302(0x8e,0x00);//禁止寫保護(hù),關(guān)閉寫保護(hù) wp=1000  111 0
  102.   for(i=0;i<7;i++)        //讀 0x81 0x83 0x85 0x87 0x89 0x8b 0x8d
  103.         {
  104.                 shijian[i]=Read_Ds1302(add);
  105.                 add+=2;
  106.         }
  107.         Write_Ds1302(0x8e,0x80);//打開寫保護(hù),使其不受外界影響
  108. }
復(fù)制代碼


CT07D電路圖.pdf

97.23 KB, 下載次數(shù): 5

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:96682 發(fā)表于 2018-6-9 21:20 | 只看該作者
只能修改后面的源碼了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表