求教 关于DS1302时钟芯片时序程序(有尝的求救)

2020-02-02 09:29发布

这几天在弄个1602液晶显示时钟,用DS1302时钟芯片提供时间;但在中途出现了点小问题,
对着DS1302时序图写完这部分程序,想试试程序是否对,就用一个8个LED灯P0接收DS1302的数据,
如果8个LED灯发生变化说明没有对着时序图写的这部分驱动没问题,但发现并不这么理想,8个LED全亮(LED共阳),
没有发生变化,说明DS1302驱动程序那部分有问题;可调试了很久也不知道哪里出错了.望好心人大神们帮助下。
(很想知道哪里错了,有尝的求救,如果有意愿帮助指导下的,可以加我QQ1097957773,,谢谢)

以下是时序图:
QQ截图未命名.png (45.26 KB, 下载次数: 0) 下载附件 DS1302时序图 2012-9-8 13:07 上传

以下是DS1302驱动部分程序:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char

void DS1302_COMMAND_WRITE(uchar adr);
void DS1302_DATA_WRITE(uchar date);
void switch_10_16(uchar transition);
void delay(uint z);
void delaynop();

uchar receive_io,shift_date;

void delaynop()
{
        _nop_();
        _nop_();       
        _nop_();
        _nop_();
}

/***************************/
/*                                    */     
/*        DS1302时钟位定义     */
/*                               */
/***************************/
sbit RST=P2^0;                          
sbit io=P2^2;                          
sbit SCLK=P2^1;                          




/***************************************/
/*                                         */
/*                延时函数                            */
/*                                       */
/***************************************/
void delay(uint z)
{
        uint x,y;

        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}



/***************************************/
/*                                         */
/*        DS1302时钟写保护函数                            */
/*                                       */
/***************************************/
void WRITE_PROTECT(uchar protect)
{
        DS1302_COMMAND_WRITE(0x8E);//DS1302 CONTROL地址位
        DS1302_DATA_WRITE(protect);//DS1302 CONTROL数据位

}



/*****************************************/
/*                                            */
/*        DS1302时钟地址写操作函数               */
/*                                          */
/*****************************************/
void DS1302_COMMAND_WRITE(uchar adr)   
{                                                                               
    uchar i;                                                         
        RST=0;                                                                                                                                 
        SCLK=0;
        RST=1;        //高电平开始
        delaynop();                                                               
        for(i=0;i<8;i++)                                         
        {                                                                         
                adr=adr>>1;                                                 
                io=adr&0x80;//1位送入DS1302芯片中
                SCLK=1;//高脉冲送出数据
                delaynop();                                                                                                          
                SCLK=0;                                                         
                delaynop();                                               
        }                                                                         
                                                                                 
}        //地址写完 SCLK为低电平                                                               



/***************************************/
/*                                          */
/*        DS1302时钟数据写操作函数            */
/*                                       */
/***************************************/
void DS1302_DATA_WRITE(uchar date)          
{                                                                          
        uchar i;
        switch_10_16(date);        //数值转换                                           
        for(i=0;i<8;i++)                                  
        {       
                io=shift_date&0x01;        //低位与运算判断0和1                                                          
                shift_date=shift_date>>1;//转换后的数据进行移位传输                                                             
                SCLK=1;//高脉冲送出数据                                                  
                delaynop();                                                  
                SCLK=0;          
                delaynop();                                                  
        }
        SCLK=1;//写完后按时序图SCLK先置高
        RST=0;//RST在置低
}                                                                          



/***************************************/
/*                                          */
/*        DS1302时钟数据读操作函数            */
/*                                       */
/***************************************/
void DS1302_READ(uchar adre)
{
        uchar i;
        receive_io=0;
        DS1302_COMMAND_WRITE(adre);//写完地址后SCLK为低电平,需要一个高电平过度在开始读操作
        SCLK=1;
        delaynop();
        for(i=0;i<8;i++)
        {
                SCLK=0;//低脉冲读走数据
                delaynop();       
                receive_io=receive_io>>1;
                if(io==1)
                        {
                                receive_io=receive_io|0x80;  //高位补1
                        }
                else
                        {
                                receive_io=receive_io|0x00;  //高位补0
                        }
               
                       
        }
        SCLK=1;
        delaynop();
        RST=0;
}



