- #include <REGX52.H>
- unsigned char NixieTable[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,0x3d};
- unsigned char KeyValue=0;
- bit flag=0;
- void Delay(unsigned int xms)
- {
- unsigned char i,j;
- while(xms)
- {
- i = 2;
- j = 239;
- do
- {
- while (--j);
- }while (--i);
- xms--;
- }
- }
- void Nixie(unsigned char Number)
- {
- static unsigned char i;
- unsigned char a[2];
- if(Number>9)
- a[0]=NixieTable[Number/10];
- else a[0]=0x00;
- a[1]=NixieTable[Number%10];
- P0=0x00;
- P2&=0xe3;
- P2|=~((0x00+i)<<2);
- P0=a[i];
- i=++i%2;
- Delay(1);
- }
- void MatrixKey()
- {
- static unsigned char count=0;
- static bit sign=0;
- P1=0xf0; //赋值P1 1111 0000
- if(P1!=0xf0) //检测有按键按下
- {
- if(++count>=10 && sign==0)
- {
- sign=1; //按键自锁标志置1
- switch(P1)
- {
- case(0Xe0):KeyValue = 0;break;
- case(0Xd0):KeyValue = 1;break;
- case(0Xb0):KeyValue = 2;break;
- case(0X70):KeyValue = 3;break;
- }
- P1=0x0f; //赋值P1 0000 1111
- switch(P1)
- {
- case(0X0e):KeyValue+= 1;break;
- case(0X0d):KeyValue+= 5;break;
- case(0X0b):KeyValue+= 9;break;
- case(0X07):KeyValue+=13;break;
- }
- }
- }
- else //键抬起
- {
- sign=0; //按键自锁标志清0
- count=0; //消抖计数清0
- }
- }
- void Timer0Init(void) //1毫秒@12.000MHz
- {
- TMOD= 0x01; //设置定时器模式
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- TR0 = 1; //定时器0开始计时
- ET0 = 1;
- EA = 1;
- }
- void main()
- {
- Timer0Init();
- while(1)
- {
- if(flag==0)
- MatrixKey();
- if(P3_0==0)
- flag=1;
- Nixie(KeyValue);
- }
- }
- void Timer0_Routine() interrupt 1
- {
- static unsigned int T0Count;
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- if(KeyValue>0&&flag==1)
- {
- T0Count++;
- if(T0Count>=1000)
- {
- T0Count=0;
- KeyValue--;
- if(KeyValue==0)
- {
- flag=0;
- }
- }
- }
- }
复制代码 |