code unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //表:共阳数码管 0-9
#define DAT P1 /按键值数据口
sbit key0=P1^0;
sbit key1=P1^1;
sbit key2=P1^2;
sbit key3=P1^3;
sbit key4=P1^4;
sbit key5=P1^5;
sbit key6=P1^6;
sbit key7=P1^7;
#define DATA_number P2 //数码管数据口
//-------数码管位选------
sbit H1=P2^0;
sbit H2=P2^1;
sbit H3=P2^2;
sbit H4=P2^3;
sbit DA=P2^4; //数码管驱动数据
sbit SC=P2^5; //数码管驱动数据移动电平
sbit ST=P2^6; //数码管驱动锁存输出电平
//-----------按键值返回函数------------------------
unsigned int key_scan(void)
{
unsigned int temp;
bit find=0;
DAT=0xf0;
if((DAT&0xf0)!=0xf0)
{
find=1;
if(key4==0){temp=3;}
else if(key5==0){temp=2;}
else if(key6==0){temp=1;}
else if(key7==0){temp=0;}
DAT=0x0f;
if(key3==0){temp+=12;}
else if(key2==0){temp+=8;}
else if(key1==0){temp+=4;}
else if(key0==0){temp+=0;}
}
if(find==1){return temp; }
else return 16;
}
//----------------数码管驱动输出数字函数--------------
void output(unsigned char cnt)
{
unsigned char i;
for(i=0;i<8;i++)
{
SC=0;
DA=cnt&0x80;
SC=1;
cnt<<=1;
}
}
//-----------主函数显示按键值-------------
void main(void)
{
unsigned int kk;
while(1)
{
kk=key_scan();
output(table[kk/10]);
DATA_number=0x0f; //关闭显示,位切换准备
ST=0; /锁存准备
ST=1; //数据锁存到输出
H1=0; //打开显示位0
output(table[kk%10]);
DATA_number=0x0f;
ST=0;
ST=1;
H2=0;
}
}
上一篇:51和VB数字量简易测试
下一篇:PCF8591(AD-DA)芯片-51单片机程序
推荐阅读最新更新时间:2024-03-16 14:04