What is the reason for the interrupt after the STM32 UART2 is transmitted?

SECTION 2

Let me talk about TC first. That is Transmission Complete. Only one byte is transmitted before entering the interrupt, here called "post-transmission interrupt." As with the original 8051 TI method, the interrupt is only sent after transmission, and a byte must be sent to trigger the interrupt in the send function. Send function is as follows

Why did Lu Qi leave Baidu and where did Baidu go?

/*******

Function: The interrupt mode sends a string. Adopt the method of judging TC. That is judged after sending interrupt bit.

Input: the first address of the string

Output: None

*******/

Void USART_SendDataString( u8 *pData )

{

pDataByte = pData;

USART_ClearFlag(USART1, USART_FLAG_TC); // Clear the transfer complete flag, otherwise the first byte of data may be lost. Netizens provide.

USART_SendData(USART1, *(pDataByte++) ); // must be ++, otherwise the first character t will be sent twice

}

The 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+ and write DR to clear 0, when sent to the end, when it reaches '\0', it will be judged to be turned off by an if

USART_ClearFlag (USART1, USART_FLAG_TC);// Otherwise TC is always set, TCIE is also open, resulting in non-stop interruption. Clear out, do not turn off TCIE

Else

USART_SendData(USART1, *pDataByte++ );

}

}

Where u8 *pDataByte; is an external pointer variable

In the interrupt handler, after the string is sent, the TCIE is enabled without turning off the TC interrupt. It is only necessary to clear the flag TC. This avoids TC == SET from repeatedly entering the interrupt.

The serial port initialization function is as follows

/*********

Name: USART_Config

Function: Set serial port parameters

Enter: None

Output: None

Return: None

**********/

Void USART_Config()

{

USART_InitTypeDef USART_InitStructure;//Define a structure containing serial port parameters

USART_InitStructure.USART_BaudRate = 9600; // baud rate 9600

USART_InitStructure.USART_WordLength = USART_WordLength_8b; / 8 bits of data

USART_InitStructure.USART_StopBits = USART_StopBits_1;//1 stop bit

USART_InitStructure.USART_Parity = USART_Parity_No;//No parity

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//No hardware flow control

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Input plus output mode

USART_InitStructure.USART_Clock = USART_Clock_Disable;//clock off

USART_InitStructure.USART_CPOL = USART_CPOL_Low;

USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;

USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

USART_Init(USART1, &USART_InitStructure); // Set to USART1

USART_ITConfig (USART1, USART_IT_TC, ENABLE);//Tramsimssion Complete before generating an interrupt. Open TC interrupt must be placed here, otherwise it will still lose the first byte

USART_Cmd(USART1, ENABLE); //Enable USART1

}

Here's a question: Open TC interrupt USART_ITConfig () will lose the first byte of the string if it is placed in my USART_SendDataString(). Must be placed in the serial port initialization function will not lose. do not know why? ?

Here the author can give an explanation, you can see SECTION1 you can know why, you do this principle and SECTION1 explained about the same, it is equivalent to the delay, and you did not lose the main reason behind the data is such a sentence in your code USART_ClearFlag(USART1, USART_FLAG_TC); // Clear the transfer complete flag, otherwise the first byte of data may be lost. Netizens provide.

Say to judge TXE again. Tx DR Empty, send register 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 lead to repeated interruptions. This is also different from TC.

The send function is as follows:

/*******

Function: The interrupt mode sends a string. Use the method of determining TC. That is judged after sending interrupt bit.

Input: the first address of the string

Output: None

*******/

Void USART_SendDataString( u8 *pData )

{

pDataByte = pData;

USART_ITConfig (USART1, USART_IT_TXE, ENABLE);// As long as the send register is empty, there will be an interrupt. Therefore, if no data is sent, the transmit interrupt is turned off and it is turned on only when the send is started.

}

The 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_TXE) == SET )

{

If( *pDataByte == '\0' )//The byte to send is sent to the end NULL

USART_ITConfig (USART1, USART_IT_TXE, DISABLE);//Because it is an interrupt that sends a register empty, it must be turned off after the string is sent, otherwise it will be interrupted as long as it is empty.

Else

USART_SendData(USART1, *pDataByte++ );

}

}

