花儿一样ha 发表于 2019-7-6 08:28

串口通信

#include "stc15.h"
#include "UART.h"
#include "queue.h"
#define uchar unsigned char
#define uintunsigned int
#define uint32   unsigned long
sbit LED1=P1^5;
sbit LED2=P1^4;
extern QUEUE que;

unsigned int pos =0;

void Uart_IRQHendler() interrupt 8 using 1
{
   unsigned char recv_data={0};
   unsigned char j;
   unsigned char ucTemp=0;
          if(S2CON & 0x01)
          {
            LED2=0;
            S2CON &=0xFE;
            ucTemp=S2BUF;
         /* if(pos<14)
               {
                     recv_data = ucTemp;//将队列中的数据均取出到这个数组中               
                     aa = ((aa<<8)|ucTemp);
               }
               else               
                      pos =0;
             if(aa==0xFFCFFFF)
               {
                  size = pos;
                  pos = 0;   //读出后复位
                  aa =0;
               }
               else
                  size = 0;
               if(size>0)
             {   
               LED1=0;
               for(j=0;j<size;j++)
               {
                     Uart1Data_SendByte(recv_data);
               }
               size =0;
             }*/               
               
               Uart1Data_SendByte(ucTemp);
            /*S2BUF=ucTemp;
            while(!(S2CON & 0x02));
                S2CON &= 0xFD;*/
         
          // Uart1Data_SendByte(ucTemp);


             if((que._head+1)%QUEUE_MAX_SIZE!=que._tail)
            {
                que._data=ucTemp;
                que._head=(que._head+1)%QUEUE_MAX_SIZE;
            }
   }
   }



   
   int main(void)
{
    unsigned char recv_data={0};
    unsigned char size =0;
    unsigned char i,j,temp;
    uint16 aa=0;
    uint32 aa=0;
    LED1=1;
    LED2=1;


    Uart2Init();
    Uart1Init();
    EA =1;
    IE2=0x01;
//    while(1){}

// 20190624      
//    while(1)
//    {
//      while(!RI);
//      RI=0;
//      temp=SBUF;
//      SBUF=temp;
//      while(!TI);
//      TI=0;
//    }
//end

    queue_reset();
    //delayms(10);

   while(1)
   {      
      if(queue_size(que)>0)
         {
             for(i=0;i<queue_size(que);i++)
            {
               if(que._tail!=que._head)//非空
               {
                     temp = que._data;
                     que._tail= (que._tail+1)%QUEUE_MAX_SIZE;
               }//此函数是从队列中取一个数据,而且从队尾开始取
                  
               if(pos==0&&temp!=0xEE)//指令第一个字节必须是帧头,否则跳过
                     continue;
                                 
               if(pos<QUEUE_MAX_SIZE)
               {
                     recv_data = temp;//将队列中的数据均取出到这个数组中               
                     aa = ((aa<<8)|temp);
               }
               else               
                      pos =0;      //溢出后复位
               
               //if(temp==0x0A)
               if(aa==0xFFFFFCFF)
               {
                  size = pos;
                  pos = 0;   //读出后复位
                  aa =0;
               }
               else
                  size = 0;
                // Uart2Data_SendByte(temp);
             }
            
             if(size>0)
             {   
               LED1=0;
               for(j=0;j<size;j++)
               {
                     Uart1Data_SendByte(recv_data);
               }
               size =0;
             }
         }
   
    }
}   
这个不定长的串口接收程序,出现的问题是用电脑的串口助手,发送一些列是数据比如EE 13 14等,都可准确接收并且返回电脑,但是用大彩屏就是另一个相当于上位机的发送一样的数据,就出现了乱码,而且只接收到一个数据,不知道原因是什么,谢谢大家可以帮忙看一下

airwill 发表于 2019-7-13 10:37

检查一下信号波形, 也可以两个对比一下, 可能不是软件的问题

牧天尊 发表于 2019-7-15 22:16

两者发送数据有多大差异?
页: [1]
查看完整版本: 串口通信