#include "pbdata.h"
#include "ds1302.h"
#include "systick.h"
/////1 3 5 7 9 B D0x81,0x83,0x85,0x87,0x89,0x8b,0x8d
uint8_t read[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//¶ÁÃë¡¢·Ö¡¢Ê±¡¢ÈÕ¡¢Ô¡¢ÖÜ¡¢ÄêµÄ¼Ä´æÆ÷µØÖ·
uint8_t write[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//дÃë¡¢·Ö¡¢Ê±¡¢ÈÕ¡¢Ô¡¢ÖÜ¡¢ÄêµÄ¼Ä´æÆ÷µØÖ·
/*PA4.6ΪÊä³ö*/
/*PA5ÅäÖÃΪ¿ªÂ©Ä£Ê½£¬´ËģʽÏÂÄܹ»ÊµÏÖÕæÕýµÄË«ÏòIO¿Ú*/
void ds1302_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = ds1302clk|ds1302rst;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStruct.GPIO_Pin = ds1302dat;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void write_1302byte(uint8_t dat)//дһ¸ö×Ö½ÚµÄÊý¾ÝsckÉÏÉýÑØдÊý¾Ý
{
uint8_t i=0;
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302clk=0;
delay_us(2);//ÑÓʱ´óÔ¼2us
for(i=0;i<8;i++)
{
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302clk=0;
if(dat&0x01)
GPIO_SetBits(GPIOA,ds1302dat);
else
GPIO_ResetBits(GPIOA,ds1302dat);
//ds1302dat=(dat&0x01);
delay_us(2);
GPIO_SetBits(GPIOA,ds1302clk);
//ds1302clk=1;
dat>>=1;
delay_us(1);
}
}
uint8_t read_1302(uint8_t add)//¶ÁÊý¾Ý
{
uint8_t i=0,dat1=0x00;
GPIO_ResetBits(GPIOA,ds1302rst);
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302rst=0;
//ds1302clk=0;
delay_us(2);//ÂÔ΢ÑÓʱ2us
GPIO_SetBits(GPIOA,ds1302rst);
//ds1302rst=1;
delay_us(3);//ʱ¼äÒª´óÔ¼3us
write_1302byte(add);//ÏÈд¼Ä´æÆ÷µÄµØÖ·
for(i=0;i<8;i++)
{
GPIO_SetBits(GPIOA,ds1302clk);
//ds1302clk=1;
dat1>>=1;
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302clk=0;//ÀµÍʱÖÓÏߣ¬ÒÔ±ãÓÚÊý¾ÝµÄ¶ÁÈë
if(GPIO_ReadInputDataBit(GPIOA,ds1302dat)==1)//Êý¾ÝÏß´ËʱΪ¸ßµçƽ
{dat1=dat1|0x80;}
}
delay_us(1);
GPIO_ResetBits(GPIOA,ds1302rst);
//ds1302rst=0;
return dat1;
}
void write_1302(uint8_t add,uint8_t dat)//ÏòÖ¸¶¨¼Ä´æÆ÷дÈëÒ»¸ö×Ö½ÚµÄÊý¾Ý
{
GPIO_ResetBits(GPIOA,ds1302rst);
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302rst=0;
//ds1302clk=0;
delay_us(1);//ÂÔ΢ÑÓʱ
GPIO_SetBits(GPIOA,ds1302rst);
//ds1302rst=1;
delay_us(2);//ʱ¼ä´óÔ¼2us
write_1302byte(add);
write_1302byte(dat);
GPIO_ResetBits(GPIOA,ds1302rst);
GPIO_ResetBits(GPIOA,ds1302clk);
//ds1302clk=0;
//ds1302rst=0;
delay_us(1);
}
void ds1302_init(uint8_t *write,uint8_t *time)//³õʼ»¯1302
{
uint8_t i=0,j=0;
write_1302(0x8e,0x00);//È¥³ýд±£»¤
for(i=0;i<7;i++)//תBCDÂë
{
j=time[i]%10;//¸öλÊý²¿·Ö
time[i]=(time[i]/10)*16+j;
}
for(i=0;i<7;i++)//½øÐжÔʱ
{
write_1302(write[i],time[i]);
}
write_1302(0x8e,0x80);//¼Óд±£»¤
}
uint8_t g[7],time[7];
void ds1302_data(uint8_t *read)//´¦ÀíÊý¾Ý²¢Í¨¹ý´®¿Ú´òÓ¡
{
uint8_t h,j,k,fen1,fen0;
uint8_t i=0;
static uint8_t s=1;
for(i=0;i<7;i++)
{
time[i]=read_1302(read[i]);
}//¶ÁÊý¾ÝÒѾÍê³É
fen1=time[0]/10;
fen0=time[0]%10;
for(i=0;i<7;i++)
{
g[i]=time[i]%16;//Ãë¸öλÊý¾Ý 16
time[i]=time[i]/16;//ÃëʮλÊý¾Ý 16
}
//´ËʱÒÑת»»³É10½øÖÆÊý£¬g[i]ÀïÃæ´æ·ÅµÄÊÇÃë·ÖʱÈÕÔÂÖÜÄêµÄ¸÷¸öλÊý¾Ý
//¶ø´ËʱµÄtime¡¾i¡¿ÀïÃæ´æ·ÅµÄÔòÊÇÃë·ÖʱÈÕÔÂÖÜÄêµÄʮλÊý¾Ý
if(s!=(time[0]+g[0]))
printf("20%d%dÄê%d%dÔÂ%d%dÈÕ%d%d:%d%d:%d%d ÐÇÆÚ%d\r\n",
time[6],g[6],time[4],g[4],time[3],g[3],time[2],g[2],time[1],g[1],time[0],g[0],g[5]);
s=time[0]+g[0];
h=time[2];
j=g[2];
k=h+j; //56
show_num(70+1*20,251,time[2],RED,WHITE);
show_num(70+2*20,251,g[2],RED,WHITE);
show_num(70+4*20,251,time[1],RED,WHITE);
show_num(70+5*20,251,g[1],RED,WHITE);
show_num(70+7*20,251,fen1,RED,WHITE); //time[0]
show_num(70+8*20,251,fen0,RED,WHITE); //g[0]
if((k<=9)){setPWM(600, 1000, 100,100);}
}
|