#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit du=P2^6;
sbit we=P2^7;
sbit nao=P2^3;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar num[]={31,28,31,30,31,30,31,31,30,31,30,31};
uchar key=0,ok,pp,count=0,h,m,s,h1=7,m1=0,s1=0,n=13,y=5,r=11,a,b,c,d,e,f;
void delayms(uint x)
{
uint i,j;
for(i=110;i>0;i--)
for(x=j;j>0;j--);
}
void dis(uchar h,uchar m,uchar s)
{
a=h/10;
b=h%10;
c=m/10;
d=m%10;
e=s/10;
f=s%10;
du=1;
P0=table[a];
du=0;
we=1;
P0=0xfe;
we=0;
delayms(10);
du=1;
P0=table[b];
du=0;
we=1;
P0=0xfd;
we=0;
delayms(10);
du=1;
P0=table[c];
du=0;
we=1;
P0=0xfb;
we=0;
delayms(10);
du=1;
P0=table[d];
du=0;
we=1;
P0=0xf7;
we=0;
delayms(10);
du=1;
P0=table[e];
du=0;
we=1;
P0=0xef;
we=0;
delayms(10);
du=1;
P0=table[f];
du=0;
we=1;
P0=0xdf;
we=0;
delayms(10);
}
void keyscan()
{
uchar temp;
P3=0xfe;
temp=P3;
temp=temp&&0xf0;
if(temp!=0xf0)
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:key=1;break;
case 0xde:ok=2;break;
case 0xbe:ok=3;break;
case 0x7e:ok=4;break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&&0xf0;
nao=1;
}
}
nao=0;
}
void main()
{
TMOD=0x01; //定时器0工作方式
TH0=0x4c; //定时器装初值
TL0=0x00;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
while(1)
{
keyscan(); //键盘扫描
if(key==1)
{
while(key==1);
count++; //读按键1的次数
if(count==4)
{
count=0;
}
}
switch(count) //计count次数
{
case 0:TR0=1;break; //正常显示
case 1:dis(h1,m1,s1);break; //显示闹钟
case 2:dis(n,y,r);break; //显示日历
case 3:TR0=0;break; //调时间
case 4:count=0;break;
}
switch(ok) //读取按键
{
case 2:if(count==1)
{
h1++; //调闹钟的小时
if(h1==24)
{
h1=0;
}
}
if(count==2)
{
n++; //调日历的年
}
if(count==3)
{
h++; //调正常时间的小时
if(h==24)
{
h=0;
}
}
key=0;
break;
case 3:if(count==1)
{
m1++;
if(m1==60) //调闹钟的分
{
m1=0;
}
}
if(count==2)
{
y++; //调日历的月
if(y==13)
{
y=1;
}
}
if(count==3)
{
m++; //调正常时间的分
if(m==60)
{
m=0;
}
}
key=0;
break;
case 4: if(count==2)
{
r++; //调日历的天数
{
if(r==num[y-1])
{
r=1;
}
}
}
key=0;
break;
}
if(count!=2&&count!=3)
{
dis(h,m,s);
}
}
}
void
time0() interrupt 1
{
TH0=0x4c;
TL0=0x00;
pp++;
if(pp==20)
{
pp=0;
s++;
if(s==60)
{
s=0;
m++;
if(m==60)
{
m=0;
h++;
if(h==24)
{
h=0;
m=0;
s=0;
r++;
if(r==num[y-1])
{
r=1;
y++;
if(y==13)
{
y=1;
n++;
}
}
}
}
}
}
}
&&是ANSI C的关系运算,做判断用的,不是位与。&这才是。其他的没看。
一周热门 更多>