问题描述: USART1可以正常使用 USART2已初始化,却并没有收发记录 用串口调试助手只能发送,没有接收 是因为开发板上还需要设置些吗?
代码详情如下:
#include "usart2.h"
void USART2_Init(u32 My_BaudRate) { GPIO_InitTypeDef GPIO_InitStrue; USART_InitTypeDef USART_InitStrue; NVIC_InitTypeDef NVIC_InitStrue;
// 外设使能时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); USART_DeInit(USART2); //复位串口2 -> 可以没有
// 初始化 串口对应IO口 TX-PA2 RX-PA3 GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStrue.GPIO_Pin=GPIO_Pin_2; GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_InitStrue.GPIO_Pin=GPIO_Pin_3; GPIO_Init(GPIOA,&GPIO_InitStrue);
// 初始化 串口模式状态 USART_InitStrue.USART_BaudRate=My_BaudRate; // 波特率 USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; // 硬件流控制 USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; // 发送 接收 模式都使用 USART_InitStrue.USART_Parity=USART_Parity_No; // 没有奇偶校验 USART_InitStrue.USART_StopBits=USART_StopBits_1; // 一位停止位 USART_InitStrue.USART_WordLength=USART_WordLength_8b; // 每次发送数据宽度为8位 USART_Init(USART2,&USART_InitStrue);
USART_Cmd(USART2,ENABLE);//使能串口 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//开启接收中断
// 初始化 中断优先级 NVIC_InitStrue.NVIC_IRQChannel=USART2_IRQn; NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; NVIC_Init(&NVIC_InitStrue); }
void USART2_IRQHandler(void) // 串口2中断服务函数 { u8 res; if(USART_GetITStatus(USART2,USART_IT_RXNE)) // 中断标志 { res= USART_ReceiveData(USART2); // 串口2 接收 USART_SendData(USART2,res); // 串口2 发送 } }
.h文件
#ifndef __USART2_H #define __USART2_H #include "stdio.h" #include "sys.h"
void USART2_Init(u32 My_BaudRate);
#endif
|