找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3329|回复: 4
收起左侧

各位大神 DS1302读数不对怎么破?

[复制链接]
ID:365752 发表于 2018-7-14 01:48 | 显示全部楼层 |阅读模式
100黑币
各位大神  小弟最近在学习DS1302和12864时遇到一个问题实在解决不了  经过测试12864能够正常使用  但是1302读数却不对 转换成十进制数 显示的第一秒是1 第二秒是0  第三秒是3  第四秒是0然后我又将二进制代码显示在12864上  第一秒是00000001  第二秒是00000000 第三秒是00000011  第四秒是00000000  所以我怀疑是1302根本就没读对 然后换了个1302芯片  但是结果是一样的  查了半天资料  就是不知道是怎么回事   希望大神们不吝赐教  万分感谢  以下是代码

这是1302的驱动
  1. /*
  2. ********************************************************************************
  3. **
  4. **写一个字节
  5. ********************************************************************************
  6. */

  7. void Write_Ds1302_Byte(unsigned  char temp)
  8. {
  9.         uchar i;
  10.         for (i=0;i<8;i++)
  11.         {
  12.                 SCK=0;
  13.                 SDA=temp&0x01;
  14.                 temp>>=1;
  15.                 SCK=1;
  16.         }
  17. }

  18. /*
  19. ********************************************************************************
  20. ** 写入DS1302
  21. ********************************************************************************
  22. */   

  23. void Write_Ds1302( uchar address,uchar dat )     
  24. {
  25.         RST=0;
  26.         _nop_();
  27.         SCK=0;
  28.         _nop_();
  29.         RST=1;       
  30.         _nop_();
  31.         Write_Ds1302_Byte(address);        //发送地址
  32.         Write_Ds1302_Byte(dat);                //发送数据
  33.         RST=0
  34. }

  35. /*
  36. ********************************************************************************
  37. **
  38. ** 读数据
  39. ********************************************************************************
  40. */

  41. uchar Read_Ds1302 ( uchar address )
  42. {
  43.         uchar i,temp=0x00;
  44.         RST=0;
  45.         _nop_();
  46.         _nop_();
  47.         SCK=0;
  48.         _nop_();
  49.         _nop_();
  50.         RST=1;
  51.         _nop_();
  52.         _nop_();
  53.         Write_Ds1302_Byte(address);
  54.         _nop_();
  55.         _nop_();
  56.         for (i=0;i<8;i++)
  57.         {               
  58.                 temp>>=1;       
  59.                 _nop_();
  60.                
  61.                 SCK=0;
  62.                 _nop_();
  63.                
  64.                 if(SDA)
  65.                         temp|=0x80;       
  66.                                 //óòòÆò»Î»
  67.                
  68.                 SCK=1;
  69.         }
  70.         RST=0;
  71.         _nop_();
  72.         _nop_();
  73.        

  74.         SCK=1;
  75.         _nop_();
  76.         _nop_();

  77.         SDA=1;
  78.         _nop_();
  79.         _nop_();
  80.         return (temp);
  81. }
  82. void Set_RTC(void)                    //设定日历
  83. {
  84.         uchar i,tmp;
  85.         for(i=0;i<7;i++)
  86.         {       //BCD处理
  87.                 tmp=l_tmpdate[i]/10;
  88.                 l_tmpdate[i]=l_tmpdate[i]%10;
  89.                 l_tmpdate[i]=l_tmpdate[i]+tmp*16;
  90.         }  
  91.         Write_Ds1302(0x8E,0X00);
  92.        

  93.         for(i=0;i<7;i++)                //7次写入  秒分时日月周年
  94.         {
  95.                   Write_Ds1302(write_rtc_address[i],l_tmpdate[i]);
  96.                  
  97.          }
  98.          Write_Ds1302(0x8E,0x80);
  99. }
  100. /*
  101. ********************************************************************************
  102. *
  103. ** 读时钟数据
  104. ********************************************************************************
  105. */

  106. void Read_RTC(void
  107. {
  108.         uchar i,*p;
  109.         p=read_rtc_address
  110.         for(i=0;i<7;i++)                    //7次读取 秒分时日月周年
  111.         {
  112.                 l_tmpdate[i]=Read_Ds1302(*p);
  113.                 p++;
  114.         }
  115. }
复制代码

这是各种定义
  1. #define uchar unsigned char
  2. #define uint  unsigned int
  3. sbit LCD_RS=P3^1;
  4. sbit LCD_RW=P3^6;
  5. sbit LCD_E=P3^5;

  6. //1302管脚定义
  7. sbit SCK=P3^4;
  8. sbit SDA=P3^3;
  9. sbit RST=P3^2;
  10. code uchar table1[]={"2018年07月12日"};
  11. code uchar table2[]={"  21: 45: 45"};
  12. code uchar table3[]={"星期四 ,28.9℃"};
  13. code uchar table4[][7]={"一","二","三","四","五","六","日"};
  14. code uchar table5[][2]={"-","."};
  15. code uchar num_tab[][10]={"0","1","2","3","4","5","6","7","8","9"};
  16. code uchar write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //1302写入地址
  17. code uchar read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //1302读取地址
  18. uchar l_tmpdate[7]={0,0,23,3,8,11,20};//秒分时日月周年2011-07-14 12:00:00
  19. uchar l_tmpdisplay[8];


复制代码
这是main函数
  1. #include "reg52.h"
  2. #include "defin.h"
  3. #include "lcd.h"
  4. #include "intrins.h"
  5. #include "ds1302.h"
  6. #include "18b02.h"

  7. void main()
  8. {
  9.         uint i=0,j=0;
  10.         delay(400);
  11.                
  12.         LED_init();
  13.         Set_RTC();
  14.        

  15.                 LED_wldate(0,0,table1);
  16.                 LED_wldate(1,0,table2);
  17.                 LED_wldate(2,0,table3);
  18.                 LED_wldate(3,0,table4);
  19.         delay(1000);
  20.         LED_wshi(23);
  21.         LED_wfen(23);
  22.         LED_wmiao(23);
  23.         LED_wnian(23);
  24.         LED_wyue(23);
  25.         LED_wri(23);
  26.         LED_wday(0);

  27.         while(1)
  28.          
  29.         {
  30.        
  31.                 Read_RTC();
  32.                 //显示秒的个位
  33.         /*        setpos(1,5);
  34.     j=l_tmpdate[0]%16;
  35.                
  36.                 LED_wldate(1,5,num_tab[j]);*/
  37.                 //显示秒的二进制
  38.                 for(i=0;i<8;i++)
  39.                 {
  40.                         j=l_tmpdate[0]&0x01;
  41.                         l_tmpdate[0]>>=1;
  42.                         setpos(3,7-i);
  43.                         delay(2);
  44.                         LED_wdate(j+0x30);
  45.                         delay(2);
  46.                 }
  47.        
  48.                 delay(5);
  49.                
  50.         }
  51.        
  52. }
复制代码

这是12864驱动
  1. void delay(unsigned int time)
  2. {
  3.   unsigned int i,j;
  4.   for(i=0;i<time;i++)
  5.     for(j=0;j<111;j++);
  6. }

  7. void LED_wcmd(uchar cmd)
  8. {
  9.         LCD_RS=0;
  10.         delay(1);
  11.         LCD_RW=0;
  12.         delay(1);
  13.         LCD_E=1;
  14.         delay(1);
  15.         P2=cmd;
  16.         delay(1);
  17.         LCD_E=0;
  18.         delay(1);
  19. }

  20. void LED_wdate(uchar date)
  21. {
  22.         LCD_RS=1;
  23.         delay(1);
  24.         LCD_RW=0;
  25.         delay(1);
  26.         LCD_E=1;
  27.         delay(1);
  28.         P2=date;
  29.         delay(1);
  30.         LCD_E=0;
  31.         delay(1);
  32. }
  33. void setpos(uchar x,uchar y)
  34. {
  35.         uchar x1;
  36.         switch (x)
  37.         {
  38.                 case 0:x1=0x80;break;
  39.                 case 1:x1=0x90;break;
  40.                 case 2:x1=0x88;break;
  41.                 case 3:x1=0x98;break;
  42.         }
  43.         x1=x1+y;
  44.         LED_wcmd(x1);
  45. }

  46. void LED_wldate(uchar x,uchar y,uchar code *date)
  47. {
  48.         uchar i=0;
  49.         setpos(x,y);
  50.         while(date[i]>=0x20)
  51.         {
  52.                 LED_wdate(date[i]);
  53.                 delay(3)        ;
  54.                 i++;
  55.                        
  56.         }
  57. }
  58.         void LED_wday(uchar x)
  59. {
  60.         uchar i=0;
  61.         setpos(2,2);
  62.         while(table4[x][i]>=0x20)
  63.         {
  64.                 LED_wdate(table4[x][i]);
  65.                 delay(3)        ;
  66.                 i++;
  67.                        
  68.         }
  69. }
  70. void LED_init()
  71. {
  72.         LED_wcmd(0x30);
  73.         delay (5);
  74.        
  75.         LED_wcmd(0x0c);
  76.         delay (5);
  77.         LED_wcmd(0x01);
  78.         delay (5);
  79.         LED_wcmd(0x06);
  80.         delay (5);
  81.        
  82.         delay (5);
  83.        
  84.         delay (5);
  85. }
  86. void LED_wmiao(uchar date)
  87. {
  88.         uchar shi,ge;
  89.         shi=date/10;
  90.         ge=date%10;
  91.         setpos(1,5);
  92.         LED_wdate(shi+0x30);
  93.         LED_wdate(ge+0x30);
  94. }
  95. void LED_wfen(uchar date)
  96. {
  97.         uchar shi,ge;
  98.         shi=date/10;
  99.         ge=date%10;
  100.         setpos(1,3);
  101.         LED_wdate(shi+0x30);
  102.         LED_wdate(ge+0x30);
  103. }
  104. void LED_wshi(uchar date)
  105. {
  106.         uchar shi,ge;
  107.         shi=date/10;
  108.         ge=date%10;
  109.         setpos(1,1);
  110.         LED_wdate(shi+0x30);
  111.        
  112.         LED_wdate(ge+0x30);
  113. }
  114. void LED_wnian(uchar date)
  115. {
  116.         uchar shi,ge;
  117.         shi=date/10;
  118.         ge=date%10;
  119.         setpos(0,1);
  120.         LED_wdate(shi+0x30);
  121.        
  122.         LED_wdate(ge+0x30);
  123. }
  124. void LED_wyue(uchar date)
  125. {
  126.         uchar shi,ge;
  127.         shi=date/10;
  128.         ge=date%10;
  129.         setpos(0,3);
  130.         LED_wdate(shi+0x30);
  131.        
  132.         LED_wdate(ge+0x30);
  133. }
  134. void LED_wri(uchar date)
  135. {
  136.         uchar shi,ge;
  137.         shi=date/10;
  138.         ge=date%10;
  139.         setpos(0,5);
  140.         LED_wdate(shi+0x30);
  141.        
  142.         LED_wdate(ge+0x30);
  143. }
复制代码



lcdTEST.zip

40.24 KB, 下载次数: 10

这是代码

回复

使用道具 举报

ID:365752 发表于 2018-7-14 09:55 | 显示全部楼层
问题已解决  在1302的三个引脚上加上10K的上啦电阻就OK了(手头没有电阻  我是直接接在51单片机的P0口上了)

评分

参与人数 1黑币 +1 收起 理由
NPC-1024 + 1 很给力!

查看全部评分

回复

使用道具 举报

ID:371423 发表于 2018-7-14 13:34 | 显示全部楼层
楼主请问一下,DS1302和DS1307以及DS12C887哪一个在程序编写上更好用啊
回复

使用道具 举报

ID:365752 发表于 2018-7-14 16:50 | 显示全部楼层
NPC-1024 发表于 2018-7-14 13:34
楼主请问一下,DS1302和DS1307以及DS12C887哪一个在程序编写上更好用啊

1307没用过,12c887功能比较强,因此初始化的时候操作的寄存器比1302多,但也就多几行代码,1302读的数据是bcd码,需要经过转换才能使用,而12c887不要转换,其实转换也就是几行代码的问题,因此从编程角度讲,两个都差不多,但是从硬件角度,个人感觉1302没有12887好搞,上面我说的问题我鼓捣了两天才搞定,但是1302是串口通讯不得不说,非常节省io口,我也是初学,这些是我的一些见解,不对勿喷
回复

使用道具 举报

ID:371423 发表于 2018-7-15 11:33 | 显示全部楼层
q2084054508 发表于 2018-7-14 16:50
1307没用过,12c887功能比较强,因此初始化的时候操作的寄存器比1302多,但也就多几行代码,1302读的数据 ...

但是我觉得DS12C887的引脚实在是太多了,想问一下那AD0~7的引脚有没有可能通过某种芯片减少对IO的占用啊?
另外想求一份DS12C887的操作函数,不知可否呀>v<
今天偶然看见有一个HT1380据说和DS1302是一样的引脚定义,操作函数可以通用,楼主试过这款芯片嘛?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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