DS1302控制代碼
/*************************************
秒鐘顯示
*************************************/
void DisplaySeconds(uchar seconds)
{
uchar shi,ge;
shi=seconds/10;
ge=seconds%10;
write_command(0x06);
write_command(0x80+0x46);
write_date(shi+48);
write_date(ge+48);
}
/*************************************
年份顯示
*************************************/
void DisplayYear(uchar years)
{
uchar shi,ge;
shi=years/10;
ge=years%10;
write_command(0x06);
write_command(0x80+0x00);
write_date(shi+48);
write_date(ge+48);
write_date(-);
}
/*************************************
月份顯示
*************************************/
void DisplayMonth(uchar month)
{
uchar shi,ge;
shi=month/10;
ge=month%10;
write_command(0x06);
write_command(0x80+0x03);
write_date(shi+48);
write_date(ge+48);
write_date(-);
}
/*************************************
日顯示
*************************************/
void DisplayDay(uchar day)
{
uchar shi,ge;
shi=day/10;
ge=day%10;
write_command(0x06);
write_command(0x80+0x06);
write_date(shi+48);
write_date(ge+48);
}
/*************************************
禮拜顯示
*************************************/
void DisplayWeek(uchar week)
{
uchar i;
i=week;
write_command(0x06);
write_command(0x80+0x09);
switch(i)
{
case 1:write_date(M);write_date(O);write_date(N);
break;
case 2:write_date(T);write_date(H);write_date(E);
break;
case 3:write_date(W);write_date(E);write_date(D);
break;
case 4:write_date(T);write_date(H);write_date(U);
break;
case 5:write_date(F);write_date(R);write_date(I);
break;
case 6:write_date(S);write_date(A);write_date(T);
break;
case 7:write_date(S);write_date(U);write_date(N);
break;
}
}
/*************************************
讀取日期
*************************************/
void read_date()
{
uchar ReadValue;
ReadValue = read_1302(0x81);
seconds=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = read_1302(0x83);
minutes=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = read_1302(0x85);
hours=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = read_1302(0x87);
day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = read_1302(0x89);
month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = read_1302(0x8d);
year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue=read_1302(0x8b);
week=ReadValue&0x07;
DisplaySeconds(seconds);
DisplayMinutes(minutes);
DisplayHours(hours);
DisplayDay(day);
DisplayMonth(month);
DisplayYear(year);
DisplayWeek(week);
}
/*************************************
修改日期
*************************************/
void change()
{
uchar temp,nian,yue,ri,zhou,shi,fen,miao;
if(K1==0)
{
delay(5);
if(K1==0)//證明功能鍵確實(shí)按下
{
cnt++;//計(jì)數(shù)器,計(jì)算按下的次數(shù)
write_1302(0x8e,0x00);//關(guān)閉寫(xiě)保護(hù)
}
}
/*******************************************
修改年份
*******************************************/
if(cnt==1)
{
temp=read_1302(0x8d);
nian=((temp&0x70)>>4)*10+(temp&0x0F);
if(K2==0)
{
delay(5);
if(K2==0)//K2鍵實(shí)現(xiàn)加
{
while(!K2);
delay(5);
while(!K2);//確保K2鍵松開(kāi)
nian++;
if(nian>=100)
{
nian=0;
}
write_1302(0x8e,0x00);
write_1302(0x8c,(nian/10)<<4|(nian%10));
DisplayYear(nian);
write_1302(0x8e,0x80);
}
}
if(K3==0)
{
delay(5);
if(K3==0)//K3鍵實(shí)現(xiàn)減
{
while(!K3);
delay(5);
while(!K3);//確保K3鍵松開(kāi)
nian--;
if(nian==-1)
{
nian=99;
}
write_1302(0x8e,0x00);
write_1302(0x8c,(nian/10)<<4|(nian%10));
DisplayYear(nian);
write_1302(0x8e,0x80);
}
}
}
/*******************************************
修改月份
*******************************************/
if(cnt==2)
{
temp=read_1302(0x89);
yue=((temp&0x70)>>4)*10+(temp&0x0F);
if(K2==0)
{
delay(5);
if(K2==0)//K2鍵實(shí)現(xiàn)加
{
while(!K2);
delay(5);
while(!K2);//確保K2鍵松開(kāi)
yue++;
if(yue>=12)
{
yue=1;
}
write_1302(0x8e,0x00);
write_1302(0x88,(yue/10)<<4|(yue%10));
DisplayMonth(yue);
write_1302(0x8e,0x80);
}
}
if(K3==0)
{
delay(5);
if(K3==0)//K3鍵實(shí)現(xiàn)減
{
while(!K3);
delay(5);
while(!K3);//確保K3鍵松開(kāi)
yue--;
if(yue==0)
{
yue=12;
}
write_1302(0x8e,0x00);
write_1302(0x88,(yue/10)<<4|(yue%10));
DisplayMonth(yue);
write_1302(0x8e,0x80);
}
}
}
/*******************************************
修改天數(shù)
*******************************************/
if(cnt==3)
{
temp=read_1302(0x87);
ri=((temp&0x70)>>4)*10+(temp&0x0F);
if(K2==0)
{
delay(5);
if(K2==0)//K2鍵實(shí)現(xiàn)加
{
while(!K2);
delay(5);
while(!K2);//確保K2鍵松開(kāi)
ri++;
if(ri>=32)
{
ri=1;
}
write_1302(0x8e,0x00);
write_1302(0x86,(ri/10)<<4|(ri%10));
DisplayDay(ri);
write_1302(0x8e,0x80);
}
}
if(K3==0)