- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code Dsy_Code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- sbit Key_Start = P2^2;
- sbit Key_Power=P2^3;
- sbit dp = P0^7; //小数点
- uchar num=0; //定时器的计数
- uchar count=0;//数码管的计数
- uchar Key_Num=0;//按键次数的计数
- uchar Power_Num=0;
- bit Key_State=1,Power_State=1;
- void DelayMS(uint x){
- uchar i;
- while(x--)
- for(i=0;i<120;i++);
- }
- void Dsy(){ //显示函数
- P2=0xfd;
- P0=Dsy_Code[num%10];
- DelayMS(2);
- P2=0xfe;
- P0=Dsy_Code[num/10];
- dp=1;
- DelayMS(2);
- }
- void Timer()interrupt 1{ //定时器
- TH0=(65536-50000)/256; //50ms
- TL0=(65536-50000)%256;
- count++;
- if(count==2){
- count=0;
- num++;
- if(num==100)
- num=0;
- }
- }
- void Key_Handle(){//按键处理
- if(Key_State==0){
- Key_Num=(Key_Num+1)%3;
- switch(Key_Num){
- case 1:IE=0x82;TR0=1;break;
- case 2:IE=0x00;TR0=0;break;
- case 0:IE=0x00;TR0=0;num=0;break;
- }
- }
- }
- void main(){
- TMOD=0x01;
- TH0=(65536-50000)/256; //10ms
- TL0=(65536-50000)%256;
- if(Power_State!=Key_Power){
- DelayMS(10);
- if(Power_State!=Key_Power){
- Power_Num++;
- if(Power_Num%2==1){
- while(1){
- if(Key_State!=Key_Start){
- DelayMS(10);
- Key_State=Key_Start;
- Key_Handle();
- }
- Dsy();
- }
- }
- }
- }
- }
复制代码
原帖:http://www.51hei.com/bbs/dpj-100200-1.html
|