STM32CubeMX completes USART serial communication

STM32CubeMX completes USART serial communication

1, Serial port protocol
2, STM32CubeMX installation
3, USART serial communication program of STM32
4, Observe the waveform
5, Summary

1, Serial port protocol

1. USART introduction
In embedded development, UART serial communication protocol is one of our commonly used communication protocols (UART, I2C, SPI, etc.). Its full name is Universal Asynchronous Receiver/Transmitter. It is a kind of asynchronous serial communication protocol. Its working principle is to transmit each character of transmitted data bit by bit, It can convert the data to be transmitted between serial communication and parallel communication, and can flexibly exchange full duplex data with external devices.
2. Functional block diagram

3. UART communication protocol
1) Start bit
When no data is sent, the data line is in the logic "1" state; First, a logical "0" signal is sent to indicate the start of character transmission.
2) Data bit
Immediately after the start bit. The number of data bits can be 4, 5, 6, 7, 8, etc., forming a character. ASCII code is usually used. The transmission starts from the lowest bit and is positioned by the clock.
3) Parity bit
After adding this bit to the data, the number of bits of "1" should be even (even check) or odd (odd check), so as to verify the correctness of data transmission.
4) Stop bit
It is the end flag of a character data. It can be 1-bit, 1.5-bit and 2-bit high level. Because the data is timed on the transmission line and each device has its own clock, there may be a small synchronization between the two devices in the communication. Therefore, the stop bit not only indicates the end of transmission, but also provides an opportunity for the computer to correct clock synchronization. The more bits applicable to stop bits, the greater the tolerance of different clock synchronization, but the slower the data transmission rate.
5) Free bit or start bit
In the logic "1" state, it means that there is no data transmission on the current line and it enters the idle state.
In the logical "0" state, it indicates that the transmission of the next data segment begins.
6) Baud rate
It represents the number of symbols transmitted per second. It is an index to measure the data transmission rate. It is expressed by the number of carrier modulation state changes per unit time.
The commonly used baud rates are 9600, 115200
Time interval calculation: the time obtained by dividing 1 second by baud rate. For example, the time interval with baud rate of 9600 is 1s / 9600 (baud rate) = 104us.
1) Serial Interface is abbreviated as serial port, also known as serial communication interface or serial communication interface (usually COM interface), which is an extended interface using serial communication mode. Serial Interface refers to the sequential transmission of data bit by bit. Its characteristic is that the communication line is simple. As long as a pair of transmission lines can realize two-way communication (the telephone line can be directly used as the transmission line), which greatly reduces the cost. It is especially suitable for long-distance communication, but the transmission speed is slow. Generally, it is the 9-pin trapezoidal interface behind the computer, which usually adopts RS232 signal.
2) RS-232: also known as standard serial port, the most commonly used serial communication interface. The traditional RS-232-C interface standard has 22 wires and adopts a standard 25 core D-type plug socket (DB25), which is later simplified to a 9-core D-type socket (DB9). RS-232 adopts unbalanced transmission mode, that is, the so-called single terminal communication. Because the difference between the transmission level and the reception level is only about 2V to 3V, its common mode suppression ability is poor. Coupled with the distributed capacitance on the twisted pair, the maximum transmission distance is about 15m and the maximum rate is 20kb/s. RS-232 is designed for point-to-point (i.e. only one pair of receiving and transmitting equipment) communication, and its driver load is 3 ~ 7k Ω. Therefore, RS-232 is suitable for communication between local devices.
3) TTL level: TTL level signal specifies that + 5V is equivalent to logic "1" and 0V is equivalent to logic "0" (when binary is used to represent data). Such data communication and level specification mode is called TTL (transistor transistor logic level) signal system. This is the standard technology of communication between various parts of equipment controlled by computer processor
4) USB to TTL: USB interface and TTL serial port cannot communicate directly, because the interface does not match and the level does not match. It is necessary to use peripheral devices to realize the conversion between interface and level. The conventional operation is to use chips such as CH340 and CP2102.

2, Installation and use of STM32CubeMX

(1) Stm32subemx installation
1. Installing the jdk environment
Download address:
2. Installing STM32CubeMX
Download address:

(1) Run the installer as an administrator and click next

(2) Check the accept... Option and click next

(3) Select the installation path. You can choose the default installation path or choose your own. The path cannot have Chinese. Click next

(4) Click OK on the pop-up page and then click next

(5) Click next after installation

