打印

串口问题

[复制链接]
1034|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dfsa|  楼主 | 2013-5-26 08:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*按下矩阵键盘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;       
}
}       
}

相关帖子

沙发
yybj| | 2013-5-26 08:51 | 只看该作者
TX线上加个上拉看看,
检查下MAX232周围有没有线接错了

使用特权

评论回复
板凳
秋天落叶| | 2013-5-26 09:11 | 只看该作者
yybj 发表于 2013-5-26 08:51
TX线上加个上拉看看,
检查下MAX232周围有没有线接错了

试试看吧

使用特权

评论回复
地板
hsbjb| | 2013-5-26 09:14 | 只看该作者
检查下MAX232

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

282

主题

2404

帖子

2

粉丝