cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO- STM32F446RE : Counter example which works on step pulse from external source/pin

TH.16.336
Associate III

Hello All,

I am looking for an example to understand timer module which will be used as COUNTER which actually drives from external step pulse.

I am using CubeMX but getting confused with the different modes or configurations available in CubeMX.

Can anyone share any good material to understand counter or share any example to understand the same.

6 REPLIES 6

Read the timer chapter (TIM2-TIM5) in Reference Manual.

It's confusing and not well written, I know; but read it through several times, including the registers' description; do some simple experiments (no need to write code, just play with registers in the debugger), and eventually it will start to make sense.

You may want to read also the related appnotes - there's a timer cookbook and also a general overview of timers across STM32 families - you should find links to them in the chip's (not the Nucleo's) web folder.

JW

PS. What you're looking for specifically is called there "external clock mode".

RomainR.
ST Employee

Hello TH.16.336

You can easly count external pulse by using any GPTimer in Slave Mode clock by using ETR input.

Here an example of CubeMX setup with NucleoF446RE and TIM3.

After generating the code, start the TIM3 with

HAL_TIM_Base_Start(&htim3);

At the end of MX_TIM3_Init() function.

0690X00000BwxphQAB.png

Declare a global volatile variable to read the pulse number (which is the TIM3_CNT value)

/* USER CODE BEGIN PV */
static __IO uint16_t pulseCount = 0;
 
/* USER CODE END PV */

And read the counter anywhere in your main loop.

 /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	pulseCount = __HAL_TIM_GET_COUNTER(&htim3);
  }
  /* USER CODE END 3 */

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.

Hello rrom,

Thanks for the reply with all the information.

Actually I want to count the step pulse from external source where I should increment the count every time I get the pulse interrupt. But the interrupt should be generated for very 10th pulse.

Timer should be Down counter and should generate an interrupt when it reacahes 0.

I have generated code using CubeMX and enabled global interrupt and I have called the below function to enable Timer and interrupt.

HAL_TIM_Base_Start_IT(&htim3);

Can you help me how to test this code using signal generator? or any means of source?

RomainR.
ST Employee

In other words you want to count the pulses and total only all the 10th pulses.

But you don't need to use the interrupts for this since you are using the configuration I gave you. In this case the TIM3 works alone without the help of the CPU.

Regarding help to validate your solution using a signal generator, here are my tips:

- Connect your generator to PD2 with the desired frequency and amplitude (be careful and check the characteristic of PD2 at maximum voltage in datasheet 3.3 or 5V tolerant)

- From the example I gave you, you should test the pulseCount variable in main loop, each time it has been incremented by 10 pulses. And you increment another variable or else you execute the code you want.

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.

Homework?

JW

Bob S
Principal

Search the ST site for AN4776 (General-purpose timer cookbook) and the newer AN4013 (STM32 cross-series timer overview).