PIC32 UART丢弃字节

Use*_*r.1 3 embedded pic uart

问题

  • 外部源发送这四个字节 0x2A, 0x42, 0x78, 0x25
  • PIC32 UART不生成int
  • 外部源再发送一个字节
  • 的PIC32 UART 然后生成一个int
  • 在该int中,仅0x25出现上一次传输的字节
  • 即,前三个消失
  • 偶尔(可能是5%的时间)UART确实正确地产生了所有4个字节

[周五晚间调试结果]

我们写了另一个echo例程; 它只是读取它得到的东西并将其写回.

我们在发布模式下构建它(在另一个的建议下)

我写了一个例程,发送了25次相同的号码,看看我得到了什么.

之后,我将值加入并发送25次,然后循环.

结果将附加在此消息的末尾.

我没有得到第一个空字节.基于各种因素,我现在不会担心这一点.

接下来的五轮比赛,我完全恢复了一切

我将尝试包含所有相关的源代码,希望它不是一个文本墙.如果需要更多代码来理解这一点,请询问.

这是有问题的UART的初始化代码.我可以包括

  //*****************************************************************//
  void initUART1(void)   // UART1 for MCU1/2 communication
  {
   U1MODE=0;                     //// Mode Register, Manual DS61168D page 180
   U1MODEbits.FRZ=0;
   U1MODEbits.SIDL=0;
   U1MODEbits.IREN=0;
   U1MODEbits.RTSMD=0;

   U1MODEbits.UEN=0b00;           //// Just TX/RX, No CTS/RTS

   U1MODEbits.WAKE=1;
   U1MODEbits.LPBACK=0;
   U1MODEbits.ABAUD=0;
   U1MODEbits.RXINV=0;
   U1MODEbits.BRGH=1;
   U1MODEbits.PDSEL=0b00;
   U1MODEbits.STSEL=0;

   U1STA=0;
   U1STAbits.ADM_EN=0;
   U1STAbits.UTXINV=0;
   U1STAbits.URXEN=1;
   U1STAbits.UTXBRK=0;
   U1STAbits.UTXEN=1;
   U1STAbits.ADDEN=0;
   U1STAbits.OERR=0;     
                                 //// Status register, Manual DS61168D page 183

   //U1BRG=21;                    ////  21 for 921600 (80 MIPS)
     U1BRG=172;                   //// 172 for 115200 (80 MIPS)

   IFS0bits.U1RXIF=0;
   IPC6bits.U1IP=5;
   IPC6bits.U1IS=3;
   IEC0bits.U1RXIE=1;

   U1MODEbits.ON=1;
  }


  //*****************************************************************//
Run Code Online (Sandbox Code Playgroud)

这是处理此特定UART的中断服务程序

  //*********************************************************//
  void __ISR(_UART1_VECTOR, ipl5) IntUart1Handler(void)   //MCU communication port
  {     
    if(INTGetFlag(INT_SOURCE_UART_RX(UART1)))  //if it's a rx interrupt
    {
       U1RxBuf=UARTGetDataByte(UART1);

    switch (CmdRecording)
    {
     case 0:
           if(U1RxBuf=='*')
           {
            CmdRecording=1;  //set the cmd recording flag
            Command_U1[0]=U1RxBuf;
            TimeOut=1;  //time out=1 means the timeout for MCU1 command receiver is enabled
            initT3(0x0100);  //time out=0 means the timeout for MCU1 command receiver is enabled
           }
           else
         {
          putcharUART1('$');
          putcharUART1('e');
          putcharUART1('2');
          putcharUART1('%');
         }
           break;
     case 1:
           CmdRecording=2;  //set the cmd recording flag
           Command_U1[1]=U1RxBuf;
           break;
     case 2:
           CmdRecording=3;  //set the cmd recording flag
           Command_U1[2]=U1RxBuf;
           break;
     case 3:
           CmdRecording=0;  //reset the cmd recording flag
           Command_U1[3]=U1RxBuf;
           if(U1RxBuf=='%') //if this is the last command byte
           {
                if((Command_U1[1]=='O'))
            {
               CMD_Err=0;      //clear error
             CMD_OK=1;      //send cmd OK;
            }
              else if((Command_U1[1]=='e'))
            {
               CMD_OK=0;      //clear OK
             CMD_Err=1;     //send cmd Err;
            }
            else
            Command_flag=1;
           }
           else
           {
            Command_flag=0;
           }

           disableT3(0);  //the command receiving is done, disable the time out #0 (command receving time out)
           TimeOut=0;  //the command receiving is done, disable the time out

           break;
     default:
           break;
    }
     INTClearFlag(INT_SOURCE_UART_RX(UART1));
    }

    if ( INTGetFlag(INT_SOURCE_UART_TX(UART1)) )  //if it's a tx interrupt
    {
        INTClearFlag(INT_SOURCE_UART_TX(UART1));
    }
  }

  //***********************************************//
Run Code Online (Sandbox Code Playgroud)

这是我通过"echo"系统运行字节时看到的数据; 即,读一个的例程,写一个.

PIC-32的运行频率为80 MHz,UART的运行速度为115200 bps,所以我并不担心速度.

  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
  02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 
  03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 
  04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 
  05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05
Run Code Online (Sandbox Code Playgroud)

Cli*_*ord 5

看起来UART可能有一个4字节的FIFO,并且在FIFO满之前不会产生中断.我不熟悉该器件,但通常可以对UART进行编程,以在特定FIFO级别或n字符超时后生成中断,以便可以读取非完整FIFO中的数据.此外,如果是这种情况,您的ISR应该循环直到FIFO清空,而不是简单地读取单个字符.