[STM32F1] DS1302不走了

[复制链接]
2813|17
 楼主| Cjy_JDxy 发表于 2017-11-28 18:30 | 显示全部楼层 |阅读模式
上程序:
#include "ds1302.h"
//#include "delay.h"

//³õʼ»¯Ê±¼ä¶¨Òå
u8 time_buf[8] = {0x20,0x17,0x01,0x01,0x23,0x59,0x55,0x02}; //³õʼʱ¼ä2010Äê6ÔÂ1ºÅ23µã59·Ö55Ãë ÐÇÆÚ¶þ
extern u8 Year,Mon,Day,Hour,Min,Sec;

void delay_us(unsigned int x)
{
        unsigned int i,j;
        for(i=0;i<x;i++)
                for(j=0;j<20;j++);
}
//DS1302³õʼ»¯
void DS1302_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_SetBits(GPIOA, GPIO_Pin_4);                 //PA4À­¸ß
        GPIO_ResetBits(GPIOA, GPIO_Pin_5 | GPIO_Pin_6); //PA5£¬PA6ÖõÍ
}

//ÏòDS1302дÈëÒ»¸ö×Ö½ÚÊý¾Ý
void DS1302_Write_Byte(u8 addr, u8 data)
{
  u8 i;
        DS1302_RST_L;      //Í£Ö¹DS1302×ÜÏß
        delay_us(10);
        DS1302_RST_H;      //Æô¶¯DS1302×ÜÏß
        addr = addr & 0xFE;  //×îµÍλÖÃÁ㣬дÊý¾Ý
        DS1302_IO_OUT();
        for(i = 0; i < 8; i ++)  //дµØÖ·
        {
          if (addr & 0x01)
                        DS1302_DATA_OUT_H;
                else
                        DS1302_DATA_OUT_L;
               
                DS1302_SCK_H;    //²úÉúʱÖÓ
                delay_us(10);
                DS1302_SCK_L;
                addr = addr>>1;
        }
        for (i = 0; i < 8; i ++) //дÊý¾Ý
        {
          if(data & 0x01)
                        DS1302_DATA_OUT_H;
                else
                        DS1302_DATA_OUT_L;
               
                DS1302_SCK_H;   //²úÉúʱÖÓ
                delay_us(10);
                DS1302_SCK_L;
          data = data>>1;
        }
        DS1302_RST_L;      //Í£Ö¹DS1302×ÜÏß
}

//´ÓDS1302¶Á³öÒ»¸ö×Ö½ÚÊý¾Ý
u8 DS1302_Read_Byte(u8 addr)
{
  u8 i,temp;
        DS1302_RST_L;      //Í£Ö¹DS1302×ÜÏß
        delay_us(10);
        DS1302_RST_H;      //Æô¶¯DS1302×ÜÏß
        addr = addr | 0x01;  //×îµÍλÖøߣ¬¶ÁÊý¾Ý
        DS1302_IO_OUT();
        for(i = 0; i < 8; i ++)  //дµØÖ·
        {
          if (addr & 0x01)
                        DS1302_DATA_OUT_H;
                else
                        DS1302_DATA_OUT_L;
               
                DS1302_SCK_H;    //²úÉúʱÖÓ
                delay_us(10);
                DS1302_SCK_L;
                addr = addr>>1;
        }
        DS1302_IO_IN();
        for (i = 0; i < 8; i ++) //¶ÁÊý¾Ý
        {
          temp = temp >> 1;
                if(DS1302_DATA_IN==1)
                        temp |= 0x80;
                else
                        temp &= 0x7F;
               
                DS1302_SCK_H;   //²úÉúʱÖÓ
                delay_us(10);
                DS1302_SCK_L;
        }
        DS1302_RST_L;      //Í£Ö¹DS1302×ÜÏß
        return temp;
}

