STM32 Basic Experiment 3 (Serial Communication)

Publisher:电子艺术大师Latest update time:2018-12-03 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Experimental Principle


Simple serial communication example 


1. The general steps of serial port settings can be summarized as follows: 

1) Serial port clock enable, GPIO clock enable 

2) Serial port reset 

3) GPIO port mode setting 

4) Serial port parameter initialization 

5) Enable interrupts and initialize NVIC (this step is only required if interrupts need to be enabled) 

6) Enable the serial port 

7) Write an interrupt handling function 


2. Specific function implementation 

1) Enable serial port clock and GPIO clock: RCC_APB2PeriphClockCmd();


    //①Serial port clock enable, GPIO clock enable

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA clock enable

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Serial port 1 clock enable


2) GPIO port mode setting: GPIO_Init(); mode is set to GPIO_Mode_AF_PP and GPIO_Mode_IN_FLOATING


    //②TX

    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; //Check the Chinese manual to find the mode that the serial port needs to be configured in

    GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;

    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;

    GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting

    //②RX

    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;

    GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;

    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;

    GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting


3) Serial port parameter initialization: USART_Init();


    //③Serial port parameter initialization

    USART_InitStrue.USART_BaudRate=115200; //Baud rate

    USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow

    USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on

    USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check

    USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits

    USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8

    USART_Init(USART1,&USART_InitStrue);


4) Enable interrupts and initialize NVIC (this step is only required if interrupts need to be enabled) 

NVIC_Init(); 

USART_ITConfig();


  //④

    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//④ Enable receive interrupt!!!!


    NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;;//Serial port 1 interrupt

    NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;

    NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; // The preemption priority is 1

    NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; //Subpriority bit 1

    NVIC_Init(&NVIC_InitStrue); //④Interrupt initialization function


5) Enable the serial port: USART_Cmd();


    //⑤Enable the serial port

    USART_Cmd(USART1,ENABLE);


6) Write interrupt handling function: USARTx_IRQHandler(); 

7) Serial port data transmission and reception: 

void USART_SendData(); //Send data to the serial port, DR 

uint16_t USART_ReceiveData(); //Receive data and read the received data from DR


//Interrupt processing function written by yourself (repeatedly defined in SYSTEM)⑥

void USART1_IRQHandler(void)

{

    u8 res;

     if(USART_GetITStatus(USART1,USART_IT_RXNE))//Judge whether there is an interrupt reception!

 {

     //Serial port data receiving and sending function⑦

     res= USART_ReceiveData(USART1); 

     USART_SendData(USART1,res);   

  }

}


8) Get the serial port transmission status (not used yet): 

FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG); 

void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT); 


3. Use the Chinese manual to check the GPIO mode that needs to be configured for the serial port


Write the picture description here


2. Experimental Code


**main.c**

#include "stm32f10x.h"


void My_USART1_Init(void)//Simple serial port initialization function written by yourself, you can put a separate uart.c

{

    //2, 3, 4 define structure pointers

    GPIO_InitTypeDef GPIO_InitStrue;

    USART_InitTypeDef USART_InitStrue;

    NVIC_InitTypeDef NVIC_InitStrue;


    //①Serial port clock enable, GPIO clock enable

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA clock enable

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Serial port 1 clock enable


    //②TX

    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; //Check the Chinese manual to find the mode that the serial port needs to be configured in

    GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;

    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;

    GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting

    //②RX

    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;

    GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;

    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;

    GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting


    //③Serial port parameter initialization

    USART_InitStrue.USART_BaudRate=115200; //Baud rate

    USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow

    USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on

    USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check

    USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits

    USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8

    USART_Init(USART1,&USART_InitStrue);


    //⑤Enable the serial port

    USART_Cmd(USART1,ENABLE);

  //④

    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//④ Enable receive interrupt!!!!


    NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;;//Serial port 1 interrupt

    NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;

    NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; // The preemption priority is 1

    NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; //Subpriority bit 1

    NVIC_Init(&NVIC_InitStrue); //④Interrupt initialization function

}


//Interrupt processing function written by yourself (repeatedly defined in SYSTEM)⑥

void USART1_IRQHandler(void)

{

    u8 res;

     if(USART_GetITStatus(USART1,USART_IT_RXNE))//Judge whether there is an interrupt reception!

 {

     //Serial port data receiving and sending function⑦

     res= USART_ReceiveData(USART1); 

     USART_SendData(USART1,res);   

  }

}



int main(void)

{

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt priority grouping function

    My_USART1_Init();

    while(1); //wait for interrupt


}


3. Experimental Results


 Write the picture description here 


The configuration in the serial port assistant needs to be consistent with the initialization


    //Serial port parameter initialization

    USART_InitStrue.USART_BaudRate=115200; //Baud rate

    USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow

    USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on

    USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check

    USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits

    USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8

    USART_Init(USART1,&USART_InitStrue);


Keywords:STM32 Reference address:STM32 Basic Experiment 3 (Serial Communication)

Previous article:STM32 Basic Experiment 4 (Key Interrupt)
Next article:stm32 basic experiment 2 (key input-query mode)

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号