关键代码:
usart2.c
/**
* @brief USART2 初始化
* @param baud: 波特率设置
* @retval None
*/
void USART2_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOG,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 使能 USART2 时钟
/* USART2 引脚复用映射 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //PA2 复用为 USART2
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); //PA3 复用为 USART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //PA2 与 PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed; // 速度 50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置生效
//PG8 推挽输出 , 用于 RS-485 模式控制
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PG8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // 输出
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed; // 速度 100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉
GPIO_Init(GPIOG, &GPIO_InitStructure); // 配置生效
/* USART2 初始化设置 */
USART_InitStructure.USART_BaudRate = baud; // 波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字长 8bit
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;// 收发模式
USART_Init(USART2, &USART_InitStructure); // 配置生效
USART_Cmd(USART2, ENABLE); // 使能 USART2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启接收中断
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); // 开启空闲中断
/* USART2 NVIC 配置 */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RS485_RX_MODE; // 默认为接收模式
}
/**
* @brief USART2 发送一个字节
* @param ch: 要发送的字节数据
* @retval None
*/
void USART2_SendByte(uint8_t ch)
{
/* 发送一个字节数据到 USART2 */
USART_SendData(USART2, ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
/**
* @brief USART2 发送一个字符串
* @param *str: 要发送的字符串
* @param strlen: 字符串长度
* @retval None
*/
void USART2_SendString(uint8_t *str, uint8_t strlen)
{
unsigned int k = 0;
RS485_TX_MODE; // 进入发送模式
do
{
USART2_SendByte(*(str + k));
} while (k++ < strlen);
RS485_RX_MODE; // 进入接收模式
}
/**
* @brief USART2 中断服务函数
* @param None
* @retval None
*/
void USART2_IRQHandler(void)
{
uint8_t Res, forclear;
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res = USART_ReceiveData(USART2);
USART2_RX_Buffer[USART2_RX_Index++] = Res;
/* 防止接收缓存下标溢出 */
if (USART2_RX_Index >= USART2_RX_MAX)
USART2_RX_Index = 0;
}
if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
USART2_RX_OverFlag = 1;
forclear = USART_ReceiveData(USART2);
}
}