cancel
Showing results for 
Search instead for 
Did you mean: 

Milliseconds Deviations in toggling led for one second

santhosh16
Associate II

Hi,
I have been using Tim11 general purpose timer in Nucleo-f410rb micro controller board to blink an led at a delay of 1 second but there is a deviation of 18 ms in it. which have a 16 bit prescaler and 16 bit auto reload counter with it. In the .ioc file the clock configuration section I have set the clock frequency of the APB2 bus timer clock as 100Mhz which is the maximum frequency can be set and the prescaler value as 9999 and Auto reload register value as 9999. Also the datasheet of the board given stm32f410rb datasheet . and the user manual Stm32f410rb user manual 
After made the changes in the .ioc file generated code and code is generated for the changes I made and after that i start the timer in the main function like below

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_TIM11_Init();
  /* USER CODE BEGIN 2 */

  HAL_TIM_Base_Start_IT(&htim11);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 and after that i have added the HAL function HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  // Check which version of the timer triggered this callback and toggle LED
  if (htim == &htim11)
  {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  }
}

I followed these based on the "Example 3: Timer Interrupts" section in the website Getting Started with STM32 - Timers and Timer Interrupts

I have also provide the ioc file photos
ioc file for forum.png

 

Screenshot from 2025-05-26 16-39-08.png

Even I have tried to blink in 500 ms, 250 ms but results in 491.4 ms and 245.9 ms respectively.
Kindly help to toggle the led after 1 second.

1 ACCEPTED SOLUTION

Accepted Solutions
RobK1
Associate III

You are using the HSI as clock source, which has an appreciable tolerance on its frequency.

 

Read this: How to optimize STM32 MCUs internal RC oscillator accuracy

View solution in original post

3 REPLIES 3
waclawek.jan
Super User

HSI as a primary clock source is not precise, see HSI parameters in datasheet.

JW

RobK1
Associate III

You are using the HSI as clock source, which has an appreciable tolerance on its frequency.

 

Read this: How to optimize STM32 MCUs internal RC oscillator accuracy

You beat me to it Jan :grinning_face: