1、实现 4 位数码管与 4x4 按键的移位显示,还有小数点的按键功能,注意,本例使用的是共阳数码管,用共阴的要修改一下段码和位选。 。 。#include#include#define uchar unsigned char#define uint unsigned intunsigned char code LED7Code = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10
2、;/uint i=0,a=16,b=16,c=16;uchar num4=16,16,16,16;uchar _FLAG = 0;uchar hang=0;uchar lie=0;uchar key_in; /读入的按键信息,检测是否有键按下/uint n=0;/uint buff;void delay(uchar t)uchar i,j;for(i=0;i0;j-); ;/*void cshh() IT1=1;/INT0 下降沿触发EX1=1; /使能 INT0EA=1; /开总中断PX1=0;*/uint scankey()P2=0xf0;delay(10);if(P2break;case
3、 0xb0: hang=2;break;case 0xd0: hang=3;break;case 0xe0: hang=4;break;default: break;delay(10);P2=0x0f;if(P2 break;case 0x0b: lie=2; break;case 0x0d: lie=3; break;case 0x0e: lie=4; break;default: break;delay(5); return(hang-1)*4+lie)-1);else return(16);uchar checkKey(void)uchar key_1, key_2;static uch
4、ar key;P2 = 0x0F;key_1 = P2;key_1 = key_1 delay(5);key_2 = P2;key_2 = key_2 if(key_1 = key_2)if(key_2 = 0x0f) /键弹起if(_FLAG = 1)_FLAG = 2; /_FLAG=2 表示按键弹起,一次按下动作结束return key;else if(_FLAG = 0)return 0xff;else /键按下if(_FLAG = 0)_FLAG = 1; /_FLAG=1 表示按键按下,还未弹起key = scankey();elsereturn 0xff;void display
5、()/显示函数uchar j, wei=0xff; /位选信号首先选中最右侧的 LED 灯(即 num7对应的 LED 灯)delay(1);wei=0xfe;delay(1);/* if(key_in=15)P0 = LED7Codenum0; /送段码信号else */for(j=0; j=0 /把_FLAG=0,为下一次按键按下作准备if(key_in10)m=key_in;keyFun(key_in); /把读入的新按键值向左滚动显示在 LED 灯上else if(key_in=15)key_in=m+n;num0 = key_in;/keyFun(key_in); /把读入的新按键值向左滚动显示在 LED 灯上display();/delay(8);