3897 views|6 replies

2865

Posts

4

Resources
The OP
 

STM32G timer setting problem [Copy link]

I used STM32CUBE to develop an experimental program for STM32G431. I felt that the setting was 1K Hz, and the TIM6 clock was very slow. About 50 interrupt cycles, that is, 1S, was 10 times slower. Because the previous experiment was successful, the project was deleted and not saved, so there is no way to verify it. Problem description

1. The clock tree is set up as shown in the figure

2. According to the setting, the clock source of TIM6 should be APB1, APB2, and the clock of peripherals are all 168M. So set TIM6 as follows:

PreScale = 168-1; //The base frequency should be 1M

Counter Period = 1000; //The interrupt interval should be 1K

3. Both low-speed and high-speed crystal oscillators are turned on.

Visually there should be no problem, but it is 10 times slower.

The question is how to set the parameters of TIM6, where did I go wrong?

I used to use NXP's LPC series of microcontrollers, and rarely used STM32 microcontrollers, so I am not very familiar with STM32. A friend recommended STM32 microcontrollers, so I got an STM32 development board to learn STM32. But in recent days, some problems have kept appearing that made me depressed!

This post is from stm32/stm8

Latest reply

The expected main frequency is 168M, but the actual frequency is 16M, which is nearly ten times higher.   Details Published on 2019-12-21 10:25
 

9702

Posts

24

Resources
2
 
First, make sure the frequency of the main clock is consistent with the setting. Check SystemCoreClock in debug mode.
This post is from stm32/stm8

Comments

Thanks, brother. SysClock=16000000, the system clock is 16M. There is no problem with the clock tree settings. Please give further answers! Thanks again.  Details Published on 2019-12-6 09:48
 
 

2865

Posts

4

Resources
3
 
littleshrimp posted on 2019-12-6 09:28 First make sure that the frequency of the main clock is consistent with the setting. Check SystemCoreClock in debug mode

Thanks, brother. SysClock=16000000, the system clock is 16M. There is no problem with the clock tree settings. Please give further answers! Thanks again.

This post is from stm32/stm8

Comments

Does it not match the actual settings? Use an external crystal? Is there a problem with the crystal? Try updating cubemx to the latest version.  Details Published on 2019-12-6 10:16
 
 

9702

Posts

24

Resources
4
 
bigbat posted on 2019-12-6 09:48 Thank you brother, SysClock=16000000, the system clock is 16M, and there is no problem with the clock tree settings. Please give further answers! Again...
Does it not match the actual settings? Use an external crystal oscillator? Is there a problem with the crystal oscillator? Update cubemx to the latest version and try
This post is from stm32/stm8

Comments

The code for clock setting seems to be correct. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; is also the same as the setting. It feels like the clock has always been HSI|HSE. The 16M clock has never changed. The external crystal oscillator is also turned on. Yesterday I encountered TI  Details Published on 2019-12-6 10:54
 
 
 

2865

Posts

4

Resources
5
 
littleshrimp posted on 2019-12-6 10:16 Does it not match the actual settings? Use an external crystal oscillator? Is there a problem with the crystal oscillator? Try updating cubemx to the latest version

The clock setting code seems to be correct.

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

It is the same as the settings. It feels like the clock has always been HSI|HSE. The 16M clock has never changed. The external crystal oscillator is also turned on.

Yesterday I encountered a problem with the TIM7 and TIM6 timers, which was also very strange. With the same settings and the same code, TIM6 worked, but TIM7 crashed! I think there should be something wrong with the HAL library code of STM32G431.

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage 
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  /** Configure LSE Drive Capability 
  */
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 42;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  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_8) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_LPUART1
                              |RCC_PERIPHCLK_ADC12;
  PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

This post is from stm32/stm8

Comments

It is possible. I used the L series before and encountered the problem that the main frequency did not match the actual designed frequency. Later, after updating CubeMX, it was normal. If it is a clock problem, you can try to use the SystemClock_Config code in the routine library to replace the SystemClock_Config generated by CubeMX.  Details Published on 2019-12-6 11:01
 
 
 

9702

Posts

24

Resources
6
 
bigbat posted on 2019-12-6 10:54 The clock setting code seems to be correct. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; ...

It is possible. I used the L series before and encountered the problem that the main frequency did not match the actual designed frequency. Later, I updated CubeMX and it became normal.

If it is a clock problem, you can try to use the SystemClock_Config code in the routine library to replace the SystemClock_Config generated by CubeMX

This post is from stm32/stm8
 
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 

15

Posts

0

Resources
7
 

The expected main frequency is 168M, but the actual frequency is 16M, which is nearly ten times higher.

This post is from stm32/stm8
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list