MM32F103_n版单片机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); 
}

 


 

posted @ 2018-05-21 17:48  地下修文  阅读(588)  评论(0编辑  收藏  举报