When you first start learning about microcontrollers, you might feel excited when you successfully light up an LED or a seven-segment display using your code. This is a good thing and serves as motivation to continue learning.
However, when it comes to experiments related to data communication, you may find it difficult to make progress. Sometimes, after writing the driver and downloading it to the microcontroller, there is no response, and you may not know where the problem lies. Data communication cannot be measured with a multimeter like an LED can.
This is a hurdle in learning about microcontrollers and STM32. Or rather, it is a river that blocks your path. What would you do if you encountered a river? There are many methods to cross it, but I believe the fastest way is to use existing tools to get across. Once you do, you will find that the other side of the river is a different world.
So, what is this existing tool? It is “serial communication”.
Introduction to Serial Communication
Serial communication refers to a method of communication between peripherals and computers, where data is transmitted bit by bit through data signal lines, ground lines, control lines, etc. … This sounds too theoretical; it seems understandable, yet it is not. Let me explain it in my own words.
Serial communication is a method that allows the results of programs running on a microcontroller or STM32 chip to be sent to a computer. Here are a few important points you need to know about using serial communication:
-
1. Baud rate. (The speed of data transmission; both devices must be set to the same speed during communication, otherwise, garbled data will occur).
-
2. Hardware connection. Serial communication is asynchronous, generally TX->RX, RX->TX.
-
3. Stop bits.
-
4. Parity check.
-
5. Hardware flow control.
Items 3, 4, and 5 generally do not need to be changed, so there is no need to elaborate on them here.
Functions of Serial Communication
What can serial communication be used for?
Anyone who has learned C programming knows that program development requires continuous debugging and validation. Many programming software have complete debugging functions, making them very convenient to use. Although we also use C language for development, our program ultimately runs not in our development environment but in a real hardware system. At this point, checking the program’s execution process or results is not as convenient as in programming software.
To solve this problem, we can use serial communication to send the results we need to know or the key steps of program execution to the computer, allowing us to determine whether there are issues with the program running in the hardware system. The 51 microcontroller can be debugged using serial communication, and STM32 can also be debugged using serial communication. Even advanced LINUX development boards use serial communication for debugging.
What Can You Do Once You Master Serial Communication
Once you master serial communication, you can start experimenting with WiFi modules, GSM modules, Bluetooth modules, GPS modules, and various sensors that use serial communication. If you have the capability, you can also write upper-level software to control devices via serial communication.
What Do You Need for Serial Communication
If you only have an STM32 core board, you will also need a serial-to-USB module, a serial data receiving software, and a few Dupont wires. If you have a more complete development board, it generally already includes a serial-to-USB module, making it even easier to use. Have you noticed that something is still missing? Yes, you are right; you still need the most important program. To use serial communication, you also need to write the serial communication program. Below, I will teach you how to do it, rather than just write it.
Serial Communication Experiment
When conducting an experiment, it is best to break it down into several key steps. The benefit of this approach is that you can clearly know what you need to do, what you have completed, and what is still pending. Below, I will divide the serial communication experiment into several key steps:
1) GPIO Pin Configuration for Serial CommunicationSTM32F103 series chips generally have three or more serial ports, and the serial port used for debugging is usually USART1. The configuration for other serial ports is the same.
The following is the program for serial port configuration:
GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);// Enable USART1, GPIOA clock//USART1_TX GPIOA.9GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;// Multiplexed push-pull outputGPIO_Init(GPIOA, &GPIO_InitStructure);// Initialize GPIOA.9//USART1_RX GPIOA.10 initializationGPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Floating inputGPIO_Init(GPIOA, &GPIO_InitStructure);// Initialize GPIOA.10
Swipe left and right to view the code
The GPIO ports used for serial communication are PA9 and PA10, so you only need to configure the input and output modes for these two IO ports.
2) Main Parameter Settings for Serial Communication (Refer to the Program)
USART_InitTypeDef USART_InitStructure;// USART initialization settingsUSART_InitStructure.USART_BaudRate = bound;// Serial port baud rateUSART_InitStructure.USART_WordLength = USART_WordLength_8b;// Data format, 8 bitsUSART_InitStructure.USART_StopBits = USART_StopBits_1;// One stop bitUSART_InitStructure.USART_Parity = USART_Parity_No;// No parity bitUSART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware flow controlUSART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;// Receive and transmit modeUSART_Init(USART1, &USART_InitStructure); // Initialize serial port 1USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// Enable serial port receive interrupt
Swipe left and right to view the code
The serial port parameter configuration includes setting the baud rate, data format, stop bits, parity check, hardware flow control, and receive/transmit mode. Except for the baud rate, which needs to be changed, the other parameters do not need to be adjusted. You can directly copy and use them.
3) Serial Interrupt Configuration
If the serial port uses interrupt reception, then you need to configure the interrupt parameters for the serial port, which includes configuring the interrupt source and priority.
NVIC_InitTypeDef NVIC_InitStructure;// USART1 NVIC configurationNVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;// Preemption priority 3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;// Sub-priority 3NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;// Enable IRQ channelNVIC_Init(&NVIC_InitStructure);// Initialize VIC register according to specified parameters
Swipe left and right to view the code
4) Enable Serial Communication
This is when you need to start using the serial function, which is just a simple function call.
USART_Cmd(USART1, ENABLE); // Enable serial port 1
Swipe left and right to view the code
5) Write the Serial Interrupt Handler Function
When using library functions for development, all interrupt functions already exist, but the interrupt function does not handle anything yet. The interrupt function is as follows:
void USART1_IRQHandler(void){// Here is where you write the interrupt handling content, but generally, you will first check the relevant standards to consider it complete}
Swipe left and right to view the code
The complete serial interrupt function:
void USART1_IRQHandler(void){int Res=0; // Define a variable to receive serial dataif(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Receive interrupt (the received data must end with 0x0d 0x0a) { Res = USART_ReceiveData(USART1);// Read the received data USART_SendData(USART1, Res ); // Send the received data back through serial port 1 }
Swipe left and right to view the code
The functions for receiving and sending serial data are provided by the library functions; you just need to find them and use them directly.
Thus, we have completed the simplest serial communication experiment. Compile the program, upload it to the STM32, and connect it to the computer using a serial-to-USB module. Use serial data receiving software like SSCOM or other data receiving software, set the baud rate, open the serial port, and if everything is normal, whatever you send to the STM32 will be received back. That completes the experiment.
This is all for now. If you have example routines for your development board, you can start by using them. The more you use them, the more you will understand. This article mainly aims to highlight the importance of serial communication. Debugging with serial communication is indeed much more convenient.


Screenshots of Some E-books from the 400G Collection


