打印
[STM32F1]

DS1302不走了

[复制链接]
2118|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文件中:
#ifndef _STM32F103_DS1302_H_
#define _STM32F103_DS1302_H_


//*****************DS1302控制命令*******************
#define   RdSec                                                  0x81
#define   RdMin                                                  0x83
#define   RdHour                                                  0x85
#define   RdDate                                                  0x87
#define   RdMonth                                                  0x89
#define   RdWeek                                                  0x8b
#define   RdYear                                                  0x8d
#define   RdControl                                  0x8f
#define   RdTrickleCharge                                 0x91
#define   RdClockBurst                                  0xbf
#define   WrSec                                                  0x80
#define   WrMin                                                  0x82
#define   WrHour                                                  0x84
#define   WrDate                                                  0x86
#define   WrMonth                                                  0x88
#define   WrWeek                                                  0x8a
#define   WrYear                                                  0x8c
#define   WrControl                                 0x8e
#define   WrTrickleCharge                                 0x90
#define   WrClockBurst                                  0xbe
//添加的信息
#define   RdRamBurst                                         0xbf


//相对应的IO口配置

#define DS1302_PORT GPIOE

#define DS1302_SCK_PIN GPIO_Pin_9                //对应的IO口
#define DS1302_IO_PIN GPIO_Pin_10
#define DS1302_CE_PIN GPIO_Pin_11



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

#define DS1302_CLRIO() (GPIO_ResetBits(GPIOE, GPIO_Pin_10) )
#define DS1302_SETIO() (GPIO_SetBits(GPIOE, GPIO_Pin_10)  )

#define DS1302_CLRCE() (GPIO_ResetBits(GPIOE, GPIO_Pin_11))
#define DS1302_SETCE() (GPIO_SetBits(GPIOE, GPIO_Pin_11))


void DS1302_IO_OUT(void );
void DS1302_IO_IN( void);


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

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



//定义时间结构体
//typedef struct
//{
//        unsigned char  year;
//        unsigned char month;
//        unsigned char date;
//        unsigned char week;
//        unsigned char hour;
//        unsigned char min;
//        unsigned char sec;
//}TIME_TypeDef;

typedef struct
{
        unsigned char  sec;
        unsigned char min;
        unsigned char hour;
        unsigned char week;
        unsigned char date;
        unsigned char month;
        unsigned char year;
}TIME_TypeDef;
       

//内部函数
void DS1302_Write8bit(unsigned char code);
unsigned char DS1302_Read8bit(void);
//外部函数
extern void ds1302_init (void);
extern unsigned char DS1302_ReadByte(unsigned char con);
extern void DS1302_WriteByte(unsigned char con,unsigned char code);

extern void DS1302_WriteTime(TIME_TypeDef* time);
extern void DS1302_ReadTime(TIME_TypeDef* time);


void time_convert(TIME_TypeDef *time_get);

#endif

使用特权

评论回复
5
天灵灵地灵灵| | 2017-11-28 19:47 | 只看该作者
在ds1302.c文件中:
/********************************copyright ythuitong by wit_yuan**************************/

#include "bsp.h"

//////////////////////////////////////////////////////////////////////////
//                                 函数名                :                ds1302_init               
//                                   功能                :                ds1302初始化部分
//                                参数                :                void
//                                作者                :                wit_yuan
//                                时间                :                2014-08-08
////////////////////////////////////////////////////////////////////////////
void ds1302_init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

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

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

        Delay_10us(10);
//        if(DS1302_ReadByte(RdTrickleCharge) != 0xA6)
//        {
//                Delay_10us(10);
//                DS1302_WriteByte(WrTrickleCharge,0xA6);
//
//                printf("进入\r\n");
//        }       
        Delay_10us(10);
        DS1302_WriteByte(WrControl,0x80);  //开启写保护,禁止写入数据
}


void DS1302_IO_OUT()
{
    GPIO_InitTypeDef GPIO_InitStructure;                                                               
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;           
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                       
    GPIO_Init(GPIOE, &GPIO_InitStructure);         
}

