0
#include
#define uint unsigned int
#define uchar unsigned char
***it rs=P2^5; //LCD1602
***it rw=P2^6;
***it en=P2^7;
***it sclk=P1^6; //DS1302
***it ds1302_io=P3^5;
***it rst=P1^7;
***it s1=P3^2; //KEY
***it s2=P3^3;
***it s3=P3^4;
uchar code table1[]=" 20 - - ";
uchar code table2[]=" : : ";
uchar miao,shi,fen,day,month,year,week,flag,s1num,temp;
void delay(uint z);
void write_ds1302(uchar command,uchar value);
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar num;
//TMOD=0x11;
//TH0=0;
//TL0=0;
//EA=1;
//ET0=1;
//TR0=1;
en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(table2[num]);
delay(5);
}
}
void write_nyr(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
//void write_temp(uchar add,uchar date)
//{
//uchar shi,ge;
//shi=date/10;
//ge=date%10;
//write_com(0x80+0x40+add);
//write_data(0x30+shi);
// write_data(0x30+ge);
//write_data(0xdf);
//write_data(0x43);
//}
void write_week(uchar week)
{
write_com(0x80+12);
switch(week)
{
case 1:write_data('M');delay(5);
write_data('O');delay(5);
write_data('N');
break;
case 2:write_data('T');delay(5);
write_data('U');delay(5);
write_data('E');
break;
case 3:write_data('W');delay(5);
write_data('E');delay(5);
write_data('D');
break;
case 4:write_data('T');delay(5);
write_data('H');delay(5);
write_data('U');
break;
case 5:write_data('F');delay(5);
write_data('R');delay(5);
write_data('I');
break;
case 6:write_data('S');delay(5);
write_data('A');delay(5);
write_data('T');
break;
case 7:write_data('S');delay(5);
write_data('U');delay(5);
write_data('N');
break;
}
}
void ds1302_init()
{
rst=0;
sclk=0;
write_ds1302(0x8e,0x00);
write_ds1302(0x8e,0x80);
}
void write_ds1302(uchar command,uchar value)
{
uchar i=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
ds1302_io=value&0x01;
sclk=0;
sclk=1;
value>>=1;
}
}
uchar read_ds1302(uchar command)
{
uchar i=0;
uchar value=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
sclk=1;
sclk=0;
if(ds1302_io)
{
value|=0x01<
}
}
rst=0;
// value = (value /16 * 10) + value %16;
return value;
}
void keyscan()
{
if(s1==0) //功能键按下
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;
while(!s1);
if(s1num==1)
{
// TR0=0;
write_com(0x80+0x40+8); //miao
write_com(0x0f);
temp=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|temp);
write_ds1302(0x8e,0x80);
}
if(s1num==2) //fen
{
write_com(0x80+0x40+5);
}
if(s1num==3) //shi
{
write_com(0x80+0x40+2);
}
if(s1num==4) //week
{
write_com(0x80+12);
//write_com(0x0f);
}
if(s1num==5) //day
{
write_com(0x80+9);
}
if(s1num==6) //month
{
write_com(0x80+6);
}
if(s1num==7) //year
{
write_com(0x80+3);
}
if(s1num==8)
{
//TR0=1;
s1num=0;
write_com(0x0c);
flag=0;
temp=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|temp);
write_ds1302(0x8e,0x80);
}
}
}
if(s1num!=0)
{
if(s2==0) //数字增加键按下
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(8,miao);
temp=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(5,fen);
temp=(fen)/10*16+(fen)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x82,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(2,shi);
temp=(shi)/10*16+(shi)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x84,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
week++;
if(week==8)
week=1;
write_com(0x80+12);
write_week(week);
temp=(week)/10*16+(week)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8a,temp);
write_ds1302(0x8e,0x80);
}
if(s1num==5)
{
day++;
if(day==32)
day=1;
write_nyr(9,day);
temp=(day)/10*16+(day)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x86,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+9);
}
if(s1num==6)
{
month++;
if(month==13)
month=1;
write_nyr(6,month);
temp=(month)/10*16+(month)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x88,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+6);
}
if(s1num==7)
{
year++;
if(year==99)
year=0;
write_nyr(3,year);
temp=(year)/10*16+(year)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8c,temp);
write_ds1302(0x8e,0x80);
write_com(0x80+3);
}
}
}
}
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
void main()
{
init();
ds1302_init();
while(1)
{
keyscan();
if(flag==0)
{
miao=BCD_Decimal(read_ds1302(0x81));
fen=BCD_Decimal(read_ds1302(0x83));
shi=BCD_Decimal(read_ds1302(0x85));
week=BCD_Decimal(read_ds1302(0x8b));
day=BCD_Decimal(read_ds1302(0x87));
month=BCD_Decimal(read_ds1302(0x89));
year=BCD_Decimal(read_ds1302(0x8d));
write_sfm(8,miao);
write_sfm(5,fen);
write_sfm(2,shi);
write_nyr(9,day);
write_nyr(6,month);
write_nyr(3,year);
write_week(week);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
|
|