使用STM32F103VCT6的串口2重定位实现串口打印,总是死机,后来发现复位后,进行串口配置,其他寄存器正常,只有BRR寄存器一直是0,可我明明配置的是115200,不知为什么?
代码如下:
void main(void)
{
SystemInit();
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART2_Configuration();
......
}
void RCC_Configuration(void)
{
SystemInit();//72M
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOB口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//开启GPIOC口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);//开启GPIOD口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//开启GPIOE口时钟 2.0版本增加的内容
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启功能复用IO时钟,PB9复用TIM4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//开启TIM4时钟,挂载在APB1上
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启TIM3时钟,挂载在APB1上
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);//开启TIM5时钟,11.28
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);//开启DAC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//开启ADC1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);//开启ADC2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3,ENABLE);//开启ADC3时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//分频:12M 最大14M
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//开启USART2时钟
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//开启USART3时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOB);
GPIO_DeInit(GPIOC);
GPIO_DeInit(GPIOD);
GPIO_DeInit(GPIOE);
//关闭JTAG口作为GPIO,保留SWD
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
//USART2管脚配置-PC通信 PD5 PD6
GPIO_InitStructure.GPIO_Pin = USART2_TX_PIN; //USART2-TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USARAT2_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_RX_PIN; //USART2-RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USARAT2_PORT,&GPIO_InitStructure);
}
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);//这一句把接收中断打开
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);//清中断标志位
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);//这一句把接收中断打开
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);//清中断标志位
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);//这一句把接收中断打开
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);//清中断标志位
}
请各位给看看,是什么原因? |