/*****************************************/
/*                                            */
/*        DS1302时钟总体写操作函数               */
/*                                          */
/*****************************************/
void DS1302_WRITE(uchar adress,uchar dat)
{                                                                                  
   DS1302_COMMAND_WRITE(adress);                  
   DS1302_DATA_WRITE(dat);                                  
}                                                                                  



/***************************************/
/*                                         */
/*                发送_数值转换函数            */
/*                                       */
/***************************************/
void switch_10_16(uchar transition)
{
        uchar ge,shi;
        shi=transition/10;
        ge=transition%10;
        shift_date=shi*16+ge;
}


void main()
{
         WRITE_PROTECT(0x00);  //去除写保护
         delaynop();       
         DS1302_WRITE(0x80,13);
         delaynop();       
         while(1)
         {
                  DS1302_READ(0x81);
                 delaynop();       
                 P0=receive_io;       
         }
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
12条回答
xtl2012
2020-02-02 21:48
很想知道哪里错了


basically, very poor structuring of your code. you should try to use 1 routine to send a byte to the chip and another routine to read a byte from the chip, and build the rest of the routines from that.

try this one
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char

  5. void DS1302_COMMAND_WRITE(uchar adr);
  6. void DS1302_DATA_WRITE(uchar date);
  7. void switch_10_16(uchar transition);
  8. void delay(uint z);
  9. void delaynop();

  10. uchar receive_io,shift_date;

  11. void delaynop()
  12. {
  13.         _nop_();
  14.         _nop_();        
  15.         _nop_();
  16.         _nop_();
  17. }

  18. /***************************/
  19. /*                                    */     
  20. /*        DS1302?????     */
  21. /*                               */
  22. /***************************/
  23. sbit RST=P2^0;                           
  24. sbit io=P2^2;                           
  25. sbit SCLK=P2^1;                           




  26. /***************************************/
  27. /*                                         */
  28. /*                ????                            */
  29. /*                                       */
  30. /***************************************/
  31. void delay(uint z)
  32. {
  33.         uint x,y;

  34.         for(x=z;x>0;x--)
  35.                 for(y=110;y>0;y--);
  36. }

  37. //reset the bus
  38. void DS1302_INIT(void) {
  39.         RST=0;                                                                                                                                 
  40.         SCLK=0;
  41. }




  42. /*****************************************/
  43. /*                                            */
  44. /*        DS1302?????????               */
  45. /*                                          */
  46. /*****************************************/
  47. void DS1302_BYTE_WRITE(uchar adr)   
  48. {                                                                                
  49.     uchar i;                                                         
  50.         //RST=0;                                                                                                                                 
  51.         //SCLK=0;
  52.         //RST=1;        //?????
  53.         //delaynop();                                                               
  54.         for(i=0;i<8;i++)                                         
  55.         {                                                                        
  56.                                 SCLK = 0;                                        //reset sclk to idle
  57.                 //adr=adr>>1;                                                
  58.                 io=(adr&0x01)?1:0;//1???DS1302???
  59.                 //delaynop();                                                                                                         
  60.                 SCLK=1;//???????
  61.                                 adr = adr >> 1;
  62.                 //SCLK=0;                                                         
  63.                 //delaynop();                                                
  64.         }                                                                        
  65.                                                                                  
  66. }        //???? SCLK????                                                               

  67. uchar DS1302_BYTE_READ(void)   
  68. {
  69.         uchar adr=0;                                        //temp variable                                                                                
  70.     uchar i;                                                         
  71.         //RST=0;                                                                                                                                 
  72.         //SCLK=0;
  73.         //RST=1;        //?????
  74.         //delaynop();                                                               
  75.                 io = 1;                                                //wait to read io
  76.         for(i=0;i<8;i++)                                         
  77.         {                                                                        
  78.                                 SCLK = 0;                                        //reset sclk to idle
  79.                 adr=adr>>1;                                                
  80.                 //delaynop();                                                                                                         
  81.                                 adr |= (io)? 0x80: 0x00;        //=(adr&0x01)?1:0;//1???DS1302???
  82.                 SCLK=1;//???????
  83.                 //SCLK=0;                                                         
  84.                 //delaynop();                                                
  85.         }                                                                        
  86.         return adr;                                                //return the value
  87. }        //???? SCLK????  
  88.                                                               

  89. /***************************************/
  90. /*                                         */
  91. /*        DS1302???????                            */
  92. /*                                       */
  93. /***************************************/
  94. void xWRITE_PROTECT(uchar protect)
  95. {
  96.         DS1302_BYTE_WRITE(0x8E);//DS1302 CONTROL???
  97.         DS1302_BYTE_WRITE(protect);//DS1302 CONTROL???

  98. }



  99. /***************************************/
  100. /*                                          */
  101. /*        DS1302?????????            */
  102. /*                                       */
  103. /***************************************/
  104. void xDS1302_DATA_WRITE(uchar date)           
  105. {                                                                           
  106.         uchar i;
  107.         switch_10_16(date);        //????                                          
  108.         for(i=0;i<8;i++)                                   
  109.         {        
  110.                 io=shift_date&0x01;        //???????0?1                                                           
  111.                 shift_date=shift_date>>1;//????????????                                                              
  112.                 SCLK=1;//???????                                                   
  113.                 delaynop();                                                   
  114.                 SCLK=0;           
  115.                 delaynop();                                                   
  116.         }
  117.         SCLK=1;//???????SCLK???
  118.         RST=0;//RST???
  119. }                                                                           


  120. /***************************************/
  121. /*                                          */
  122. /*        DS1302?????????            */
  123. /*                                       */
  124. /***************************************/
  125. uchar DS1302_READ(uchar adre)
  126. {
  127.         uchar i;
  128.         //receive_io=0;
  129.         //reset the bus
  130.                 RST=0;                                                                                                                                 
  131.         SCLK=0;
  132.         RST=1;        //?????
  133.         DS1302_BYTE_WRITE(adre);//?????SCLK????,???????????????
  134.         i=DS1302_BYTE_READ();
  135.                 //return the bus to idle
  136.                 SCLK=0;
  137.                 RST=0;
  138.                 return i;                                //return the value
  139. /*                SCLK=1;
  140.         delaynop();
  141.         for(i=0;i<8;i++)
  142.         {
  143.                 SCLK=0;//???????
  144.                 delaynop();        
  145.                 receive_io=receive_io>>1;
  146.                 if(io==1)
  147.                         {
  148.                                 receive_io=receive_io|0x80;  //???1
  149.                         }
  150.                 else
  151.                         {
  152.                                 receive_io=receive_io|0x00;  //???0
  153.                         }
  154.                
  155.                         
  156.         }
  157.         SCLK=1;
  158.         delaynop();
  159.         RST=0;
  160. */

  161. }



  162. /*****************************************/
  163. /*                                            */
  164. /*        DS1302?????????               */
  165. /*                                          */
  166. /*****************************************/
  167. void DS1302_WRITE(uchar adress,uchar dat)
  168. {                                                                                 
  169.         //reset the bus
  170.                 RST=0;                                                                                                                                 
  171.         SCLK=0;
  172.         RST=1;        //?????
  173.    DS1302_BYTE_WRITE(adress);                  
  174.    DS1302_BYTE_WRITE(dat);                                 
  175.                 //return the bus to idle
  176.                 SCLK=0;
  177.                 RST=0;
  178. }                                                                                 



  179. /***************************************/
  180. /*                                         */
  181. /*                ??_??????            */
  182. /*                                       */
  183. /***************************************/
  184. uchar dec2bcd(uchar dec)
  185. {
  186. /*        uchar ge,shi;
  187.         shi=transition/10;
  188.         ge=transition%10;
  189.         shift_date=shi*16+ge;
  190. */
  191.         return ((dec / 10) << 4) | (dec % 10);
  192. }


  193. void main()
  194. {
  195.         DS1302_INIT();                                //reset the bus
  196.          //WRITE_PROTECT(0x00);  //?????
  197.                  DS1302_WRITE(0x8e, 0x00);
  198.          delaynop();        
  199.          DS1302_WRITE(0x80,13);
  200.          delaynop();        
  201.          while(1)
  202.          {
  203.                  receive_io= DS1302_READ(0x81);
  204.                  delaynop();        
  205.                  P0=receive_io;        
  206.          }
  207. }
复制代码it is largely based on yours, but not fully cleaned up.

一周热门 更多>