本文介绍了stm32f0 uart编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让usart在我的stm32f0发现上工作,但是现在我发现有关这种缺乏"的文档,所以有人能为usm工作于stm32f050的例子吗?

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?

谢谢.

巴特Teunissen

Bart Teunissen

推荐答案

在互联网上搜索两天之后,可以.我找到了这小段代码,并设法使其正常工作:

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');
  }
}

*注意:此uart使用引脚PA2和PA3(RX和TX).

*note: this uart uses pin PA2 and PA3 (RX and TX).

当uart的sendbuffer的缓冲区为空时,它发送X.更好的是,这段代码仅使用stm32f0xx.h文件.因此去除了所有不必要的部分.

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.

希望有人也可以使用它,因为找到这个简单的代码花了我很多精力.也许有一天我会写一篇关于使用stm32f0进行uart编程的指南.

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.

*

我确实写了关于usart的教程.可以在这里找到:

I have indeed written a tutorial about the usart. And it can be found here:

教程usart stm32f0 希望这可以帮助很多人.

Tutorial usart stm32f0 Hope this helps loads of people.

这篇关于stm32f0 uart编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:14