void DS1302_IO_IN()
{
    GPIO_InitTypeDef GPIO_InitStructure;                                                               
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;           
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;      
                       
    GPIO_Init(GPIOE, &GPIO_InitStructure);         
}



//DS1302写入8bit
void DS1302_Write8bit(unsigned char code)
{
        unsigned char i;
        DS1302_IO_OUT();                                                //输出模式
        DS1302_CLRSCK();                                                //SCK = 0
        for(i=0;i<8;i++)
        {
                  Delay_10us(5);
                  if(code&0x01)
                                (DS1302_SETIO());                        //I/0  = 1
                  else
                                (DS1302_CLRIO());                        //I/0 = 0
                  Delay_10us(5);
                  
                  DS1302_SETSCK();                                //SCLK  = 1
                  Delay_10us(5);
                                                                                                                       
                  DS1302_CLRSCK();                                //SCLK = 0
                  code = code >> 1;
        }
}

//DS1302读取8bit的数据
unsigned char DS1302_Read8bit(void)
{
        unsigned char i,code;
        unsigned char temp;
        DS1302_IO_IN();
        code = 0;
        DS1302_CLRSCK();                                                //SCLK = 0

        Delay_10us(5);

        for(i=0;i<8;i++)
        {
                       
                  code = code >>1;
                       
                  if(DS1302_READ_SDA())
                        {
                                code = code | 0x80;
                        }       
                                       
                               
                                       
                  Delay_10us(5);
                  DS1302_SETSCK();                        //SCLK = 1
                  Delay_10us(5);
       
                  DS1302_CLRSCK();                        //SCLK = 0

        }
       
        temp = code /16;
        code = code % 16;
        code = code + temp * 10;                        //数据的相关转换
       
        return code;
}



//读取DS1302指定的1Byte
unsigned char DS1302_ReadByte(unsigned char con)
{
        unsigned char code;
        DS1302_CLRCE();                   //关闭DS1302                                        //CE = 0
        Delay_10us(5);      
        DS1302_CLRSCK();                                                                                                        //SCLK = 0
        Delay_10us(5);
        DS1302_SETCE();          //使能DS1302                        //CE = 1;
        Delay_10us(5);
        DS1302_Write8bit(con);   //读取代码                                //发送地址
        code = DS1302_Read8bit();  //返回读取数字
       
        //printf("code = %d\r\n" ,code );
        Delay_10us(5);
        DS1302_SETSCK();                                                                                                        //SCLK = 1
        Delay_10us(5);
        DS1302_CLRCE();         //关闭DS1302                        //CE = 0
        return code;
}


//u8  DS1302_ReadRam(u8 addr)
//{
//        u8        tmp,res;
//       
//        tmp = addr;
//        res = DS1302_ReadByte(tmp);
//        return(res);       
//}

////连续读取DS1302所有数据
//void DS1302_ReadBurst(unsigned char *rstr)
//{
//        int i = 0;
//        unsigned char code;
//        DS1302_CLRCE();                                           //关闭DS1302                                        //CE = 0
//        Delay_10us(5);      
//        DS1302_CLRSCK();                                                                                                        //SCLK = 0
//        Delay_10us(5);
//        DS1302_SETCE();                          //使能DS1302                        //CE = 1;
//        Delay_10us(5);
//        DS1302_Write8bit(RdRamBurst);   //读取代码                                //发送地址
//       
//           for(i = 0 ;i < 31 ; i++)
//        {
//                   rstr[i] = DS1302_ReadRam(2 * i + 1 + 0xc0);                  //返回读取数字
//        }
//       
//        //printf("code = %d\r\n" ,code );
//        Delay_10us(5);
//        DS1302_SETSCK();                                                                                                        //SCLK = 1
//        Delay_10us(5);
//        DS1302_CLRCE();         //关闭DS1302                        //CE = 0
//
//        for(i = 0 ;i < 31 ; i ++)
//        {
//                 printf("rstr[%d] = %d\r\n",i,rstr[i]);
//        }
//
//}





