STM32 serial port settings and library function introduction

Publisher:科技革新者Latest update time:2017-09-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The general steps of serial port settings can be summarized as follows:
1) Enable serial port clock, enable GPIO clock 
2) Reset serial port
3) Set GPIO port mode
4) Initialize serial port parameters 
5) Enable interrupts and initialize NVIC (only if interrupts need to be enabled)
6) Enable serial port

7) Write an interrupt handling function 


Next, we will briefly introduce these firmware library functions that are directly related to the basic configuration of the serial port. These functions and definitions are mainly distributed in the stm32f10x_usart.h and stm32f10x_usart.c files. 

1. Enable the serial port clock. The serial port is mounted on APB2, so the enabling function is:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1)

2. Serial port reset. When an abnormality occurs in a peripheral, the peripheral can be reset by resetting it, and then reconfiguring the peripheral to make it work again. Generally, when the system is just beginning to be configured, the peripheral will be reset first to make it work again. The reset is completed in the function USART_DeInit():

void USART_DeInit(USART_TypeDef* USARTx)

3. Serial port parameter initialization


void USART_Init() function: 



     voidUSART_Init(USART_TypeDef*USARTx,USART_InitTypeDef*USART_InitStruct);

effect:

          Initialize the corresponding serial port according to the specified parameters (baud rate, word length, stop bit, parity, hardware flow control, etc.)

           Mainly used to initialize register BRR and CR1, CR2, CR3 control registers

Example:

         USART_InitTypeDefUSART_InitStructure;  

         USART_InitStructure.USART_BaudRate = 9600; //Baud rate setting;

        USART_InitStructure.USART_WordLength= USART_WordLength_8b; //Word length is 8-bit data format

        USART_InitStructure.USART_StopBits= USART_StopBits_1; //One stop bit

        USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit

        USART_InitStructure.USART_HardwareFlowControl= USART_HardwareFlowControl_None; //No hardware data flow control

       USART_InitStructure.USART_Mode= USART_Mode_Rx| USART_Mode_Tx; //Transmit and receive mode

      USART_Init(USART1,&USART_InitStructure); //Initialize the serial port



4. void USART_Cmd() function: 


prototype:

      voidUSART_Cmd(USART_TypeDef*USARTx,FunctionalStateNewState);

effect:

            Enable the corresponding serial port, which is used to set the serial port enable bit of register CR1

 Example:

           USART_Cmd(USART1,ENABLE); //Enable serial port 1


5. void USART_ITConfig() function: 

Prototype: voidUSART_ITConfig(USART_TypeDef*USARTx,

   uint16_t USART_IT, FunctionalStateNewState);

 Function: Enable the corresponding interrupt of the serial port and set the serial port control register

  Example:

            USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //Enable the read data register non-empty interrupt


6.USART_SendData() function: 

prototype:


       voidUSART_SendData(USART_TypeDef* USARTx, uint16_t Data);

  effect:

            Send data to the serial port.

 Example:

          USART_SendData(USART1,0x12);                    

7.uint16_tUSART_ReceiveData() function: 

Prototype: uint16_t USART_ReceiveData(USART_TypeDef*USARTx)

Get the value most recently received by the serial port.

 Example:

     USART_ReceiveData(USART1);

8. Functions related to the four status flags:


FlagStatusUSART_GetFlagStatus(USART_TypeDef*USARTx,uint16_t USART_FLAG);

void USART_ClearFlag(USART_TypeDef*USARTx,uint16_t USART_FLAG);


ITStatusUSART_GetITStatus(USART_TypeDef*USARTx,uint16_t USART_IT);


void USART_ClearITPendingBit(USART_TypeDef*USARTx,uint16_t USART_IT);


The following is a complete initialization serial port function and an interrupt service function:

//Initialize IO serial port 1 
//bound: baud rate
void uart_init(u32 bound){
    //GPIO port settings
    GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);//Enable USART1, GPIOA clock
   USART_DeInit(USART1); //Reset serial port 1
//USART1_TX PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//Multiplexed push-pull output
    GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize PA9
   
    //USART1_RX PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//Floating input
    GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize PA10


   //Usart1 NVIC configuration


    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//Preemption priority 3
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//Sub priority 3
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ channel enable
 NVIC_Init(&NVIC_InitStructure); // Initialize VIC registers according to the specified parameters
  
   // USART initialization settings


USART_InitStructure.USART_BaudRate = bound; // Generally set to 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // Word length is 8-bit data format
USART_InitStructure.USART_StopBits = USART_StopBits_1; // One stop bit
USART_InitStructure.USART_Parity = USART_Parity_No; // No parity bit
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // No hardware data flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //transmit and receive mode


    USART_Init(USART1, &USART_InitStructure); //initialize the serial port
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //enable interrupt
    USART_Cmd(USART1, ENABLE); //enable the serial port 


}



void USART1_IRQHandler(void) //Serial port 1 interrupt service routine
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //If the clock tick number is defined, it means that ucosII is to be used.
OSIntEnter();    
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //Receive interrupt (the received data must end with 0x0d 0x0a)
{
Res =USART_ReceiveData(USART1);//(USART1->DR);//Read received data

if((USART_RX_STA&0x8000)==0)//Reception is not completed
{
if(USART_RX_STA&0x4000)//Received 0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//Reception error, restart
else USART_RX_STA|=0x8000;//Reception is completed 
}
else //Has not received 0X0D yet
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//Receive data error, restart receiving 
}  
}
}    
     } 

#ifdef OS_TICKS_PER_SEC//If the clock tick number is defined, it means that ucosII is to be used.
OSIntExit();   
#endif


Reference address:STM32 serial port settings and library function introduction

Previous article:Summary of STM32 serial port setting process
Next article:Understanding of STM32's nvic

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号