|
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define play P0
sbit LSA=P1^0;
sbit LSB=P1^1;
sbit LSC=P1^2;
sbit led=P2^2; //闹钟检测
sbit k1=P2^7;// 功能切换
sbit k2=P2^6;//设置加
sbit k3=P2^5;// 设置减
sbit k4=P2^4; //设置位选
sbit beet=P1^5; //蜂鸣器输出口
uchar y;//按键松开
uchar kai;//闹钟开启标志
uchar she;//设置标志
uchar t;//时间计数
uchar num;//功能切换
uchar fen;
uchar shi;
uchar miao;
uchar yue;
uchar nian;
uchar ri;
uchar shijian[8];
uchar shi1; //闹铃时
uchar fen1; //闹铃分
unsigned char code shuma[12]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};
uchar brunyue[12]={31,28,31,30,31,30,31,31,30,31,30,31};
uchar runyue[12]={31,29,31,30,31,30,31,31,30,31,30,31};
void zhongshe(); void qishe();
void naoshe(); void shezhi();
void delay10ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void naozhong()
{
uint a,m;
if((shi1==shi)&&(fen1==fen)&&(kai%2))
{
for(m=20;m>0;m--)
for(a=10;a>0;a--)
if((k1==0)||(k2==0)||(k3==0)||(k4==0))
{ kai=0;break;
}
}
}
void plays() //输出显示
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0): LSA=0;LSB=0;LSC=0; break;//显示第0位
case(1): LSA=1;LSB=0;LSC=0; break;//显示第1位
case(2): LSA=0;LSB=1;LSC=0; break;//显示第2位
case(3): LSA=1;LSB=1;LSC=0; break;//显示第3位
case(4): LSA=0;LSB=0;LSC=1; break;//显示第4位
case(5): LSA=1;LSB=0;LSC=1; break;//显示第5位
case(6): LSA=0;LSB=1;LSC=1; break;//显示第6位
case(7): LSA=1;LSB=1;LSC=1; break;//显示第7位
}
play=shuma[shijian[i]];
if(t>50)
{
switch(she)
{
case(3):
if((i==0)||(i==1))
play=0x00;
break;
case(2):
if((i==3)||(i==4))
play=0x00; break;
case(1):
if((i==6)||(i==7))
play=0x00; break;
}
}
j=10; //扫描间隔时间设定
while(j--);
play=0x00;//消隐
}
}
void nao() //闹钟赋值
{
shijian[7]=kai;
shijian[6]=11;
shijian[5]=10;
shijian[4]=fen1%10;
shijian[3]=fen1/10;
shijian[2]=10;
shijian[1]=shi1%10;
shijian[0]=shi1/10;
}
void rili() //日期赋值
{
shijian[7]=ri%10;
shijian[6]=ri/10;
shijian[5]=10;
shijian[4]=yue%10;
shijian[3]=yue/10;
shijian[2]=10;
shijian[1]=nian%10;
shijian[0]=nian/10;
}
void zhong() //电子钟赋值
{
shijian[7]=miao%10;
shijian[6]=miao/10;
shijian[5]=10;
shijian[4]=fen%10;
shijian[3]=fen/10;
shijian[2]=10;
shijian[1]=shi%10;
shijian[0]=shi/10;
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x0D8;
TL0 = 0x0F0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void shezhi() //功能设置函数
{
if((she!=0)&&((k2==0)||(k3==0)))
{
delay10ms();
if((she!=0)&&((k2==0)||(k3==0)))
{
switch(num)
{
case(0):
zhongshe(); break;
case(1):
qishe(); break;
case(2):
naoshe();break;
}
}
}
}
void main()
{
miao=0;fen=0; shi=0;
ri=19; yue=5; nian=15;
InitTimer0();
while(1)
{
if(k1==0)
{
delay10ms();
if(k1==0)
{num=(num+4)%3;
}
}
if(k4==0)
{
delay10ms();
if(k4==0)
she=(she+5)%4;
while(!k4);
}
shezhi();
plays();
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x0D8;
TL0 = 0x0F0;
t++;
if(t==100)
{
t=0;
miao++;
if(miao>59)
{
miao=0;
fen++;
if(fen>59)
{
fen=0;
shi++;
if(shi>23)
{
shi=0;
ri++;
if(nian%4==0)
{
if(ri>runyue[yue-1])
{
ri=1;
yue++;
}
if(yue>12)
{
yue=1;
nian++;
}
}
else
{
if(ri>brunyue[yue-1])
{
ri=0;
yue++;
}
if(yue>12)
{
yue=0;
nian++ ;
}
}
}
}
}}
switch(num)
{
case(0): zhong();break;
case(1): rili();break;
default: zhong();
}
}
void zhongshe()
{
switch(she)
{
case(1): miao=0;InitTimer0();break;
case(2): if(k2==0) fen=(fen+61)%60;
else fen=(fen+59)%60; break;
case(3): if(k2==0) shi=(shi+25)%24;
else shi=(shi+23)%24;
break;
}
while((!k2)||(!k3));
}
void naoshe()
{
switch(she)
{
case(1): if(kai==1) kai=0;
else kai=1; break;
case(2): if(k2==0) fen1=(fen1+61)%60;
else fen1=(fen1+59)%60; break;
case(3): if(k2==0) shi1=(shi1+25)%24;
else shi1=(shi1+23)%24; break;
}
while((!k2)||(!k3));
}
void qishe()
{
switch(she)
{
case(1):
if(k2==0)
{if(nian%4==0)
ri=(ri+runyue[yue-1]+2)%(runyue[yue-1]+1) ;
else
ri=(ri+brunyue[yue-1]+2)%(brunyue[yue-1]+1);
}
else
{
if(nian%4==0) ri=(ri+runyue[yue-1])%(runyue[yue-1]+1) ;
else
ri=(ri+brunyue[yue-1])%(brunyue[yue-1]+1);
}
break;
case(2): if(k2==0) yue=(yue+14)%13;
else yue=(yue+12)%13;
break;
case(3): if(k2==0) nian++;
else nian--;
}
while((!k2)||(!k3));
}
|
|