//写DS1302指定的1Byte
void DS1302_WriteByte(unsigned char con,unsigned char code)
{
        DS1302_CLRCE();                         //关闭DS1302                //CE = 0
        Delay_10us(5);
        DS1302_CLRSCK();                                                                                                                //SCK = 0
        Delay_10us(5);
       
        DS1302_SETCE();                          //使能DS1302                //CE = 1
        Delay_10us(5);
        DS1302_Write8bit(con);             //写控制命令                //发送地址
        DS1302_Write8bit(code);                 //写入数据                        //发送数据
        Delay_10us(5);
        DS1302_SETSCK();
        Delay_10us(5);
        DS1302_CLRCE();          //关闭DS1302

}


//写入时间
void DS1302_WriteTime(TIME_TypeDef* time)
{
        DS1302_WriteByte(WrControl,0x00);                  //关闭写保护,可以写入数据
               
        DS1302_WriteByte(WrYear,time->year);
        DS1302_WriteByte(WrMonth,time->month);
        DS1302_WriteByte(WrDate,time->date);
        DS1302_WriteByte(WrWeek,time->week);
        DS1302_WriteByte(WrHour,time->hour);
        DS1302_WriteByte(WrMin,time->min);
        DS1302_WriteByte(WrSec,time->sec);

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

}

u8  DS1302_ReadRam(u8 addr)
{
        u8        tmp,res;
       
        tmp = addr;
        res = DS1302_ReadByte(tmp);
        return(res);       
}

typedef struct
{
        unsigned char  sec;
        unsigned char min;
        unsigned char hour;
        unsigned char date;
        unsigned char month;
        unsigned char week;
        unsigned char year;
}Time;








//连续读取DS1302所有数据
void DS1302_ReadBurst(unsigned char *rstr)
{
        int i = 0;
        unsigned char code;
        DS1302_CLRCE();                                           //关闭DS1302                                        //CE = 0
        Delay_10us(5);      
        DS1302_CLRSCK();                                                                                                        //SCLK = 0
        Delay_10us(5);
        DS1302_SETCE();                          //使能DS1302                        //CE = 1;
        Delay_10us(5);
        DS1302_Write8bit(RdRamBurst);   //读取代码                                //发送地址
       
           for(i = 0 ;i < 7 ; i++)
        {
                rstr[i] = DS1302_Read8bit();
        }
       
        //printf("code = %d\r\n" ,code );
        Delay_10us(5);
        DS1302_SETSCK();                                                                                                        //SCLK = 1
        Delay_10us(5);
        DS1302_CLRCE();         //关闭DS1302                        //CE = 0

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

}
//////////////测试而已/////////////////////////////////////////////////////
void readTimeTest()
{
        Time myTime;
        DS1302_ReadBurst((u8 *)&myTime);

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

}

