//#include "stm32f10x.h"
#include "ds1302_stm32.h"
//#define NOP() __NOP
//-----------------------------------------------------
struct DateTime SystemTime;
//-----------------------------------------------------
bool ds_DateTimeOK() //确定SystemTime填充的日期和时间合法
{
u8 ret,a,b,c,d,e,f;
ret=a=b=c=d=e=f=0;
if(SystemTime.year>15)
a=1;
if((SystemTime.month>=1) && (SystemTime.month<=12))
b=1;
if((SystemTime.day>=1) && (SystemTime.day<=31))
c=1;
if((SystemTime.hour>=1) && (SystemTime.hour<=12))
d=1;
if(SystemTime.minute<60)
e=1;
if(SystemTime.second<60)
f=1;
ret= a && b && c && d && e && f;
return ret;
}
//-----------------------------------------------------
bool ds_DateTime_stru_OK(struct DateTime dt) //确定dt填充的日期和时间合法
{
u8 ret,a,b,c,d,e,f;
ret=a=b=c=d=e=f=0;
if(dt.year>=15)
a=1;
if((dt.month>=1) && (dt.month<=12))
b=1;
if((dt.day>=1) && (dt.day<=31))
c=1;
if((dt.hour>=1) && (dt.hour<=12))
d=1;
if(dt.minute<60)
e=1;
if(dt.second<60)
f=1;
ret= a && b && c && d && e && f;
return ret;
}
//-----------------------------------------------------
void ds_SystemTime_zero()
{
memset(&SystemTime,0,sizeof(SystemTime));
}//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
void DS1302_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
}
//-----------------------------------------------------
void ds_init_gpio()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = RTCreset|RTCclock|RTCio;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(port_RTCreset, &GPIO_InitStructure);
GPIO_ResetBits(port_RTCreset,RTCreset|RTCclock|RTCio );
}
//-----------------------------------------------------
void ds_set_RTCio_RW(enum DS1302_RW dr)
{
GPIO_InitTypeDef GPIO_InitStructure;
if(dr==DS1302_write)
{
GPIO_InitStructure.GPIO_Pin = RTCio;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(port_RTCio, &GPIO_InitStructure);
}
else
{
GPIO_InitStructure.GPIO_Pin = RTCio;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(port_RTCio, &GPIO_InitStructure);
}
}
//-----------------------------------------------------
void ds_set_RTCio(u8 level)
{
if(level==1)
GPIO_SetBits(port_RTCio,RTCio);
else
GPIO_ResetBits(port_RTCio,RTCio);
}
//-----------------------------------------------------
void NOP()
{
}
static void DelayNOP(u32 count)
{
while(count--)
NOP();
}
//static void DS1302_OUT(void)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//}
//static void DS1302_IN(void)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//}
void DS1302SendByte(u8 byte)
{
u8 i;
for(i=0x01;i;i<<=1)
{
if(byte&i)
{
//DS1302_OUT_H();
ds_set_RTCio(SET);
}
else
{
//DS1302_OUT_L();
ds_set_RTCio(RESET);
}
DS1302_CLK_H();
DelayNOP(50); //加延时
DS1302_CLK_L();
}
}
u8 DS1302ReceiveByte(void)
{
u8 a,i,byte=0;
for(i=0x01;i;i<<=1)
{
a=GPIO_ReadInputDataBit(port_RTCio,RTCio);
if(a)
byte |= i;
DS1302_CLK_H();
DelayNOP(50); //加延时
DS1302_CLK_L();
}
return(byte);
}
void Write1302(u8 addr,u8 data)
{
ds_set_RTCio_RW(DS1302_write);//DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(addr);
DS1302SendByte(data);
DelayNOP(100);
DS1302_RST_L();
}
u8 Read1302(u8 addr)
{
u8 data=0;
ds_set_RTCio_RW(DS1302_write);//DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(addr|0x01);
ds_set_RTCio_RW(DS1302_read);//DS1302_IN();
data = DS1302ReceiveByte();
DelayNOP(100);
DS1302_RST_L();
return(data);
}
//读取时间函数
void DS1302_GetTime(u8 *time)
{
//u8 tmp;
time[0] = Read1302(DS1302_YEAR);
time[1] = Read1302(DS1302_WEEK);
time[2] = Read1302(DS1302_MONTH);
time[3] = Read1302(DS1302_DAY);
time[4] = Read1302(DS1302_HOUR);
time[5] = Read1302(DS1302_MINUTE);
time[6] = Read1302(DS1302_SECOND);
}
/*
读取DS1302中的RAM
addr:地址,从0到30,共31个字节的空间
返回为所读取的数据
*/
u8 ReadDS1302Ram(u8 addr)
{
u8 tmp,res;
tmp = (addr<<1)|0xc0;
res = Read1302(tmp);
return(res);
}
/*
写DS1302中的RAM
addr:地址,从0到30,共31个字节的空间
data:要写的数据
*/
void WriteDS1302Ram(u8 addr,u8 data)
{
u8 tmp;
Write1302(DS1302_WRITE,0x00); //关闭写保护
tmp = (addr<<1)|0xc0;
Write1302(tmp,data);
Write1302(DS1302_WRITE,0x80); //打开写保护
}
void ReadDSRam(u8 *p,u8 add,u8 cnt)
{
u8 i;
if(cnt>30) return;
for(i=0;i<cnt;i++)
{
*p = ReadDS1302Ram(add+1+i);
p++;
}
}
void WriteDSRam(u8 *p,u8 add,u8 cnt)
{
u8 i;
if(cnt>30) return;
for(i=0;i<cnt;i++)
{
WriteDS1302Ram(add+1+i,*p++);
}
}
/*
读时间函数,顺序为:年周月日时分秒
*/
void ReadDS1302Clock(u8 *p)
{
ds_set_RTCio_RW(DS1302_write);//DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(0xbf); //突发模式
ds_set_RTCio_RW(DS1302_read);//DS1302_IN();
p[5] = DS1302ReceiveByte(); //秒
p[4] = DS1302ReceiveByte(); //分
p[3] = DS1302ReceiveByte(); //时
p[2] = DS1302ReceiveByte(); //日
p[1] = DS1302ReceiveByte(); //月
DS1302ReceiveByte(); //周
p[0] = DS1302ReceiveByte(); //年
DS1302ReceiveByte(); //保护标志字节
DelayNOP(100);
DS1302_RST_L();
ds_set_RTCio_RW(DS1302_write);//DS1302_OUT();
}
/*
写时间函数,顺序为:年周月日时分秒
*/
void WriteDS1302Clock(u8 *p)
{
Write1302(DS1302_WRITE,0x00); //关闭写保护
ds_set_RTCio_RW(DS1302_write);//DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(0xbe); //突发模式
DS1302SendByte(p[5]); //秒
DS1302SendByte(p[4]); //分
DS1302SendByte(p[3]); //时
DS1302SendByte(p[2]); //日
DS1302SendByte(p[1]); //月
DS1302SendByte(0x01); //周,设置成周一,没有使用
DS1302SendByte(p[0]); //年
DS1302SendByte(0x80); //保护标志字节
DelayNOP(100);
DS1302_RST_L();
}
void InitClock(void)
{
u8 tmp;
ds_init_gpio();
tmp = ReadDS1302Ram(0);
if(tmp^0xa5)
{
WriteDS1302Ram(0,0xa5);
Write1302(DS1302_WRITE,0x00); //关闭写保护
Write1302(0x90,0x03); //禁止涓流充电
Write1302(DS1302_HOUR,0x00); //设置成24小时制
Write1302(DS1302_SECOND,0x00); //使能时钟运行
Write1302(DS1302_WRITE,0x80); //打开写保护
}
}
//-----------------------------------------------------
void debug1_test_ds1302()
{
#if 1
u8 dt[10];
dt[0]=0x15;
dt[1]=0x6;
dt[2]=0x28;
dt[3]=0x10;
dt[4]=0x11;
dt[5]=0x12;
WriteDS1302Clock(&dt[0]);
os_dly_wait(100);
memset(&dt,0,10);
ReadDS1302Clock(&dt[0]);
#else
int i;
for(i=0;i<1000;i++)
{
InitClock();
os_dly_wait(100);
}
#endif
}
//-----------------------------------------------------
void ds_write_dateTime(struct DateTime dt)
{
st