问题
i'm trying to get the usart to work on my stm32f0-discovery but now i found out that the documentation about this kinda " lacks" so is there anyone who has an example of any usart working for the stm32f050?
thanks.
Bart Teunissen
回答1:
Okay after two days of searching around on the internet. I found this little piece of code, and i managed to get it working:
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');
}
}
*note: this uart uses pin PA2 and PA3 (RX and TX).
it sends a X when the buffer of the sendbuffer of the uart is empty. Better still, this piece of code only uses the stm32f0xx.h file. So its stripped of any unneassesary parts.
hope someone can use this as well, because it cost me lots of efforts to find this simple code. Maybe i'll write a guide some day about uart programming with the stm32f0.
*Edit:
I have indeed written a tutorial about the usart. And it can be found here:
Tutorial usart stm32f0 Hope this helps loads of people.
来源:https://stackoverflow.com/questions/14706314/stm32f0-uart-programming