Skip to main content
Associate III
July 17, 2025
Solved

SystemCoreClock vs HCLK vs APB1

  • July 17, 2025
  • 2 replies
  • 948 views

I am learning to use the PWM Input mode to measure input frequency. I am using examples from the internet to help me along.

In my .ioc file SYSCLK is set to 8MHz and with a prescaler set to /8 my HCLK and APB1 are set 1Mhz

xuraax_0-1752765749639.png

In the examples I studied in the callback routine Frequency is calculated either:

1:   1000000 / capture_value

while another uses this method

2:  SystemCoreClock / capture_value

Method 1: works but in case you eventually need to change the prescaler you also need to change the constant.

Method 2: sounds more robust but when I hover over "SystemCoreClock" in main.c a window comes up stating that SystemCoreClock is set to 8000000 anyway. What variable do I need to use to ensure that the value used in the calculation is in fact HCLK or APB1?

Best answer by TDK

Since it's a timer, you want to get the timer tick rate, which is either APB1 or APB1 * 2. You can get this using HAL_RCC_GetPCLK1Freq and HAL_RCC_GetClockConfig.

2 replies

TDK
TDKBest answer
Super User
July 17, 2025

Since it's a timer, you want to get the timer tick rate, which is either APB1 or APB1 * 2. You can get this using HAL_RCC_GetPCLK1Freq and HAL_RCC_GetClockConfig.

"If you feel a post has answered your question, please click ""Accept as Solution""."
xuraaxAuthor
Associate III
July 18, 2025

Ok thanks. I need to read up on that.

 

Whilst studying file stm32f1xx_hal_rcc.c I noted the following:

 

uint32_t HAL_RCC_GetHCLKFreq(void)
{
 return SystemCoreClock;
}

As I understand it this is the routine to use if you want to get HCLK which for me is the same as APB1 at 1000000.

This too returns SystemCoreClock so shouldn't this also show 1000000 when I hover on it in main.c?

mƎALLEm
Technical Moderator
July 18, 2025

@xuraax wrote:

 

As I understand it this is the routine to use if you want to get HCLK which for me is the same as APB1 at 1000000.

This too returns SystemCoreClock so shouldn't this also show 1000000 when I hover on it in main.c?


That depends on the APB prescalers:

mALLEm_0-1752843594897.png

If APB1 prescaler = 1, HCLK and APB1 frequencies are equal. As said by @TDK you need to use HAL_RCC_GetPCLK1Freq and HAL_RCC_GetClockConfig no matter the APB division.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
xuraaxAuthor
Associate III
July 27, 2025

For some reason the button "Accept as Solution" is not coming up on my screen

TDK
Super User
July 27, 2025

You've already accepted a solution here. If you did so in error, you can undo it. Otherwise, you're good to go.

"If you feel a post has answered your question, please click ""Accept as Solution""."