//ÏòDS1302дÈëʱ¼äÊý¾Ý
void DS1302_Write_Time(void)
{
  DS1302_Write_Byte(ds1302_control_add, 0x00);      //¹Ø±Õд±£»¤
  DS1302_Write_Byte(ds1302_sec_add, 0x80);          //ÔÝͣʱÖÓ
        //DS1302_Write_Byte(ds1302_charger_add, 0xA9);      //ä¸Á÷³äµç
        DS1302_Write_Byte(ds1302_year_add,time_buf[1]);                //Äê
        DS1302_Write_Byte(ds1302_month_add,time_buf[2]);        //ÔÂ
        DS1302_Write_Byte(ds1302_date_add,time_buf[3]);                //ÈÕ
        DS1302_Write_Byte(ds1302_hr_add,time_buf[4]);                  //ʱ
        DS1302_Write_Byte(ds1302_min_add,time_buf[5]);                //·Ö
        DS1302_Write_Byte(ds1302_sec_add,time_buf[6]);                //Ãë
        DS1302_Write_Byte(ds1302_day_add,time_buf[7]);                //ÖÜ
        DS1302_Write_Byte(ds1302_control_add,0x80);                    //´ò¿ªÐ´±£»¤
}
//ÏòDS1302дÈëÈÕÆÚÊý¾Ý
void DS1302_Write_Date(void)
{
        Year=((Year/10)<<4)+((Year%10)&0x0f);
        Mon=((Mon/10)<<4)+((Mon%10)&0x0f);
        Day=((Day/10)<<4)+((Day%10)&0x0f);
        Hour=((Hour/10)<<4)+((Hour%10)&0x0f);
        Min=((Min/10)<<4)+((Min%10)&0x0f);
        Sec=((Sec/10)<<4)+((Sec%10)&0x0f);
  DS1302_Write_Byte(ds1302_control_add, 0x00);      //¹Ø±Õд±£»¤
  DS1302_Write_Byte(ds1302_sec_add, 0x80);          //ÔÝͣʱÖÓ
        //DS1302_Write_Byte(ds1302_charger_add, 0xA9);      //ä¸Á÷³äµç
        DS1302_Write_Byte(ds1302_year_add,Year);                //Äê
        DS1302_Write_Byte(ds1302_month_add,Mon);        //ÔÂ
        DS1302_Write_Byte(ds1302_date_add,Day);                //ÈÕ
        DS1302_Write_Byte(ds1302_hr_add,Hour);                  //ʱ
        DS1302_Write_Byte(ds1302_min_add,Min);                //·Ö
        DS1302_Write_Byte(ds1302_sec_add,Sec);                //Ãë
        DS1302_Write_Byte(ds1302_day_add,time_buf[7]);                //ÖÜ
        DS1302_Write_Byte(ds1302_control_add,0x80);                    //´ò¿ªÐ´±£»¤
}
//´ÓDS302¶Á³öʱÖÓÊý¾Ý
void DS1302_Read_Time(void)  
{
        time_buf[1] = DS1302_Read_Byte(ds1302_year_add);                   //Äê
        time_buf[2] = DS1302_Read_Byte(ds1302_month_add);                   //ÔÂ
        time_buf[3] = DS1302_Read_Byte(ds1302_date_add);                   //ÈÕ
        time_buf[4] = DS1302_Read_Byte(ds1302_hr_add);                     //ʱ
        time_buf[5] = DS1302_Read_Byte(ds1302_min_add);                     //·Ö
        time_buf[6] = (DS1302_Read_Byte(ds1302_sec_add))&0x7f; //Ã룬ÆÁ±ÎÃëµÄµÚ7룬±ÜÃⳬ³ö59
        time_buf[7] = DS1302_Read_Byte(ds1302_day_add);                     //ÖÜ        
}

//DS1302ÏòÉϲ㷵»ØÊ±¼äÊý¾Ý
void DS1302_Get_Time(u8 *time)
{
        DS1302_Read_Time();
        time[0]=(time_buf[0]>>4);   //Äê   
  time[1]=(time_buf[0]&0x0f);

  time[2]=(time_buf[1]>>4);   
  time[3]=(time_buf[1]&0x0f);

  time[4]=(time_buf[2]>>4);   //Ô  
  time[5]=(time_buf[2]&0x0f);

  time[6]=(time_buf[3]>>4);   //ÈÕ   
  time[7]=(time_buf[3]&0x0f);

  time[8]=(time_buf[7]&0x07); //ÐÇÆÚ

  time[9]=(time_buf[4]>>4);   //ʱ   
  time[10]=(time_buf[4]&0x0f);   

  time[11]=(time_buf[5]>>4);  //·Ö   
  time[12]=(time_buf[5]&0x0f);   

  time[13]=(time_buf[6]>>4);  //Ãë
  time[14]=(time_buf[6]&0x0f);
}








原来走的,后来用DS1302_Write_Date()这个程序设置了一下,就不走了。

