27,373
社区成员
发帖
与我相关
我的任务
分享
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp;
void init();
void display(uchar);
void keyscan();
void main()
{
init();
while(1)
{
keyscan();
display(num);
}
}
void init()
{
//初始化时显示所有数码管
num=1;
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xc0;
wela=0;
}
void display(uchar i)
{
dula=1;
P0=table[i-1];
dula=0;
}
void keyscan()
{
uchar i;
P3=0xfe; //初始化检测第一排键盘
for(i=0;i<4;i++)
{
//由于是用Proteus模拟,所以没有消抖检测
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp&0xf0) //因为用了for循环,所以只比较高4位即可
{
case 0xe0:num=1;
break;
case 0xd0:num=2;
break;
case 0xb0:num=3;
break;
case 0x70:num=4;
break;
}
num=num+i*4; //i表示第几排
while(temp!=0xf0) //键盘释放显示数字
{
temp=P3&0xf0;
}
}
P3=_crol_(P3,1); //每左移一位检测下一排按键
}
}
#include<reg51.h>
#define uchar unsigned char;
#define uint unsigned int;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code tablew[]={0xfe,0xfd,0xfb,0xf7};
uchar temp,num,i,j,t;
delay(t)
{
for(i=t;i>0;i--)
for(j=110;j>0;j--) ;
}
void main()
{
P1=0xff;
P0=0x00;
while(1)
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5); // 确认是否真有键按下
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1; //读回键的值
switch(temp)
{
case 0xee: num=1;
break;
case 0xde: num=2;
break;
case 0xbe: num=3;
break;
case 0x7e: num=4;
break;
}
while(temp!=0xf0) //松手检测 ,否则跳不出循环
{
temp=P1;
temp=temp&0xf0;
}
P0=table[num-1];
delay(1000);
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed: num=5;
break;
case 0xdd: num=6;
break;
case 0xbd: num=7;
break;
case 0x7d: num=8;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
P0=table[num-1];
delay(1000);
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb: num=9;
break;
case 0xdb: num=10;
break;
case 0xbb: num=11;
break;
case 0x7b: num=12;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
P0=table[num-1];
delay(1000);
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7: num=13;
break;
case 0xd7: num=14;
break;
case 0xb7: num=15;
break;
case 0x77: num=16;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
P0=table[num-1];
delay(1000);
}
}
}
}