STM32 board serial port pins and usage

1. The pins corresponding to the STM32f1 serial port

The serial port is a commonly used data transmission interface. The STM32F103 series microcontroller has a total of 5 serial ports, of which 1-3 are universal synchronous/asynchronous serial interfaces USART, and 4 and 5 are universal asynchronous serial interfaces UART.
The corresponding pins are as shown below:
Insert image description here

2. Configure the serial port

Contents included in configuring the serial port:

1). Configure the corresponding I/O port: configure the TX pin as a multiplexed push-pull output (GPIO_Mode_AF_PP), and configure the RX pin as a floating input (GPIO_Mode_IN_FLOATING).
2). Configure the serial port: set parameters such as baud rate, data bits, stop bits, etc.
3). Configure interrupt vector: If you need to use interrupt mode to receive data, configure the corresponding interrupt source and interrupt service function.
4). Turn on the clock of the corresponding serial port: Select the corresponding clock enable command according to the serial port module to be used, and hang the serial port module on the corresponding bus.
5). Initialize the serial port: Use the initialization structure to configure the corresponding serial port module, and make the configuration effective through the initialization function.

Attached schematic diagram:
Insert image description here
Insert image description here

Precautions:

  1. USART1 is hung on APB2, and the enable clock command is:
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    others are hung on APB1, such as port 2:
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  2. When configuring port 4 and port 5, the interrupt names are UART4 and UART5, and the interrupt entries are UART4_IRQn and UART5_IRQn respectively. The
    corresponding interrupt service functions are
    void UART4_IRQHandler(void) and
    void UART5_IRQHandler(void).

3. The following is the configuration function and data sending and receiving function code of the 5 serial ports.

#include “stm32f10x.h”
#include “misc.h”
#include “stm32f10x_gpio.h”
#include “stm32f10x_usart.h”

void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //Port A;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

USART_INITSTRUCTURURURT.USART_BAUDRATE = 9600; // Potter rate;
USART_INITSTRUCTRURURTURT.USART_WORDLENGTH = USART_WORDLENGTH_8B; // data bit 8 bit;
usart_initStructions.usart_st opBits = USART_STOPBITS_1; // Stop bit 1 digit;
usart_initStruction.usart_parity = USART_PARITY_NO; // No school test ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//No hardware flow control;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//Transceiver mode;
USART_Init(USART1, &USART_InitStructure); //Configure serial port parameters;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt group, 4-bit preemption priority, 4-bit response priority;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Interrupt number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC _Init(&NVIC_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //Enable serial port;
}

void USART1_Send_Byte(u8 Data) //Send a byte;
{ USART_SendData(USART1,Data); while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET ); }


void USART1_Send_String(u8 *Data) //Send string;
{ while(*Data) USART1_Send_Byte(*Data++); }


void USART1_IRQHandler(void) //Interrupt handling function;
{ u8 res; if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //Determine whether an interrupt occurs; { USART_ClearFlag(USART1, USART_IT_RXNE); //Clear the flag bit; res= USART_ReceiveData(USART1); //Receive data; USART1_Send_Byte(res); //User-defined; } }







void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //Port A;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART2 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

USART_INITSTRUCTURURURT.USART_BAUDRATE = 9600; // Potter rate;
USART_INITSTRUCTRURURTURT.USART_WORDLENGTH = USART_WORDLENGTH_8B; // data bit 8 bit;
usart_initStructions.usart_st opBits = USART_STOPBITS_1; // Stop bit 1 digit;
usart_initStruction.usart_parity = USART_PARITY_NO; // No school test ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//No hardware flow control;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//Transceiver mode;
USART_Init(USART2, &USART_InitStructure); //Configure serial port parameters;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt group, 4-bit preemption priority, 4-bit response priority;

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //Interrupt number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC _Init(&NVIC_InitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE); //Enable serial port;
}

void USART2_Send_Byte(u8 Data) //Send a byte;
{ USART_SendData(USART2,Data); while( USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET ); }


void USART2_Send_String(u8 *Data) //Send string;
{ while(*Data) USART2_Send_Byte(*Data++); }


