今天调试STM32F030 USART2,收发不了数据,仿真的时候进入不了中断,但是配置也是正确的。搞了很久,原因是先初始化的串口再初始化Flash数据,导致读入的波特率是0,进而无法正常使用USART2,浪费了大量的时间,血的教训呀。
void UartIntial(void){
int i;
GPIO_InitTypeDef GPIO_uInitStructure;
USART_InitTypeDef USART_uInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
for(i=0; i memset((unsigned char*)&uart[i], 0x00, sizeof(struct UartApiBuf)); uart[i].Snd.IsSndIng = FALSE; uart[i].Snd.IsRcvIng = FALSE; uart[i].Snd.lpChar = 0; uart[i].Snd.lpIdle = 0; uart[i].Rcv.IsSndIng = FALSE; uart[i].Rcv.IsRcvIng = FALSE; uart[i].Rcv.lpChar = 0; uart[i].Rcv.lpIdle = 0; } // USART2_EN -> PA0 // USART2_TX -> PA2 // USART2_RX -> PA3 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1); GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP; GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50M GPIO_Init(GPIOA,&GPIO_uInitStructure); GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP; GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50M GPIO_Init(GPIOA,&GPIO_uInitStructure); GPIO_ResetBits(GPIOA, GPIO_Pin_0); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_uInitStructure.USART_BaudRate = s.Baud; USART_uInitStructure.USART_Parity = USART_Parity_No; USART_uInitStructure.USART_StopBits = USART_StopBits_1; USART_uInitStructure.USART_WordLength = USART_WordLength_8b; USART_uInitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_uInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART2,&USART_uInitStructure); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_Cmd(USART2,ENABLE); }
上一篇:STM32关于操作内部FLASH整理
下一篇:STM32F030 Nucleo-多样的SPI通信之Master标准模式-SPIFlash读写
推荐阅读最新更新时间:2024-03-16 16:08