1、/*这里的按键是左起第4个位修改时间和闹钟的先是选择修改时间 在是修改闹钟 第五个按键进位按一下加一次,左起第三个是选择是时钟和秒表之间的转换。如果选择的是秒表在再按左起第二个是开始计时 第四个是暂停。*/#include#define ucharunsigned char sbitkey1 = P36; sbitkey2 = P37;sbitkey3 = P14;sbitkey4 = P15;sbitbell = P27;ucharcodetable = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;ucharnumber104;ucha
2、r aa,num = 0,tt;ucharqie = 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;voiddisplay(uchar qie,uchar bai,uchar shi,ucharge);voiddelay(uchark);voidjingwei();voidintter2();voidintter3();voiddisplay1(ucharqie);voiddisplay2(ucharbai);voiddisplay3(ucharshi);vo
3、iddisplay4(ucharge);voiddisplays1(uchar qie,uchar bai,uchar shi,ucharge);voiddisplays2(uchar qie,uchar bai,uchar shi,ucharge);voiddisplays3(uchar qie,uchar bai,uchar shi,ucharge);voiddisplays4(uchar qie,uchar bai,uchar shi,ucharge);voidbells(uchar qie,uchar bai,uchar shi,ucharge,uchar yi,uchar er,uc
4、har san,uchar si,uchar mszhuan,uchar tt);voidmzhuan();voidmiaobiao();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, y
5、i,er,san,si,mszhuan,tt);if(mszhuan=0)/当选择时钟的时候才能做修改时钟 和闹钟intter2();intter3();switch(tt)case0:display(qie,bai,shi,ge);break;case1:displays1(qie,bai,shi,ge);break;case2:displays2(qie,bai,shi,ge);break;case3:displays3(qie,bai,shi,ge);break;case4:displays4(qie,bai,shi,ge);break;case5:displays1(yi,er,san
6、,si);break;case6:displays2(yi,er,san,si);break;case7:displays3(yi,er,san,si);break;case8: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(!k
7、ey1); 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)case1: numbert2-10 = four;numbert2
8、-11 = three;numbert2-12 = two;numbert2-13 = one;break;case2: numbert2-10 = four;numbert2-11 = three;numbert2-12 = two;numbert2-13 = one; break;case3: numbert2-10 = four;numbert2-11 = three;numbert2-12 = two;numbert2-13 = one; break;case4: numbert2-10 = four;numbert2-11 = three;numbert2-12 = two;numb
9、ert2-13 = one; break;case5: numbert2-10 = four;numbert2-11 = three;numbert2-12 = two;numbert2-13 = one; break;if(t1t2)switch(t1)case1: display(number00,number01,number02,number03);break;case2: display(number10,number11,number12,number13);break;case3: display(number20,number21,number22,number23);brea
10、k;case4: display(number30,number31,number32,number33);break;case5: display(number40,number41,number42,number43);break;elset1 = 0;*/ voidinter1()interrupt1TH0 = (65536-50000)/256;TL0 = (65536-50000)%256;aa+;mm+;/*voidinter3()interrupt3TH1 = (65536-50000)/256;TL1 = (65536-50000)%256;bb+; */ voiddelay(
11、uchark)for(j=0;j0;i-); voiddisplay(uchar qie,uchar bai,uchar shi,ucharge)/显示函数 P1 = 0xf7;P0 = tableqie;delay(1);P1 = 0xfd;P0 = tablebai;delay(1);P1 = 0xfb;P0 = tableshi;delay(1);P1 = 0xfe;P0 = tablege;delay(1);voidjingwei()/中断里面中断一次为50ms所以中断二十次为一秒钟 if(aa=20)aa = 0;ge+;if(ge=10)ge = 0;shi+;if(shi=6)s
12、hi = 0;bai+;if(bai=10&qie!=2)bai = 0;qie+; if(bai=4&qie=2) qie = 0; bai = 0;voidintter2()/当按键左起第四个 按下 tt加一次if(key1=0)delay(10);if(key1=0)tt+;if(tt=9)tt = 0;while(!key1);voidintter3() /按键左起第五个 按下 当tt不同值时选择修改的值不同所以用switch来选择 1表示修改时钟的最高位依次 到5表示选择了闹钟的修改if(key2=0)delay(10);if(key2=0)switch(tt)case1:qie+;
13、if(qie=3)qie=0;break;case2:bai+;if(bai=10)bai=0;break;case3:shi+;if(shi=7)shi=0;break;case4:ge+;if(ge=10)ge=0;break;case5:yi+;if(yi=3)yi=0;break;case6:er+;if(er=10)er=0;break;case7:san+;if(san=6)san=0;break;case8:si+;if(si=10)si=0;break;while(!key2);voiddisplays1(uchar qie,uchar bai,uchar shi,ucharg
14、e) /因为在修改的事要显示出修改哪个 所以用了下面的函数P1 = 0xf7; /以为数码管第一个延迟时间长所以第一个比后面三个要亮一点P0 = tableqie; /下面的函数同理delay(20);P1 = 0xfd;P0 = tablebai;delay(1);P1 = 0xfb;P0 = tableshi;delay(1);P1 = 0xfe;P0 = tablege;delay(1);voiddisplays2(uchar qie,uchar bai,uchar shi,ucharge)P1 = 0xf7;P0 = tableqie;delay(1);P1 = 0xfd;P0 = t
15、ablebai;delay(20);P1 = 0xfb;P0 = tableshi;delay(1);P1 = 0xfe;P0 = tablege;delay(1);voiddisplays3(uchar qie,uchar bai,uchar shi,ucharge)P1 = 0xf7;P0 = tableqie;delay(1);P1 = 0xfd;P0 = tablebai;delay(1);P1 = 0xfb;P0 = tableshi;delay(20);P1 = 0xfe;P0 = tablege;delay(1);voiddisplays4(uchar qie,uchar bai
16、,uchar shi,ucharge)P1 = 0xf7;P0 = tableqie;delay(1);P1 = 0xfd;P0 = tablebai;delay(1);P1 = 0xfb;P0 = tableshi;delay(1);P1 = 0xfe;P0 = tablege;delay(20);voidbells(uchar qie,uchar bai,uchar shi,ucharge,uchar yi,uchar er,uchar san,ucharsi,uchar mszhuan,uchar tt)/闹钟函数我与了一个tt就是在时钟正常显示的时候才会响 当左起第二个 if(qie=
17、yi&bai=er&shi=san&mszhuan=0&tt=0) /按下闹钟停止 bell = 0;if(key3=0)delay(10);if(key3=0)bell = bell;while(!key1); voidmzhuan() if(key4=0) delay(10);if(key4=0)mszhuan+;if(mszhuan=2)mszhuan = 0;while(!key4);voidmiaobiao() /这个秒表进位函数 与前面的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;