Finally, click done to complete the installation

  • Installation of solid silo
    Run CubeMX and select the manage option under Help
    Select the download version and click install now

    (2) Use STM32CubeMX to generate relevant codes
    1. Click new project to select the chip

    2. Click SYS and select Serial Wire in the debug option

    3. High speed clock crystal / ceramic resonator after clicking RCC

    4. Click clock consultation pllclk

5. Set the corresponding pin to GPIO_OUT mode

6. Set the project name, path, and Toolchain/IDE, and then click GENERATE CODE in the upper right corner

7. Add code
Open the set project directory and open the MDK-ARM file. Open the project in keil and click main.c

Locate the while loop of the main function and insert the code



Click the magic wand, check Create Hex File in the output interface, and the Debug interface is set as follows

Open mcuisp, select LED.hex file, set relevant parameters as follows, and burn

LED light effect

STMCubemx implementation of LED lamp

3, USART serial communication

(1) Implementation of HAL Library
1. Create a new project

  • File – new project – select STM32F103C8 chip and Start Project in the upper right corner
  • Configure the clock. Select RCC – crystal / ceramic resonator

  • Select USART2 – Mode – Asynchronous
  • Clock Configuration – set PLLCLK, HSE and PLLMul
  • Interrupt settings, USART2 – NVIC Settings – NVIC Interrupt Table
  • Set the project name and path, and modify other settings as follows:
  • Click Code Generator, check relevant items, and click GENERATE CODE

    2 insert the code. Open the set project directory, enter the MDK ARM file, and open the keil file Hello
  • Open the main.c file, locate the while loop of the main function, and insert the following code
char data[]="hello windows!\n";//Data sent
HAL_UART_Transmit(&huart2, (uint8_t *)data, 15, 0xffff);//send out	

  • Click the magic wand to open the Output and Debug interfaces respectively. The settings are as follows

  • Serial port download
    Pin connection:
    BOOT0 is set to 1 and BOOT1 is set to 0.
    Open mcuisp and select the Hello.hex file to burn

(2) Register GPIO port mode implementation

  • Open kei and select project → new vision project to create a new project (this time, it is not necessary to check CORE and Start up)

  • Insert code after selecting Add asm file

