MM32单片机UART不能使用问题

Posted 地下修文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MM32单片机UART不能使用问题相关的知识,希望对你有一定的参考价值。

UART1默认RX/TX引脚为PA9和PA10,

现重定义到PB6和PB7,
修改后程序如下,使能UART1和GPIOB时钟,初始化IO、中断、UART相关配置,使能串口中断。
void
TEST_Uart_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; UART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_InitStructure.UART_BaudRate = 115200; USART_InitStructure.UART_WordLength = UART_WordLength_8b; USART_InitStructure.UART_StopBits = UART_StopBits_1; USART_InitStructure.UART_Parity = UART_Parity_No; USART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None; USART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx; UART_Init(UART1, &USART_InitStructure); UART_ITConfig(UART1, UART_IT_RXIEN, ENABLE); UART_Cmd(UART1, ENABLE); }
编译下载后串口无法使用,检查后发现,修改引脚使用重定义需要使用remap寄存器,需要使能GPIO_Remap_UART1,
因此要调用
 GPIO_PinRemapConfig(GPIO_Remap_UART1,ENABLE);
将UART1管脚重映射。
而使用重映射功能需要使能AFIO时钟,可调用
RCC_APB2PeriphClockCmd函数使能RCC_APB2Periph_AFIO,
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
修改后程序如下:
void TEST_Uart_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    UART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
                           
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

      
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_PinRemapConfig(GPIO_Remap_UART1,ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    
    USART_InitStructure.UART_BaudRate = 115200;  //2¨ì??ê
    USART_InitStructure.UART_WordLength = UART_WordLength_8b;  //8??êy?Y?£ê?
    USART_InitStructure.UART_StopBits = UART_StopBits_1;  //�1?? 1??
    USART_InitStructure.UART_Parity = UART_Parity_No;
    USART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    USART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(UART1, &USART_InitStructure);
    UART_ITConfig(UART1, UART_IT_RXIEN, ENABLE); 
    UART_Cmd(UART1, ENABLE); 
}

 


 

以上是关于MM32单片机UART不能使用问题的主要内容,如果未能解决你的问题,请参考以下文章

ESP32 单片机学习笔记 - 01 - gpio&ledc&uart

灵动微MM32SPIN423C低压单片式电机驱动MCU

STM32F407单片机UART串口gpio口复用功能配置问题

STM32 UART串口通讯编程方法

STM32/MM32/GD32 IAP跳转到APP后无法进中断

在MM32单片机上使用MicroPython开发智能小车