首页 > 其他 > 详细

MM32单片机UART不能使用问题

时间:2018-05-21 18:34:21      阅读:416      评论:0      收藏:0      [点我收藏+]
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不能使用问题

原文:https://www.cnblogs.com/MyFlyLife/p/9068468.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!