;RCC Register address mapping             
RCC_BASE            EQU    0x40021000 
RCC_CR              EQU    (RCC_BASE + 0x00) 
RCC_CFGR            EQU    (RCC_BASE + 0x04) 
RCC_CIR             EQU    (RCC_BASE + 0x08) 
RCC_APB2RSTR        EQU    (RCC_BASE + 0x0C) 
RCC_APB1RSTR        EQU    (RCC_BASE + 0x10) 
RCC_AHBENR          EQU    (RCC_BASE + 0x14) 
RCC_APB2ENR         EQU    (RCC_BASE + 0x18) 
RCC_APB1ENR         EQU    (RCC_BASE + 0x1C) 
RCC_BDCR            EQU    (RCC_BASE + 0x20) 
RCC_CSR             EQU    (RCC_BASE + 0x24) 
;AFIO Register address mapping            
AFIO_BASE           EQU    0x40010000 
AFIO_EVCR           EQU    (AFIO_BASE + 0x00) 
AFIO_MAPR           EQU    (AFIO_BASE + 0x04) 
AFIO_EXTICR1        EQU    (AFIO_BASE + 0x08) 
AFIO_EXTICR2        EQU    (AFIO_BASE + 0x0C) 
AFIO_EXTICR3        EQU    (AFIO_BASE + 0x10) 
AFIO_EXTICR4        EQU    (AFIO_BASE + 0x14) 
;GPIOA Register address mapping              
GPIOA_BASE          EQU    0x40010800 
GPIOA_CRL           EQU    (GPIOA_BASE + 0x00) 
GPIOA_CRH           EQU    (GPIOA_BASE + 0x04) 
GPIOA_IDR           EQU    (GPIOA_BASE + 0x08) 
GPIOA_ODR           EQU    (GPIOA_BASE + 0x0C) 
GPIOA_BSRR          EQU    (GPIOA_BASE + 0x10) 
GPIOA_BRR           EQU    (GPIOA_BASE + 0x14) 
GPIOA_LCKR          EQU    (GPIOA_BASE + 0x18) 
;GPIO C Mouth control                   
GPIOC_BASE          EQU    0x40011000 
GPIOC_CRL           EQU    (GPIOC_BASE + 0x00) 
GPIOC_CRH           EQU    (GPIOC_BASE + 0x04) 
GPIOC_IDR           EQU    (GPIOC_BASE + 0x08) 
GPIOC_ODR           EQU    (GPIOC_BASE + 0x0C) 
GPIOC_BSRR          EQU    (GPIOC_BASE + 0x10) 
GPIOC_BRR           EQU    (GPIOC_BASE + 0x14) 
GPIOC_LCKR          EQU    (GPIOC_BASE + 0x18) 
;Serial port 1 control                       
USART1_BASE         EQU    0x40013800 
USART1_SR           EQU    (USART1_BASE + 0x00) 
USART1_DR           EQU    (USART1_BASE + 0x04) 
USART1_BRR          EQU    (USART1_BASE + 0x08) 
USART1_CR1          EQU    (USART1_BASE + 0x0c) 
USART1_CR2          EQU    (USART1_BASE + 0x10) 
USART1_CR3          EQU    (USART1_BASE + 0x14) 
USART1_GTPR         EQU    (USART1_BASE + 0x18) 
;NVIC Register address                
NVIC_BASE           EQU    0xE000E000 
NVIC_SETEN          EQU    (NVIC_BASE + 0x0010)     
;SETENA Starting address of register array 
NVIC_IRQPRI         EQU    (NVIC_BASE + 0x0400)     
;Start address of interrupt priority register array 
NVIC_VECTTBL        EQU    (NVIC_BASE + 0x0D08)     
;Address of vector table offset register     
NVIC_AIRCR          EQU    (NVIC_BASE + 0x0D0C)     
;Address of application interrupt and reset control register                                                
SETENA0             EQU    0xE000E100 
SETENA1             EQU    0xE000E104 
;SysTick Register address            
SysTick_BASE        EQU    0xE000E010 
SYSTICKCSR          EQU    (SysTick_BASE + 0x00) 
SYSTICKRVR          EQU    (SysTick_BASE + 0x04) 
;FLASH Buffer register address image     
FLASH_ACR           EQU    0x40022000 
;SCB_BASE           EQU    (SCS_BASE + 0x0D00) 
MSP_TOP             EQU    0x20005000               
;Starting value of main stack                
PSP_TOP             EQU    0x20004E00               
;Process stack start value             
BitAlias_BASE       EQU    0x22000000               
;Bit alias area start address         
Flag1               EQU    0x20000200 
b_flas              EQU    (BitAlias_BASE + (0x200*32) + (0*4))               
;Bit address 
b_05s               EQU    (BitAlias_BASE + (0x200*32) + (1*4))               
;Bit address 
DlyI                EQU    0x20000204 
DlyJ                EQU    0x20000208 
DlyK                EQU    0x2000020C 
SysTim              EQU    0x20000210 

;Constant definition 
Bit0                EQU    0x00000001 
Bit1                EQU    0x00000002 
Bit2                EQU    0x00000004 
Bit3                EQU    0x00000008 
Bit4                EQU    0x00000010 
Bit5                EQU    0x00000020 
Bit6                EQU    0x00000040 
Bit7                EQU    0x00000080 
Bit8                EQU    0x00000100 
Bit9                EQU    0x00000200 
Bit10               EQU    0x00000400 
Bit11               EQU    0x00000800 
Bit12               EQU    0x00001000 
Bit13               EQU    0x00002000 
Bit14               EQU    0x00004000 
Bit15               EQU    0x00008000 
Bit16               EQU    0x00010000 
Bit17               EQU    0x00020000 
Bit18               EQU    0x00040000 
Bit19               EQU    0x00080000 
Bit20               EQU    0x00100000 
Bit21               EQU    0x00200000 
Bit22               EQU    0x00400000 
Bit23               EQU    0x00800000 
Bit24               EQU    0x01000000 
Bit25               EQU    0x02000000 
Bit26               EQU    0x04000000 
Bit27               EQU    0x08000000 
Bit28               EQU    0x10000000 
Bit29               EQU    0x20000000 
Bit30               EQU    0x40000000 
Bit31               EQU    0x80000000 

;Vector table 
    DCD    MSP_TOP            ;Initialize main stack 
    DCD    Start              ;Reset vector 
    DCD    NMI_Handler        ;NMI Handler 
    DCD    HardFault_Handler  ;Hard Fault Handler 
    DCD    0                   
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    0 
    DCD    SysTick_Handler    ;SysTick Handler 
    SPACE  20                 ;Reserved space 20 bytes 
