#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P0^0;
sbit key2=P0^1;
sbit key3=P0^2;
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
sbit IO=P3^4;
sbit RST=P3^5;
sbit SCLK=P3^6;
void waibuzhongduan();
void delay10ms(uint a); //按键延时
void delay5ms(uint i); //液晶延迟 //
void shizhongwrite(uchar addr,uchar dat); //
uchar shizhongread(uchar addr); //
void yejingcommand(uchar com); //
void yejingdata(uchar dat); //
void init(); //液晶初始化 //
void dsinit(); //进行时间数据初始化 //
void readdat();
void yejingxianshi(); //
uchar mingren[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //读地址
uchar chutian[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //写地址
char huoying[7]={0,0,0x12,0x01,0x01,0x02,0x13};
uchar ajzs,wuming; //初始化时间
void main()
{uchar num=0;
waibuzhongduan();
init();
dsinit();
while(1)
{if(wuming==0)
{readdat();}
else
{if(key1==0)
{delay10ms(1);
if(key1==0)
{ajzs++;
if(ajzs>=7)
ajzs=0; }
while((num<10)&&(key1==0))
{delay10ms(1);
num++;}}
if(key2==0)
{delay10ms(1);
if(key2==0)
{huoying[ajzs]++; //将七位数中的一位加一
if((huoying[ajzs]&0x0f)>9)
{
huoying[ajzs]=huoying[ajzs]+6;
}
if((huoying[ajzs]>=0x60)&&(ajzs<2)) //分秒
{
huoying[ajzs]=0;
}
if((huoying[ajzs]>=0x24)&&(ajzs==2)) // 小时
{
huoying[ajzs]=0;
}
if((huoying[ajzs]>=0x32)&&(ajzs==3)) // 日
{
huoying[ajzs]=0;
}
if((huoying[ajzs]>=0x13)&&(ajzs==4)) // 月
{
huoying[ajzs]=0;
}
if((huoying[ajzs]>=0x07)&&(ajzs==5)) // 周
{
huoying[ajzs]=1;
}}
while((num<10)&&(key2==0))
{delay10ms(1);
num++;}}}
yejingxianshi();
}}
void yejingxianshi()
{
yejingcommand(0x80+0x40);
yejingdata('0'+huoying[2]/16);
yejingdata('0'+(huoying[2]&0x0f));
yejingdata('-');
yejingdata('0'+huoying[1]/16);
yejingdata('0'+(huoying[1]&0x0f));
yejingdata('-');
yejingdata('0'+huoying[0]/16);
yejingdata('0'+(huoying[0]&0x0f));
yejingcommand(0x80);
yejingdata('2');
yejingdata('0');
yejingdata('0'+huoying[6]/16);
yejingdata('0'+(huoying[6]&0x0f));
yejingdata('-');
yejingdata('0'+huoying[4]/16);
yejingdata('0'+(huoying[4]&0x0f));
yejingdata('-');
yejingdata('0'+huoying[3]/16);
yejingdata('0'+(huoying[3]&0x0f));
yejingcommand(0x8D);
yejingdata('0'+(huoying[5]&0x07));
}
void shizhongwrite(uchar addr,uchar dat)
{char n;
SCLK=0;
_nop_();
RST=1;
_nop_();
for(n=0;n<8;n++)
{IO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();}
for(n=0;n<8;n++)
{IO=dat&0x01;
dat>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();}
RST=0;
_nop_();}
uchar shizhongread(uchar addr)
{char n,dat,g;
SCLK=0;
_nop_();
RST=1;
_nop_();
for(n=0;n<8;n++)
{IO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();}
_nop_();
for(n=0;n<8;n++)
{g=IO;
dat=(dat>>1)|(g<<7);
SCLK=1;
_nop_();
SCLK=0;
_nop_();}
RST=0;
_nop_();
SCLK=1;
_nop_();
IO=0;
_nop_();
IO=1;
_nop_();
return dat;}
void dsinit()
{char n;
shizhongwrite(0x8e,0x00);
for(n=0;n<7;n++)
{shizhongwrite(chutian[n],huoying[n]);}
shizhongwrite(0x8e,0x80); }
void readdat()
{char n;
for(n=0;n<7;n++)
{huoying[n]=shizhongread(mingren[n]);}}
void yejingcommand(uchar com)
{
E=0;
RS=0;
RW=0;
P0=com;
delay5ms(10);
E=1;
delay5ms(10);
E=0;}
void yejingdata(uchar dat)
{ E=0;
RS=1;
RW=0;
P0=dat;
delay5ms(10);
E=1;
delay5ms(10);
E=0;}
void init()
{yejingcommand(0x38);
yejingcommand(0x0c);
yejingcommand(0x06);
yejingcommand(0x01);
yejingcommand(0x80);
}
void delay10ms(uint a)
{uint b,c;
{for(;a>0;a--)
{for(b=38;b>0;b--)
for(c=130;c>0;c--);
}}}
void delay5ms(uint i)
{uint j,k;
for(j=i;j>0;j--)
for(k=110;k>0;k--);
}
void waibuzhongduan()
{IT0=1;
EX0=1;
EA=1;}
void time() interrupt 0
{ uchar m=0;
if(key3==0)
{delay10ms(1);
if(key3==0)
{ wuming=~wuming;
ajzs=0;
dsinit();}
while((m<10)&&(key3==0))
{delay10ms(1);
m++;}}
}
|