How to learn microcontroller data communication? This tool should be used well: serial communication

Publisher:JoyfulHeartedLatest update time:2022-12-08 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

If you are just starting to learn microcontrollers, will you be happy because you can use a program to light up an LED? Will you be happy because you can use a program to light up a digital tube? This is a good thing, and it is also the motivation to continue learning.


But when it comes to data-related experiments, it feels difficult to make progress. Sometimes, after writing the driver and downloading it to the microcontroller, there is no response at all, but I don't know where the problem lies. Data communication is not like LED, where you can use a multimeter to test whether there is power.


This is a hurdle for learning microcontrollers and STM32. In other words, this is a river, a river blocking your way. What will you do if there is a river? There were many methods in the past, but I think the faster way is to use the original tools to get through it. After passing by, you will find that there is a different world on the other side of the river.


So what is this original tool? That is "serial communication".

Introduction to serial communication

Serial communication refers to a communication method that transmits data bit by bit between peripherals and computers through data signal lines, ground lines, control lines, etc. This is too theoretical and seems to make sense. But I don’t understand. Let me use my own words as the author.

Serial communication is a communication method that can send the results of the program running in the microcontroller or STM32 chip to the computer. How to use serial communication, you need to know several important knowledge points:

  • 1. Baud rate. (The speed of data transmission must be set to the same speed when the two devices communicate, otherwise garbled characters will appear).

  • 2. Hardware connection. Serial communication is a kind of asynchronous communication, generally TX->RX, RX->TX.

  • 3. Stop bit.

  • 4. Parity check.

  • 5. Hardware data flow.

3, 4, and 5 generally do not need to be changed, so there is no need to say more here.
Serial port function

What can the serial port be used for?

Anyone who has learned C language knows that program development requires constant debugging and verification. Many language programming software have complete program debugging functions. It is very convenient to use. Although we also use C language for development, the environment in which our program ultimately runs is not on our development platform but on a real hardware system. At this time, it is not as convenient as programming software to view the running process or results of the program.


To solve this problem, we can use serial communication to send the results we need to know or the key steps of the program to the computer. Then we will know whether there is a problem running the program in the hardware system. The 51 microcontroller can be debugged using the serial port, and the STM32 can be debugged using the serial port. More advanced LINUX development boards also use the serial port for debugging.


Learn what serial communication can do

When you learn serial communication, you can start playing with WIFI modules, GSM modules, Bluetooth modules, GPS modules, and various sensors that use serial communication, etc. If you have the ability, you can also write host computer software to control the device through serial communication.


What is needed for serial communication

If you only have an STM32 core board, then you also need a serial port to USB module, a serial port data receiving software and a few DuPont cables. If yours is a relatively complete development board, it usually comes with a serial port to USB module, which makes it easier to use. If you are smart, you may find that something is missing, right? That's right because the most important program is missing. If you want to use serial communication, of course you also need to write a serial communication program. The following will teach you how to use it instead of writing it.


Serial port experiment

When doing an experiment, it is best to divide the experiment into several key steps. The advantage of this is that you can clearly know what you need to do and what you have completed. What else has not been done yet. Below, the author divides the serial port experiment into several key steps:

1) GPIO pin configuration used for serial communication.

STM32F103 series chips generally have more than three serial ports. The serial port used for debugging generally uses USART1. Other serial port configurations are the same.

The following section is the serial port configuration program:

The GPIO ports used by the serial port are PA9 and PA10, so you only need to configure the input and output modes of these two IO ports.

2) Setting the main parameters of the serial port (see the program directly)

Serial port parameter configuration can only configure the baud rate, data format, stop bits, parity, hardware stream, and transceiver mode of the serial port. Except for the baud rate that needs to be changed, other parameters do not need to be controlled. Just copy and use.
3) Serial port interrupt configuration

If the serial port uses interrupt reception, then you need to configure the interrupt parameters of the serial port. The configuration item cannot be just to configure the interrupt source and interrupt priority.

4) Serial port enable

It is an original function when you need to start using the serial port function.

5) Write serial port interrupt processing function

When developing using library functions, all interrupt functions already exist, but nothing is processed in the interrupt function. The interrupt function is as follows:

Complete serial port interrupt function:

The functions for receiving and sending serial port data are all provided by library functions. When you want to use them, you only need to find it and use it directly.

The above completes the simplest serial port experiment. Compile and program the program to STM32 and then use the serial port to USB module to connect to the computer. Use the serial port data receiving software SSCOM or other data receiving software to set the baud rate and open the serial port. If it is normal, what is sent to the STM32 will be received. To what. That's it.

That’s all for this time. If there is a supporting routine for the development board, you can use it first. After using it more, you will gradually understand it. This article mainly wants to explain the importance of the serial port. It is really convenient to use the serial port to debug.


Reference address:How to learn microcontroller data communication? This tool should be used well: serial communication

Previous article:Microcontroller keyboard detection and application
Next article:Common methods and precautions for microcontroller output PWM

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号