我试图让usart在我的stm32f0发现上工作,但现在我发现关于这个的文档有点“缺乏”,所以有谁有为stm32f050工作的usart的例子吗?
谢谢。
巴特·特尼森
发布于 2013-02-07 18:17:30
好的,在网上搜索了两天之后。我找到了这段代码,并设法让它工作起来:
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');
}
}*注意:此通用异步收发器使用引脚PA2和PA3 (RX和TX)。
当uart的sendbuffer的缓冲区为空时,它发送一个X。更妙的是,这段代码只使用了stm32f0xx.h文件。所以它去掉了任何不整洁的部分。
希望有人也能使用它,因为它花费了我很多努力来找到这个简单的代码。也许有一天我会写一篇关于使用stm32f0进行uart编程的指南。
*编辑:
我确实写了一篇关于usart的教程。它可以在这里找到:
Tutorial usart stm32f0希望这能帮助很多人。
https://stackoverflow.com/questions/14706314
复制相似问题