;Code snippet 
    AREA |.text|, CODE, READONLY 
    ;Main program start 
    ;Instructs the program to execute from here 
    ;Clock system settings 
    ldr    r0, =RCC_CR 
    ldr    r1, [r0] 
    orr    r1, #Bit16 
    str    r1, [r0] 
    ;Enable external crystal oscillator  
    ;Start external 8 M Crystal oscillator 
    ldr    r1, [r0] 
    ands   r1, #Bit17 
    beq    ClkOk 
    ;Wait for the external crystal oscillator to be ready 
    ldr    r1,[r0] 
    orr    r1,#Bit17 
    str    r1,[r0] 
    ;FLASH Buffer 
    ldr    r0, =FLASH_ACR 
    mov    r1, #0x00000032 
    str    r1, [r0] 
    ;set up PLL The PLL magnification is 7,HSE Input no frequency division 
    ldr    r0, =RCC_CFGR 
    ldr    r1, [r0] 
    orr    r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) 
    orr    r1, #Bit10 
    str    r1, [r0] 
    ;start-up PLL Phase locked loop 
    ldr    r0, =RCC_CR 
    ldr    r1, [r0] 
    orr    r1, #Bit24 
    str    r1, [r0] 
    ldr    r1, [r0] 
    ands   r1, #Bit25 
    beq    PllOk 
    ;choice PLL Clock as system clock 
    ldr    r0, =RCC_CFGR 
    ldr    r1, [r0] 
    orr    r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) 
    orr    r1, #Bit10 
    orr    r1, #Bit1 
    str    r1, [r0] 
    ;other RCC Related settings 
    ldr    r0, =RCC_APB2ENR 
    mov    r1, #(Bit14 :OR: Bit4 :OR: Bit2) 
    str    r1, [r0]      

    ;IO port settings 
    ldr    r0, =GPIOC_CRL 
    ldr    r1, [r0] 
    orr    r1, #(Bit28 :OR: Bit29)          
    ;PC.7 Output mode,Maximum speed 50 MHz  
    and    r1, #(~Bit30 & ~Bit31)   
    ;PC.7 Universal push-pull output mode 
    str    r1, [r0]            
    ;PA9 Serial port 0 transmitting pin 
    ldr    r0, =GPIOA_CRH 
    ldr    r1, [r0] 
    orr    r1, #(Bit4 :OR: Bit5)          
    ;PA.9 Output mode,Maximum speed 50 MHz  
    orr    r1, #Bit7 
    and    r1, #~Bit6 
    ;10: Multiplexing function push-pull output mode 
    str    r1, [r0]    

    ldr    r0, =USART1_BRR   
    mov    r1, #0x271 
    str    r1, [r0] 
    ;Configure baud rate-> 115200 
    ldr    r0, =USART1_CR1   
    mov    r1, #0x200c 
    str    r1, [r0] 
    ;USART Module total enable send and receive enable 
    ;71 02 00 00   2c 20 00 00 
    ;AFIO Parameter setting             
    ;Systick Parameter setting 
    ldr    r0, =SYSTICKRVR           
    ;Systick Initial installation value 
    mov    r1, #9000 
    str    r1, [r0] 
    ldr    r0, =SYSTICKCSR           
    ;set up,start-up Systick 
    mov    r1, #0x03 
    str    r1, [r0] 
    ;ldr   r0, =SETENA0 
    ;mov   r1, 0x00800000 
    ;str   r1, [r0] 
    ;ldr   r0, =SETENA1 
    ;mov   r1, #0x00000100 
    ;str   r1, [r0] 
    ;Switch to user level line program mode 
    ldr    r0, =PSP_TOP                   
    ;Initialize thread stack 
    msr    psp, r0 
    mov    r0, #3 
    msr    control, r0 
    ;initialization SRAM register 
    mov    r1, #0 
    ldr    r0, =Flag1 
    str    r1, [r0] 
    ldr    r0, =DlyI 
    str    r1, [r0] 
    ldr    r0, =DlyJ 
    str    r1, [r0] 
    ldr    r0, =DlyK 
    str    r1, [r0] 
    ldr    r0, =SysTim 
    str    r1, [r0]           
