STM32一个函数配置所有串口的方法
/**
* config usartx
* @param USARTx [USART lable, example: USART1, USART2, USART3]
* @param baudRate [usart baudRate]
*/
void Usart_InitConfig(USART_TypeDef* USARTx, uint32_t baudRate)
{
GPIO_InitTypeDef GPIO_InitStruction;
USART_InitTypeDef Usartx_InitStruction;
NVIC_InitTypeDef NVIC_InitStruction;
if(USARTx == USART1)
{
/******************** usart1 *************************************/
/************** set usart1 parameter **********************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/************************* usart1 GPIO config ***********************/
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruction);
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruction);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // usart1 clock
Usartx_InitStruction.USART_BaudRate = baudRate;
Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usartx_InitStruction.USART_Parity = USART_Parity_No;
USART_Init(USART1, &Usartx_InitStruction);
/************** set usart1 NVIC ***************************/
NVIC_InitStruction.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruction.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruction);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //set usart1 interrupt type
USART_Cmd(USART1, ENABLE);
}
else if(USARTx == USART2)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/************************* usart2 ***********************/
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruction);
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruction);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // usart2 clock
Usartx_InitStruction.USART_BaudRate = baudRate;
Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usartx_InitStruction.USART_Parity = USART_Parity_No;
USART_Init(USART2, &Usartx_InitStruction);
/************** set usart2 NVIC ***************************/
NVIC_InitStruction.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruction.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruction);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //set usart2 interrupt type
USART_Cmd(USART2, ENABLE);
}
else if(USARTx == USART3)
{
/************************* usart3 ***********************/
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruction);
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruction);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // usart3 clock
Usartx_InitStruction.USART_BaudRate = baudRate;
Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usartx_InitStruction.USART_Parity = USART_Parity_No;
USART_Init(USART3, &Usartx_InitStruction);
/************** set usart3 NVIC ***************************/
NVIC_InitStruction.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruction.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruction);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //set usart3 interrupt type
USART_Cmd(USART3, ENABLE);
}
/**************************** usartbuff struct init**************************/
memset(UsartBuff.UsartRecBuff, '\0', BUFF_SIZE);
memset(UsartBuff.UsartSendBuff, '\0', BUFF_SIZE);
}
网友评论