问题

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

  • 【周五晚调试结果】

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

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

    我写了一个例程来发送相同的数字 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;
      }
    
    
      //*****************************************************************//
    

    这是处理此特定 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));
        }
      }
    
      //***********************************************//
    

    这是我在通过“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
    

    最佳答案

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

    关于embedded - PIC32 UART 丢弃字节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15277816/

    10-13 03:40