用DS1302_Write_Time()再设置也不行。高手支招,谢谢!
天灵灵地灵灵 发表于 2017-11-28 19:43 | 显示全部楼层
重新恢复成之前的也不行了吗
天灵灵地灵灵 发表于 2017-11-28 19:43 | 显示全部楼层
断电重启试试看。
天灵灵地灵灵 发表于 2017-11-28 19:47 | 显示全部楼层
在ds1302.h文件中:
  1. #ifndef _STM32F103_DS1302_H_
  2. #define _STM32F103_DS1302_H_


  3. //*****************DS1302控制命令*******************
  4. #define   RdSec                                                  0x81
  5. #define   RdMin                                                  0x83
  6. #define   RdHour                                                  0x85
  7. #define   RdDate                                                  0x87
  8. #define   RdMonth                                                  0x89
  9. #define   RdWeek                                                  0x8b
  10. #define   RdYear                                                  0x8d
  11. #define   RdControl                                  0x8f
  12. #define   RdTrickleCharge                                 0x91
  13. #define   RdClockBurst                                  0xbf
  14. #define   WrSec                                                  0x80
  15. #define   WrMin                                                  0x82
  16. #define   WrHour                                                  0x84
  17. #define   WrDate                                                  0x86
  18. #define   WrMonth                                                  0x88
  19. #define   WrWeek                                                  0x8a
  20. #define   WrYear                                                  0x8c
  21. #define   WrControl                                 0x8e
  22. #define   WrTrickleCharge                                 0x90
  23. #define   WrClockBurst                                  0xbe
  24. //添加的信息
  25. #define   RdRamBurst                                         0xbf


  26. //相对应的IO口配置

  27. #define DS1302_PORT GPIOE

  28. #define DS1302_SCK_PIN GPIO_Pin_9                //对应的IO口
  29. #define DS1302_IO_PIN GPIO_Pin_10
  30. #define DS1302_CE_PIN GPIO_Pin_11



  31. #define DS1302_CLRSCK() (GPIO_ResetBits(GPIOE, GPIO_Pin_9))                //寄存器IO口操作状态
  32. #define DS1302_SETSCK() (GPIO_SetBits(GPIOE, GPIO_Pin_9))

  33. #define DS1302_CLRIO() (GPIO_ResetBits(GPIOE, GPIO_Pin_10) )
  34. #define DS1302_SETIO() (GPIO_SetBits(GPIOE, GPIO_Pin_10)  )

  35. #define DS1302_CLRCE() (GPIO_ResetBits(GPIOE, GPIO_Pin_11))
  36. #define DS1302_SETCE() (GPIO_SetBits(GPIOE, GPIO_Pin_11))


  37. void DS1302_IO_OUT(void );
  38. void DS1302_IO_IN( void);


  39. //#define DS1302_IO_IN()  DS1302_IO_IN()  //操作输入输出状态
  40. //#define DS1302_IO_OUT() DS1302_IO_OUT()

  41. #define DS1302_READ_SDA()    (GPIO_ReadInputDataBit(DS1302_PORT, DS1302_IO_PIN))



  42. //定义时间结构体
  43. //typedef struct
  44. //{
  45. //        unsigned char  year;
  46. //        unsigned char month;
  47. //        unsigned char date;
  48. //        unsigned char week;
  49. //        unsigned char hour;
  50. //        unsigned char min;
  51. //        unsigned char sec;
  52. //}TIME_TypeDef;

  53. typedef struct
  54. {
  55.         unsigned char  sec;
  56.         unsigned char min;
  57.         unsigned char hour;
  58.         unsigned char week;
  59.         unsigned char date;
  60.         unsigned char month;
  61.         unsigned char year;
  62. }TIME_TypeDef;
  63.        

  64. //内部函数
  65. void DS1302_Write8bit(unsigned char code);
  66. unsigned char DS1302_Read8bit(void);
  67. //外部函数
  68. extern void ds1302_init (void);
  69. extern unsigned char DS1302_ReadByte(unsigned char con);
  70. extern void DS1302_WriteByte(unsigned char con,unsigned char code);

  71. extern void DS1302_WriteTime(TIME_TypeDef* time);
  72. extern void DS1302_ReadTime(TIME_TypeDef* time);


  73. void time_convert(TIME_TypeDef *time_get);

  74. #endif
