资源描述
/*这里的按键是左起第4个位修改时间和闹钟的先是选择修改时间 在是修改闹钟 第五个按键进位按一下加一次,左起第三个是选择是时钟和秒表之间的转换。如果选择
的是秒表在再按左起第二个是开始计时 第四个是暂停。*/
#include<reg51.h>
#define uchar unsigned char
sbit key1 = P3^6;
sbit key2 = P3^7;
sbit key3 = P1^4;
sbit key4 = P1^5;
sbit bell = P2^7;
uchar code table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar number[10][4];
uchar aa,num = 0,tt;
uchar qie = 1,bai = 9,shi =5,ge =5,i,j,yi,er,san,si,mszhuan,one,two,three,four,mm,t3,t2,t1,k1,k2,k3,k4;
void display(uchar qie,uchar bai,uchar shi,uchar ge);
void delay(uchar k);
void jingwei();
void intter2();
void intter3();
void display1(uchar qie);
void display2(uchar bai);
void display3(uchar shi);
void display4(uchar ge);
void displays1(uchar qie,uchar bai,uchar shi,uchar ge);
void displays2(uchar qie,uchar bai,uchar shi,uchar ge);
void displays3(uchar qie,uchar bai,uchar shi,uchar ge);
void displays4(uchar qie,uchar bai,uchar shi,uchar ge);
void bells(uchar qie,uchar bai,uchar shi,uchar ge,uchar yi,uchar er,uchar san,uchar si,uchar mszhuan,uchar tt);
void mzhuan();
void miaobiao();
void main()
{
//unchar aa;
TMOD = 0x01;
EA = 1;
TR0 = 1;
ET0 = 1;
// ET1 = 1;
aa=0;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
// TH1 = (65536-50000)/256;
// TL1 = (65536-50000)%256;
while(1)
{
miaobiao();
mzhuan();
jingwei();
bells(qie,bai,shi,ge, yi,er,san,si,mszhuan,tt);
if(mszhuan==0) //当选择时钟的时候才能做修改时钟 和闹钟
{
intter2();
intter3();
switch(tt)
{
case 0: display(qie,bai,shi,ge);
break;
case 1: displays1(qie,bai,shi,ge);
break;
case 2: displays2(qie,bai,shi,ge);
break;
case 3: displays3(qie,bai,shi,ge);
break;
case 4: displays4(qie,bai,shi,ge);
break;
case 5: displays1(yi,er,san,si);
break;
case 6: displays2(yi,er,san,si);
break;
case 7: displays3(yi,er,san,si);
break;
case 8: displays4(yi,er,san,si);
break;
}
}
else if(mszhuan==1) //下面的是秒表
{
if(t1==0)
display(four,three,two,one);
if(key3==0)
{
delay(10);
if(key3==0)
{
t3++;
if(t3==2)
t3 = 0;
}
while(!key3);
}
if(key1==0)
{
delay(10);
if(key1==0)
{
t2++;
if(t2==6)
t2 = 0;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
t1++;
if(t1==3)
t1 = 0;
}
while(!key1);
}
if(t3==1)
{
four = 0; three = 0; two = 0; one = 0;
t3 = 0;
t1 = 0;
t2 = 0;
}
if(t1==1)
{
k1 = four; k2 = three; k3 = two; k4 = one;
t1 = 2;
}
if(t1==2)
{
display(k1,k2,k3,k4);
four = k1; three = k2; two = k3; one = k4;
}
/* switch(t2)
{
case 1:
{
number[t2-1][0] = four;
number[t2-1][1] = three;
number[t2-1][2] = two;
number[t2-1][3] = one;
}
break;
case 2:
{
number[t2-1][0] = four;
number[t2-1][1] = three;
number[t2-1][2] = two;
number[t2-1][3] = one;
}
break;
case 3:
{
number[t2-1][0] = four;
number[t2-1][1] = three;
number[t2-1][2] = two;
number[t2-1][3] = one;
}
break;
case 4:
{
number[t2-1][0] = four;
number[t2-1][1] = three;
number[t2-1][2] = two;
number[t2-1][3] = one;
}
break;
case 5:
{
number[t2-1][0] = four;
number[t2-1][1] = three;
number[t2-1][2] = two;
number[t2-1][3] = one;
}
break;
}
if(t1<t2)
{
switch(t1)
{
case 1: display(number[0][0],number[0][1],number[0][2],number[0][3]);
break;
case 2: display(number[1][0],number[1][1],number[1][2],number[1][3]);
break;
case 3: display(number[2][0],number[2][1],number[2][2],number[2][3]);
break;
case 4: display(number[3][0],number[3][1],number[3][2],number[3][3]);
break;
case 5: display(number[4][0],number[4][1],number[4][2],number[4][3]);
break;
}
}
else
t1 = 0;*/
}
}
}
void inter1() interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
aa++;
mm++;
}
/*void inter3() interrupt 3
{
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
bb++;
} */
void delay(uchar k)
{
for(j=0;j<100;j++)
for(i=k;i>0;i--);
}
void display(uchar qie,uchar bai,uchar shi,uchar ge) //显示函数
{
P1 = 0xf7;
P0 = table[qie];
delay(1);
P1 = 0xfd;
P0 = table[bai];
delay(1);
P1 = 0xfb;
P0 = table[shi];
delay(1);
P1 = 0xfe;
P0 = table[ge];
delay(1);
}
void jingwei() //中断里面中断一次为50ms所以中断二十次为一秒钟
{
if(aa==20)
{
aa = 0;
ge++;
if(ge==10)
{
ge = 0;
shi++;
if(shi==6)
{
shi = 0;
bai++;
if(bai==10&&qie!=2)
{
bai = 0;
qie++;
}
if(bai==4&&qie==2)
{
qie = 0;
bai = 0;
}
}
}
}
}
void intter2() //当按键左起第四个 按下 tt加一次
{
if(key1==0)
{
delay(10);
if(key1==0)
{
tt++;
if(tt==9)
tt = 0;
}
while(!key1);
}
}
void intter3() //按键左起第五个 按下 当tt不同值时选择修改的值不同所以用switch来选择 1表示修改时钟的最高位依次 到5表示选择了闹钟的修改
{
if(key2==0)
{
delay(10);
if(key2==0)
{
switch(tt)
{
case 1:
{
qie++;
if(qie==3)
qie=0;
}
break;
case 2:
{
bai++;
if(bai==10)
bai=0;
}
break;
case 3:
{
shi++;
if(shi==7)
shi=0;
}
break;
case 4:
{
ge++;
if(ge==10)
ge=0;
}
break;
case 5:
{
yi++;
if(yi==3)
yi=0;
}
break;
case 6:
{
er++;
if(er==10)
er=0;
}
break;
case 7:
{
san++;
if(san==6)
san=0;
}
break;
case 8:
{
si++;
if(si==10)
si=0;
}
break;
}
}
while(!key2);
}
}
void displays1(uchar qie,uchar bai,uchar shi,uchar ge) //因为在修改的事要显示出修改哪个 所以用了下面的函数
{
P1 = 0xf7; //以为数码管第一个延迟时间长所以第一个比后面三个要亮一点
P0 = table[qie]; //下面的函数同理
delay(20);
P1 = 0xfd;
P0 = table[bai];
delay(1);
P1 = 0xfb;
P0 = table[shi];
delay(1);
P1 = 0xfe;
P0 = table[ge];
delay(1);
}
void displays2(uchar qie,uchar bai,uchar shi,uchar ge)
{
P1 = 0xf7;
P0 = table[qie];
delay(1);
P1 = 0xfd;
P0 = table[bai];
delay(20);
P1 = 0xfb;
P0 = table[shi];
delay(1);
P1 = 0xfe;
P0 = table[ge];
delay(1);
}
void displays3(uchar qie,uchar bai,uchar shi,uchar ge)
{
P1 = 0xf7;
P0 = table[qie];
delay(1);
P1 = 0xfd;
P0 = table[bai];
delay(1);
P1 = 0xfb;
P0 = table[shi];
delay(20);
P1 = 0xfe;
P0 = table[ge];
delay(1);
}
void displays4(uchar qie,uchar bai,uchar shi,uchar ge)
{
P1 = 0xf7;
P0 = table[qie];
delay(1);
P1 = 0xfd;
P0 = table[bai];
delay(1);
P1 = 0xfb;
P0 = table[shi];
delay(1);
P1 = 0xfe;
P0 = table[ge];
delay(20);
}
void bells(uchar qie,uchar bai,uchar shi,uchar ge,uchar yi,uchar er,uchar san,uchar si,uchar mszhuan,uchar tt)
{ //闹钟函数我与了一个tt就是在时钟正常显示的时候才会响 当左起第二个
if(qie==yi&&bai==er&&shi==san&&mszhuan==0&&tt==0)
{ //按下闹钟停止
bell = 0;
if(key3==0)
{
delay(10);
if(key3==0)
{
bell = ~bell;
}
while(!key1);
}
}
}
void mzhuan()
{
if(key4==0)
{
delay(10);
if(key4==0)
{
mszhuan++;
if(mszhuan==2)
mszhuan = 0;
}
while(!key4);
}
}
void miaobiao() //这个秒表进位函数 与前面的jingwei()同理
{
if(mm==20)
{
mm = 0;
one++;
if(one==10)
{
one = 0;
two++;
if(two==10)
{
two = 0;
three++;
if(three==10)
{
three = 0;
four++;
if(four==10)
four = 0;
}
}
}
}
}
展开阅读全文