cancel
Showing results for 
Search instead for 
Did you mean: 

USART dropping last byte

daniel2
Associate II
Posted on February 09, 2015 at 18:27

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(USART_GetITStatus(TEST_COM1, USART_IT_TXE) != RESET)

      {   

        if (TxCount == TX_LEN){

          TxCount = 0;

          USART_ITConfig(TEST_COM1, USART_IT_TXE, DISABLE);

          USART_ClearFlag(TEST_COM1, USART_IT_TXE);

          USART_DirectionModeCmd(TEST_COM1, USART_Mode_Rx, ENABLE);

          usartOnFlag = 0;

        }

        else{

          /* Write one byte to the transmit data register */

          USART_SendData(TEST_COM1, TxBuffer[TxCount]);

          TxCount = TxCount + 1;

        }

      }

    }

2 REPLIES 2
Posted on February 09, 2015 at 18:55

TXE is Transmit Buffer Empty, it flags when the the FIRST bit of the LAST byte hits the wire, and the holding buffer for the NEXT byte is empty.

You need to use TC (Transmit Complete), when the LAST bit of the LAST byte has hit the wire.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daniel2
Associate II
Posted on February 09, 2015 at 19:51

Perfect, that was the issue. Thanks!