矩阵键盘赋值求助
在89c51中用矩阵键盘给数组赋值,实现不了,那位好心的高手帮我看看我的程序有什么问题呀,万分感谢。#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
static uchar key_flag;
void delay_ms(uint timer)
{
uchar j = 0;
while(timer--)
{
for(j = 124; j>0; j--)
{
;
}
}
}
uchar code DIS_CODE={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x0c};
void disp_num(uchar num)
{ uchar sep_num;
uchar ten_num;
sep_num=num%10;
ten_num=num/10;
P1=0x02;
P0=DIS_CODE;
delay_ms(5);
P1=0x01;
P0=DIS_CODE;
delay_ms(5);
}
uchar scan_key(void)
{ uchar key=0;
uchar tmp=0;
uchar tmp1;
P2=0x0f;
delay_ms(1);
if(0x0f!=P2)
{
delay_ms(10) ;
if(0x0f!=P2)
{ tmp=P2^0x0f;
key_flag=1;
switch(tmp)
{
case 1: key=0;
break;
case 2: key=1;
break;
case 4: key=2;
break;
case 8: key=3;
break;
default:key=0;
}
P2=0xf0;
delay_ms(1);
tmp1=P2;
tmp=P2^0xf0;
switch(tmp)
{ case 0x10:key+=0;
break;
case 0x20:key+=4;
break;
case 0x40:key+=8;
break;
case 0x80:key+=12;
break;
default:key=0;
}
while(tmp1==P2){ ; }
}
}
if(tmp1==P2){key_flag=1; }
else{key_flag=0;}
return key;
}
void main(void)
{ uchar key=0;
uchar tmp=0;
uint j=11;
uint b=0;
uint k=0;
uchar phone;
P0=0xff;
P1=0xff;
key=0;
while(j)
{tmp=scan_key();
if(key!=tmp)
{ if(tmp!=0)
{key=tmp;
}
}
disp_num(key);
if(key_flag==1)
{
if(key>=1&&key<10){ phone=key; b++;j--;}
}
}
for(k=0;k<11;k++)
{ disp_num(phone);
delay_ms(300); }
}
if(key>=1&&key<10){
写成if((key>=1)&&(key<10))试试
非常感谢lcw——swust的帮助,是这样的,改成这样就可以了!
页:
[1]