天灵灵地灵灵 发表于 2017-11-28 19:47 | 显示全部楼层
在ds1302.c文件中:
  1. /********************************copyright ythuitong by wit_yuan**************************/

  2. #include "bsp.h"

  3. //////////////////////////////////////////////////////////////////////////
  4. //                                 函数名                :                ds1302_init               
  5. //                                   功能                :                ds1302初始化部分
  6. //                                参数                :                void
  7. //                                作者                :                wit_yuan
  8. //                                时间                :                2014-08-08
  9. ////////////////////////////////////////////////////////////////////////////
  10. void ds1302_init(void)
  11. {
  12.         GPIO_InitTypeDef GPIO_InitStructure;

  13.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE );          
  14.         GPIO_InitStructure.GPIO_Pin = (DS1302_SCK_PIN | DS1302_IO_PIN | DS1302_CE_PIN);
  15.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   
  16.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17.         GPIO_Init(DS1302_PORT, &GPIO_InitStructure);

  18.         DS1302_WriteByte(WrControl,0x00);  //关闭写保护,可以写入数据了       

  19.         Delay_10us(10);
  20. //        if(DS1302_ReadByte(RdTrickleCharge) != 0xA6)
  21. //        {
  22. //                Delay_10us(10);
  23. //                DS1302_WriteByte(WrTrickleCharge,0xA6);
  24. //
  25. //                printf("进入\r\n");
  26. //        }       
  27.         Delay_10us(10);
  28.         DS1302_WriteByte(WrControl,0x80);  //开启写保护,禁止写入数据
  29. }


  30. void DS1302_IO_OUT()
  31. {
  32.     GPIO_InitTypeDef GPIO_InitStructure;                                                               
  33.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;           
  34.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  35.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  36.                        
  37.     GPIO_Init(GPIOE, &GPIO_InitStructure);         
  38. }

  39. void DS1302_IO_IN()
  40. {
  41.     GPIO_InitTypeDef GPIO_InitStructure;                                                               
  42.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;           
  43.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;      
  44.                        
  45.     GPIO_Init(GPIOE, &GPIO_InitStructure);         
  46. }



  47. //DS1302写入8bit
  48. void DS1302_Write8bit(unsigned char code)
  49. {
  50.         unsigned char i;
  51.         DS1302_IO_OUT();                                                //输出模式
  52.         DS1302_CLRSCK();                                                //SCK = 0
  53.         for(i=0;i<8;i++)
  54.         {
  55.                   Delay_10us(5);
  56.                   if(code&0x01)
  57.                                 (DS1302_SETIO());                        //I/0  = 1
  58.                   else
  59.                                 (DS1302_CLRIO());                        //I/0 = 0
  60.                   Delay_10us(5);
  61.                   
  62.                   DS1302_SETSCK();                                //SCLK  = 1
  63.                   Delay_10us(5);
  64.                                                                                                                        
  65.                   DS1302_CLRSCK();                                //SCLK = 0
  66.                   code = code >> 1;
  67.         }
  68. }

  69. //DS1302读取8bit的数据
  70. unsigned char DS1302_Read8bit(void)
  71. {
  72.         unsigned char i,code;
  73.         unsigned char temp;
  74.         DS1302_IO_IN();
  75.         code = 0;
  76.         DS1302_CLRSCK();                                                //SCLK = 0

  77.         Delay_10us(5);

  78.         for(i=0;i<8;i++)
  79.         {
  80.                        
  81.                   code = code >>1;
  82.                        
  83.                   if(DS1302_READ_SDA())
  84.                         {
  85.                                 code = code | 0x80;
  86.                         }       
  87.                                        
  88.                                
  89.                                        
  90.                   Delay_10us(5);
  91.                   DS1302_SETSCK();                        //SCLK = 1
  92.                   Delay_10us(5);
  93.        
  94.                   DS1302_CLRSCK();                        //SCLK = 0

  95.         }
  96.        
  97.         temp = code /16;
  98.         code = code % 16;
  99.         code = code + temp * 10;                        //数据的相关转换
  100.        
  101.         return code;
  102. }



  103. //读取DS1302指定的1Byte
  104. unsigned char DS1302_ReadByte(unsigned char con)
  105. {
  106.         unsigned char code;
  107.         DS1302_CLRCE();                   //关闭DS1302                                        //CE = 0
  108.         Delay_10us(5);      
  109.         DS1302_CLRSCK();                                                                                                        //SCLK = 0
  110.         Delay_10us(5);
  111.         DS1302_SETCE();          //使能DS1302                        //CE = 1;
  112.         Delay_10us(5);
  113.         DS1302_Write8bit(con);   //读取代码                                //发送地址
  114.         code = DS1302_Read8bit();  //返回读取数字
  115.        
  116.         //printf("code = %d\r\n" ,code );
  117.         Delay_10us(5);
  118.         DS1302_SETSCK();                                                                                                        //SCLK = 1
  119.         Delay_10us(5);
  120.         DS1302_CLRCE();         //关闭DS1302                        //CE = 0
  121.         return code;
  122. }


  123. //u8  DS1302_ReadRam(u8 addr)
  124. //{
  125. //        u8        tmp,res;
  126. //       
  127. //        tmp = addr;
  128. //        res = DS1302_ReadByte(tmp);
  129. //        return(res);       
  130. //}

  131. ////连续读取DS1302所有数据
  132. //void DS1302_ReadBurst(unsigned char *rstr)
  133. //{
  134. //        int i = 0;
  135. //        unsigned char code;
  136. //        DS1302_CLRCE();                                           //关闭DS1302                                        //CE = 0
  137. //        Delay_10us(5);      
  138. //        DS1302_CLRSCK();                                                                                                        //SCLK = 0
  139. //        Delay_10us(5);
  140. //        DS1302_SETCE();                          //使能DS1302                        //CE = 1;
  141. //        Delay_10us(5);
  142. //        DS1302_Write8bit(RdRamBurst);   //读取代码                                //发送地址
  143. //       
  144. //           for(i = 0 ;i < 31 ; i++)
  145. //        {
  146. //                   rstr[i] = DS1302_ReadRam(2 * i + 1 + 0xc0);                  //返回读取数字
  147. //        }
  148. //       
  149. //        //printf("code = %d\r\n" ,code );
  150. //        Delay_10us(5);
  151. //        DS1302_SETSCK();                                                                                                        //SCLK = 1
  152. //        Delay_10us(5);
  153. //        DS1302_CLRCE();         //关闭DS1302                        //CE = 0
  154. //
  155. //        for(i = 0 ;i < 31 ; i ++)
  156. //        {
  157. //                 printf("rstr[%d] = %d\r\n",i,rstr[i]);
  158. //        }
  159. //
  160. //}





  161. //写DS1302指定的1Byte
  162. void DS1302_WriteByte(unsigned char con,unsigned char code)
  163. {
  164.         DS1302_CLRCE();                         //关闭DS1302                //CE = 0
  165.         Delay_10us(5);
  166.         DS1302_CLRSCK();                                                                                                                //SCK = 0
  167.         Delay_10us(5);
  168.        
  169.         DS1302_SETCE();                          //使能DS1302                //CE = 1
  170.         Delay_10us(5);
  171.         DS1302_Write8bit(con);             //写控制命令                //发送地址
  172.         DS1302_Write8bit(code);                 //写入数据                        //发送数据
  173.         Delay_10us(5);
  174.         DS1302_SETSCK();
  175.         Delay_10us(5);
  176.         DS1302_CLRCE();          //关闭DS1302

  177. }


  178. //写入时间
  179. void DS1302_WriteTime(TIME_TypeDef* time)
  180. {
  181.         DS1302_WriteByte(WrControl,0x00);                  //关闭写保护,可以写入数据
  182.                
  183.         DS1302_WriteByte(WrYear,time->year);
  184.         DS1302_WriteByte(WrMonth,time->month);
  185.         DS1302_WriteByte(WrDate,time->date);
  186.         DS1302_WriteByte(WrWeek,time->week);
  187.         DS1302_WriteByte(WrHour,time->hour);
  188.         DS1302_WriteByte(WrMin,time->min);
  189.         DS1302_WriteByte(WrSec,time->sec);

  190.         DS1302_WriteByte(WrControl,0x80);                  //开启写保护,禁止写入数据

  191. }

  192. u8  DS1302_ReadRam(u8 addr)
  193. {
  194.         u8        tmp,res;
  195.        
  196.         tmp = addr;
  197.         res = DS1302_ReadByte(tmp);
  198.         return(res);       
  199. }

  200. typedef struct
  201. {
  202.         unsigned char  sec;
  203.         unsigned char min;
  204.         unsigned char hour;
  205.         unsigned char date;
  206.         unsigned char month;
  207.         unsigned char week;
  208.         unsigned char year;
  209. }Time;








  210. //连续读取DS1302所有数据
  211. void DS1302_ReadBurst(unsigned char *rstr)
  212. {
  213.         int i = 0;
  214.         unsigned char code;
  215.         DS1302_CLRCE();                                           //关闭DS1302                                        //CE = 0
  216.         Delay_10us(5);      
  217.         DS1302_CLRSCK();                                                                                                        //SCLK = 0
  218.         Delay_10us(5);
  219.         DS1302_SETCE();                          //使能DS1302                        //CE = 1;
  220.         Delay_10us(5);
  221.         DS1302_Write8bit(RdRamBurst);   //读取代码                                //发送地址
  222.        
  223.            for(i = 0 ;i < 7 ; i++)
  224.         {
  225.                 rstr[i] = DS1302_Read8bit();
  226.         }
  227.        
  228.         //printf("code = %d\r\n" ,code );
  229.         Delay_10us(5);
  230.         DS1302_SETSCK();                                                                                                        //SCLK = 1
  231.         Delay_10us(5);
  232.         DS1302_CLRCE();         //关闭DS1302                        //CE = 0

  233. //        for(i = 0 ;i < 7 ; i ++)
  234.         {
  235.                  //printf("rstr[%d] = %d\r\n",i,rstr[i]);
  236.         }

  237. }
  238. //////////////测试而已/////////////////////////////////////////////////////
  239. void readTimeTest()
  240. {
  241.         Time myTime;
  242.         DS1302_ReadBurst((u8 *)&myTime);

  243.         //printf("time:%d-%d-%d %d:%d:%d\r\n",myTime.year, myTime.month, myTime.date,
  244.         //        myTime.hour,myTime.min,myTime.sec);

  245. }

  246. void DS1302_ReadTime(TIME_TypeDef* time)
  247. {
  248.         Time myTime;
  249.         DS1302_ReadBurst((u8 *)&myTime);
  250.        
  251.         time->year         = myTime.year;
  252.         time->month = myTime.month;       
  253.         time->date         = myTime.date;

  254.         time->hour         = myTime.hour;
  255.         time->min         = myTime.min;
  256.         time->sec         = myTime.sec;

  257. //        printf("time:%d-%d-%d %d:%d:%d\r\n",myTime.year, myTime.month, myTime.date,
  258. //                myTime.hour,myTime.min,myTime.sec);
  259. }

  260. ////读出时间
  261. //void DS1302_ReadTime(TIME_TypeDef* time)
  262. //{
  263. //        u8 i = 0;
  264. //        u8 year_temp[3];
  265. //        u8 month_temp[3];               
  266. //        u8 date_temp[3];
  267. //        u8 hour_temp[3];
  268. //        u8 min_temp[3];
  269. //        u8 sec_temp[3];
  270. //
  271. //        u8 max;
  272. //        u8 min;
  273. //
  274. //        for(i = 0 ; i < 3 ; i ++)
  275. //        {
  276. //                year_temp[i]  =  DS1302_ReadByte(RdYear);
  277. //                month_temp[i] =  DS1302_ReadByte(RdMonth);
  278. //                date_temp[i]  =  DS1302_ReadByte(RdDate);
  279. //                hour_temp[i]  =  DS1302_ReadByte(RdHour);
  280. //                min_temp[i]   =  DS1302_ReadByte(RdMin);
  281. //                sec_temp[i]   =  DS1302_ReadByte(RdSec);
  282. //        }
  283. //        //年
  284. //        max = year_temp[0];
  285. //        min = year_temp[0];
  286. //        for(i = 1 ; i < 3 ; i ++)
  287. //        {
  288. //                if(year_temp[i] > max)
  289. //                {
  290. //                        max = year_temp[i];
  291. //                }
  292. //                if(year_temp[i] < min)
  293. //                {
  294. //                        min = year_temp[i];
  295. //                }
  296. //        }
  297. //        time->year = year_temp[0] + year_temp[1] + year_temp[2] - max - min;
  298. //        //月
  299. //        max = month_temp[0];
  300. //        min = month_temp[0];
  301. //        for(i = 1 ; i < 3 ; i ++)
  302. //        {
  303. //                if(month_temp[i] > max)
  304. //                {
  305. //                        max = month_temp[i];
  306. //                }
  307. //                if(month_temp[i] < min)
  308. //                {
  309. //                        min = month_temp[i];
  310. //                }
  311. //        }
  312. //        time->month = month_temp[0] + month_temp[1] + month_temp[2] - max - min;
  313. //        //日
  314. //        max = date_temp[0];
  315. //        min = date_temp[0];
  316. //        for(i = 1 ; i < 3 ; i ++)
  317. //        {
  318. //                if(date_temp[i] > max)
  319. //                {
  320. //                        max = date_temp[i];
  321. //                }
  322. //                if(date_temp[i] < min)
  323. //                {
  324. //                        min = date_temp[i];
  325. //                }
  326. //        }
  327. //        time->date = date_temp[0] + date_temp[1] + date_temp[2] - max - min;
  328. //        //时
  329. //        max = hour_temp[0];
  330. //        min = hour_temp[0];
  331. //        for(i = 1 ; i < 3 ; i ++)
  332. //        {
  333. //                if(hour_temp[i] > max)
  334. //                {
  335. //                        max = hour_temp[i];
  336. //                }
  337. //                if(hour_temp[i] < min)
  338. //                {
  339. //                        min = hour_temp[i];
  340. //                }
  341. //        }
  342. //        time->hour = hour_temp[0] + hour_temp[1] + hour_temp[2] - max - min;       
  343. //
  344. //        //分
  345. //        max = min_temp[0];
  346. //        min = min_temp[0];
  347. //        for(i = 1 ; i < 3 ; i ++)
  348. //        {
  349. //                if(min_temp[i] > max)
  350. //                {
  351. //                        max = min_temp[i];
  352. //                }
  353. //                if(min_temp[i] < min)
  354. //                {
  355. //                        min = min_temp[i];
  356. //                }
  357. //        }
  358. //        time->min = min_temp[0] + min_temp[1] + min_temp[2] - max - min;
  359. //
  360. //        //秒
  361. //        max = sec_temp[0];
  362. //        min = sec_temp[0];
  363. //        for(i = 1 ; i < 3 ; i ++)
  364. //        {
  365. //                if(sec_temp[i] > max)
  366. //                {
  367. //                        max = sec_temp[i];
  368. //                }
  369. //                if(sec_temp[i] < min)
  370. //                {
  371. //                        min = sec_temp[i];
  372. //                }
  373. //        }
  374. //        time->sec = sec_temp[0] + sec_temp[1] + sec_temp[2] - max - min;
  375. //
  376. ////        time->year         = DS1302_ReadByte(RdYear);
  377. ////        time->month = DS1302_ReadByte(RdMonth);       
  378. ////        time->date         = DS1302_ReadByte(RdDate);
  379. ////        time->week         = DS1302_ReadByte(RdWeek);
  380. ////
  381. ////        time->hour         = DS1302_ReadByte(RdHour);
  382. ////        time->min         = DS1302_ReadByte(RdMin);
  383. ////        time->sec         = DS1302_ReadByte(RdSec);
  384. //
  385. ////        year_temp  =  DS1302_ReadByte(RdYear);
  386. ////        year_month =  DS1302_ReadByte(RdMonth);
  387. ////        year_date  =  DS1302_ReadByte(RdDate);
  388. ////        year_hour  =  DS1302_ReadByte(RdHour);
  389. ////        year_min   =  DS1302_ReadByte(RdMin);
  390. ////        year_sec   =  DS1302_ReadByte(RdSec);
  391. //
  392. ////        printf("year = %d\r\n",time->year);
  393. ////        printf("month = %d\r\n",time->month);
  394. ////        printf("year = %d\r\n",time->date);
  395. ////        printf("year = %d\r\n",time->hour);
  396. ////        printf("year = %d\r\n",time->min);
  397. ////        printf("year = %d\r\n",time->sec);
  398. ////
  399. ////        printf("year_temp = %d\r\n",year_temp);
  400. ////        printf("year_month = %d\r\n",year_month);
  401. ////        printf("year_date = %d\r\n",year_date);
  402. ////        printf("year_hour = %d\r\n",year_hour);
  403. ////        printf("year_min = %d\r\n",year_min);
  404. ////        printf("year_sec = %d\r\n",year_sec);
  405. //
  406. //
  407. ////        if(        (year_temp + year_month + year_date + year_hour + year_min + year_sec)
  408. ////                < ( time->year + time->month +  ))
  409. ////        {
  410. ////       
  411. ////        }
  412. //}
  413.        
  414.        
  415. unsigned char time[20]="\0";

  416. void time_convert(TIME_TypeDef *time_get)
  417. {
  418.         time[0] = '2';                                                                                                                        //2
  419.         time[1] = '0';                                                                                                                        //0
  420.         time[2] = time_get->year / 10 + '0';                //1
  421.         time[3] = time_get->year % 10 + '0';        //4
  422.         time[4] = '-';                                    //-
  423.         time[5] = time_get->month / 10 + '0';                                //0
  424.         time[6] = time_get->month % 10 + '0';       //4
  425.         time[7] = '-';                                                                                                                        //-
  426.        
  427.         time[8] = time_get->date / 10 + '0';        //1
  428.         time[9] = time_get->date % 10 + '0';              //0
  429.         time[10] = ' ';                             //
  430.         time[11] = time_get->hour / 10 + '0';       //1
  431.         time[12] = time_get->hour % 10 + '0';       //4
  432.         time[13] = ':';                                                                                                                        //:
  433.         time[14] = time_get->min / 10 + '0';        //2
  434.         time[15] = time_get->min % 10 + '0';              //1
  435.         time[16] = ':';                                                                                                                        //:
  436.         time[17] = time_get->sec / 10 + '0';                    //3
  437.         time[18] = time_get->sec % 10 + '0';              //0
  438.         time[19] = '\0';                                                                                                                        //
  439.        
  440.        
  441. //        printf("time = %s\r\n",time);
  442. }


  443. /************************************end of file 2014-08-08*********************************/


