if( INT_0 != 0 ) //再次判断是否有键按下
{
EA=0;
scan_key();
delay(50);
INT_0 =judge_hitkey();
while( INT_0 !=1); //等待按键释放
EA=1;
}
在EA=0;这句前,有无加上了,重新把 INT_0 =judge_hitkey(); 再赋回来了?,,我在郭天祥的板子上调试不行; EXITEC(93): warning C206: 'manage_key1': missing function-prototype;
这是我花两个多小时写得51单片机,矩阵键盘的显示,希望能对你有帮助;
#include<reg52h>
#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};
sbit dula=P2^6;
sbit wela=P2^7;
void init();
void display(uchar);
uchar keyscan();
uchar temp,num; //键盘扫描
void delay(uint xms) //延时子函数
{
int i,j;
for(i=xms;i>0;i--) //延时xms毫秒
for(j=110;j>0;j--);
}
void main()
{
init();
while(1)
{
keyscan();//不断扫描键盘;
display(num);
}
}
void init()
{
num=0xff; //控制让程序开始时不出现乱码;
wela=1;
P0=0xc0; //打开数码管显示,静态显示;
wela=0;
}
uchar keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;//按位与,只能用&;
if(temp!=0xf0) //在用while语句时,一定要加上去抖动,否则程序会停不下来的哦;
{ //还是统一用if语句吧,只有在去抖动才一定要用if语句;
delay(5); //至于中括号加在那都无所谓啦;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
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) //去抖动只能用while ,只有松开手才会执行下面的,才会有数码显;
{ ////一松开手,就相当于把P3口的电平改变了;所以去抖不能在switch前
temp=P3;
temp=temp&0xf0; //不能不要,只有松开手,才会退出这个循环;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;//按位与,只能用&;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
// temp=temp&0xf0; //该句子一定不能要,因为下面判断的,只是P3口的电平状态而已;
}
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=P3;
temp=temp&0xf0;
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;//按位与,只能用&;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
if(temp!=0xf0)
{
temp=P3;
// temp=temp&0xf0;
}
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) //去抖动只能用while ,只有松开手才会执行下面的,才会有数码显;
{ ////一松开手,就相当于把P3口的电平改变了;所以去抖不能在switch前
temp=P3;
temp=temp&0xf0; //不能不要,只有松开手,才会退出这个循环;
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;//按位与,只能用&;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
if(temp!=0xf0) //该句子不能用while语句,因为如果用while语句,如果松手了,P3的电平就改变了,temp的值也会改变的。
//因此建议,非必要,还是用if语句;
{
temp=P3;
// temp=temp&0xf0;
}
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) //去抖动只能用while ,只有松开手才会执行下面的,才会有数码显;
{ ////一松开手,就相当于把P3口的电平改变了;所以去抖不能在switch前
temp=P3;
temp=temp&0xf0; //不能不要,只有松开手,才会退出这个循环;
}
return num;
}
void display(uchar num)
{
P0=table[num-1];
dula=1;
dula=0;
}
下面的是郭天祥老师写的
#include<reg52h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp,num1;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan();
void display(uchar aa);
void main()
{
num=17;
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xc0;
wela=0;
while(1)
{
display(keyscan());
}
}
void display(uchar aa)
{
dula=1;
P0=table[aa-1];
dula=0;
}
uchar keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
return num;
}
控制台程序 还是 MFC ?
1 如果是控制台程序,使用 GetAsyncKeyState 函数
我以前写个一个小程序,你可以参考一下
2 如果是MFC程序,你需要在PreTranslateMessage(MSG pMsg)中捕获按键的消息,然后设置焦点(SetFocus())。类似下面的样子:
BOOL CXXXDialog(or CXXXView)::PreTranslateMessage(MSG pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_LEFT) {
//左键
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_RIGHT)
{
//右键
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_UP)
{
//上键
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_DOWN)
{
//下键
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_TAB)
{
//Tab 键
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_ESCAPE)
{
//ESC 键
return CWnd::PreTranslateMessage(pMsg);
}
else if(pMsg->wParam == VK_RETURN )
{
//回车键
return CWnd::PreTranslateMessage(pMsg);
}
}
a
return CWnd::PreTranslateMessage(pMsg);
}
为了你这个问题,我特地找出来我2008年写的东西。
>
#include<stdioh>
#include<biosh>
#include<conioh>
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x11b
void main()
{
int l=15,r=35,u=10,d=18,x,y,key;
void frame(int,int,int,int);
void move(int,int ,int ,int ,int ,int ,int );
clrscr();
x=(l+r)/2;
y=(u+d)/2;
textcolor(YELLOW);
frame(l,r,u,d);
gotoxy(x,y);
cprintf("%c",1);
key=bioskey(0);
while(key!=ESC)
{
move(key,&x,&y,&l,&r,&u,&d);
key=bioskey(0);
}
}
void frame(int l,int r,int u,int d)
{
int i;
for(i=l;i<r+1;i++)
{
gotoxy(i,u);
printf("%c",219);
gotoxy(i,d);
printf("%c",219);
}
for(i=u+1;i<d+1;i++)
{
gotoxy(l,i);
printf("%c",219);
gotoxy(r,i);
printf("%c",219);
}
}
void move(int key,int x,int y,int l,int r,int u,int d)
{
gotoxy(x,y);
putchar(32);
if(key==UP)
{
if(y==u+1)y=d-1;
else (y)--;
}
if(key==DOWN)
{
if(y==d-1)y=u+1;
else (y)++;
}
if(key==LEFT)
{
if(x==l+1)x=r-1;
else (x)--;
}
if(key==RIGHT)
{
if(x==r-1)x=l+1;
else (x)++;
}
gotoxy(x,y);
putchar(1);
}
回复:这个程序是在tc20上运行成功的,是c程序,根据错误提示,你是把它作为c++来运行的。
补充:在保存文件时扩展名改为c
以上就是关于51单片机4*5键盘程序。全部的内容,包括:51单片机4*5键盘程序。、9、编写一个Windows应用程序,在窗口用户区中绘制一个矩形,用键盘上的上下左右光标键可以使该矩形分别向4、C51单片机矩形键盘的组合键怎样实现(C语言)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)