|
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define LCDIO P0
- sbit rs=P2^4;
- sbit rd=P2^5;
- sbit lcden=P2^6;
- uchar second,minute,hour,day,month,year,week,count=0;
- uchar ReadValue,num;
- uchar tflag;
- uchar code table[]={"2018-06-15 FRI"};
- uchar code table1[]={"13:14:00 "};
- uchar code table2[]= "THUFRISATSUNMONTUEWES";
- uchar data disdata[5];
- sbit DATA=P1^1;
- sbit RST= P1^0;
- sbit SCLK=P1^2;
- sbit menu=P3^0;
- sbit add=P3^1;
- sbit dec=P3^2;
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void delay1(uint z)
- {
- for(;z>0;z--);
- }
- void write_com(uchar com)
- {
- rs=0;
- rd=0;
- lcden=0;
- P0=com;
- delay(20);
- lcden=1;
- delay(10);
- lcden=0;
- }
- void write_date(uchar date)
- {
- rs=1;
- rd=0;
- lcden=0;
- P0=date;
- delay(20);
- lcden=1;
- delay(10);
- lcden=0;
- }
- void init()
- {
- uchar num;
- lcden=0;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- delay(5);
- write_com(0x80);
- for(num=0;num<15;num++)
- {
- write_date(table[num]);
- delay(5);
- }
- write_com(0x80+0x40);
- for(num=0;num<16;num++)
- {
- write_date(table1[num]);
- delay(5);
- }
- }
- void Write1302(uchar dat)
- {
- uchar i;
- SCLK=0;
- delay1(2);
- for(i=0;i<8;i++)
- {
- DATA = dat&0x01;
- delay1(2);
- SCLK=1;
- delay1(2);
- SCLK=0;
- dat>>=1;
- }
- }
- void WriteSet1302(uchar Cmd,uchar dat)
- {
- RST=0;
- SCLK=0;
- RST=1;
- delay1(2);
- Write1302(Cmd);
- Write1302(dat);
- SCLK=1;
- RST=0;
- }
- uchar Read1302(void)
- {
- uchar i,dat;
- delay(2);
- for(i=0;i<8;i++)
- {
- dat>>=1;
- if(DATA==1)
- dat|=0x80;
- SCLK=1;
- delay1(2);
- SCLK=0;
- delay1(2);
- }
- return dat;
- }
- uchar ReadSet1302(uchar Cmd)
- {
- uchar dat;
- RST=0;
- SCLK=0;
- RST=1;
- Write1302(Cmd);
- dat=Read1302();
- SCLK=1;
- RST=0;
- return dat;
- }
- void Ini_DS1302(void)
- {
- WriteSet1302(0x8E,0x00);
- WriteSet1302(0x80,((0/10)<<4|(0%10)));
- WriteSet1302(0x82,((45/10)<<4|(45%10)));
- WriteSet1302(0x84,((15/10)<<4|(15%10)));
- WriteSet1302(0x86,((29/10)<<4|(29%10)));
- WriteSet1302(0x88,((11/10)<<4|(11%10)));
- WriteSet1302(0x8c,((10/10)<<4|(10%10)));
- WriteSet1302(0x8a,((4/10)<<4|(4%10)));
- }
- void DisplaySecond(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x%10;
- write_com(0xc7);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayMinute(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x%10;
- write_com(0xc4);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayHour(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x%10;
- write_com(0xc1);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayDay(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x% 10;
- write_com(0x89);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayMonth(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x%10;
- write_com(0x86);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayYear(uchar x)
- {
- uchar i,j;
- i=x/10;
- j=x%10;
- write_com(0x83);
- write_date(0x30+i);
- write_date(0x30+j);
- }
- void DisplayWeek(uchar x)
- {
- uchar i;
- x=x*3;
- write_com(0x8c);
- for(i=0;i<3;i++)
- {
- write_date(table2[x]);
- x++;
- }
- }
- void read_date(void)
- {
- ReadValue = ReadSet1302(0x81);
- second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x83);
- minute=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x85);
- hour=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x87);
- day=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x89);
- month=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x8d);
- year=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- ReadValue = ReadSet1302(0x8b);
- week=ReadValue&0x70;
- DisplaySecond(second);
- DisplayMinute(minute);
- DisplayHour(hour);
- DisplayDay(day);
- DisplayMonth(month);
- DisplayYear(year);
- DisplayWeek(week);
- }
- void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num)
- {
- newval=ReadSet1302(newaddr);
- newval=((newval&0x70)>>4)*10+(newval&0x0f);
- if(flag)
- {
- newval++;
- switch(s1num)
- {
- case 1:if(newval>99) newval=0;
- DisplayYear(newval);
- break;
- case 2:if(newval>12) newval=1;
- DisplayMonth(newval);
- break;
- case 3:if(newval>31) newval=1;
- DisplayDay(newval);
- break;
- case 4:if(newval>6) newval=0;
- DisplayWeek(newval);
- break;
- case 5:if(newval>23) newval=0;
- DisplayHour(newval);
- break;
- case 6:if(newval>59) newval=0;
- DisplayMinute(newval);
- break;
- case 7:if(newval>59) newval=0;
- DisplaySecond(newval);
- break;
- default:break;
- }
- }
- else
- {
- newval-- ;
- switch(s1num)
- {
- case 1:if(newval==0) newval=99;
- DisplayYear(newval);
- break;
- case 2:if(newval==0) newval=12;
- DisplayMonth(newval);
- break;
- case 3:if(newval==0) newval=31;
- DisplayDay(newval);
- break;
- case 4:if(newval==0) newval=6;
- DisplayWeek(newval);
- break;
- case 5:if(newval==0) newval=23;
- DisplayHour(newval);
- break;
- case 6:if(newval==0) newval=59;
- DisplayMinute(newval);
- break;
- case 7:if(newval==0) newval=59;
- DisplaySecond(newval);
- break;
- default:break;
- }
- }
- WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10));
- }
- void key_scan(void)
- {
- uchar miao, s1num=0;
- if(menu==0)
- {
- delay(5);
- if(menu==0)
- {
- while(!menu);
- s1num ++ ;
- while(1)
- {
- if(menu==0)
- {
- delay(5);
- if(menu==0)
- {
- while(!menu);
- s1num ++;
- }
- }
- rd=0;
- miao = ReadSet1302(0x81);
- second= miao;
- WriteSet1302(0x80, miao|0x80);
- write_com(0x0f);
- if(s1num==1)
- {
- year = ReadSet1302(0x8d);
- write_com(0x80+4);//????
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(year,1,0x8d,1);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(year,0,0x8d,1);
- }
- }
- }
- if(s1num==2)
- {
- month = ReadSet1302(0x89);
- write_com(0x80+7);
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(month, 1,0x89,2);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val( month,0,0x89,2);
- }
- }
- }
- if(s1num==3)
- {
- day = ReadSet1302(0x87);
- write_com(0x80+10);
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(day,1,0x87,3);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(day,0,0x87,3);
- }
- }
- }
- if(s1num==4)
- {
- week=ReadSet1302(0x8b);
- write_com(0x80+14);
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(week,1,0x8b, 4);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(week, 0, 0x8b,4);
- }
- }
- }
- if(s1num==5)
- {
- hour = ReadSet1302(0x85);
- write_com(0x80+0x40+2);
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(hour,1,0x85,5);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(hour,0,0x85,5);
- }
- }
- }
- if(s1num==6)
- {
- minute=ReadSet1302(0x83);
- write_com(0x80+0x40+5);
- if(add==0)
- {
- delay(5);
- if(add==0)
- {
- while(!add);
- turn_val(minute,0,0x83,6);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(minute,0,0x83,6);
- }
- }
- }
- if(s1num==7)
- {
- second = ReadSet1302(0x81);
- write_com(0x80+0x40+8);
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- if(second == 0x60)
- second =0x00;
- turn_val(second,1,0x81,7);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(second,0,0x81,7);
- }
- }
- }
- if(s1num==8)
- {
- miao=ReadSet1302(0x81);
- second = miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0; //
- write_com(0x0c);
- break;
- }
- }
- }
- }
- }
- void main( )
- {
- init();
- Init_DS1302();
- while(1)
- {
- read_date();
- key_scan();
- }
- }
复制代码
|
|