/*按下矩阵键盘1,2,3,4以1200BPS向上位机发送1,2,3,4;矩阵键盘
5,6,7,8以2400BPS向上位机发送5,6,7,8;类推4800BPS,9600BPS,*/
//串口调试器改为4800,9600BPS发送时,按键输出对,但是会一直向上位机发送字符。
#include <stdio.h>
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar key,flag;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void matrixkeyscan(){
uchar temp;
P3=0xfe;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0){
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0){
TH1=0xe8;
TL1=0xe8;
temp=P3;
switch(temp){
case 0xee:key=1;break;
case 0xde:key=2;break;
case 0xbe:key=3;break;
case 0x7e:key=4;break;
}
while(temp!=0xf0){
temp=P3;
temp=temp&0xf0;
}
flag=1;
}
}
P3=0xfd;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0){
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0){
TH1=0xf4;
TL1=0xf4;
temp=P3;
switch(temp){
case 0xed:key=5;break;
case 0xdd:key=6;break;
case 0xbd:key=7;break;
case 0x7d:key=8;break;
}
while(temp!=0xf0){
temp=P3;
temp=temp&0xf0;
}
flag=1;
}
}
P3=0xfb;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0){
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0){
TH1=0xfa;
TL1=0xfa;
temp=P3;
switch(temp){
case 0xeb:key=9;break;
case 0xdb:key=10;break;
case 0xbb:key=11;break;
case 0x7b:key=12;break;
}
while(temp!=0xf0){
temp=P3;
temp=temp&0xf0;
}
flag=1;
}
}
P3=0xf7;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0){
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0){
TH1=0xfd;
TL1=0xfd;
temp=P3;
switch(temp){
case 0xe7:key=13;break;
case 0xd7:key=14;break;
case 0xb7:key=15;break;
case 0x77:key=16;break;
}
while(temp!=0xf0){
temp=P3;
temp=temp&0xf0;
}
flag=1;
}
}
TR1=1;
}
void init(){
TMOD=0x20;
SM0=0;
SM1=1;
TH1=0xf4;
TL1=0xf4;
}
void main(){
init();
while(1){
matrixkeyscan();
P3=0xff;
if(flag==1){
flag=0;
SBUF=key;
while(!TI);
TI=0;
}
}
} |