新聞中心

stm32控制DS1302

作者: 時(shí)間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
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


//相對(duì)應(yīng)的IO口配置

#define DS1302_PORT GPIOE

#define DS1302_SCK_PIN GPIO_Pin_9 //對(duì)應(yīng)的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口操作狀態(tài)
#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() //操作輸入輸出狀態(tài)
//#define DS1302_IO_OUT() DS1302_IO_OUT()

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



//定義時(shí)間結(jié)構(gòu)體
//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;


//內(nèi)部函數(shù)
void DS1302_Write8bit(unsigned char code);
unsigned char DS1302_Read8bit(void);
//外部函數(shù)
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

本文引用地址:http://butianyuan.cn/article/201612/325184.htm
在ds1302.c文件中:

/********************************copyright ythuitong by wit_yuan**************************/

#include "bsp.h"

//////////////////////////////////////////////////////////////////////////
// 函數(shù)名 : ds1302_init
// 功能 : ds1302初始化部分
// 參數(shù) : void
// 作者 : wit_yuan
// 時(shí)間 : 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); //關(guān)閉寫保護(hù),可以寫入數(shù)據(jù)了

Delay_10us(10);
// if(DS1302_ReadByte(RdTrickleCharge) != 0xA6)
// {
// Delay_10us(10);
// DS1302_WriteByte(WrTrickleCharge,0xA6);
//
// printf("進(jìn)入rn");
// }
Delay_10us(10);
DS1302_WriteByte(WrControl,0x80); //開啟寫保護(hù),禁止寫入數(shù)據(jù)
}


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的數(shù)據(jù)
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; //數(shù)據(jù)的相關(guān)轉(zhuǎn)換

return code;
}



//讀取DS1302指定的1Byte
unsigned char DS1302_ReadByte(unsigned char con)
{
unsigned char code;
DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
Delay_10us(5);
DS1302_CLRSCK(); //SCLK = 0
Delay_10us(5);
DS1302_SETCE(); //使能DS1302 //CE = 1;
Delay_10us(5);
DS1302_Write8bit(con); //讀取代碼 //發(fā)送地址
code = DS1302_Read8bit(); //返回讀取數(shù)字

//printf("code = %drn" ,code );
Delay_10us(5);
DS1302_SETSCK(); //SCLK = 1
Delay_10us(5);
DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
return code;
}


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

////連續(xù)讀取DS1302所有數(shù)據(jù)
//void DS1302_ReadBurst(unsigned char *rstr)
//{
// int i = 0;
// unsigned char code;
// DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
// Delay_10us(5);
// DS1302_CLRSCK(); //SCLK = 0
// Delay_10us(5);
// DS1302_SETCE(); //使能DS1302 //CE = 1;
// Delay_10us(5);
// DS1302_Write8bit(RdRamBurst); //讀取代碼 //發(fā)送地址
//
// for(i = 0 ;i < 31 ; i++)
// {
// rstr[i] = DS1302_ReadRam(2 * i + 1 + 0xc0); //返回讀取數(shù)字
// }
//
// //printf("code = %drn" ,code );
// Delay_10us(5);
// DS1302_SETSCK(); //SCLK = 1
// Delay_10us(5);
// DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
//
// for(i = 0 ;i < 31 ; i ++)
// {
// printf("rstr[%d] = %drn",i,rstr[i]);
// }
//
//}





//寫DS1302指定的1Byte
void DS1302_WriteByte(unsigned char con,unsigned char code)
{
DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
Delay_10us(5);
DS1302_CLRSCK(); //SCK = 0
Delay_10us(5);

DS1302_SETCE(); //使能DS1302 //CE = 1
Delay_10us(5);
DS1302_Write8bit(con); //寫控制命令 //發(fā)送地址
DS1302_Write8bit(code); //寫入數(shù)據(jù) //發(fā)送數(shù)據(jù)
Delay_10us(5);
DS1302_SETSCK();
Delay_10us(5);
DS1302_CLRCE(); //關(guān)閉DS1302

}


//寫入時(shí)間
void DS1302_WriteTime(TIME_TypeDef* time)
{
DS1302_WriteByte(WrControl,0x00); //關(guān)閉寫保護(hù),可以寫入數(shù)據(jù)

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); //開啟寫保護(hù),禁止寫入數(shù)據(jù)

}

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;








//連續(xù)讀取DS1302所有數(shù)據(jù)
void DS1302_ReadBurst(unsigned char *rstr)
{
int i = 0;
unsigned char code;
DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0
Delay_10us(5);
DS1302_CLRSCK(); //SCLK = 0
Delay_10us(5);
DS1302_SETCE(); //使能DS1302 //CE = 1;
Delay_10us(5);
DS1302_Write8bit(RdRamBurst); //讀取代碼 //發(fā)送地址

for(i = 0 ;i < 7 ; i++)
{
rstr[i] = DS1302_Read8bit();
}

//printf("code = %drn" ,code );
Delay_10us(5);
DS1302_SETSCK(); //SCLK = 1
Delay_10us(5);
DS1302_CLRCE(); //關(guān)閉DS1302 //CE = 0

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

}
//////////////測(cè)試而已/////////////////////////////////////////////////////
void readTimeTest()
{
Time myTime;
DS1302_ReadBurst((u8 *)&myTime);

//printf("time:%d-%d-%d %d:%d:%drn",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:%drn",myTime.year, myTime.month, myTime.date,
// myTime.hour,myTime.min,myTime.sec);
}

////讀出時(shí)間
//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;
// //時(shí)
// 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 = %drn",time->year);
//// printf("month = %drn",time->month);
//// printf("year = %drn",time->date);
//// printf("year = %drn",time->hour);
//// printf("year = %drn",time->min);
//// printf("year = %drn",time->sec);
////
//// printf("year_temp = %drn",year_temp);
//// printf("year_month = %drn",year_month);
//// printf("year_date = %drn",year_date);
//// printf("year_hour = %drn",year_hour);
//// printf("year_min = %drn",year_min);
//// printf("year_sec = %drn",year_sec);
//
//
//// if( (year_temp + year_month + year_date + year_hour + year_min + year_sec)
//// < ( time->year + time->month + ))
//// {
////
//// }
//}


unsigned char time[20]="