In the serial port initialization function does not need to open the TXE interrupt (is open in the send function) as follows:

/************

Name: USART_Config

Function: Set serial port parameters

Enter: None

Output: None

Return: None

************/

Void USART_Config()

{

USART_InitTypeDef USART_InitStructure;//Define a structure containing serial port parameters

USART_InitStructure.USART_BaudRate = 9600; // baud rate 9600

USART_InitStructure.USART_WordLength = USART_WordLength_8b; / 8 bits of data

USART_InitStructure.USART_StopBits = USART_StopBits_1;//1 stop bit

USART_InitStructure.USART_Parity = USART_Parity_No;//No parity

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//No hardware flow control

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Input plus output mode

USART_InitStructure.USART_Clock = USART_Clock_Disable;//clock off

USART_InitStructure.USART_CPOL = USART_CPOL_Low;

USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;

USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

USART_Init(USART1, &USART_InitStructure); // Set to USART1

USART_Cmd(USART1, ENABLE); //Enable USART1

}

SECTION 3

There are two registers on the sending end of the USART. One is the USART_DR register that the program can see (the TDR in the shaded part in the figure below), and the other is the shift register that is not visible to the program.

Corresponding USART data transmission has two flags, one is TXE=transmit data register empty, and the other is TC=transmission end; compared with the figure below, when TDR data is transferred to the shift register, TXE is set and shift The register begins to transmit data bit by bit to the TX signal line, but since the TDR is already empty, the program can write the next byte to be transmitted (operation USART_DR) into TDR without waiting for all the bits in the shift register to end. At the end of the bit transmission (after sending the stop bit) the hardware sets the TC flag.

On the other hand, when the USART has just been initialized without sending any data yet, it will also have the TXE flag because the transmit data register is empty at this time.

The significance of TXEIE and TCIE is simple. TXEIE allows an interrupt to be generated when the TXE flag is '1', while TCIE allows an interrupt 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 the program to have more time to fill in the TDR register to ensure that the transmitted data flow is uninterrupted. The TC can let the program know the exact time of the end of the transmission, which helps the program control the timing of the external data flow.

SECTION 4

In general, the serial port of the STM32 microcontroller is well understood and programming is not too complicated. Of course I prefer to expect that the interrupt system is as simple as the 51 MCU.

For the receiving terminal, it is RXNE. This is only generated after the reception is completed. The ISR is not entered when the USART_ITConfig (USART1, USART_IT_RXNE, ENABLE) code is executed. But the trouble is to send the relevant interrupt: TXE or TC. According to the data and test results, TXE is set after reset. That is, an interrupt request will be generated immediately after executing USART_ITConfig (USART1, USART_IT_TXE, ENABLE). So this creates a nuisance problem: if there is no real data to send, the TXE interrupt will happen, and it will not stop, which will take up a lot of CPU time, and even affect the operation of other programs!

Therefore, it is recommended not to enable TXE interrupts during initialization. TXE is enabled only when data is to be sent (especially for strings and arrays). Immediately after the completion of the transmission to close it, so as not to cause unnecessary trouble.

For transmission, it is necessary to pay attention to the difference between TXE and TC - briefly describe here, assuming that the serial data register is DR, the serial port shift register is SR, and the TXD pin TXDpin, the relationship is DR-"SR-" TXDpin. When the data in the DR is transferred to the SR, TXE is set, TXE can be set to 0 when data is written to the DR, and TC is set to 1 if all the data in the SR is shifted out by the TXDpin and no data has entered the DR. Also note that TXE can only be set to 0 by writing to DR. It cannot be cleared directly, and TC can write and clear it directly.

For sending a single character can be considered without interruption, directly to complete the query.