void DS1302_ReadTime(TIME_TypeDef* time)
{
        Time myTime;
        DS1302_ReadBurst((u8 *)&myTime);
       
        time->year         = myTime.year;
        time->month = myTime.month;       
        time->date         = myTime.date;

        time->hour         = myTime.hour;
        time->min         = myTime.min;
        time->sec         = myTime.sec;

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

////读出时间
//void DS1302_ReadTime(TIME_TypeDef* time)
//{
//        u8 i = 0;
//        u8 year_temp[3];
//        u8 month_temp[3];               
//        u8 date_temp[3];
//        u8 hour_temp[3];
//        u8 min_temp[3];
//        u8 sec_temp[3];
//
//        u8 max;
//        u8 min;
//
//        for(i = 0 ; i < 3 ; i ++)
//        {
//                year_temp[i]  =  DS1302_ReadByte(RdYear);
//                month_temp[i] =  DS1302_ReadByte(RdMonth);
//                date_temp[i]  =  DS1302_ReadByte(RdDate);
//                hour_temp[i]  =  DS1302_ReadByte(RdHour);
//                min_temp[i]   =  DS1302_ReadByte(RdMin);
//                sec_temp[i]   =  DS1302_ReadByte(RdSec);
//        }
//        //年
//        max = year_temp[0];
//        min = year_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(year_temp[i] > max)
//                {
//                        max = year_temp[i];
//                }
//                if(year_temp[i] < min)
//                {
//                        min = year_temp[i];
//                }
//        }
//        time->year = year_temp[0] + year_temp[1] + year_temp[2] - max - min;
//        //月
//        max = month_temp[0];
//        min = month_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(month_temp[i] > max)
//                {
//                        max = month_temp[i];
//                }
//                if(month_temp[i] < min)
//                {
//                        min = month_temp[i];
//                }
//        }
//        time->month = month_temp[0] + month_temp[1] + month_temp[2] - max - min;
//        //日
//        max = date_temp[0];
//        min = date_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(date_temp[i] > max)
//                {
//                        max = date_temp[i];
//                }
//                if(date_temp[i] < min)
//                {
//                        min = date_temp[i];
//                }
//        }
//        time->date = date_temp[0] + date_temp[1] + date_temp[2] - max - min;
//        //时
//        max = hour_temp[0];
//        min = hour_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(hour_temp[i] > max)
//                {
//                        max = hour_temp[i];
//                }
//                if(hour_temp[i] < min)
//                {
//                        min = hour_temp[i];
//                }
//        }
//        time->hour = hour_temp[0] + hour_temp[1] + hour_temp[2] - max - min;       
//
//        //分
//        max = min_temp[0];
//        min = min_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(min_temp[i] > max)
//                {
//                        max = min_temp[i];
//                }
//                if(min_temp[i] < min)
//                {
//                        min = min_temp[i];
//                }
//        }
//        time->min = min_temp[0] + min_temp[1] + min_temp[2] - max - min;
//
//        //秒
//        max = sec_temp[0];
//        min = sec_temp[0];
//        for(i = 1 ; i < 3 ; i ++)
//        {
//                if(sec_temp[i] > max)
//                {
//                        max = sec_temp[i];
//                }
//                if(sec_temp[i] < min)
//                {
//                        min = sec_temp[i];
//                }
//        }
//        time->sec = sec_temp[0] + sec_temp[1] + sec_temp[2] - max - min;
//
////        time->year         = DS1302_ReadByte(RdYear);
////        time->month = DS1302_ReadByte(RdMonth);       
////        time->date         = DS1302_ReadByte(RdDate);
////        time->week         = DS1302_ReadByte(RdWeek);
////
////        time->hour         = DS1302_ReadByte(RdHour);
////        time->min         = DS1302_ReadByte(RdMin);
////        time->sec         = DS1302_ReadByte(RdSec);
//
////        year_temp  =  DS1302_ReadByte(RdYear);
////        year_month =  DS1302_ReadByte(RdMonth);
////        year_date  =  DS1302_ReadByte(RdDate);
////        year_hour  =  DS1302_ReadByte(RdHour);
////        year_min   =  DS1302_ReadByte(RdMin);
////        year_sec   =  DS1302_ReadByte(RdSec);
//
////        printf("year = %d\r\n",time->year);
////        printf("month = %d\r\n",time->month);
////        printf("year = %d\r\n",time->date);
////        printf("year = %d\r\n",time->hour);
////        printf("year = %d\r\n",time->min);
////        printf("year = %d\r\n",time->sec);
////
////        printf("year_temp = %d\r\n",year_temp);
////        printf("year_month = %d\r\n",year_month);
////        printf("year_date = %d\r\n",year_date);
////        printf("year_hour = %d\r\n",year_hour);
////        printf("year_min = %d\r\n",year_min);
////        printf("year_sec = %d\r\n",year_sec);
//
//
////        if(        (year_temp + year_month + year_date + year_hour + year_min + year_sec)
////                < ( time->year + time->month +  ))
////        {
////       
////        }
//}
       
       
unsigned char time[20]="\0";

void time_convert(TIME_TypeDef *time_get)
{
        time[0] = '2';                                                                                                                        //2
        time[1] = '0';                                                                                                                        //0
        time[2] = time_get->year / 10 + '0';                //1
        time[3] = time_get->year % 10 + '0';        //4
        time[4] = '-';                                    //-
        time[5] = time_get->month / 10 + '0';                                //0
        time[6] = time_get->month % 10 + '0';       //4
        time[7] = '-';                                                                                                                        //-
       
        time[8] = time_get->date / 10 + '0';        //1
        time[9] = time_get->date % 10 + '0';              //0
        time[10] = ' ';                             //
        time[11] = time_get->hour / 10 + '0';       //1
        time[12] = time_get->hour % 10 + '0';       //4
        time[13] = ':';                                                                                                                        //:
        time[14] = time_get->min / 10 + '0';        //2
        time[15] = time_get->min % 10 + '0';              //1
        time[16] = ':';                                                                                                                        //:
        time[17] = time_get->sec / 10 + '0';                    //3
        time[18] = time_get->sec % 10 + '0';              //0
        time[19] = '\0';                                                                                                                        //
       
       
//        printf("time = %s\r\n",time);
}


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


使用特权

评论回复
6
天灵灵地灵灵| | 2017-11-28 19:47 | 只看该作者
注意事项:
        ds1302属于获取时间的芯片,因此,在获取时间的时候,如果要一个个按顺序读取时间,时间会出错,因为,如果先拿到分寄存器时间的时候,刚好秒从59--->00这个时候,就会出现慢一分钟,如果先拿秒,会出现快一分钟,那么获取时间这种方式不可取,只能使用一种突发模式!!!!外部函数调用的时候,只需要DS1302_ReadTime()即可。写的时候,突发模式或者不突发都可以。

使用特权

评论回复
7
天灵灵地灵灵| | 2017-11-28 19:48 | 只看该作者
从网上找的可以用的,拿走用个试试吧。

使用特权

评论回复
8
Cjy_JDxy|  楼主 | 2017-11-29 08:33 | 只看该作者
天灵灵地灵灵 发表于 2017-11-28 19:43
重新恢复成之前的也不行了吗

不行了

使用特权

评论回复
9
Cjy_JDxy|  楼主 | 2017-11-29 08:34 | 只看该作者
天灵灵地灵灵 发表于 2017-11-28 19:48
从网上找的可以用的,拿走用个试试吧。

谢谢

使用特权

评论回复
10
Cjy_JDxy|  楼主 | 2017-11-29 10:03 | 只看该作者
好了。设置秒的时候,最高位要清零。Sec&=0x7f

使用特权

评论回复
11
Cjy_JDxy|  楼主 | 2017-11-29 13:36 | 只看该作者
Cjy_JDxy 发表于 2017-11-29 10:03
好了。设置秒的时候,最高位要清零。Sec&=0x7f

又不行了,真郁闷

使用特权

评论回复
12
Cjy_JDxy|  楼主 | 2017-11-29 13:58 | 只看该作者
我怕虚焊,重新过一下锡,发现走了,可是走的太快了

使用特权

评论回复
13
mcuzone| | 2017-11-29 14:40 | 只看该作者
走太快就检查晶振和起振电路,话说DS1302之类RTC国产仿货比较多

使用特权

评论回复
14
Cjy_JDxy|  楼主 | 2017-11-29 15:07 | 只看该作者
mcuzone 发表于 2017-11-29 14:40
走太快就检查晶振和起振电路,话说DS1302之类RTC国产仿货比较多

好了。是我感觉快,感觉不靠谱。实测和电脑时间差不多。

使用特权

评论回复
15
捉虫天师| | 2017-11-29 15:30 | 只看该作者
最后那个是什么问题导致不走的?

使用特权

评论回复
16
Cjy_JDxy|  楼主 | 2017-11-29 16:02 | 只看该作者
捉虫天师 发表于 2017-11-29 15:30
最后那个是什么问题导致不走的?

可能电路虚焊吧

使用特权

评论回复
17
捉虫天师| | 2017-11-29 16:32 | 只看该作者

好吧,之前好用,你加 了程序不好用,还以为是锁死老牛饿。

使用特权

评论回复
18
一路向北lm| | 2017-11-29 21:10 | 只看该作者
1302很好操作,底层时序对的话,然后就那读写操作了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

553

主题

3520

帖子

19

粉丝