高级会员

- 积分
- 562
- 金钱
- 562
- 注册时间
- 2019-11-11
- 在线时间
- 71 小时
|
F407的uart4映射PA0,PA1引脚能发不能收:
拿示波器看过,PA1(RX)引脚上在上位机发送数据时有波形信号。仿真调试时发现无法进入接收中断。
//初始化IO 串口4 bound:波特率
void uart4_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
//串口4对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4); //GPIOA0复用为UART4
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_UART4); //GPIOA1复用为UART4
//USART4端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure);
//USART4 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(UART4, &USART_InitStructure); //初始化串口4
USART_Cmd(UART4, ENABLE); //使能串口4
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启相关中断
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口4中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=14;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void UART4_IRQHandler(void) //串口4中断服务程序
{
u8 Res4;
USART_GetFlagStatus(UART4,USART_FLAG_TXE);
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //
{
Res4 =USART_ReceiveData(UART4);//(UART4->DR); //读取接收到的数据
Usart_Rx_ISR_LoopBuffer(3,Res4);
}
USART_ClearFlag(UART4,USART_IT_RXNE);
}
|
|