[Reprinted] How to use TXE and TC logo when sending data from USART of STM32

There are 2 registers at the sending end of USART, one is the USART_DR register that the program can see, and the other is the shift register that the program cannot see. There are two flags corresponding to the USART data transmission, and one is TXE = transmit data register is empty The other is TC = end of transmission.

When the data in USART_DR is transferred to the shift register, TXE is set. At this time, the shift register begins to transmit data to the TX signal line bit by bit, but because TDR has become empty, the program can send the next byte to be sent (operation USART_DR) is written into TDR without waiting for the transmission of all bits in the shift register to end. When all bits are transmitted (after the stop bit is sent), the hardware will set the TC flag.

  On the other hand, when the USART has just been initialized and no data has been sent, there will also be a TXE flag because the transmit data register is empty. The meaning of TXEIE and TCIE is very simple. TXEIE allows interrupts to be generated when the TXE flag is '1', while TCIE allows interrupts to be generated when the TC flag is '1'.

  As for when to use which logo, you need to decide according to your needs. But I think TXE allows programs to have more time to fill in the TDR register to ensure uninterrupted data flow. TC can let the program know the exact time when the transmission ends, which is helpful for the program to control the timing of the external data flow.

 

TXE--write register DR cleared

RXNE--Read register DR to clear, can also be cleared manually by software

TC-- The read / write register DR is cleared, or it can be cleared manually by software

 

Let me talk about TC first. That is Transmission Complete. The interrupt is entered after sending a byte, which is called "interrupt after sending". In the same way as the original TI method of 8051, interrupts are sent after sending. You need to send a byte in the sending function to trigger the interrupt. The sending function is as follows

/ *******
function: interrupt mode to send a string. Use the method of judging TC. That is to judge the interrupt bit after transmission.
Input: the first address of the string
output: no
***
void USART_SendDataString (u8 * pData)
{
    pDataByte = pData;
 
    USART_ClearFlag (USART1, USART_FLAG_TC); // Clear the transmission completion flag, otherwise the data of the first byte may be lost.
    
    Provided by netizens. USART_SendData (USART1, * (pDataByte ++) ); // Must be ++, otherwise the first character t will be sent twice
}


Interrupt handler function is as follows
/ ********
* Function Name: USART1_IRQHandler
* Description: This function handles USART1 global interrupt request.
* Input: None
* Output: None
* Return: None
********* /
void USART1_IRQHandler (void)
{
    if (USART_GetITStatus (USART1, USART_IT_TC) == SET)
    {
        if (* pDataByte == '\ 0') // TC needs to read SR + write DR to clear 0, when sent to the end, go to '\ 0 'Use an if to turn off
            USART_ClearFlag (USART1, USART_FLAG_TC); // Otherwise, TC is always set, TCIE is also open, which will cause continuous interruption. Clear it, you do n’t need to turn off TCIE
        else
            USART_SendData (USART1, * pDataByte ++);
    }

}

where u8 * pDataByte; is an external pointer variable

in the interrupt handler. After sending the string, you do not need to close the TC interrupt to enable TCIE, you only need to clear the flag bit TC; this can be avoided TC == SET caused repeated entry interruption.

void USART_Config ()
{
 ........................................

 USART_ITConfig (USART1, USART_IT_TC, ENABLE); // Tramsimssion Complete, the interrupt is generated. The TC interrupt must be placed here, otherwise the first byte will still be lost

 USART_Cmd (USART1, ENABLE); // Enable USART1
}

.....................................................................

Let's talk about judging TXE. That is, Tx DR Empty, the transmit register is empty. When TXEIE is enabled, an interrupt will be generated as long as Tx DR is empty. Therefore, it must be turned off after sending the string, otherwise it will cause repeated entry interruption. This is also different from TC.

The sending function is as follows:
/ *******
Function: send string in interrupt mode. Use the method of judging TC. That is, judge the interrupt bit after sending.
Input: the first address of the string
output: no
****** /
void USART_SendDataString (u8 * pData)
{
    pDataByte = pData;
    USART_ITConfig (USART1, USART_IT_TXE, ENABLE); // As long as the send register is empty, there will always be an interrupt, so if you do not send data, turn off the send interrupt, only It is only opened when sending begins.
    
}

Interrupt handler functions are as follows:

/ ********
* Function Name: USART1_IRQHandler
* Description: This function handles USART1 global interrupt request.
* Input: None
* Output: None
* Return: None
******* * /
void USART1_IRQHandler (void)
{
    if (USART_GetITStatus (USART1, USART_IT_TXE) == SET)
    {
        if (* pDataByte == '\ 0') // The bytes to be sent to the end are NULL
            USART_ITConfig (USART1, USART_IT_TXE, DISABLE); // Because it is sent The register is empty, so it must be turned off after sending the string, otherwise as long as it is empty, it will enter the interrupt
        else
            USART_SendData (USART1, * pDataByte ++);
    }

}

In the serial port initialization function, it is not necessary to open the TXE interrupt (is in Open in the send function)

Transfer from: How to use the TXE and TC flags when the USART of STM32 sends data

Published 91 original articles · praised 17 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_23327993/article/details/105494939