STM32 Frequency Measurement.
In one of my previous articles I talked about the STM32 capture mode and gave an example of how to use it to measure a signal's period. But this method can't ACCURATELY measure the period of a high-frequency signal (it will become clear below which signal counts as high-frequency).
Suppose the frequency of the signal under test is 1MHz and our timer runs at 72MHz. Let's convert the frequency to a period.
1MHz = 1/10^6 = 1 uS
72MHz = 1/72*10^6 = 0,0138 uS
Let's calculate how many ticks the timer makes in 1uS:
1/0.0138 = 72.46 ticks
Rounding to 73, the error is 0.54 ticks, which is less than 1 percent.
Now let's increase the frequency of the signal under test by a factor of 10 and do the same calculations.
10MHz = 1/10^7 = 0.1 uS
72MHz = 1/72*10^6 = 0.0138 uS
Let's calculate how many ticks the timer makes in 1uS:
0.1/0.0138 = 7.246 ticks
Rounding to 8 ticks, the error is now almost 10%, i.e. instead of 10MHz we get 11MHz.
Clearly such accuracy is useless, and the reason is that we were measuring the pulse duration, whereas to get a more accurate result we need to measure the number of pulses per second (i.e. the frequency) and then calculate the period.
The number of pulses per second can be obtained in two ways.
The first way is the brute-force one: configure one of the MCU pins for an external interrupt (the signal under test is fed to it) and start a timer that counts one second. Each time the interrupt fires, increment a variable by one, and after one second we have the frequency of the signal under test.
The second way uses the ETR (external trigger input) pin. Here everything is simpler: you just need to initialize this pin correctly, and it will count the pulses on the selected edge by itself. I'll describe how to configure it below.

First, configure the filter in the TIMx_SMCR register, bit field ETF[3:0] (N is the number of events)

In the same register, set the prescaler value
ETPS[1:0]: External trigger prescaler
Select the trigger edge there as well
ETP: External trigger polarity
And configure the pin in ETR mode
Enable the timer by setting CEN = 1 in the CR1 register.
Since the CNT register is 16-bit, the maximum frequency we can measure is 65536 Hz. I fed 50 kHz from a signal generator and ran the code below on an STM32F103VE chip.
The result was pleasing; you can enlarge the screenshot by clicking on it.
So how do you measure 10 MHz after all?
For that you need to use two timers: one counts the pulses on the ETR pin, and the second counts how many times the first one has overflowed.
Suppose the frequency of the signal under test is 1MHz and our timer runs at 72MHz. Let's convert the frequency to a period.
1MHz = 1/10^6 = 1 uS
72MHz = 1/72*10^6 = 0,0138 uS
Let's calculate how many ticks the timer makes in 1uS:
1/0.0138 = 72.46 ticks
Rounding to 73, the error is 0.54 ticks, which is less than 1 percent.
Now let's increase the frequency of the signal under test by a factor of 10 and do the same calculations.
10MHz = 1/10^7 = 0.1 uS
72MHz = 1/72*10^6 = 0.0138 uS
Let's calculate how many ticks the timer makes in 1uS:
0.1/0.0138 = 7.246 ticks
Rounding to 8 ticks, the error is now almost 10%, i.e. instead of 10MHz we get 11MHz.
Clearly such accuracy is useless, and the reason is that we were measuring the pulse duration, whereas to get a more accurate result we need to measure the number of pulses per second (i.e. the frequency) and then calculate the period.
The number of pulses per second can be obtained in two ways.
The first way is the brute-force one: configure one of the MCU pins for an external interrupt (the signal under test is fed to it) and start a timer that counts one second. Each time the interrupt fires, increment a variable by one, and after one second we have the frequency of the signal under test.
The second way uses the ETR (external trigger input) pin. Here everything is simpler: you just need to initialize this pin correctly, and it will count the pulses on the selected edge by itself. I'll describe how to configure it below.

First, configure the filter in the TIMx_SMCR register, bit field ETF[3:0] (N is the number of events)

In the same register, set the prescaler value
ETPS[1:0]: External trigger prescaler
- 00 — off
- 01 — divide by 2
- 10 — divide by 4
- 11 — divide by 8
Select the trigger edge there as well
ETP: External trigger polarity
- 0 — rising
- 1 — falling
And configure the pin in ETR mode
- ECE = 1
Enable the timer by setting CEN = 1 in the CR1 register.
Since the CNT register is 16-bit, the maximum frequency we can measure is 65536 Hz. I fed 50 kHz from a signal generator and ran the code below on an STM32F103VE chip.
#include "stm32f10x.h"
volatile uint16_t tic = 0;
void TIM6_IRQHandler(void)
{
TIM2->CR1 &= ~TIM_CR1_CEN;//stop counting
tic = TIM2->CNT;
TIM2->CNT = 0;
TIM6->CNT = 0;
TIM6->SR &= ~TIM_SR_UIF; //clear the interrupt flag
TIM2->CR1 |= TIM_CR1_CEN;//start the timer
TIM6->CR1 |= TIM_CR1_CEN;//start the timer
}
void ETR_Input_Init(void)
{
// Clocking for TIM2 and GPIOA, PA0 input-float
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
//the basic timer settings still apply
TIM2->PSC = 1 - 1;
TIM2->CNT = 0;
TIM2->ARR = 60000 - 1;
TIM2->CR1 &= ~TIM_CR1_DIR;//count up
TIM2->SMCR &= ~TIM_SMCR_ETF;//configure the filter
TIM2->SMCR &= ~TIM_SMCR_ETPS;//prescaler 0
TIM2->SMCR &= ~TIM_SMCR_ETP;//rising edge
TIM2->SMCR |= TIM_SMCR_ECE; //external clock
}
void Tim6_Init(void)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
TIM6->PSC = 7200 -1;//new frequency = 10kHz
TIM6->ARR = 10000;//period = 1sec
TIM6->CR1 &= ~TIM_CR1_DIR;//count up
TIM6->CR1 |= TIM_CR1_OPM; //the timer stops after counting to the end
TIM6->DIER |= TIM_DIER_UIE; //enable interrupts
NVIC->ISER[1] = NVIC_ISER_SETENA_22;
}
int main(void)
{
ETR_Input_Init();
Tim6_Init();
TIM2->CR1 |= TIM_CR1_CEN;//start the timer
TIM6->CR1 |= TIM_CR1_CEN;//start the timer
while(1){}
}
The result was pleasing; you can enlarge the screenshot by clicking on it.
So how do you measure 10 MHz after all?
For that you need to use two timers: one counts the pulses on the ETR pin, and the second counts how many times the first one has overflowed.