For sending string/array data, the only thing to consider is to turn off the transmit interrupt only after the last character is sent. There are two cases here: For sending a displayable string, it ends with 0x00, so In the ISR, 0x00 is used as the condition to close the transmit interrupt (TXE or TC); the second case is to send binary data, which is any data in the middle of 0x00~0xFF, and you cannot use 0x00 to judge the end. You must know The specific length of the data.

Here is a simple analysis of the implementation of the above code: TXE interrupt generated from the DR into the SR, the effect is the latter character into the DR. For the first case, if it is a displayable character, execute USART_SendData to write DR (ie, clear TXE). After the last displayable character is sent from SR to SR, the generated TXE interrupt is found to be sent to DR. The character is 0x00 - of course not - at this point the TXE interrupt is closed and the string sending process is complete. Of course, an implied result cannot be ignored at this time: that is, the last displayable character transitions from DR to SR and TXE is set to 1, but the TXE interrupt is turned off. Therefore, the ISR will be entered as soon as the TXE interrupt is turned on again. For the second case, the result is the same as the first one.

For the first case, the program can be written like this: where TXS is the string that holds the data to send, and TxCounter1 is the index value:

Extern __IO uint8_t TxCounter1;

Extern uint8_t *TXS;

Extern __IO uint8_t TxLen;

Void USART1_IRQHandler(void)

{

If(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

{

If(TXS[TxCounter1]) //If it is a displayable character

{ USART_SendData(USART1,TXS[TxCounter1++]);}

Else //Turn off the TXE interrupt after the send completes.

{USART_ITConfig(USART1,USART_IT_TXE,DISABLE);}

}

}

For the second case, the same as above, where TXLen represents the length of the binary data to be sent:

Void USART1_IRQHandler(void)

{

If(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // This bit is cleared to USART_DR write operation.

{

If(TxCounter1TxLen)

{ USART_SendData(USART1,TXS[TxCounter1++]);}

Else //Turn off the TXE interrupt after sending is complete

{USART_ITConfig(USART1,USART_IT_TXE,DISABLE);}

}

}

In fact, the first case is the special form of the second, that is to say, you can use the second case to send displayable characters - of course, no one has the freedom to count how many letter spaces and punctuation marks in a sentence!

In use, as long as the TXS points to the string or array to be sent, set TxLen to the length of the data to be sent, and then execute USART_ITConfig(USART1, USART_IT_TXE, ENABLE) to immediately start the sending process. The user can check TxCounter1 to determine how many bytes were sent. Take the second case as an example:

Uint32_t *TXS;

Uint8_t TxBuffer1[]=“0123456789ABCDEF”;

Uint8_t DST2[]=“ASDFGHJKL”;

__IO uint8_t TxLen = 0x00;

TxLen=8; //Send 8 characters, and finally send 01234567

TXS=(uint32_t *)TxBuffer1; //Point TXS to string TxBuffer1

TxCounter1=0; //Reset index value

USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //Enable TXE interrupt, ie start sending process

While(TxCounter1!=TxLen); // waits for sending to complete

TXS = (uint32_t *) TxBuffer2; / / Ibid, the final sent ASDFGHJK

TxCounter1=0;

USART_ITConfig (USART1, USART_IT_TXE, ENABLE);

While(TxCounter1!=TxLen);

The above is the best solution I think, but how long the serial port interrupt mode data is interrupted, I think it still takes a lot of CPU time, compared to DMA mode is much better, because the DMA send a string interrupt up to two Times (semi-transfer completed, full transfer completed), and the serial port into a 16C550-like device.

E-bikes Wiring Harness

In the e-bike market where innovative, minimalistic designs are talk of the day, we see that manufacturers are constantly looking for smart electrical wiring systems and reliable connections to support these developments. With years of experience with our e-bike customers, we have already worked successfully around themes like the integration of brake & signal, battery and number plate lighting, high power electrical connection.

Related products:bike wire assembly, ebikes connector,e-bike cable assemblies, waterproof e-bike cable, waterproof e-bike connector

bike wire assembly, ebikes connector,e-bike cable assemblies, waterproof e-bike cable, waterproof e-bike connector

ETOP WIREHARNESS LIMITED , https://www.wireharnessetop.com