天灵灵地灵灵 发表于 2017-11-28 19:47 | 显示全部楼层
注意事项:
        ds1302属于获取时间的芯片,因此,在获取时间的时候,如果要一个个按顺序读取时间,时间会出错,因为,如果先拿到分寄存器时间的时候,刚好秒从59--->00这个时候,就会出现慢一分钟,如果先拿秒,会出现快一分钟,那么获取时间这种方式不可取,只能使用一种突发模式!!!!外部函数调用的时候,只需要DS1302_ReadTime()即可。写的时候,突发模式或者不突发都可以。
天灵灵地灵灵 发表于 2017-11-28 19:48 | 显示全部楼层
从网上找的可以用的,拿走用个试试吧。
 楼主| Cjy_JDxy 发表于 2017-11-29 08:33 | 显示全部楼层
天灵灵地灵灵 发表于 2017-11-28 19:43
重新恢复成之前的也不行了吗

不行了
 楼主| Cjy_JDxy 发表于 2017-11-29 08:34 | 显示全部楼层
天灵灵地灵灵 发表于 2017-11-28 19:48
从网上找的可以用的,拿走用个试试吧。

谢谢
 楼主| Cjy_JDxy 发表于 2017-11-29 10:03 | 显示全部楼层
好了。设置秒的时候,最高位要清零。Sec&=0x7f
 楼主| Cjy_JDxy 发表于 2017-11-29 13:36 | 显示全部楼层
