STM32 SysTick 的计数速度是应有的两倍 [英] STM32 SysTick counting twice as fast as it should

查看:58
本文介绍了STM32 SysTick 的计数速度是应有的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块 STM32L476RC 核电路板,用于学习 STM32.我正在使用 STM32Cube HAL 和 AC6 System Workbench 进行开发.我试图远离 CubeMX,因为我的目标更多是学习,而不仅仅是让一些东西工作.

I have a STM32L476RC nucleo board that I am using to learn STM32. I am using STM32Cube HAL and AC6 System Workbench to develop on. I am trying to stay away from CubeMX as my goal is more towards learning than just getting something to work.

我遇到的问题是,当我尝试使用下面的代码设置 systick 计时器时,它的计数速度似乎是应有的两倍.

The problem that I am having is when I try to set up the systick timer using the code below, it seems to be counting twice as fast as it should.

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

但是,如果我只是在开机时将其保留为默认值,那么它会以正确的速度进行计数.

However if I just leave it default form the power-up then it is counting at the correct speed.

我已使用 CubeMX 生成以下时钟设置,并将其直接粘贴到在 System Workbench 中创建的新项目中,但是 systick 计数器的计数速度似乎仍是应有的两倍.CubeMX 生成的项目似乎运行得很好但是

I have used CubeMX to generate the following clock setup, and paste it directly into a fresh project created in System Workbench, however the systick counter still seems to be counting twice as fast as it should be. The project that was generated by CubeMX seems to be running just fine however

    /** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    //Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    //Error_Handler();
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    //Error_Handler();
  }

    /**Configure the main internal regulator output voltage
    */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    //Error_Handler();
  }

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

一定是我遗漏了什么.也许以某种方式在其他地方配置了滴答计数器,并调用 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);只是设置另一个滴答计数器?我不知道!请帮助我完全不知道发生了什么!

There must be something I am missing. Maybe somehow the tick counter is configured elsewhere, and calling HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); is just setting another tick counter? I have no idea! Please help I am completely lost as to what is going on!

我通过简单地闪烁 LED 并在逻辑分析仪上测量频率来测量滴答速度:

I am measuring the tick speed by simply flashing a LED and and measuring the frequency on a logic analyser:

if (HAL_GetTick() - LEDstopwatch > 1000)
{
    // Toggle the LED
    //BSP_LED_Toggle(LED2);
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_10);
    // Reset the stopwatch
    LEDstopwatch = HAL_GetTick();
}

我的测试项目的完整代码在这里:https://github.com/c-herring/STM32L476_Nucleo_FirstTest

The full code for my test project is here: https://github.com/c-herring/STM32L476_Nucleo_FirstTest

谢谢!

推荐答案

在你复制的SystemClock_Config函数中,STM32CubeMX初始化系统时钟为80MHz.为此,它使用 HSI (16MHz) 作为 PLL 的输入,然后将其除以 1 (PLLM),再乘以 10 (PLLN),最后除以 2 (PLLR).

In the SystemClock_Config function you copied, STM32CubeMX initializes the system clock at 80MHz. To do this, it uses HSI (16MHz) as input of the PLL, then divide it by 1 (PLLM), then multiplies it by 10 (PLLN) and finally divides its by 2 (PLLR).

当您不使用STM32CubeMX并从头开始创建项目时,时钟在system_stm32l4xx.c中的SystemInit中初始化.SystemInit 在启动文件 (startup_stm32l476xx.S) 中的 main 之前被调用.根据注释 SystemInit 使用 MSI (4MHz) 作为 PLL 的输入将时钟初始化为 40MHz.

When you don't use STM32CubeMX and creates a project from scratch, the clock is initialized in SystemInit in the system_stm32l4xx.c. SystemInit is called before main in the startup file (startup_stm32l476xx.S). According to the comments SystemInit initializes the clock at 40MHz using the MSI (4MHz) as input of the PLL.

系统时钟快两倍,因此您可以在 Systick 中看到差异.

The system clock is twice faster therefore you see the difference in the Systick.

这篇关于STM32 SysTick 的计数速度是应有的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