First, serial communication concept 1, abbreviation
Usart:universal synchronous/asynchronous Receiver/transmitter
Universal synchronous/asynchronous receive and transmitter 2, use
(1) Synchronous communication
The two sides must first establish the synchronization, that is, the clock to adjust to a frequency, send and receive the double-sent and receive continuous synchronization bitstream.
(2) Asynchronous communication
The receiving end must be ready at all times, while the sending side can choose when to send, but it needs to send a start flag and an end flag to indicate a send phase.
The advantage of asynchronous communication is simplicity.
3. Classification
4, STM32F103CX: Has 3 Usart
RX: Indicates an acceptable end
TX: Indicates when the sending side
Second, STM32 usart Register configuration 1, each Usart port: A total of 7 settings register
① A status register (USART_SR)
② a data register (USART_DR): It is composed of two registers, one for send (send register TDR), one to receive (Receive register RDR)
③ a baud rate register (USART_BRR)
④ a control register 1 (USART_CR1)
⑤ A control Register 2 (USART_CR2)
⑥ A control register 3 (USART_CR3)
⑦ a protection time and Prescaler Register (USART_GTPR)
2, in the firmware function library "stm32f10x_map.h" file, the corresponding definition:
/* ----------------Universal Synchronous Asynchronous Receiver transmitter-- */ struct{ vu16 SR; U16 RESERVED0; Vu16 DR; U16 RESERVED1; Vu16 BRR; U16 RESERVED2; Vu16 CR1; U16 RESERVED3; Vu16 CR2; U16 RESERVED4; Vu16 CR3; U16 RESERVED5; Vu16 GTPR; U16 // definition of USARTX serial port with structural usart_typedef (i.e., 7 settings register for the definition of the USARTX serial port)
3. Register Detail function table (1) Status register (USART_SR)
(2) Data register
(3) Bit rate register
(4) Control register
(5) Control Register 2
(6) Control Register 3
(7) Protection time and Prescaler Register (USART_GTPR)
Three, STM32 serial communication programming 1, programming steps
(1) When programming, the specific configuration of the Usartx serial port is starting from the USARTX register group. First, the USARTX Register group is defined with the struct usart_typedef. There is code, there is a well-packaged haha haha a
#include"Bsp_usart1.h"voidUsart1_config (void) {gpio_inittypedef gpio_initstructure; Usart_inittypedef usart_initstructure; /*config USART1 clock*/Rcc_apb2periphclockcmd (Rcc_apb2periph_usart1|Rcc_apb2periph_gpioa, ENABLE); /*USART1 GPIO config*/ /*Configure USART1 Tx (pa.09) as alternate function Push-pull*/Gpio_initstructure.gpio_pin=Gpio_pin_9; Gpio_initstructure.gpio_mode=gpio_mode_af_pp; Gpio_initstructure.gpio_speed=Gpio_speed_50mhz; Gpio_init (Gpioa,&gpio_initstructure); /*Configure USART1 Rx (pa.10) as input floating*/Gpio_initstructure.gpio_pin=gpio_pin_10; Gpio_initstructure.gpio_mode=gpio_mode_in_floating; Gpio_init (Gpioa,&gpio_initstructure); /*USART1 mode config*/usart_initstructure.usart_baudrate=115200; Usart_initstructure.usart_wordlength=usart_wordlength_8b; Usart_initstructure.usart_stopbits=Usart_stopbits_1; Usart_initstructure.usart_parity=Usart_parity_no; Usart_initstructure.usart_hardwareflowcontrol=Usart_hardwareflowcontrol_none; Usart_initstructure.usart_mode= Usart_mode_rx |Usart_mode_tx; Usart_init (USART1,&usart_initstructure); Usart_cmd (USART1, ENABLE);}
(2) If the initialization function is available, we can program the next function directly.
//x?? ′?· ¢?í/*? èá?? ó′?? Ú£?′ò?a′?? Ú£????? 1152002¨ì?? Ê£? ??????? Ú£????? 3ìdò£?μ???? Óê?μ?x?? ′?£? μ??? · ¢?íx?? ′?£?3¤?èd?óú100£?μ¥??? Úè?2? ? óê?oó μ???? Μ???? £ò2?éò? Dt?? 3ìdò£?? Óê? Ò??? · Μ?? Ò??? £?? éò? ¢?íè?òa3¤?èμ?x?? ′?£? ′ú??? úx¢êí2?? £? ¢?íêyx?? Ò?? X??? £??? Ê? Μ?ê?êyx?? Ó1£?? Ò?? X???? Ó1. ′?? Ú ¢?íμ?ê?x?? Μ? Ascii?? £?êyx?0?a9ê? 0X30-0X39,A-ZÊ?0X61-0X7A,A-ZÊ?0X41-0X5A? éò????? ′?? Ú?? ê?? Ê?? Ahex?? Ê?£¨ê?áù?????? Ê?£? 2é?′?á1?? £*/#include"stm32f10x.h"#include"Bsp_usart1.h"voidUsart1_config (void); uint_least8_t ch[ -],re[ -],i,j;#defineEndChar '. '//ê?è?μ?x?? ′?? áê??voidDelayvoid) {uint16_t i=6000; while(i--);} unsignedChardisplay[]="???? Μ?xóéè?? D?á401êμ?éêò?£";//Μ¥??? Ú ¢?í?? Pc?úμ?x?? What?/*int main (void) {usart1_config (); ′?? Ú3?ê?? ˉfor (j=0;display[j]!= ' "; j + +)//¢?íx?? ′? {Usart_senddata (usart1,display[j]); ¢?íò??? Êy? Y while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET); Μè′y ¢?í?áê? } while (1) {while (Usart_getflagstatus (USART1, usart_flag_rxne) = = RESET); Μè′ypc?úμ?êy? Yμ?à′i = Usart_receivedata (USART1); ? óê?êy? Y if (i== ' 9 ') i= ' 0 '-1; if (i== ' z ') i= ' a '-1; if (i== ' Z ') i= ' A '-1; Usart_senddata (usart1,i+1); ¢?íò??? Êy? Y while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET);//μè′y ¢?í?áê? }}*//**/intMainvoid) {usart1_config (); //What do you do ? Ú3?ê?? ˉt for(j=0;d isplay[j]!=' /'; j + +)//¢?íx?? What?{usart_senddata (usart1,display[j]); //¢?íò??? Êy? Y while(Usart_getflagstatus (USART1, usart_flag_txe) = = RESET);//μè′y ¢?í?áê? } while(1) { while(Usart_getflagstatus (USART1, usart_flag_rxne) = = RESET);//μè′ypc?úμ?êy? yμ?à′re[0] = Usart_receivedata (USART1);//? óê?êy? YI=0; while(Re[i]!=endchar)//êy? y?1?? ¢?í?áê?{i++; while(Usart_getflagstatus (USART1, usart_flag_rxne) = = RESET);//μè′yêy? yμ?à′Re[i] = Usart_receivedata (USART1);//? óê?êy? Y } for(j=0; j<i+1; j + +) Ch[j]=re[j];//??? X?? What?Delay ();//′úì?êμ?êó|ó?? Dμ?′|àíoˉêy for(j=0; j<i;j++)//¢?í?? Pc?ú{usart_senddata (usart1,ch[j]); //¢?íò??? Êy? Y while(Usart_getflagstatus (USART1, usart_flag_txe) = = RESET);//μè′y ¢?í?áê? } }}
Electric Racing Rookie Camp Training (III)--STM32F103CB serial communication