;Main cycle            
    ldr    r0, =Flag1 
    ldr    r1, [r0] 
    tst    r1, #Bit1                 
    ;SysTick Generate 0.5s,Set bit 1 
    beq    main                  ;0.5s The flag is not set yet       
    ;0.5s The flag has been set 
    ldr    r0, =b_05s                
    ;Bit band operation reset 0.5s sign 
    mov    r1, #0 
    str    r1, [r0] 
    bl     LedFlas 

    mov    r0, #'h' 
    bl     send_a_char
	mov    r0, #'e' 
    bl     send_a_char
	mov    r0, #'l' 
    bl     send_a_char
	mov    r0, #'l' 
    bl     send_a_char
	mov    r0, #'o' 
    bl     send_a_char
	mov    r0, #' ' 
    bl     send_a_char
	mov    r0, #'w' 
    bl     send_a_char
	mov    r0, #'i' 
    bl     send_a_char
	mov    r0, #'n' 
    bl     send_a_char
	mov    r0, #'d' 
    bl     send_a_char
	mov    r0, #'o' 
    bl     send_a_char
	mov    r0, #'w' 
    bl     send_a_char
    mov    r0, #'s' 
    bl     send_a_char

	mov    r0, #'\n' 
    bl     send_a_char
	b      main             
;Subroutine serial port 1 sends a character 
    push   {r0 - r3} 
    ldr    r2, =USART1_DR   
    str    r0, [r2] 
    ldr    r2, =USART1_SR  
    ldr    r2, [r2] 
    tst    r2, #0x40 
    beq    b1 
    ;Send complete(Transmission complete)wait for 
    pop    {r0 - r3} 
    bx     lr                 
;subroutine led twinkle 
    push   {r0 - r3} 
    ldr    r0, =Flag1 
    ldr    r1, [r0] 
    tst    r1, #Bit0 
    ;bit0 Flashing flag bit 
    beq    ONLED        ;Open for 0 led lamp 
    ;Off for 1 led lamp 
    ldr    r0, =b_flas 
    mov    r1, #0 
    str    r1, [r0] 
    ;The flashing flag position is 0,The next status is on 
    ;PC.7 Output 0 
    ldr    r0, =GPIOC_BRR 
    ldr    r1, [r0] 
    orr    r1, #Bit7 
    str    r1, [r0] 
    b      LedEx 
    ;Open for 0 led lamp 
    ldr    r0, =b_flas 
    mov    r1, #1 
    str    r1, [r0] 
    ;The flashing flag position is 1,The next status is off 
    ;PC.7 Output 1 
    ldr    r0, =GPIOC_BSRR 
    ldr    r1, [r0] 
    orr    r1, #Bit7 
    str    r1, [r0] 
    pop    {r0 - r3} 
    bx     lr 
;Abnormal program 
    bx     lr 

    bx     lr 
    ldr    r0, =SysTim 
    ldr    r1, [r0] 
    add    r1, #1 
    str    r1, [r0] 
    cmp    r1, #500 
    bcc    TickExit 
    mov    r1, #0 
    str    r1, [r0] 
    ldr    r0, =b_05s  
    ;The clock tick counter is set to 0 when it is greater than or equal to 500 times of clearing.5s Flag bit 
    ;Bit band operation set 1 
    mov    r1, #1 
    str    r1, [r0] 
    bx     lr 
    ;By using zero or null instructions NOP fill,Aligns the current position with a specified boundary 

Click the magic wand, and the Output and Debug settings are the same as before.
Download the serial port, connect the corresponding pin and port, and open mcuisp burning

Serial port debugging

4, Observe the waveform

(1) Water lamp waveform
1. Register mode
See the blog for details
Waveform analysis diagram

2. HAL library mode
Turn on the logic analyzer

Add a pin and set the display type to bit

Click Run(F5) in the upper left corner,

(2) Serial port protocol
(1) Register mode
Compile and debug the program, open UART#1 and logic analyzer in serial windows in the menu bar, add USART1_SR, and set the Display Type to Bit.
The waveform diagram is as follows:

(2) HAL library mode
Open UART#1 and logic analyzer in serial windows in the menu bar, add usart2#u Sr, and set the Display Type to Bit.
The waveform diagram is as follows:

5, Summary

Thanks for helping students in the project process and providing information for a few unknown bloggers. Actually, this is supposed to be a relatively easy experiment to implement, but the software is too suck for the experiment task to be half done. The rest of the process is spent on restoring the normal use of keil5, because keil5 is unable to compile the program or to debug it in the process. Unable to output such and such errors as hello windows, but the solution of multi-party query on the Internet can not be changed as desired. It was delayed to complete the task of this experimental project as desired. It seems that their independent solution ability needs to be improved.

Tags: stm32 ARM

Posted on Wed, 03 Nov 2021 03:37:50 -0400 by CybJunior