Embedded 02 STM32 07 serial communication

STM32 serial communication (F1 series contains 3 USARTs and 2 UARTs)

1. The purpose and significance of the serial communication between the microcontroller and the PC:

Since its birth, the single-chip microcomputer has been widely used in intelligent instruments, industrial equipment and consumer electronics products for its stable performance, low price, and powerful functions. In the input and output control of the single-chip microcomputer, in addition to directly connecting the small keyboard and LCD display screen and other methods, generally communicate with the upper computer PC through the serial port. In this way, not only can remote control be realized, but also the powerful data processing functions of the PC and the friendly control interface can be utilized. In the general occasions where a PC is used to control a single-chip computer, the operating system is used as the platform of the upper computer. Its advantage is that the interface is friendly, and the programming and operation are relatively easy. Therefore, the serial communication between the PC and the single-chip computer has important practical and industrial significance.

  UART: Universal Asynchronous Receiver/Transmitter Universal Asynchronous Receiver

  USART: Universal Synchronous Asynchronous Receiver/Transmitter Universal Synchronous Asynchronous Receiver/Transmitter< /span>

Serial communication

  According to the data transmission direction:

    a, Simplex: data transmission only supports data transmission in one direction

    b, half-duplex: allows data transmission in two directions, but at a certain moment, only Allow data to be transmitted in one direction. It is actually a simplex communication that switches directions.

    c, full-duplex: Allow data to be transmitted in both directions at the same time, so full-duplex communication is two The combination of a simplex communication method requires that the sending device and the receiving device have independent receiving and sending capabilities.

Share a picture

  According to the communication mode of serial communication:

    a, synchronous communication: with clock synchronization signal transmission SPI, IIC communication interface

    b, asynchronous communication : Without clock synchronization signal transmission UART (synchronous asynchronous transceiver), single bus

Share pictures

                   USART: 1, 2,

4 >2. Serial communication process

    

alt image “src=”/wp-content/uploads/images/hardware/motherboard/1626799302414.png” >

Three, STM32F10x Programming

  The function of this program is to send ‘1’ to STM32 through the serial port, light up LED1, send’2’, and light up LED2. When receiving the data, the microcontroller sends the received data back to the computer

Sub function

 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); //Enable GPIOA clock
11 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable USART1 clock
12
13 KST_GPIO_Structure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
14 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_9;
15 KST_GPIO_Structure.GPIO_Speed ​​= GPIO_Speed_10MHz;
16 GPIO_Init(GPIOA, &KST_GPIO_Structure); // Initialize GPIOA.9
17
18 KST_GPIO_Structure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
19 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_10;
20 KST_GPIO_Structure.GPIO_Speed ​​= GPIO_Speed_10MHz;
21 GPIO_Init(GPIOA, &KST_GPIO_Structure); // Initialize GPIOA.10
22
23 KST_USART_Structure.USART_BaudRate = 115200; //Set the baud rate to 115200
24 KST_USART_Structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control
25 KST_USART_Structure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //Transceiver mode
26 KST_USART_Structure.USART_Parity = USART_Parity_No; //No parity check
27 KST_USART_Structure.USART_StopBits = USART_StopBits_1; //One ​​stop bit
28 KST_USART_Structure.USART_WordLength = USART_WordLength_8b; //The word length is 8-bit data mode
29
30 USART_Init(USART1, &KST_USART_Structure); //Initialize serial port 1
31 USART_Cmd(USART1, ENABLE); //Enable the serial port
32 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Enable interrupts
33
34 KST_NVIC_Structure.NVIC_IRQChannel = USART1_IRQn;
35 KST_NVIC_Structure.NVIC_IRQChannelCmd = ENABLE; //< span style="color: #008000;">IRQ channel enable
36 KST_NVIC_Structure.NVIC_IRQChannelPreemptionPriority = 1; //Preempt priority 1
37 KST_NVIC_Structure.NVIC_IRQChannelSubPriority = 1; //Subpriority 1
38 NVIC_Init(&KST_NVIC_Structure); //Initialize the NVIC register according to the specified parameters
39 }
40
41 void USART1_IRQHandler(void) //Serial port 1 terminal service function
42 {
43 u8 res;
44 if(USART_GetITStatus(USART1, USART_IT_RXNE)) //receive interruption
45 {
46 res = USART_ReceiveData(USART1); // Read the received data
47 if(res == '1')
48 {
49 GPIO_ResetBits(GPIOB, GPIO_Pin_5); // Light up LED1
50 USART_SendData(USART1, res); //Send data
51 }
52 if(res == '2')
53 {
54 GPIO_ResetBits(GPIOE, GPIO_Pin_5); // Light up LED2
55 USART_SendData(USART1, res); //Send data
56 }
57
58 }
59 }

