Is is possible to measure the frequency of an input analog signal with the input capture mode of Timers in STM32F4 ? If not, then what is the alternative ? Thank you in advance.
1 Answer
Introduction
It seems like you are trying to build a frequency counter. You can achieve such a device with the timer module in a microcontroller. Be aware that it will have some limitations in terms of how fast of a signal it can observe.
Analog Processing
You will need to first condition the input signal. To know what is required, I would first need to know a bit more about your signal, but I will assume you are trying to measure the frequency of something that is basically a sine/square wave perhaps with some DC offset. We need to get from this general analog signal to a digital square wave that represents the same edges. We can do that with the following comparator circuit:
simulate this circuit – Schematic created using CircuitLab
Microcontroller Configuration
Configure your microcontroller to count up until it sees a rising edge on the output of the comparator. Increase the timer frequency as high as possible to achieve the frequency resolution you require. You will also want to fire an interrupt on a capture event so you can do something with the new data.
Algorithm
When the interrupt is fired grab the value in the capture register and compute the frequency of the signal as follows:
$$f_{in} = \frac{f_{timer}}{count}$$
You may then wish to average several of these readings.