#include <reg51.h>#define u8 unsigned char
#define u16 unsigned int #define GPIO_KEY P1sbit Key_Set = P1^0;
sbit Key_Add = P1^1;u8 k1,KeyValue_buf;
u8 KeyValue; //按键值u8 gewei_volue=0;//个十百千位显示的值
u8 shiwei_volue=0;
u8 baiwei_volue=0;
u8 qianwei_volue=0;u8 mun=0;
u8 flag=0;
u8 key_num=0;//按键设置值 u8 wei_set=0;void delay(u16 time)
{u16 i,j;for(i=time;i>0;i--) for(j=100;j>0;j--);
}//位选
sbit ge =P3^3;
sbit shi =P3^2;
sbit bai =P3^1;
sbit qian =P3^0;u8 code bable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xc0};u8 code bable_buf[];void T0_Init()
{TMOD =0x01;TH0 =(65536-5000)/255;TL0 =(65536-5000)%255;TR0 =1;ET0 =1;EA =1;
}void T0_chuli() interrupt 1
{TH0 =(65536-5000)/255;TL0 =(65536-5000)%255;P3 = 1<<flag;switch(flag){case 0: P2 = bable[qianwei_volue]; break;case 1: P2 = bable[baiwei_volue]; break;case 2: P2 = bable[shiwei_volue]; break;case 3: P2 = bable[gewei_volue]; break;}flag++;if(flag==4){flag=0;}
}void Scan_Key()
{if(Key_Set==0)//按键位选{delay(150);if(Key_Set==0) {key_num++; while(!Key_Set);if(key_num==5){key_num=1;}switch(key_num){case 1: wei_set=1; break;case 2: wei_set=2; break;case 3: wei_set=3; break;case 4: wei_set=4; break;}}}if(Key_Add==0)//显示+{delay(200);if(Key_Add==0){
// mun++;
// if(mun==10)
// {
// mun=0;
// };}switch(wei_set){case 1: gewei_volue +=1; if(gewei_volue>9) {gewei_volue=0;} break;case 2: shiwei_volue +=1; if(shiwei_volue>9) {shiwei_volue=0;} break;case 3: baiwei_volue +=1; if(baiwei_volue>9) {baiwei_volue=0;}break;case 4: qianwei_volue +=1; if(qianwei_volue>9) {qianwei_volue=0;}break;}while(!Key_Add);}
}/****************************************
*
* 10MS延时
*
****************************************/
void Delay10ms(void)
{unsigned char a,b,c;for(c=1;c>0;c--)for(b=38;b>0;b--)for(a=130;a>0;a--);
}/*******************************************
*
* 键盘扫描
*
********************************************/
char KeyDown(void)
{u8 a=0;GPIO_KEY=0x0f;if(GPIO_KEY!=0x0f)//行拉高{Delay10ms();//延时10ms消抖if(GPIO_KEY!=0x0f)//当有按键按下{//JIESHOU
// GPIO_KEY=0X0F;switch(GPIO_KEY){case(0X0e): KeyValue=0;break;case(0X0d): KeyValue=1;break;case(0X0b): KeyValue=2;break;case(0X07): KeyValue=3;break;}//列扫描GPIO_KEY=0XF0;switch(GPIO_KEY){case(0X70): KeyValue=KeyValue+1;break;case(0Xb0): KeyValue=KeyValue+5;break;case(0Xd0): KeyValue=KeyValue+9;break;case(0Xe0): KeyValue=KeyValue+13;break;}while((a<200)&&(GPIO_KEY!=0xf0)) //{Delay10ms();a++;}return KeyValue;}} else {return 0xff;}
}/*
按键处理 4位数码管键值处理
*/
void KeyDown_chuli(void)
{u8 val = KeyDown();if (val != 0xff) {qianwei_volue=baiwei_volue;baiwei_volue=shiwei_volue;shiwei_volue=gewei_volue;gewei_volue= val;}
}//共阳是数码管
void main()
{T0_Init();while(1){KeyDown_chuli();}
}