Cjy_JDxy 发表于 2017-11-29 10:03
好了。设置秒的时候,最高位要清零。Sec&=0x7f

又不行了,真郁闷
 楼主| Cjy_JDxy 发表于 2017-11-29 13:58 | 显示全部楼层
我怕虚焊,重新过一下锡,发现走了,可是走的太快了
mcuzone 发表于 2017-11-29 14:40 | 显示全部楼层
走太快就检查晶振和起振电路,话说DS1302之类RTC国产仿货比较多
 楼主| Cjy_JDxy 发表于 2017-11-29 15:07 | 显示全部楼层
mcuzone 发表于 2017-11-29 14:40
走太快就检查晶振和起振电路,话说DS1302之类RTC国产仿货比较多

好了。是我感觉快,感觉不靠谱。实测和电脑时间差不多。
捉虫天师 发表于 2017-11-29 15:30 | 显示全部楼层
最后那个是什么问题导致不走的?
 楼主| Cjy_JDxy 发表于 2017-11-29 16:02 | 显示全部楼层
捉虫天师 发表于 2017-11-29 15:30
最后那个是什么问题导致不走的?

可能电路虚焊吧
捉虫天师 发表于 2017-11-29 16:32 | 显示全部楼层

好吧,之前好用,你加 了程序不好用,还以为是锁死老牛饿。
一路向北lm 发表于 2017-11-29 21:10 | 显示全部楼层
1302很好操作,底层时序对的话,然后就那读写操作了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:绿水本无忧因风皱面,青山原不老为雪白头。

553

主题

3530

帖子

20

粉丝
快速回复 在线客服 返回列表 返回顶部