实现该代码之后发现没有循环,经过第一次按键之后按键就失灵了
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #include<absacc.h>
- sbitkgnxz=P3^5; //功能选择键,按一次设定时间,按两次设定闹钟时间,按三次又显示时间表
- sbit kmin=P3^6;//调整分
- sbitkhour=P3^7;//调整时
- sbit bear=P1^0;//蜂鸣器控制口
- unsigned charsecshi=0,secge=0,minshi=0,minge=0,hourshi=0,hourge=0,flag=0,//flag是按键的计数
- secshi1=0,secge1=0,minshi1=0,minge1=0,hourshi1=0,hourge1=0;//定义存储时间和闹钟时分秒的个位和十位
- unsigned intnum=0,sec=0,min=0,hour=0,sec1=30,min1=0,hour1=1, num1=0;//定义时间和闹钟的时分秒
- unsigned char codetable[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码管段控口
- voidkeyscan();//键盘扫描函数
- voiddisplay();//时钟显示函数
- voiddisplay1();//闹钟显示函数
- voidclock();//闹钟函数即蜂鸣器函数
- /***延时函数****/
- void delay(unsigned char z)
- {unsigned char x,y;
- for(x=z;x>0;x--)
- for(y=255;y>0;y--);
- }
- /***中断函数****/
- void time0() interrupt 1
- {num++;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- if(num==20)
- {num=0;
- sec++;
- if(sec==60)
- {sec=0;
- min++;
- if(min==60)
- {min=0;
- hour++;
- if(hour==24)
- {hour=0;
- }
- }
- }
- display(); }
- }
- /******时钟显示函数******/
- void display()
- {
- secge=sec%10;
- secshi=sec/10;
- minge=min%10;
- minshi=min/10;
- hourge=hour%10;
- hourshi=hour/10;
- P2=0xfe;
- P0=table[secge];
- delay(5);
- P2=0xfd;
- P0=table[secshi];
- delay(5);
- P2=0xfb;
- P0=0x40;
- delay(5);
- P2=0xf7;
- P0=table[minge];
- delay(5);
- P2=0xef;
- P0=table[minshi];
- delay(5);
- P2=0xdf;
- P0=0x40;
- delay(5);
- P2=0xbf;
- P0=table[hourge];
- delay(5);
- P2=0x7f;
- P0=table[hourshi];
- delay(5);
- }
- /*****闹钟显示函数****/
- void display1()
- {
- secge1=sec1%10;
- secshi1=sec1/10;
- minge1=min1%10;
- minshi1=min1/10;
- hourge1=hour1%10;
- hourshi1=hour1/10;
- P2=0xfe;
- P0=table[secge1];
- delay(10);
- P2=0xfd;
- P0=table[secshi1];
- delay(15);
- P2=0xfb;
- P0=0x40;
- delay(15);
- P2=0xf7;
- P0=table[minge1];
- delay(15);
- P2=0xef;
- P0=table[minshi1];
- delay(15);
- P2=0xdf;
- P0=0x40;
- delay(15);
- P2=0xbf;
- P0=table[hourge1];
- delay(15);
- P2=0x7f;
- P0=table[hourshi1];
- delay(15);
- }
- /*****键盘扫描*********/
- void keyscan()
- {if(kgnxz==0) //功能选择键是否按下
- { delay(10);//延时消抖
- if(kgnxz==0)//再次判断键是否按下
- {flag++;} //计数,功能选择键按键次数加一
- while(!kgnxz);} //释放键
- if(flag==1)//功能选择键
- {
- if(kmin==0)
- {delay(10);
- if(kmin==0)
- {min++;
- if(min>=60)
- min=0;
- }
- while(kmin==0)
- display();
- }
- if(khour==0)
- {delay(10);
- if(khour==0)
- {hour++;
- if(hour==24)
- hour=0;
- }
- while(khour==0)
- display();
- }
- }
- if(flag==2)//闹钟调整
- if(kmin==0)
- {delay(5);
- if(kmin==0)
- {min1++;
- if(min1==60)
- min1=0;}
- while(!kmin)
- {display1();}
- }
- if(khour==0)
- {delay(5);
- if(khour==0)
- {hour1++;
- if(hour1==24)
- hour1=0;}
- while(!khour)
- {display1();}
- } }
- /*****蜂鸣器*********/
- void clock()
- { unsigned int i;
- for(i=1000;i>0;i--)
- {bear=1;
- P1=0xfe;
- delay(10);
- bear=0;
- P1=0xff;
- delay(10);}}
- /*****主函数*********/
- main()
- {TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;ET0=1;TR0=1;
- while(1)
- {
- keyscan();display();
- if(flag==1) {keyscan(); display();}
- if(flag==2) {keyscan(); display1();}
- if(flag==3) {display();}
- if((hour==hour1)&&(min==min1)&&(sec==sec1))
- {clock();}//闹钟设定时间与时钟时间相等,调用蜂鸣器函数
- }
- }
复制代码
|