cancel
Showing results for 
Search instead for 
Did you mean: 

I am having trouble getting the timer working. The count is always returning 0

CMysz.1
Associate

Here is my system clock config, my Timer 2 initialization, and the read function.

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

 __PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

 RCC_OscInitStruct.PLL.PLLM = 4;

 RCC_OscInitStruct.PLL.PLLN = 216;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ = 9;

 HAL_RCC_OscConfig(&RCC_OscInitStruct);

 HAL_PWREx_ActivateOverDrive();

 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_DIV8;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);

 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_CLK48;

 PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;

 PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;

 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_TIM;

 PeriphClkInitStruct.TIMPresSelection = RCC_PERIPHCLK_TIM;

// PeriphClkInit.TIMPresSelection = RCC_USART2CLKSOURCE_PCLK1;

 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 /* SysTick_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);

}

static void MX_TIM2_Init(void)

{

 /* USER CODE BEGIN TIM2_Init 0 */

 /* USER CODE END TIM2_Init 0 */

 uint8_t error_count;

 TIM_ClockConfigTypeDef sClockSourceConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 /* USER CODE BEGIN TIM2_Init 1 */

 /* USER CODE END TIM2_Init 1 */

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 0;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.RepetitionCounter = 20;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

 {

error_count++;

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)

 {

error_count++;

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

 {

error_count++;

 }

 /* USER CODE BEGIN TIM2_Init 2 */

 /* USER CODE END TIM2_Init 2 */

}

uint16_t read_TIM2() {

 return TIM2->CNT;

}

5 REPLIES 5

>>htim2.Init.Period = 0;

Surely this is going to be constraining?

Not sure what part you're using, but lets guess this is a 32-bit timer

htim2.Init.Period = 0xFFFFFFFF;

If all the TIM2 registers are zero, check you've enabled the APB clock for TIM2 properly. Review peripheral register in a debugger.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
CMysz.1
Associate

Its a 16 bit timer right now.

I tried

htim2.Init.Period = 0xFFFF;

I also used the

 __HAL_RCC_TIM2_CLK_ENABLE();

I verified that the APB1ENR bit 0 is set to 1

Unfortunately the timer is not running.

TMR2->CNT = 0

Dominik Lorych
Associate III

I think you are missing the HAL_TIM_Base_Start(&htim2) call. You just initialized the counter, but did not start it.

But I have the same problem even with the timer being started. Did you get your timer to work? If yes i would like to know what you changed :)

Read out and check/post the TIM registers' content.

If they are all 0, check in RCC if its clock has been enabled (again, by reading out and checking the respective RCC_APBxENR).

JW

Dominik Lorych
Associate III

I found the mistake, i had configured the timer wrong (SlaveMode). (But the mistake is not existent in the code of CMysz.1, i guess he just forgot the HAL_TIM_Base_Start() call).

Thanks