×
嵌入式 > 嵌入式开发 > 详情

stm32控制DS1302

发布时间:2024-06-29 发布时间:
|
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

本文引用地址:/zixunimg/eepwimg/www.eepw.com.cn/article/201612/325184.htm
在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*********************************/




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


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM8 UART