Main function

 1 #include "stm32f10x.h"

2 #include "my_usart.h"
3 #include "led.h"
4
5 int main(void)
6 {
7 LED_Init(); //LED initialization
8 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set NVIC interrupt group 2: 2-bit preemption priority, 2-bit response priority
9 My_USART1_Init(); //Serial port initialization
10 while(1);
11 }

 1 #include "my_usart.h< span style="color: #800000;">"

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); //Enable GPIOA clock
11 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable USART1 clock
12
13 KST_GPIO_Structure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
14 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_9;
15 KST_GPIO_Structure.GPIO_Speed ​​= GPIO_Speed_10MHz;
16 GPIO_Init(GPIOA, &KST_GPIO_Structure); // Initialize GPIOA.9
17
18 KST_GPIO_Structure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
19 KST_GPIO_Structure.GPIO_Pin = GPIO_Pin_10;
20 KST_GPIO_Structure.GPIO_Speed ​​= GPIO_Speed_10MHz;
21 GPIO_Init(GPIOA, &KST_GPIO_Structure); // Initialize GPIOA.10
22
23 KST_USART_Structure.USART_BaudRate = 115200; //Set the baud rate to 115200
24 KST_USART_Structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control
25 KST_USART_Structure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //Transceiver mode
26 KST_USART_Structure.USART_Parity = USART_Parity_No; //No parity check
27 KST_USART_Structure.USART_StopBits = USART_StopBits_1; //One ​​stop bit
28 KST_USART_Structure.USART_WordLength = USART_WordLength_8b; //The word length is 8-bit data mode
29
30 USART_Init(USART1, &KST_USART_Structure); //Initialize serial port 1
31 USART_Cmd(USART1, ENABLE); //Enable the serial port
32 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Enable interrupts
33
34 KST_NVIC_Structure.NVIC_IRQChannel = USART1_IRQn;
35 KST_NVIC_Structure.NVIC_IRQChannelCmd = ENABLE; //< span style="color: #008000;">IRQ channel enable
36 KST_NVIC_Structure.NVIC_IRQChannelPreemptionPriority = 1; //Preempt priority 1
37 KST_NVIC_Structure.NVIC_IRQChannelSubPriority = 1; //Subpriority 1
38 NVIC_Init(&KST_NVIC_Structure); //Initialize the NVIC register according to the specified parameters
39 }
40
41 void USART1_IRQHandler(void) //Serial port 1 terminal service function
42 {
43 u8 res;
44 if(USART_GetITStatus(USART1, USART_IT_RXNE)) //receive interruption
45 {
46 res = USART_ReceiveData(USART1); // Read the received data
47 if(res == '1')
48 {
49 GPIO_ResetBits(GPIOB, GPIO_Pin_5); // Light up LED1
50 USART_SendData(USART1, res); //Send data
51 }
52 if(res == '2')
53 {
54 GPIO_ResetBits(GPIOE, GPIO_Pin_5); // Light up LED2
55 USART_SendData(USART1, res); //Send data
56 }
57
58 }
59 }

 1 #include "stm32f10x.h"

2 #include "my_usart.h"
3 #include "led.h"
4
5 int main(void)
6 {
7 LED_Init(); //LED initialization
8 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set NVIC interrupt group 2: 2-bit preemption priority, 2-bit response priority
9 My_USART1_Init(); //Serial port initialization
10 while(1);
11 }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1274 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.