|
- #include<reg52.h>
- #include<intrins.h>
- #include<absacc.h>
- sbit kgnxz=P3^0; //功能选择键,按一次设定时间,按两次设定闹钟时间,按三次又显示时间表
- sbit ksec=P3^1;//调整秒
- sbit kmin=P3^2;//调整分
- sbit khour=P3^3;//调整时
- sbit kzh=P3^4;//24与12小时制转换
- sbit bear=P1^0;//蜂鸣器控制口
- unsigned char secshi=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 int num=0,sec=0,min=0,hour=0,sec1=30,min1=0,hour1=1, num1=0;//定义时间和闹钟的时分秒
- unsigned char code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码管段控口
- void keyscan();//键盘扫描函数
- void display();//时钟显示函数
- void display1();//闹钟显示函数
- void clock();//闹钟函数即蜂鸣器函数
- /***延时函数****/
- 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(ksec==0)//秒调整键
- {delay(10);//延时消抖
- if(ksec==0)//再次判断键是否按下
- {sec++;//秒加加
- if(sec>=60)//如果秒大于60
- sec=0;//秒清零
- }
- while(ksec==0)//释放键
- display();}
- 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(ksec==0)
- {delay(5);
- if(ksec==0)
- {sec1++;
- if(sec1==60)
- sec1=0;}
- while(!ksec)
- {display1();delay(50);}
- }
- 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();} } }}
复制代码
|
-
-
电子时钟.zip
60.37 KB, 下载次数: 214, 下载积分: 黑币 -5
|