STM32的UART的驱动代码如下:
void BSP_Uart_SetBaud(uint32 u32Baud)
{
GPIO_InitTypeDef s_GPIO_InitStructure;
USART_InitTypeDef s_USART_InitStructure;
NVIC_InitTypeDef s_NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);/* 使能GPIOA时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);/* 使能USART1时钟*/
USART_DeInit(USART2);
s_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /* PA9*/
s_GPIO_InitStructure.e_GPIO_Speed = GPIO_Speed_50MHz;
s_GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_AF_PP; /* 复用推挽*/
GPIO_Init(GPIOA, &s_GPIO_InitStructure);
s_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; /* PA10*/
s_GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_IN_FLOATING;/* 浮空输入*/
GPIO_Init(GPIOA, &s_GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); /* 复位串口2*/
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);/* 停止复位*/
s_USART_InitStructure.USART_BaudRate = u32Baud;
s_USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 8位数据长度*/
s_USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 一个停止位*/
s_USART_InitStructure.USART_Parity = USART_Parity_No; /* 奇偶校验位*/
s_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/* 无硬件数据流控制*/
s_USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 收发模式*/
USART_Init(USART2, &s_USART_InitStructure); /* 初始化串口*/
USART_ClearITPendingBit(USART2, USART_IT_RXNE);/* 清楚中断位*/
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);/* 开启输入中断*/
USART_Cmd(USART2, ENABLE); /* 使能串口 */
s_NVIC_InitStructure.NVIC_IRQChannel = (uint8)USART2_IRQn; /* 使能串口2中断*/
s_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0U; /* 先占优先级2级*/
s_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1U; /* 从优先级2级*/
s_NVIC_InitStructure.e_NVIC_IRQChannelCmd = ENABLE; /* 使能外部中断通道*/
NVIC_Init(&s_NVIC_InitStructure);
/* 调试串口Uart1设置*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);/* 使能USART1时钟*/
USART_DeInit(USART1);
s_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /* PA9*/
s_GPIO_InitStructure.e_GPIO_Speed = GPIO_Speed_50MHz;
s_GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_AF_PP; /* 复用推挽*/
GPIO_Init(GPIOA, &s_GPIO_InitStructure);
s_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* PA10*/
s_GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_IN_FLOATING;/* 浮空输入*/
GPIO_Init(GPIOA, &s_GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); /* 复位串口2*/
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);/* 停止复位*/
s_USART_InitStructure.USART_BaudRate = u32Baud;
s_USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 8位数据长度*/
s_USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 一个停止位*/
s_USART_InitStructure.USART_Parity = USART_Parity_No; /* 奇偶校验位*/
s_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/* 无硬件数据流控制*/
s_USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 收发模式*/
USART_Init(USART1, &s_USART_InitStructure); /* 初始化串口*/
USART_Cmd(USART1, ENABLE); /* 使能串口 */
}
使用的库函数如下:
/*
***********************************************************
文 件 名: driver_usart.c
模 块: 片内外设usart的驱动模块
***********************************************************
*/
#include "driver_usart.h"
#include "driver_rcc.h"
#define CR1_UE_Set ((uint16)0x2000) /* !< USART Enable Mask */
#define CR1_UE_Reset ((uint16)0xDFFF) /* !< USART Disable Mask */
#define CR1_CLEAR_Mask ((uint16)0xE9F3) /* !< USART CR1 Mask */
#define CR2_STOP_CLEAR_Mask ((uint16)0xCFFF) /* !< USART CR2 STOP Bits Mask */
#define CR3_CLEAR_Mask ((uint16)0xFCFF) /* !< USART CR3 Mask */
#define IT_Mask ((uint16)0x001F) /* !< USART Interrupt Mask */
#define CR1_OVER8_Set ((uint16)0x8000) /* USART OVER8 mode Enable Mask */
/*
* @brief Deinitializes the USARTx peripheral registers to their default reset values.
* @param USARTx: Select the USART or the UART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @retval None
*/
void USART_DeInit(const USART_TypeDef* p_USARTx)
{
if (p_USARTx == USART1)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
}
else
{
if (p_USARTx == USART2)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
}
else/* 其他*/
{
/* nothing*/
}
}
}
/*
* @brief Initializes the USARTx peripheral according to the specified
* parameters in the USART_InitStruct .
* @param USARTx: Select the USART or the UART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param USART_InitStruct: pointer to a USART_InitTypeDef structure
* that c