cancel
Showing results for 
Search instead for 
Did you mean: 

Count External Clock pulses

ram_jan
Visitor

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

}

}

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

5 REPLIES 5
mƎALLEm
ST Employee

Hello @ram_jan and welcome to the ST community,

Please review how to post a question in this community in this link, especially how to share a code. I've edited your post.

Thank you for your understanding.

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.

Hi mƎALLEm,

Thank you editing my post, Sorry I was not knowing before.

When can I get the response the for the query I mentioned above ?

Thanks and Regards,

ram jan

 

Hi Team,

How can I utilize minimum of 6 channels of two timer pins to convert external pulses ? Can you give any solution on this ?

Thanks and Regards,

TDK
Guru

An external clock rate of 100 kHz is no problem. The max rate is going to be about a quarter of the timer frequency, in the MHz region.

 

Let the timer free-run and take the difference in the counts between successful calls to calculate the speed.

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

You can use a circular DMA transfer to capture the IC value and use the DMA's NDTR register to see how many transfers have taken place. This is somewhat more complicated to set up than using only one channel per timer as the external clock.

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