- #include<reg51.h>
- typedef unsigned int uint;
- typedef unsigned char uchar;
- uchar led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar a[3];
- uchar b[]={0x01,0x02,0x04,};
- uint second;
- uchar key;
- uint z;
- uchar flag=0;
- sbit keyif=P3^7;
- void delay(uchar i){
- uchar j;
- for(;i>0;i--){
- for(j=0;j<100;j++);
- }
- }
- void init(void){
- TMOD=0x02;
- second=0;
- EA=1;
- ET0=1;
- key=0;
- z=0;
- }
- void display(void){
- uchar i;
- a[0]=led[second/100];
- a[1]=led[second%100/10];
- a[2]=led[second%10];
- for(i=0;i<3;i++){
- P2=b[i];
- P0=a[i];
- delay(10);
- }
- }
- void main(){
- init ( ) ;
- P0 = led[0];
- P2 = 0;
- while(1){
- if(keyif ==0){
- key++;
- switch(key){
- case 1:
- second =0;
- flag =0;
- TH0 = 0x00;
- TL0 = 0x00;
- TR0 = 1;
- break;
- case 3:
- second =0;
- P0 = led[0];
- P2 = 0;
- flag =2;
- break;
- }
- while(keyif ==0);
- }
- switch(key){
- case 2 :
- z = 0;
- TR0 = 0;
- display();
- break;
- case 4:
- second =404;
- flag =1;
- display();
- key = 0;
- break;
- }
- if(flag==1)
- display();
- }
- }
- void timer()interrupt 1 using 0
- {
- TR0 = 0;
- TH0 = 0x00;
- TL0 = 0x00;
- z++;
- display();
- if(z == 15) {
- display();
- second++;
- z = 0;
- }
- if (second ==999){
- TR0=0;
- second =0;
- key = 1;
- }
- TR0 = 1;
- }
复制代码 |