STM32F030_USART detailed configuration description

Publisher:SerendipityGlowLatest update time:2017-09-20 Source: eefocusKeywords:STM32F030 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The serial port is the most frequently used problem in programming. It is usually used to send and receive data. At the same time, it has another function - to detect whether the program is correct. The STM32F030 series microcontroller naturally has a serial port. This article mainly introduces several commonly used simple applications of STM32F030_USART and its functional configuration.

1 Overview

The Universal Synchronous Asynchronous Receiver/Transmitter (USART) provides a flexible way for the MCU to communicate with external devices in full-duplex asynchronous serial data in the form of industrial standard NRZ. The USART can use a fractional baud rate generator to provide an ultra-wide baud rate setting range. DMA can be used to implement multi-buffer settings to support high-speed data communication.

  • Full-duplex, asynchronous communication

  • Configurable 16x or 8x oversampling method provides flexible choice between speed and clock tolerance

  • Fractional Baud Rate Generator

  • Automatic baud rate detection

  • Single-line half-duplex communication

  • The number of stop bits can be set - supports 1 or 2 stop bits

  • Fourteen interrupt sources and interrupt flags 
    - CTS toggle 
    - LIN break detect 
    - Transmit data register empty 
    - Transmit complete 
    - Receive data register full 
    - Line idle detected 
    - Overrun error 
    - Framing error 
    - Noise error 
    - Parity error 
    - Address/character match 
    - Receive timeout interrupt 
    - Block end interrupt 
    - Wake-up from Stop mode

  • Parity control: 
    - Send parity bit 
    - Receive parity check

2. Preparation

  1. Read the STM32F030x data sheet carefully

  2. Understand the operation principle of USART

  3. View the STM32F030 development board schematics and package diagrams

  4. The computer is equipped with keil and other compilation software

3. Register Description

Control Register 1 (USART_CR1) 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Control Register 2 (USART_CR2) 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Control Register 3 (USART_CR3) 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Baud Rate Register (USART_BRR) 
Write the picture description here 
Guard Time and Prescaler Register (USART_GTPR) 
Write the picture description here 
Interrupt and Status Register (USART_ISR) 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Write the picture description here 
Interrupt Flag Clear Register (USART_ICR) 
Write the picture description here 
Write the picture description here 
Data Receive Register (USART_RDR) 
Write the picture description here 
Data Transmit Register (USART_TDR
Write the picture description here

4. USART Configuration

  1. USART Schematic 
    Write the picture description here

    Write the picture description here

  2. USART code analysis①USART 
    initialization

void Usart_Init(uint32_t BaudRate)
{
    USART_InitTypeDef USART_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    /*
    PA9-TX-Push-Pull Multiplexing
    PA10-RX-Floating input/pull-up input
    */
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);  

    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    /*USART basic configuration*/
    USART_InitStruct.USART_BaudRate=BaudRate;
    USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
    USART_InitStruct.USART_Parity=USART_Parity_No;
    USART_InitStruct.USART_StopBits=USART_StopBits_1;
    USART_InitStruct.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART1,&USART_InitStruct);

    /*Enable receive interrupt*/
    NVIC_Config(USART1_IRQn);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1,ENABLE);}12345678910111213141516171819202122232425262728293031323334353637383940414243

② USART sends data

void USART1_SendBuf(uint8_t *pBuf, uint32_t u32Len){    while(u32Len--)
    { /*Judge whether the send buffer is empty*/
        while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE));        USART_SendData(USART1,*pBuf++);
    }
}123456789

③USART receives data

uint8_t USART1_ReceiverBuf(void){ /*Judge whether the receive buffer is not empty*/
    while(!USART_GetFlagStatus(USART1,USART_FLAG_RXNE));
    return USART_ReceiveData(USART1);
}123456

3. Remapping of printf function

int fputc(int ch, FILE *f)
{
    USART_SendData(USART1,(uint8_t)ch); while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE)); return (ch);
}123456

5. Summary

When using the printf function of USART, be sure to open the micro library: click the magic wand symbol on the Keil toolbar, enter the Target configuration, and check Use MicroLib


Keywords:STM32F030 Reference address:STM32F030_USART detailed configuration description

Previous article:Redirection problem of printf and scanf in STM32
Next article:STM32F030_I2C detailed configuration instructions

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号