STM32-Serial Port Program

Publisher:kappa20Latest update time:2018-08-15 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    When learning about various hardware, the serial port is an essential and first peripheral to learn, because with the serial port, you can interact with the hardware in a simple way, and it is also easier to verify the correctness of the written program. 
   STM32 has multiple USARTs and supports multiple modes of configuration. For details, please refer to the relevant manual of STM32 for learning.

Write the picture description here
Let's take a look at the functional block diagram of USART: 
Write the picture description here
Before using any peripheral function, you should look at its corresponding functional block diagram, which will help you understand the use of the function. 
Next, take a look at the relevant registers and operable library functions of USART. 
Write the picture description here

Write the picture description here
Finally, we need to look at the pin multiplexing of GPIO and USART: 
Write the picture description here
Because USART communicates with the outside world using the multiplexing function of the controller's GPIO pins, you need to configure GPIO to the corresponding input and output mode before using the USART function. 
At the same time, if you need to enable interrupts, you also need to enable the interrupt channel and set the priority grouping.

#include "stm32f10x.h"

/**

  * @brief Initialize GPIO, the default speed is GPIO_Speed_50MHz;

  *         

  * @param GPIO group

  * @param GPIO pin

    * @param GPIO pin mode

  * @retval None

  */

void GPIO_init(GPIO_TypeDef * GPIOx,u16 GPIO_Pin,GPIOMode_TypeDef Mode)

{

    GPIO_InitTypeDef GPIO_InitStruct;                                       

    GPIO_InitStruct.GPIO_Mode=Mode;                         

    GPIO_InitStruct.GPIO_Pin=GPIO_Pin;

    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_Init(GPIOx,&GPIO_InitStruct);

}

/**

  * @brief Initialize USART

  *         

  * @param baud rate, number of data bits, stop bits, send/receive mode


  * @retval None

  */

void UART_init(int BaudRate,u16 WordLength,u16 StopBits,u16 Parity,u16 Mode)

{

    USART_InitTypeDef USART_InitStruct;

    USART_InitStruct.USART_BaudRate=BaudRate;

    USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;

    USART_InitStruct.USART_Mode=Mode;   

    USART_InitStruct.USART_Parity=Parity;

    USART_InitStruct.USART_StopBits=StopBits;

    USART_InitStruct.USART_WordLength=WordLength;


    USART_Init(USART1,&USART_InitStruct);

    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);


    USART_Cmd(USART1,ENABLE);

}

/**

  * @brief Initialize interrupt vector

  *         

  * @param interrupt channel, preemption priority, corresponding priority, enable/disable


  * @retval None

  */

void NVIC_init(u8 NVIC_IRQChannel,u8 NVIC_IRQChannelPreemptionPriority,u8 NVIC_IRQChannelSubPriority,u8 NVIC_IRQChannelCmd)

{


        NVIC_InitTypeDef NVIC_InitStrue;

        NVIC_InitStrue.NVIC_IRQChannel=NVIC_IRQChannel;

        NVIC_InitStrue.NVIC_IRQChannelCmd=NVIC_IRQChannelCmd;

        NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=NVIC_IRQChannelPreemptionPriority;

        NVIC_InitStrue.NVIC_IRQChannelSubPriority=NVIC_IRQChannelSubPriority;

        NVIC_Init(&NVIC_InitStrue);


}

int main()

{

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


        GPIO_init(GPIOA,GPIO_Pin_9,GPIO_Mode_AF_PP);

        GPIO_init(GPIOA,GPIO_Pin_10,GPIO_Mode_IN_FLOATING);


        NVIC_init(USART1_IRQn,1,1,ENABLE);


        GPIO_init(GPIOA,GPIO_Pin_0,GPIO_Mode_Out_PP);

        GPIO_ResetBits(GPIOA,GPIO_Pin_0);

        UART_init(115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_Mode_Tx|USART_Mode_Rx);

        while(1);




}


void USART1_IRQHandler(void)

{

    u8 res;

    if(USART_GetITStatus(USART1,USART_IT_RXNE))

    {

        res=USART_ReceiveData(USART1);

        USART_SendData(USART1,res);


    }


}


The last function is the response function of the serial port receive interrupt. When the serial port receives data and the response flag is set to 1, it enters the interrupt function, provided that the serial port receive interrupt is enabled. 

   The above program realizes the reception and transmission of the serial port, sending and receiving the incoming data.


Keywords:STM32 Reference address:STM32-Serial Port Program

Previous article:STM32 - printf redirection to USART
Next article:STM32-light up the LED (GPIO configuration)

Recommended ReadingLatest update time:2024-11-16 15:36

STM32 ADC configuration
For STM32, several parameters need to be configured when using ADC. (1)     The first parameter is ADC_Mode, which is set to independent mode here: ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; In this mode, the dual ADCs cannot be synchronized, and each ADC interface works independently. So if ADC synchroni
[Microcontroller]
STM32 serial port uart
uart initialization steps Baud rate calculation formula: Baud rate = Fpclkx / (16 * USARTDIV) Take uart1 as an example to illustrate that uart1 uses PA9, PA10 1. Calculate the baud rate parameter  temp = (float) (pclk2 * 1000000) / (bound * 16); // Get USARTDIV  mantissa = temp; // Get the integer part  fraction
[Microcontroller]
Based on STM32-button input and eight IO port modes
Key detection uses the basic input function of the GPIO peripheral. When the mechanical contacts of the key are opened or closed, due to the elastic effect of the contacts, the key switch will not be immediately stably connected or disconnected. When the key is used, a ripple signal as shown in the figure will be gene
[Microcontroller]
Based on STM32-button input and eight IO port modes
How to design a digital oscilloscope based on stm32
With the development of integrated circuits and the adoption of digital signal processing technology, digital oscilloscopes have become intelligent measuring instruments that integrate various functions such as display, measurement, calculation, analysis, and recording. Digital oscilloscopes have gradually surpassed a
[Test Measurement]
How to design a digital oscilloscope based on stm32
Light up LED lamp using buttons based on STM32
The basic steps to light up the LED lamp using buttons based on STM32: (1) Open stm32CubeMX and create a new project file (2) Select the required chip. This experiment uses STM32F411RETx (3) Yellow represents pins that can be used, and green represents pins whose functions have been determined. The red part in t
[Microcontroller]
stm32 SPI interface
1. Introduction to SPI  SPI is the abbreviation of Serial Peripheral interface. As the name implies, it is a serial peripheral device interface. It was first defined by Motorola on its MC68HCXX series processors. The SPI interface is mainly used in EEPROM, FLASH, real-time clock, AD converter, and between digital sign
[Microcontroller]
stm32 SPI interface
STM32 port multiplexing & remapping
Let me tell you about the port remapping of the STM32 microcontroller, because I use myself as an example. Here I take the remapping of USART1 as an example. Because I want a TFT_LCD screen main control board, considering FSMC, I chose STM32F103VCT6 model CPU, and accidentally connected the serial port to USART1. Bec
[Microcontroller]
STM32 port multiplexing & remapping
STM32 read protection function and clear read protection function settings
1. STM32 protection measures for internal Flash       All STM32 chips provide protection for Flash to prevent illegal access to Flash - write protection and read protection.      1) Read protection is commonly referred to as "encryption", which acts on the entire Flash storage area. Once the Flash read protection is
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号