STM32 serial communication (F1 series comprising two and three USART UART)
First, SCM and PC serial communication purpose and significance:
Since the birth of the microcontroller from its stable performance, low cost, powerful, it has been widely used in smart instrumentation, industrial equipment, and household electronic consumer products. Input and output control of the microcontroller, in addition directly connected to a keypad and LCD display or the like, in general, are in communication through the serial port and the host computer PC. This will not only be able to remote control, and can take advantage of a PC powerful data processing capabilities as well as friendly control interface. Controlling the microcontroller in the case of using a general PC, and are based on the operating system of the host computer as a platform, the advantage friendly interface, and programming operations are relatively easy. Therefore, the PC industry has important practical significance and Serial communication.
The UART : Universal Asynchronous Receiver / Transmitter UART
The USART : Universal Synchronous Asynchronous Receiver / Transmitter Universal Synchronous / Asynchronous Receiver Transmitter
Serial Communications
Press the direction of data transfer:
a, simplex: data transmission only support data transmission in one direction
B, half-duplex: allowing data transmission in both directions, however, at a time, allows data transmission only in one direction, it is actually Simplex A handoff direction
C, full duplex: allowing data transmission in both directions simultaneously, and therefore is a combination of two full-duplex communication simplex communication mode, he requests the sending and receiving apparatus by a separate receive and transmit capability.
Communication by serial communication:
a, synchronous communication: synchronization with clock signal transmission SPI, IIC communication interface
B, asynchronous communication: the clock synchronization signal transmission without UART (Synchronous Asynchronous Receiver Transmitter), a single bus
USART:1、2、3 UART:4、5
Second, the serial communication procedure
Three, STM32F10x programming
This procedure is to function through the serial port to a STM32 '1', lit LED1, transmitting '2', the lighting LED2. Simultaneously receive data to the microcontroller received data back to the computer
Functions
1 #include "my_usart.h" 2 #include "stm32f10x.h" 3 4 void My_USART1_Init(void) 5 { 6 GPIO_InitTypeDef KST_GPIO_Structure; 7 USART_InitTypeDef KST_USART_Structure; 8 NVIC_InitTypeDef KST_NVIC_Structure; 9 10 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 11 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1时钟 12 13 = GPIO_Mode_AF_PP KST_GPIO_Structure.GPIO_Mode; // multiplexing push-pull output 14 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_9; 15 KST_GPIO_Structure.GPIO_Speed = GPIO_Speed_10MHz; 16 GPIO_Init (with GPIOA, & KST_GPIO_Structure); // initialize GPIOA.9 . 17 18 is KST_GPIO_Structure.GPIO_Mode = GPIO_Mode_IN_FLOATING; / / floating input . 19 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_10; 20 is KST_GPIO_Structure.GPIO_Speed = GPIO_Speed_10MHz; 21 is GPIO_Init (with GPIOA, & KST_GPIO_Structure); //Initialization GPIOA.10 22 is 23 is KST_USART_Structure.USART_BaudRate = 115200 ; // set the baud rate 115200 24 KST_USART_Structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // no hardware flow control 25 KST_USART_Structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // transceiver mode 26 is KST_USART_Structure. = USART_Parity_No USART_Parity; // no parity bit 27 KST_USART_Structure.USART_StopBits = USART_StopBits_1; // a stop bit 28 KST_USART_Structure.USART_WordLength = USART_WordLength_8b; //8-bit word length mode data 29 30 USART_Init (the USART1, & KST_USART_Structure); // initialize the serial port. 1 31 is USART_Cmd (the USART1, the ENABLE); // enable serial port 32 USART_ITConfig (the USART1, USART_IT_RXNE, the ENABLE); // enable interrupts 33 is 34 is = KST_NVIC_Structure.NVIC_IRQChannel USART1_IRQn; 35 KST_NVIC_Structure.NVIC_IRQChannelCmd = the eNABLE; // the IRQ enable passage 36 KST_NVIC_Structure.NVIC_IRQChannelPreemptionPriority = . 1 ; // preemption priority 1 37 [ = KST_NVIC_Structure.NVIC_IRQChannelSubPriority 1 ; // child priority 1 38 is NVIC_Init (& KST_NVIC_Structure); // the specified parameters are initialized NVIC registers 39 } 40 41 is void USART1_IRQHandler ( void ) // COM1 terminal services function 42 is { 43 is U8 RES; 44 is IF (USART_GetITStatus (the USART1, USART_IT_RXNE)) // receive interrupt 45 { 46 is RES = USART_ReceiveData (the USART1); // read the received data 47 IF (RES ==' . 1 ' ) 48 { 49 GPIO_ResetBits (GPIOB, GPIO_Pin_5); // lighting LEDl 50 USART_SendData (the USART1, RES); // send data 51 is } 52 is IF (RES == ' 2 ' ) 53 is { 54 is GPIO_ResetBits (GPIOE., GPIO_Pin_5); // lit LED2 55 USART_SendData (the USART1, RES); // send data 56 } 57 is 58 } 59 }
The main function
. 1 #include " stm32f10x.h " 2 #include " my_usart.h " . 3 #include " led.h " . 4 . 5 int main ( void ) . 6 { . 7 LED_Init (); // the LED initialized . 8 NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2); // provided NVIC interrupt packet 2: 2 preemption priority, priority 2 response . 9 My_USART1_Init (); // serial port initialization 10 the while ( . 1 ); . 11 }