cancel
Showing results for 
Search instead for 
Did you mean: 

Count External Clock pulses

ram_jan
Associate II

Hi Team,

Currently I am working with STM32U575ZiTx SOC and I am using Tim3 for counting external clock pulses,

Can you guide me with the code please.

Below is my requirement : 

1. What is the maximum frequency of the incoming signal? (To ensure the timer can handle it without missing counts)
Answer : The frequency is measuring the spin rate of a physical system; a wind-turbine RPM or something similar.
I think we can very safely say the maximum edge rate will be less than 100 kHz (heh) and probably closer to a max of 3600.

2. Should the rolling counter value be transmitted as-is, or should it be reset after each transmission?
Answer : We confirmed with the end customer that the counter will roll over and is expected to reset on reboot.

 

Code Reference : 

 
static void MX_TIM3_Init(void) { /* USER CODE BEGIN TIM3_Init 0 */ /* USER CODE END TIM3_Init 0 */ TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_IC_InitTypeDef sConfigIC = {0}; /* USER CODE BEGIN TIM3_Init 1 */ /* USER CODE END TIM3_Init 1 */ htim3.Instance = TIM3; htim3.Init.Prescaler = 0; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 4294967295; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim3) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2; sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED; sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1; sClockSourceConfig.ClockFilter = 0; if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } if (HAL_TIM_IC_Init(&htim3) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM3_Init 2 */ /* USER CODE END TIM3_Init 2 */ } void Timer_Handling_Thread(void *argument) { char iridium_buffer[64] = {0}; HAL_TIM_Base_Start(&htim3); while(1) { uint32_t count = __HAL_TIM_GET_COUNTER(&htim3); snprintf(iridium_buffer, sizeof(iridium_buffer),"Pulse Count: %lu (dec), 0x%lX (hex)\r\n", count, count); HAL_UART_Transmit(&huart2, (uint8_t *)iridium_buffer, strlen(iridium_buffer), HAL_MAX_DELAY); } }
View more

Can you help me with respect to the configurations of CubeMX ? If possible please provide a suggestions on this post ?

10 REPLIES 10

Timers with IC/DMA as given above, or some EXTI/DMA setup. Not sure offhand if 6 timers have an external clock, don't think so.

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