Bar*_*sen 10
好的,经过两天的互联网搜索.我发现了这段小代码,我设法让它工作:
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
//Configure USART2 pins: Rx and Tx ----------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
while(1)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, 'X');
}
}
Run Code Online (Sandbox Code Playgroud)
*注意:此uart使用引脚PA2和PA3(RX和TX).
当uart的sendbuffer的缓冲区为空时,它发送一个X. 更好的是,这段代码只使用stm32f0xx.h文件.所以它被剥夺了任何不必要的部分.
希望有人也可以使用它,因为我花了很多精力找到这个简单的代码.也许有一天我会用stm32f0写一篇关于uart编程的指南.
*编辑:
我确实写了一篇关于usart的教程.它可以在这里找到:
教程usart stm32f0 希望这有助于大量人群.