STM32L476R quick start-serial communication with the host computer

There are two ways of serial communication. The first is to communicate using the TX and RX interfaces learned in the microcomputer principle class. But according to last year's experience, this board directly uses the TX and RX two interfaces for communication and it is easy to burn out, so we use the simpler usb communication.
(More articles can be obtained on my Zhihu or CSDN number)

When sending stm32, the teacher will give you a usb power supply line, this line can also be used as a serial line. And I also recommend this, because you can use one wire for power supply and communication. If you don't use this usb to connect to the host computer to communicate, you have to buy another power bank to supply power through usb, which is a bit redundant.

So let's first look at what the host computer needs. For computers, you need to download a serial port debugging assistant. Then open the serial port.
For stm32, you need to use a program to open the serial port. Let’s take a look at how to do it:

#include "mbed.h"   //头文件

Serial s1(USBTX, USBRX); //这里是调用串口的命令,USBTX和USBRX指明了是通过USB先进行串口连接
//如果使用电脑做为另一个串口设备的话,需要提前安装电脑端的串口助手
DigitalOut myled(LED1);
int main() {
    
    
    s1.baud(9600);//设置波特率为9600,上位机的波特率也要设置一样,不然会乱码
    while (true) {
    
     
    s1.printf("hellow world \n");
    char c = pc.getc();
    if(c=='a')
    {
    
    
	myled = !myled
	}
    

#include "mbed.h"There is nothing to say, but the header file is introduced.
Serial s1 (USBTX, USBRX); It uses the Serial function, which is the calling function of the serial port. Its specification is Serial A (t, r). The first t refers to which TX interface is used. Here, the system has set the TX of the USB serial port It is packaged as USBTX, and the RX of the USB serial port is packaged as USBRX.

That is to say, when using the serial port s1, the usb is used for communication.
In the main function, we must first define the baud rate, that is, how many symbols are transmitted through the serial port in one second. Here we must pay attention to the baud rate of the upper computer and the lower computer must be the same, otherwise it will be garbled . The reason is also very simple. If your transmission rate is a and the rate I accept is b, then we are sure that we are not talking on the same channel (inter-server chat). This will inevitably be garbled.

Just set it directly on the computer, and use the s1.baud(9600);setting on stm32. The value in the brackets represents the baud rate. As for why it is 9600, it can only be said to be a habit.

s1.printf(“hellow world \n”);It is equivalent printf(“hellow world \n”);to using the serial port to transmit the string "hellow world \n" and then print it out.

c = pc.getc();It means to receive a character from the host computer and save it as c.
This has the advantage that the host computer can directly control the stm32 through the serial port, and the
stm32 only needs to make different actions according to the difference of the character c.

In the last part of the code, I made an example, as long as the upper computer passes through a letter'a', it will change the on and off state of the LED light.

char c = pc.getc();
    if(c=='a')
    {
    
    
	myled = !myled
	

Guess you like

Origin blog.csdn.net/yikuanglancheng/article/details/105408345