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;/uint i
2、=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;/使能INT0 EA=1; /开总中断 PX1=0; */uint scankey() P2=0xf0; delay(10);if(P2&0xf0)!=0xf0
3、)switch(P2)case 0x70: hang=1;break;case 0xb0: hang=2;break;case 0xd0: hang=3;break;case 0xe0: hang=4;break;default: break;delay(10);P2=0x0f;if(P2&0x0f)!=0x0f)switch(P2)case 0x07: lie=1; break;case 0x0b: lie=2; break;case 0x0d: lie=3; break;case 0x0e: lie=4; break;default: break;delay(5); return(hang
4、-1)*4+lie)-1);else return(16); uchar checkKey(void)uchar key_1, key_2;static uchar key;P2 = 0x0F;key_1 = P2;key_1 = key_1 & 0x0F;delay(5);key_2 = P2;key_2 = key_2 & 0x0F;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 /
5、键按下if(_FLAG = 0)_FLAG = 1;/_FLAG=1表示按键按下,还未弹起key = scankey();elsereturn 0xff;void display()/显示函数uchar j, wei=0xff;/位选信号首先选中最右侧的LED灯(即num7对应的LED灯)delay(1);wei=0xfe;delay(1);/*if(key_in=15) P0 = LED7Codenum0; /送段码信号else */for(j=0; j4; j+)/P0 = 0xff; /delay(1);P0 = LED7Codenumj; /送段码信号P1 = wei;/送某一个LED灯的位选信号delay(1);P1 = 0xff;/清当前的位选信号delay(1);wei = wei =0 & key_in=15) /flag=2表示一次按键按下过程结束_FLAG = 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);