#include<reg52.h> #define uchar unsigned char #define uint unsigned int void delay10ms(); void delay(uint t); void display(uint num,uchar weishu); uchar keyscan(); sbit wei=P2^7; sbit duan=P2^6; uchar code weima[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; uchar code duanma[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff}; void main() { while(1) { if(keyscan()!=16) display(keyscan(),1); }
}
void delay(uint t) { while(t--) ; } void display(uint num,uchar weishu) { uchar i; while(1) { for(i=0;i<weishu;i++) { P0=0xff; //很重要,用来每次显示前的各段清除 duan=1; duan=0; P0=weima; wei=1; wei=0; if(num<=9) P0=duanma[num]; else if(num<=99) { switch (i) { case 0: P0=duanma[num/10];break; case 1: P0=duanma[num%10];break; } } else if(num<=999) { switch (i) { case 0: P0=duanma[num/100];break; case 1: P0=duanma[num/10%10];break; case 2: P0=duanma[num%10]; break; } } else if(num<=9999) { switch (i) {case 0: P0=duanma[num/1000];break; case 1: P0=duanma[num/100%10];break; case 2: P0=duanma[num/10%10];break; case 3: P0=duanma[num%10];break; } } duan=1; duan=0; delay(300); } }
} void delay10ms() { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } uchar keyscan() { uchar templ,temph,key; P3=0x0f; temph=P3; if(temph!=0x0f) { delay10ms(); temph=P3; if(temph!=0x0f) { temph=P3; P3=0xf0; templ=P3; while(P3!=0xf0); key=templ|temph; switch (key) { case 01111110 : return 0;break; case 10111110 : return 1;break; case 11011110 : return 2;break; case 11101110 : return 3;break; case 01111101 : return 4;break; case 10111101 : return 5;break; case 11011101 : return 6;break; case 11101101 : return 7;break; case 01111011 : return 8;break; case 10111011 : return 9;break; case 11011011 : return 10;break; case 11101011 : return 11;break; case 01110111 : return 12;break; case 10110111 : return 13;break; case 11010111 : return 14;break; case 11100111 : return 15;break;
} } } return 16; }
|