cancel
Showing results for 
Search instead for 
Did you mean: 

Timer input capture mode

FIann.2
Associate II

hi, i am working with an example from the book Mastering STM32 written by Carmine Noviello.

The example is the 6th of chapter11, it is about Input capture mode.

I have a nucleo board STM32F401RE and i am building examples with STM32CUBEIDE tool.

I have configured two timers:

i use TIM1 in DMA circular mode to blink the LED PA5, which is high every 50khz.

i want detect this frequency by connecting the led to channel 1 of TIM3 in input capture mode.

TIM3 clock is connected to LSE through the MCO pin, which is running at 32768 hz.

I configured TIM3 in DMA circular mode so that when the LED is high, the content of CCR is trasferred to a 16bit Captures array of two elements(Captures[2]).

The DMA transfer is of halfword size since the CCR1 is a 16 bit register.

The global variable captureDone is set to 1 by the HAL_TIM_IC_CaptureCallback() callback function, which is invoked at the end of the capture process.

When this happens the frequency of the sample signal is computed using the following equation:

if (captureDone != 0) {

          if (captures[1] >= captures[0]) {

             diffCapture = (captures[1] - captures[0]);

          }else{

             diffCapture = (htim3.Instance->ARR - captures[0]) + captures[1];

          }

          frequency = LSE_VALUE/ (htim3.Instance->PSC + 1);

          frequency = (float) frequency / diffCapture;

          sprintf(msg, "Input frequency: %.3f\r\n", frequency);

          HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY);

          while (1);

        }

The problem is that is executed the ELSE statement even if from the debugging i read that captures[1] has a value greater than captures[0].

Can you help with this exercise please?

1 ACCEPTED SOLUTION

Accepted Solutions

> blink the LED PA5, which is high every 50khz.

> TIM3 clock is connected to LSE through the MCO pin, which is running at 32768 hz.

Are you trying to measure a 50kHz signal using a timer running at 32kHz?

> I configured TIM3 in DMA circular mode

You want to capture two values, so don't set DMA to circular mode, but to non-circular.

Otherwise, unless you stop the measured pulses source (maybe in this particular case you can do that using the respective DBGMCU freeze register bit for TIM1), DMA keeps running and keeps overwriting the captures[] array over and over.

JW

JW

View solution in original post

2 REPLIES 2

> blink the LED PA5, which is high every 50khz.

> TIM3 clock is connected to LSE through the MCO pin, which is running at 32768 hz.

Are you trying to measure a 50kHz signal using a timer running at 32kHz?

> I configured TIM3 in DMA circular mode

You want to capture two values, so don't set DMA to circular mode, but to non-circular.

Otherwise, unless you stop the measured pulses source (maybe in this particular case you can do that using the respective DBGMCU freeze register bit for TIM1), DMA keeps running and keeps overwriting the captures[] array over and over.

JW

JW

>Are you trying to measure a 50kHz signal using a timer running at 32kHz?

yes...then i changed to internal clock for input capture timer, which runs at 84Mz

>You want to capture two values, so don't set DMA to circular mode, but to non-circular

i have tried then in non circular mode as you suggested

>result: input capture is working, i can red the 50khz frequency of led.

Thank you much for help