天旭 发表于 2018-12-12 13:54 您好,可以把图单独发一下吗,不清晰,谢谢啦 |
#include <reg52.h> #define uchar unsigned char #define uint unsigned int uchar code tab[] ={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92, 0x82,0xf8,0x80,0x90,0xff}; //0,12,3,4,5,6,7,8,9,关显示 uchar b,d,t; //定义变量 uchar fen=10,miao=0; //定时初始时间变量 uchar flag; //标志位 uchar temp; //矩形键盘键值 sbit beep =P1^7 ; //蜂鸣器 void delay(uint z) //延时函数 { uint x,y; for(x=z;x>0;x--) for( y= 110;y>0;y--); } void dispaly() //定时时间显示 { uchar miaoge,fenge,miaoshi,fenshi; miaoge = miao%10; P2 = 0x80;; P0 =tab[miaoge]; delay(1); miaoshi = miao/10; P2 =0x40; P0 =tab[miaoshi]; delay(1); fenge = fen%10; P2=0x20; P0 =tab[fenge]; delay(1); fenshi =fen/10; P2 =0x10; P0 =tab[fenshi]; delay(1); P2 =0x00; } void dispaly1(char a) //甲队比分显示 { uchar ge1,shi1; b=a; ge1=b%10; P2=0x02; P0=tab[ge1]; delay(1); shi1=b/10; P2=0x01; P0=tab[shi1]; delay(1); P2=0x00; } void dispaly2(char c) //乙队比分显示 { uchar ge2,shi2; d=c; ge2 =d%10; P2=0x08; P0=tab[ge2]; delay(1); shi2 =d/10; P2 = 0x04; P0 =tab[shi2]; delay(1); P2 =0x80; } void keyscan() //矩阵键盘扫描控制 { char a,c,e,f; dispaly1(a); dispaly2(c); P3 =0xfe; temp=P3; temp =temp&0xf0; while(temp!=0xf0) { delay(5); temp =P3; temp =temp&0xf0; while(temp!=0xf0) { temp =P3; if(temp==0xee) { delay(5); if(temp == 0xee) { a++; //甲队比分加1 if(a>=100) a=99; dispaly1(a); } } if(temp==0xde) { delay(5); if(temp==0xde) { a=a+2; //甲队比分加2 if(a>=100) a=99; dispaly1(a); } } if(temp==0xbe) { delay(5); if(temp==0xbe) { a=a+3; //甲队比分加3 if(a>=100) a=99; dispaly1(a); } } if(temp ==0x7e) { delay(5); if(temp ==0x7e) { a--; //甲队比分减1 if(a<= -1) a=0; dispaly1(a); } } while(temp!= 0xf0) //松手检测 { temp =P3; temp=temp&0xf0; dispaly1(a); } } } P3=0xfd; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp =P3; temp=temp&0xf0; while(temp!=0xf0) { temp =P3; if(temp==0xed) { delay(5); if(temp ==0xed) { c++; //乙队比分加1 if(c>=100) c=99; dispaly2(c); } } if(temp==0xdd) { delay(5); if(temp==0xdd) { c= c+2; //乙队比分加2 if(c>=100) c=99; dispaly2(c); } } if(temp==0xbd) { delay(5); if(temp ==0xbd) { c=c+3; //乙队比分3 if(c>=100) c=99; dispaly2(c); } } if(temp ==0x7d) { delay(5); if(temp ==0x7d) { c--; //乙队比分减1 if(c <= -1) c=0; dispaly2(c); } } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; dispaly2(c); } } } P3=0xfb; temp=P3; temp =temp&0xf0; while(temp!=0xf0) { delay(5); temp =P3; temp=temp&0xf0; while(temp!=0xf0) { temp =P3; if(temp==0xeb) { delay(5); if(temp ==0xeb) { a=0; //双方比分清零 dispaly1(a); c=0; dispaly2(c); } } if(temp == 0xdb) { delay(5); if(temp ==0xdb) { e=a; f=c; a=f; dispaly1(a); //双方比分切换 c=e; dispaly2(c); } } if(flag!=1) //避免误操作,只有在时间停止的情况下才能加/减定时时间 { if(temp==0xbb) { delay(5); if(temp==0xbb) { fen++; // 定时时间加1 if(fen==99) fen=0; } } if(temp ==0x7b) { delay(5); if(temp==0x7b) { fen--; //定时时间减1 if(fen== -1) fen=99; } } } while(temp!=0xf0) { temp=P3; temp =temp&0xf0; dispaly2(c); } } } P3=0xf7; temp =P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp =temp&0xf0; while(temp!=0xf0) { temp=P3; if(temp==0xe7) { delay(5); if(temp==0xe7) { TR0=1; //比赛开始计时按键 flag=1; } } if(temp==0xd7) { delay(5); if(temp==0xd7) { TR0=0; //比赛暂停计时按键 flag=0; } } if(temp==0xb7) { delay(5); if(temp==0xb7) { fen=0; //比赛时间清零 miao=0; } } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; dispaly2(c); } } } } void timer0(void)interrupt 1 //T0中断服务 { TH0=0x4c; //50ms延时初值 TL0=0x00; t++; if(t==20) //50ms走20次刚好1S,1s时间到,减1操作 { t=0; miao--; if(miao == -1) { fen--; miao=59; //当59s减完,分减1 } if(fen == -1) { fen=0; //分清零 miao=0; beep=0; //时间停止蜂鸣器响 } } } void init() //T0中断初始化 { TMOD=0x01; TH0= (65536-50000)/256; TL0= (65536-50000)%256; ET0=1; EA=1; TR0=0; } void main() //主程序 { P2=0xfe; init(); while(1) { dispaly(); keyscan(); } } 图看我头像 希望对你有帮助 ![]() |
用8051单片机 |