51单片机 c语言 按键和数码管显示
3个回答
2013-08-22
展开全部
#include<reg51.h>
unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void led_display(unsigned char num);
unsigned char key_scan();
sbit led1=P2^0;
sbit led2=P2^1;
void main()
{
unsigned char led_key;
P1=0;
while(1)
{
led_key=key_scan();
led_display(led_key);
}
}
void led_display(unsigned char num)
{
unsigned char ten,single;
int delay;
ten=num/10;
single=num%10;
delay=100;
for(delay=200;delay>0;delay--)
{
P0=led[ten];
led1=0;
led1=1;
P0=led[single];
led2=0;
led2=1;
}
}
/////////////////////////////////////////
unsigned char key_scan(void)
{
unsigned char temp,key;
P1=0xfe;
temp=P1&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=1;
break;
case 0xd0: key=2;
break;
case 0xb0: key=3;
break;
case 0x70: key=4;
}
return key;
}
P1=0xfd;
temp=P1&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=5;
break;
case 0xd0: key=6;
break;
case 0xb0: key=7;
break;
case 0x70: key=8;
}
return key;
}
P1=0xfb;
temp=P1&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=9;
break;
case 0xd0: key=10;
break;
case 0xb0: key=11;
break;
case 0x70: key=12;
}
return key;
}
P1=0xf7;
temp=P1&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: key=13;
break;
case 0xd0: key=14;
break;
case 0xb0: key=15;
break;
case 0x70: key=16;
}
return key;
}
}
你好!很高兴为您解答!我有欢迎采纳!!以上传附件!!
追问
并不是我想要的结果哦
展开全部
只有两个按键?
数码管有几个?共阴、共阳?
追问:我没学移位 请给个 【 每按键一次,移位一次}的编程 谢谢
下面的即是:
disp_buff [7] = disp_buff [6];
disp_buff [6] = disp_buff [5];
disp_buff [5] = disp_buff [4];
disp_buff [4] = disp_buff [3];
disp_buff [3] = disp_buff [2];
disp_buff [2] = disp_buff [1];
disp_buff [1] = disp_buff [0];
disp_buff [0] = key;
全部程序,可参考:
http://hi.baidu.com/do_sermon/item/65af71290819959eb6326365
数码管有几个?共阴、共阳?
追问:我没学移位 请给个 【 每按键一次,移位一次}的编程 谢谢
下面的即是:
disp_buff [7] = disp_buff [6];
disp_buff [6] = disp_buff [5];
disp_buff [5] = disp_buff [4];
disp_buff [4] = disp_buff [3];
disp_buff [3] = disp_buff [2];
disp_buff [2] = disp_buff [1];
disp_buff [1] = disp_buff [0];
disp_buff [0] = key;
全部程序,可参考:
http://hi.baidu.com/do_sermon/item/65af71290819959eb6326365
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询