你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32L072RB LPUART波特率不准

[复制链接]
fufufuchi 提问时间:2019-11-8 19:47 /
各位大神,请教一下,我有一块Nucleo 64开发板,把MCU换成STM32L072RB。使用LSE作为LPUART的源,用示波器测试PA2(LPUART_TX)发现波特率不准。而且一个字节中高位和低位保持时间不一致。
高位的波特率误差是(8200-9600)/9600=-14.5%
低位的波特率误差是(10900-9600)/9600=13.5%
如图所示
代码如下:

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Configure the system clock to 2.097 MHz */
  SystemClock_Config();

  /* Add your application code here */
        /** 配置LSE*/
        Configure_LSE();
       
        /** Leds初始化*/
        Leds_Init();
               
        /** LPUART初始化*/
        Configure_LPUART();
               
       
  /* Infinite loop */
  while (1)
  {
                delay_ms(200);
                printf("-------------------------------------------------------------\r\n");
  }
}


void SystemClock_Config(void)
{
  /* MSI configuration and activation */
  LL_RCC_PLL_Disable();
  /* Set new latency */
  LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);

  LL_RCC_MSI_Enable();
  while(LL_RCC_MSI_IsReady() != 1)
  {
  };
  LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_5);  
  LL_RCC_MSI_SetCalibTrimming(0x0);

  /* Sysclk activation  */
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI);
  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI)
  {
  };

  /* Set APB1 & APB2 prescaler*/
  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);

  /* Set systick to 1ms in using frequency set to 2MHz */
  LL_Init1msTick(2097000);
        LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);

  /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
  LL_SetSystemCoreClock(2097000);  

  /* Enable Power Control clock */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  /* The voltage scaling allows optimizing the power consumption when the device is
     clocked below the maximum system frequency, to update the voltage scaling value
     regarding system frequency refer to product datasheet.  */
  LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE3);
       
        /** Enable SysTick exception request **/
        LL_SYSTICK_EnableIT();

}


void Configure_LSE(void)
{
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  LL_PWR_EnableBkUpAccess();
  /* Enable LSE only if disabled.*/
  if (LL_RCC_LSE_IsReady() == 0)
  {
    LL_RCC_LSE_Enable();
    while (LL_RCC_LSE_IsReady() != 1)
    {
    }
  }
}


void Leds_Init(void)
{
        LL_GPIO_InitTypeDef gpio_initstruct;
        /* Enable the LED2 Clock */
  LED2_GPIO_CLK_ENABLE();

  /* Configure IO in output push-pull mode to drive external LED2 */
  gpio_initstruct.Pin        = LED2_PIN;
  gpio_initstruct.Mode       = LL_GPIO_MODE_OUTPUT;
  gpio_initstruct.Speed      = LL_GPIO_SPEED_FREQ_LOW;
  gpio_initstruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  gpio_initstruct.Pull       = LL_GPIO_PULL_NO;
       
        LL_GPIO_Init(LED2_GPIO_PORT, &gpio_initstruct);
       
        BSP_Led_Off();
}


void Configure_LPUART(void)
{
  LL_LPUART_InitTypeDef lpuart_initstruct;

  /* (1) Enable GPIO clock and configures the LPUART1 pins *******************/
  /*    (TX on PC.1, RX on PC.0)                        **********************/

  /* Enable the peripheral clock of LUART */
        LUART_GPIO_CLK_ENABLE();

  /* Configure TX Pin as : Alternate function, High Speed, PushPull, Pull up */
  LL_GPIO_SetPinMode(LUART_GPIO_PORT, LUART_TX_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetAFPin_0_7(LUART_GPIO_PORT, LUART_TX_PIN, LUART_TX_AF);
  LL_GPIO_SetPinSpeed(LUART_GPIO_PORT, LUART_TX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(LUART_GPIO_PORT, LUART_TX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
  LL_GPIO_SetPinPull(LUART_GPIO_PORT, LUART_TX_PIN, LL_GPIO_PULL_NO);

  /* Configure RX Pin as : Alternate function, High Speed, PushPull, Pull up */
  LL_GPIO_SetPinMode(LUART_GPIO_PORT, LUART_RX_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetAFPin_0_7(LUART_GPIO_PORT, LUART_RX_PIN, LUART_RX_AF);
  LL_GPIO_SetPinSpeed(LUART_GPIO_PORT, LUART_RX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(LUART_GPIO_PORT, LUART_RX_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  LL_GPIO_SetPinPull(LUART_GPIO_PORT, LUART_RX_PIN, LL_GPIO_PULL_NO);

  /* (3) Enable the LPUART1 peripheral clock and clock source ****************/
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1);

  /* Set LPUART1 clock source as LSE */
  LL_RCC_SetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE_LSE);

  /* (4) Configure LPUART1 functional parameters ********************************/

  /* Disable LPUART1 prior modifying configuration registers */
  /* Note: Commented as corresponding to Reset value */
  // LL_LPUART_Disable(LPUART1);

  /* Set fields of initialization structure                   */
  /*  - BaudRate            : 9600                            */
  /*  - DataWidth           : LL_LPUART_DATAWIDTH_8B          */
  /*  - StopBits            : LL_LPUART_STOPBITS_1            */
  /*  - Parity              : LL_LPUART_PARITY_NONE           */
  /*  - TransferDirection   : LL_LPUART_DIRECTION_TX_RX       */
  /*  - HardwareFlowControl : LL_LPUART_HWCONTROL_NONE        */
  lpuart_initstruct.BaudRate            = 9600;
  lpuart_initstruct.DataWidth           = LL_LPUART_DATAWIDTH_8B;
  lpuart_initstruct.StopBits            = LL_LPUART_STOPBITS_1;
  lpuart_initstruct.Parity              = LL_LPUART_PARITY_NONE;
  lpuart_initstruct.TransferDirection   = LL_LPUART_DIRECTION_TX_RX;
  lpuart_initstruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;

  /* Initialize LPUART instance according to parameters defined in initialization structure */
  LL_LPUART_Init(LPUART1, &lpuart_initstruct);

  /* (5) Enable LPUART1 **********************************************************/
  LL_LPUART_Enable(LPUART1);
}


int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
        LL_LPUART_TransmitData8(LPUART1, ch);

  /* Loop until transmit data complete */
  /* Wait for TXE flag to be raised */
  while (!ConsoleFlag_TXE(LPUART1))
        {}
  return ch;
}

请大神们支援!
IMG_20191108_191950.jpg
IMG_20191108_192237.jpg
路径.jpg
收藏 评论4 发布时间:2019-11-8 19:47

举报

4个回答
深山明月 回答时间:2019-11-8 21:15:31
为什么Configure the system clock to 2.097 MHz,一般不都是整数吗,看看时针设置

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

fufufuchi 回答时间:2019-11-8 21:45:42
深山明月 发表于 2019-11-8 21:15
为什么Configure the system clock to 2.097 MHz,一般不都是整数吗,看看时针设置

因为使用的是MSI时钟。
慎微 回答时间:2019-11-9 10:13:01
1)您换了CPU,这个CPU与原Nucleo 64板哪里不匹配,不知道,产生的问题 可能不止一个,这仅是其一;
2)就算换了CPU,如果设置正确的主频,Luart 时钟主频,其波特率还是差不离的;
3)如果您不会设置CPU主频,GPIO 主频,LUART时钟,您大可用STMcubeMX 神器来设定初始化啊

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

fufufuchi 回答时间:2019-11-10 10:56:30
我想我知道原因了
32768/3=10922(和10900相近)
32768/4=8192(和8200相近)
硬伤,没辄。
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版