第一次用STM32F0系列的MCU,在用STM32F030C8T6的USART2,即PA2和PA3作为串口使用时,始终发送不出去,USART1可以正常使用,USART2的配置与USART1一样,但是USART2不能正常工作,不知道哪里的问题,希望可以得到大神的帮助,谢谢!源码如下:void USART1_INIT(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE ); USART_InitStructure.USART_BaudRate = 9600; 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(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } void USART2_INIT(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE ); USART_InitStructure.USART_BaudRate = 9600; 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_Cmd(USART2, ENABLE); } void UART2_Send(uint8_t *Buffer, uint32_t Length) { while(Length != 0) { while(!((USART2->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê USART2->TDR = *Buffer; Buffer++; Length--; } } void UART1_Send(uint8_t *Buffer, uint32_t Length) { while(Length != 0) { while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê USART1->TDR = *Buffer; Buffer++; Length--; } } uint8_t Tx2_Buffer[] = "good"; uint8_t Tx1_Buffer[] = "nice"; #define countof(a) (sizeof(a) / sizeof(*(a))) int main(void) { NVIC_Configuration(); SysTick_Start(5); USART1_INIT(); USART2_INIT(); while (1) { if (SysTick->CTRL & 0x10000)// 5ms一次任务 { UART1_Send(Tx1_Buffer, countof(Tx1_Buffer)-1); UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1); } } } 在主函数中每隔5ms发送Tx1_Buffer[]和Tx2_Buffer[],但是在进入UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1);之后就在while(!((USART2->ISR)&(1<<7)));这里死掉了,即出不来这个while,谢谢! |
【STM32F0开发日志/评测/笔记】+互补PWM波的产生
STM32F030 PB14和PB15无法输出PWM求助
【STM32F030探索套件】序列之五 外部中断
【STM32F0开发日志---二】+ucosii.2.92移植在STM32F030
上传个STM32F0+5110+内部温度传感器的菜鸟实例
【STM32F030探索套件使用问题】STM32F030 SPI方式驱动ST7565LCD失败
求一份STM32F051 I2C驱动LCD 12864的例程
STM32F0 M0 向结构体赋值进入HardFault异常
STM32F0 ADC-DMA方式采集2路数据时出现问题
STM32F030C8T6,TIM16定时慢很多问题?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
评分
查看全部评分
评分
查看全部评分
串口2在PA2,PA3
评分
查看全部评分
评分
查看全部评分
之前曾写过一个Cubemx配置串口的例子,供您参考。
看看你的工程中
接收时,需要设置的函数
HAL_UART_RxCpltCallback(.....)
HAL_UART_Receive_IT
发送时需要设置的内容:
PUTCHAR_PROTOTYPE
用Printf(....)
看看呢?
串口很简单的。
评分
查看全部评分
评分
查看全部评分