AnsweredAssumed Answered

USART dropping last byte

Question asked by bujak.dan on Feb 9, 2015
Latest reply on Feb 9, 2015 by bujak.dan
Hi All,

For my application I need to send 5 bytes over UART repeatedly. I store the 5 bytes in an array (TxBuffer) and iterate through it (index 0-4) on every USART_IT_TXE interrupt. When my counter reaches 5, I disable TX and reset my counter.

The issue is that when I do this the last byte is almost always sent as 0xFF (even though I can see the TDR being updated correctly). However, if I step through the code, it sends properly. I've also tried expanding my counter by one, in which case it sends my 5 bytes correctly and then sends 0xFF. I'm assuming I'm missing some sort of timing restriction, but from what I've seen in examples and the datasheet I'm not sure what it is. 

Here's my interrupt routine:

    char TxBuffer[TX_LEN] = {"1234"};
    #define TX_LEN 5

    void USART1_IRQHandler(void)
      /* Send UART ---------------------------------------------------------------*/
        if (TxCount == TX_LEN){
          TxCount = 0;
          USART_ClearFlag(TEST_COM1, USART_IT_TXE);
          USART_DirectionModeCmd(TEST_COM1, USART_Mode_Rx, ENABLE);
          usartOnFlag = 0;
          /* Write one byte to the transmit data register */
          USART_SendData(TEST_COM1, TxBuffer[TxCount]);
          TxCount = TxCount + 1;