打印

51串口通信程序

[复制链接]
1747|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
w2340|  楼主 | 2007-8-15 16:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大虾,帮忙看一下下面的程序错在哪里?怎么接收数据丢失?

#include <reg52.h>
sbit in1 = P0^5;
sbit in2 = P0^4;
sbit stepcontl = P2^6;
sbit out = P2^1;
sbit cs = P3^3;
sbit inc = P3^4;
sbit u = P3^5;


unsigned char test,count,point=0;

unsigned char temp[4]={0};
unsigned char buf[4]={0};
unsigned char flag;

void delay (int timer);


void com_intilize (void)
     {
      SCON=0x50;          //方式1
      TMOD|=0x20;          //TR1启动,方式1
      PCON=0;
      TH1=0xfd;             //9600
      TR1=1;
      ES=1;
      REN=1;
      EA=1;                                          
      }    
  
 
  void recive (void) interrupt 4 using 3

        {   
             unsigned char ch;

             if (RI)
             {

                RI=0; 

             ch =SBUF;

             if(point==0)
             {
                if((ch=='y')|(ch=='m'))
                    
                    point++;
                
                  else point=0;
              }

              else if((point>0)&(point<4))

                     buf[point++]=ch;

               else point=0;

             }
          }


main()        

  {
    int j,i=0;

    com_intilize();

    stepcontl = 0;

    out=0;

    delay(100);

    
       while (1){

           if(point==4){

           temp[0]=buf[0];

           count=temp;

           //count=count&0x80;
        
          
           buf[0]=0;
          
          if(count==0x88){
        
          cs= 0;
          

          while(count)
          {
           
           temp[1]=buf[1];
  
           buf[1]=0; 

           temp[2]=buf[2];

           buf[2]=0;

           temp[3]=buf[3];

           buf[3]=0;

           test=temp[1];

           test=test&0x01;  

           if(test==0x01){     //正向加

           u = 1;

           inc =0;
  
           
         for(j=0;j<temp[2];j++)
        
         {
           inc=1;

           delay(50);

           inc=0;
                                 
           }

           delay(1000);

            u=0;       //要求的零电平
          
          for(i=0;i<32;i++)  
         
             {

                inc=1;
                 
                delay(50);

                inc=0;

             }
              
            delay(6670);

            out=1;

            for(j=0;j<(temp[3]);j++)

            {
               delay(100);

               }

            out=0;
  

           }
         else     // 正向减
        
          {

           u = 0;

           inc =0;    
    
           
           for(j=0;j<temp[2];j++)
        
         {
           inc=1;

           delay(50);

           inc=0;
           
           }

           delay(100);
           
          u=0;       //要求的零电平
          
          for(i=0;i<32;i++)  
         
             {

                inc=1;
                 
                delay(50);

                inc=0;

             }

 


             delay(6670);

             out=1;

            for(j=0;j<(temp[3]);j++)

            {
               delay(200);

               }


             out=0;

             }


           if(test==0x01){

           u = 1;

           inc =0;
  

          for(j=0;j<(temp[2]);j++)
        
         {
           inc=1;

           delay(50);

           inc=0;
                                 
           }

           delay(100);
   

           }
         else
        
          {

           u = 0;

           inc =0;    
    

          for(j=0;j<(temp[2]);j++)
        
         {
           inc=1;

           delay(50);

           inc=0;
           
           }
           }

       count=0;

        }
             
        } 
       }    
       }
           } 

  void delay(int timer)
     {
     while(--timer);
     }

相关帖子

沙发
w2340|  楼主 | 2007-8-15 16:12 | 只看该作者

有个错误

中断函数有个错误
应该是
void recive (void) interrupt 4 using 3

        {   
             unsigned char ch;

             if (RI)
             {

                RI=0; 

             ch =SBUF;

             if(point==0)
             {
                if((ch==0xff)|(ch==0x88))
                    
                    point++;
                
                  else point=0;
              }

              else if((point>0)&(point<4))

                     buf[point++]=ch;

               else point=0;

             }
          }

下面还有程序没有贴出

使用特权

评论回复
板凳
w2340|  楼主 | 2007-8-16 00:28 | 只看该作者

这个程序用来控制数字电位器

这个程序用来控制数字电位器,别的地方没有问题,就是接收PC机的数据不对

使用特权

评论回复
地板
星空之子| | 2007-8-16 13:44 | 只看该作者

PC机接收到的是什么数据

PC机接到数据没有,如果有收到的话,那就是你的数据类型没有处理好嘛。

使用特权

评论回复
5
w2340|  楼主 | 2007-8-16 19:37 | 只看该作者

没有

这个程序只要求单片机接收数据,PC机没有要求的

使用特权

评论回复
6
lbx_00| | 2007-8-16 20:03 | 只看该作者

if((ch==0xff)|(ch==0x88))和if((point>0)&(point<4)),错误

有语法错误,应该是

if((ch==0xff)||(ch==0x88))和if((point>0)&&(point<4))

使用特权

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

本版积分规则

17

主题

49

帖子

0

粉丝