void USART2_IRQHandler(void) //Interrupt handling function;
{ u8 res; if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //Determine whether an interrupt occurs; { USART_ClearFlag(USART2, USART_IT_RXNE); //Clear the flag bit; res= USART_ReceiveData(USART2); //Receive data; USART2_Send_Byte(res); //User-defined; } }







void USART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART3 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //Port B;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOB, &GPIO_InitStructure); //端口B;

USART_INITSTRUCTURURURT.USART_BAUDRATE = 9600; // Potter rate;
USART_INITSTRUCTRURURTURT.USART_WORDLENGTH = USART_WORDLENGTH_8B; // data bit 8 bit;
usart_initStructions.usart_st opBits = USART_STOPBITS_1; // Stop bit 1 digit;
usart_initStruction.usart_parity = USART_PARITY_NO; // No school test ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//No hardware flow control;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//Transceiver mode;
USART_Init(USART3, &USART_InitStructure); //Configure serial port parameters;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt group, 4-bit preemption priority, 4-bit response priority;

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //Interrupt number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC _Init(&NVIC_InitStructure);

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE); //Enable serial port;
}

void USART3_Send_Byte(u8 Data) //Send a byte;
{ USART_SendData(USART3,Data); while( USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET ); }


void USART3_Send_String(u8 *Data) //Send string;
{ while(*Data) USART3_Send_Byte(*Data++); }


void USART3_IRQHandler(void) //Interrupt handling function;
{ u8 res; if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) //Determine whether an interrupt occurs; { USART_ClearFlag(USART3, USART_IT_RXNE); //Clear the flag bit; res= USART_ReceiveData(USART3); //Receive data; USART3_Send_Byte(res); //User-defined; } }







void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //UART4 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //Port C;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //UART4 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;

USART_INITSTRUCTURURURT.USART_BAUDRATE = 9600; // Potter rate;
USART_INITSTRUCTRURURTURT.USART_WORDLENGTH = USART_WORDLENGTH_8B; // data bit 8 bit;
usart_initStructions.usart_st opBits = USART_STOPBITS_1; // Stop bit 1 digit;
usart_initStruction.usart_parity = USART_PARITY_NO; // No school test ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//No hardware flow control;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//Transceiver mode;
USART_Init(UART4, &USART_InitStructure); //Configure serial port parameters;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt group, 4-bit preemption priority, 4-bit response priority;

NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //Interrupt number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC _Init(&NVIC_InitStructure);

USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE); //Enable serial port;
}

void UART4_Send_Byte(u8 Data) //Send a byte;
{ USART_SendData(UART4,Data); while( USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET ); }


void UART4_Send_String(u8 *Data) //Send string;
{ while(*Data) UART4_Send_Byte(*Data++); }


void UART4_IRQHandler(void) //Interrupt handling function;
{ u8 res; if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) //Determine whether an interrupt occurs; { USART_ClearFlag(UART4, USART_IT_RXNE); //Clear the flag bit; res= USART_ReceiveData(UART4); //Receive data; UART4_Send_Byte(res); //User-defined; } }







void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //UART5 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //Port C;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART5 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOD, &GPIO_InitStructure); //端口D;

USART_INITSTRUCTURURURT.USART_BAUDRATE = 9600; // Potter rate;
USART_INITSTRUCTRURURTURT.USART_WORDLENGTH = USART_WORDLENGTH_8B; // data bit 8 bit;
usart_initStructions.usart_st opBits = USART_STOPBITS_1; // Stop bit 1 digit;
usart_initStruction.usart_parity = USART_PARITY_NO; // No school test ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//No hardware flow control;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//Transceiver mode;
USART_Init(UART5, &USART_InitStructure); //Configure serial port parameters;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt group, 4-bit preemption priority, 4-bit response priority;

NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //Interrupt number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC _Init(&NVIC_InitStructure);

USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART5, ENABLE); //Enable serial port;
}

void UART5_Send_Byte(u8 Data) //Send a byte;
{ USART_SendData(UART5,Data); while( USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET ); }


void UART5_Send_String(u8 *Data) //Send string;
{ while(*Data) UART5_Send_Byte(*Data++); }


void UART5_IRQHandler(void) //Interrupt handling function;
{ u8 res; if(USART_GetITStatus(UART5, USART_IT_RXNE) == SET) //Determine whether an interrupt occurs; { USART_ClearFlag(UART5, USART_IT_RXNE); //Clear the flag bit; res= USART_ReceiveData(UART5); //Receive data; UART5_Send_Byte(res); //User-defined; } }







Guess you like

Origin blog.csdn.net/der_power/article/details/131548924