打印

stc89c52串口传输问题,请指教

[复制链接]
1704|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
popotang|  楼主 | 2011-12-28 23:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int
void send(uchar a)
{

SBUF=a;
ES=1;
while(!TI);
TI=0;
ES=0;
}
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
  for(y=110;y>0;y--);   
}
void init()
{  
TMOD=0x20;
TH1=0xFD;
TL1=0xFD;
// PCON=0;
TR1=1;
SM0=0;
SM1=1;
EA=1;
ES=0;
TI=0;
}
void keyscan()
{
   uchar temp,num;
   P3=0xfe;
   temp=P3;
   
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
     delay(10);
     temp=P3;
     temp=temp&0xf0;
     if(temp!=0xf0)
     {
      temp=P3;
     switch(temp)
      {
       case 0xee:num=0;
        break;
       case 0xde:num=1;
        break;
       case 0xbe:num=2;
        break;
       case 0x7e:num=3;
        break;
      }
     while(temp!=0xf0)
      {
       temp=P3;
       temp=temp&0xf0;
      }
      send(num);
     }
    }
   P3=0xfd;
   temp=P3;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
     delay(10);
     temp=P3;
     temp=temp&0xf0;
     if(temp!=0xf0)
     {
      temp=P3;
      switch(temp)
      {
       case 0xed:num=4;
        break;
       case 0xdd:num=5;
        break;
       case 0xbd:num=6;
        break;
       case 0x7d:num=7;
        break;
      }
     while(temp!=0xf0)
      {
       temp=P3;
       temp=temp&0xf0;
      }
      send(num);
     }
    }

   P3=0xfb;
   temp=P3;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
     delay(10);
     temp=P3;
     temp=temp&0xf0;
     if(temp!=0xf0)
     {
      temp=P3;
     switch(temp)
      {
       case 0xeb:num=8;
        break;
       case 0xdb:num=9;
        break;
       case 0xbb:num=10;
        break;
       case 0x7b:num=11;
        break;
      }
     while(temp!=0xf0)
      {
       temp=P3;
       temp=temp&0xf0;
      }
      send(num);
     }
    }

   P3=0xf7;
   temp=P3;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
     delay(100);
     temp=P3;
     temp=temp&0xf0;
     if(temp!=0xf0)
     {
      temp=P3;
      switch(temp)
      {
       case 0xe7:num=12;
        break;
       case 0xd7:num=13;
        break;
       case 0xb7:num=14;
        break;
       case 0x77:num=15;
        break;
      }
     while(temp!=0xf0)
      {
       temp=P3;
       temp=temp&0xf0;
      }
      send(num);
     }
    }
//return num;
}
void main()
{
init();
while(1)
{
  keyscan();      
}
}
实验板上做键盘检测,发送0-15,在发送4、5、6、7时,串口助手一直收“00”而不是4、5、6、7,我的理解是不是因为P3^1口本身是做为发送端口,在检测4、5、6、7时,已经被定义为0而造成的,不知道对不对,如果是那样,可P3^1被定义为1时,为什么不发送“FF”,还有就是在把波特率改为4800或者2400时,串口助手一直接受数据,可我还没有去按下按键,又是为什么?请老师指点,已经闹了2天了,找不到原因啊!!谢谢!!

相关帖子

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

本版积分规则

0

主题

1

帖子

1

粉丝