//----------------------------------------//----------------------------------------voidallinit();
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba);
voidkey();
voiddsinit();
voidreadtime();
voidDelayms(uintms);
voidcontrol();
voidTimer0Init(void);
//----------------------------------------uchards[]={50,29,8};
ucharshijian[2];
ucharseg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0X82,0XF8,
0X80,0X90,0XFF,0xBF};//数码管显示值ucharyi=10,er=10,san=10,si=10,wu=10,liu=10,qi=10,ba=10;
ucharfazhi=50;
ucharzhuangtai=0;
uintshidu=0;
ucharbell_flag=0;
ucharshezhi=0;
ucharkai;
//----------------------------------------voidmain()
{
allinit();
dsinit();
eeprom_write(0x01,fazhi);
Timer0Init();
while(1)
{
control();
display(yi,er,san,si,wu,liu,qi,ba);
}
}
voidallinit()
{
P2=0xa0;P0=0x00;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭ledP2=0xe0;P0=0xff;//段选P2=0xc0;P0=0xff;//位选}
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba)
{
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[yi];//段选P2=0xc0;P0=0x01;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[er];//段选P2=0xc0;P0=0x02;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[san];//段选P2=0xc0;P0=0x04;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[si];//段选P2=0xc0;P0=0x08;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[wu];//段选P2=0xc0;P0=0x10;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[liu];//段选P2=0xc0;P0=0x20;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[qi];//段选P2=0xc0;P0=0x40;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[ba];//段选P2=0xc0;P0=0x80;//位选Delayms(1);
}
voidkey()
{
if(S7==0)
{
Delayms(5);
if(S7==0)
{
if(zhuangtai==0)
zhuangtai=1;
elseif(zhuangtai==1)
zhuangtai=0;
}
while(!S7)
{
display(yi,er,san,si,wu,liu,qi,ba);
};
}
if(S6==0)
{
Delayms(5);
if(S6==0)
{
if(zhuangtai==0)
{
shezhi=~(shezhi);
if(shezhi!=0)
fazhi=eeprom_read(0x01);
elseeeprom_write(0x01,fazhi);
}
elseif(zhuangtai==1)
{
bell_flag=~(bell_flag);
}
}
while(!S6)
{
display(yi,er,san,si,wu,liu,qi,ba);
};
}
if(S5==0)
{
Delayms(5);
if(S5==0)
{
if(zhuangtai==0)
{
if(shezhi!=0)
{
fazhi+=1;
if(fazhi==100)
fazhi=0;
}
}
elseif(zhuangtai!=0)
{
kai=1;
}
}
while(!S5)
{
display(yi,er,san,si,wu,liu,qi,ba);
};
}
if(S4==0)
{
Delayms(5);
if(S4==0)
{
if(zhuangtai==0)
{
if(shezhi!=0)
{
fazhi-=1;
if(fazhi==0)
fazhi=99;
}
}
elseif(zhuangtai!=0)
{
kai=0;
}
}
while(!S4)
{
display(yi,er,san,si,wu,liu,qi,ba);
};
}
}
voiddsinit()
{
ucharadd=0x80;
uchari;
Write_Ds1302(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302(add,ds[i]);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
voidreadtime()
{
ucharadd=0x83;
uchari;
Write_Ds1302(0x8e,0x80);
for(i=0;i<2;i++)
{
shijian[i]=Read_Ds1302(add);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
voidcontrol()
{
if(shezhi!=0)
{
yi=11;
er=11;
san=10;
si=10;
wu=10;
liu=10;
qi=fazhi/10%10;
ba=fazhi%10;
}
else {
yi=shijian[1]/16;
er=shijian[1]%16;
san=11;
si=shijian[0]/16;
wu=shijian[0]%16;
liu=10;
qi=shidu/10%10;
ba=shidu%10;
}
if(zhuangtai==0)
{
P2=0x80;P0=0xfe;//关闭ledshidu=(uint)((ad_read(0x03))*0.39);
if(shidu<fazhi)
{
P2=0xa0;P0=0x10;
}
else {
P2=0xa0;P0=0x00;
}
}
elseif(zhuangtai!=0)
{
P2=0x80;P0=0xfd;//关闭ledshidu=(uint)((ad_read(0x03))*0.39);
if((shidu<fazhi)&&(bell_flag==0))
{
P2=0xa0;P06=1;
}
else {
P2=0xa0;P06=0;
}
if(kai==1)
{
P2=0xa0;P04=1;
}
else {
P2=0xa0;P04=0;
}
}
}
voidDelayms(uintms) //@11.0592MHz{
unsignedchari, j,k;
for(k=ms;k>0;k--)
{
_nop_();
_nop_();
_nop_();
i=11;
j=190;
do {
while (--j);
} while (--i);
}
}
voidTimer0Init(void) //1毫秒@11.0592MHz{
AUXR|=0x80; //定时器时钟1T模式TMOD&=0xF0; //设置定时器模式TL0=0xCD; //设置定时初值TH0=0xD4; //设置定时初值TF0=0; //清除TF0标志TR0=1; //定时器0开始计时EA=1;
ET0=1;
}
voidtimer0() interrupt1{
readtime();
key();
}