STM32F407的串口通信

2019-07-20 15:47发布

我仔细看了探索者STM32F407的串口通信实验的讲解,关于波特率的计算我有一些疑问。PPT和中文参考手册中都详细讲解了波特率的计算方法,但是在实验代码中只是在库函数中设置USART_InitStructure.USART_BaudRate=115200就行了,那么是如何设置波特率寄存器的呢?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
11条回答
maple2
1楼-- · 2019-07-20 16:22
这位同学!
老师不得不说你了,你就不能跳过这个问题继续奋斗么?等你觉悟高了,再返回来解决这个问题,不行吗?
/*---------------------------- USART BRR Configuration -----------------------*/
  /* Configure the USART Baud Rate */
  RCC_GetClocksFreq(&RCC_ClocksStatus);

  if ((USARTx == USART1) || (USARTx == USART6))
  {
    apbclock = RCC_ClocksStatus.PCLK2_Frequency;
  }
  else
  {
    apbclock = RCC_ClocksStatus.PCLK1_Frequency;
  }
  
  /* Determine the integer part */
  if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  {
    /* Integer part computing in case Oversampling mode is 8 Samples */
    integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));   
  }
  else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
  {
    /* Integer part computing in case Oversampling mode is 16 Samples */
    integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));   
  }
  tmpreg = (integerdivider / 100) << 4;

  /* Determine the fractional part */
  fractionaldivider = integerdivider - (100 * (tmpreg >> 4));

  /* Implement the fractional part in the register */
  if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  {
    tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
  }
  else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
  {
    tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
  }
  
  /* Write to USART BRR register */
  USARTx->BRR = (uint16_t)tmpreg;
}

至于计算公式,想必你已经弄到手了!
luojue10330
2楼-- · 2019-07-20 22:05
视频中直接就是设置成115200,不需要计算吗?还是说我们只需设置想要的波特率就行了,单片机替我们去计算好了波特率寄存器的值??
luojue10330
3楼-- · 2019-07-21 02:47
@正点原子, @八度空间
八度空间
4楼-- · 2019-07-21 07:42
luojue10330 发表于 2017-3-28 10:46
视频中直接就是设置成115200,不需要计算吗?还是说我们只需设置想要的波特率就行了,单片机替我们去计算好 ...

看下USART_Init()函数里边,官方是怎么计算的
丶路常寻走不
5楼-- · 2019-07-21 12:59
 精彩回答 2  元偷偷看……
丶路常寻走不
6楼-- · 2019-07-21 15:44
在usart.c中串口初始化程序有个

        USART2->BRR=mantissa;        
这个就是设置寄存器波特率的

一周热门 更多>