Stm32 Serial Port--sign Learning

Source: Internet
Author: User

/*There are 2 registers on the transmitter side of the Usart, one is the USART_DR register that the program can see, the other is the shift register that the program cannot see, the corresponding Usart data is sent with two flags, one is the txe= sends the data register empty, the other is tc= sends the end. When the data in the USART_DR is transferred to the shift register, the TXE is set, at which point the shift register starts to transmit data to the TX signal line, but since the TDR is empty, the program can write the next byte (Operation Usart_dr) to be sent to the TDR.  Instead of waiting for all bits in the shift register to end, the hardware sets the TC flag at the end of all bits sent (after the stop bit is sent out). On the other hand, there will also be a TXE flag when the Usart is not yet sending any data, because the sending data register is empty.  The meaning of Txeie and Tcie is simple, Txeie allows an interrupt when the TXE flag is ' 1 ', while Tcie allows an interrupt 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 out the TDR registers, ensuring that the transmitted data stream is uninterrupted. TC allows the program to know the exact time of the end of the sending process and facilitates the sequencing of external data streams. txe--Write Register Dr Clear 0 rxne--Read Register Dr Clear, can also be software manual clear 0 tc--read/write register Dr Clear Zero, can also be software manual clear 0 first said TC. That is transmission complete. A byte is sent before the interrupt is entered, and this is called "post-send Break". And the original 8051 ti way, are sent only after the interrupt, you need to send a byte in the Send function to trigger the interrupt. The Send function is as follows*//******* function: Interrupt mode to send a string. Use the method of judging TC. That is, the interrupt bit is determined after sending. Input: The first address of the string output: no *******/voidUsart_senddatastring (U8 *pData) {Pdatabyte=PData; Usart_clearflag (USART1, USART_FLAG_TC);//clear the transfer completion flag bit, or you may lose the 1th byte of data. NET friend provides.Usart_senddata (USART1,* (pdatabyte++));//must be + +, or the first character T will be sent two times}//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*********/voidUsart1_irqhandler (void){    if(Usart_getitstatus (USART1, usart_it_tc) = =SET) {        if(*pdatabyte = =' /')//TC needs to read sr+ write Dr can clear 0, when sent to the last, to ' "" when you decide to turn offUsart_clearflag (USART1, USART_FLAG_TC);//otherwise the TC has been set, and Tcie is open, causing the interruption to continue. Clear off, don't turn off the Tcie.ElseUsart_senddata (USART1,*pdatabyte++ ); }}/*where U8 *pdatabyte; is an external pointer variable in the interrupt handler, after sending the string, do not close the TC interrupt enable Tcie, only need to clear the flag bit TC, so that can avoid the TC = = SET caused repeated entry interrupted. */voidUsart_config () {//........................................Usart_itconfig (USART1, USART_IT_TC, ENABLE);//tramsimssion Complete before an interruption occurs. Open TC interrupt must be placed here, or you will lose the first byteUsart_cmd (USART1, ENABLE);//Enable to USART1}/* ..................................................................... Judge Txe again. That is, TX DR empty, send register empty. When the Txeie is enabled, an interrupt is generated as long as the TX dr is empty. Therefore, you must turn off the string after it is sent, or it will cause repeated entry interrupts. This is also different from TC. The Send function is as follows:*//******* function: Interrupt mode to send a string. Use the method of judging TC. That is, the interrupt bit is determined after sending. Input: The first address of the string output: no *******/voidUsart_senddatastring (U8 *pData) {Pdatabyte=PData; Usart_itconfig (USART1, Usart_it_txe, ENABLE);//as long as the send register is empty, there will always be interrupts, so if you do not send the data, the send interrupt is closed and only opened 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********/voidUsart1_irqhandler (void){    if(Usart_getitstatus (USART1, usart_it_txe) = =SET) {        if(*pdatabyte = =' /')//The bytes to be sent are sent to the end null.Usart_itconfig (USART1, Usart_it_txe, DISABLE);//because it is a null interrupt for the send register, it must be turned off after the string is sent, or it will be interrupted if it is empty.ElseUsart_senddata (USART1,*pdatabyte++ ); }}/*in the serial port initialization function does not have to open Txe interrupt (is open in the Send function)---------------------This article from zyboy2000 's CSDN blog, full-text address click:7566647?utm_source=copy  */

Stm32 Serial Port--sign Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.