Today I have finally decided to write on this topic. Timers are and always have been one of the most challenging topic in any microcontroller. So today We are going to use one of the functionality of STM32 Timers i.e Input Capture.
As the name implies, Input capture is used to capture the input signal given to the microcontroller and measures its frequency and pulse width. In this tutorial, I will only cover the frequency part and the pulse width will be covered in next one where we will use PWM Input to do that.
Input capture works in the following way:-
(1) Whenever a rising edge is detected, a callback function is called.
(2) Here we are going to capture the TIMESTAMP and store it in some variable
(3) On detecting the second rising edge, another TIMESTAMP will be captured and stored
(4) Now the frequency can be calculated by first finding the difference between these 2 captured values and than dividing the timer clock by this difference.
Yeah I know it’s not that hard to understand it. But while implementing, we need to take care of few things and we will see them along this explanation. So let me start with the CubeMx first
As you can see in the clock configuration that all Timer clocks are at 45 MHz. Anyway I am going to select TIM3 which is connected to the APB1 Clock in F446RE and which is running at 45MHz.
This part is very important. The minimum frequency, which the device can read will depend on it. Below is the configuration for the TIM3
You can see above that the only thing I changed from default setup is that I enabled the Channel 1 Input Capture mode and the ARR is set to 0xffff. This is the max value to which the time is going to count.
The minimum frequency that the Timer can read is equal to (TIMx CLOCK/ARR). In our case it will be (45MHz/65536) = 687 Hz. You can check this in the video below that if the frequency input is less than this, the controller will just show some garbage value.
We can decrease the TIMx clock to reduce the minimum frequency but this will also reduce the maximum frequency that can be measured. So you have to be very reasonable about the range here.
Some Insight into the CODE
The above callback function is called whenever the rising edge is detected. When called first time, Is_First_Captured was 0 so the hence the IC_Val1 will be recorded. When called in the second rising edge, the Is_First_Captured is 1 now so IC_Val2 will be recorded and you can see the rest.
Frequency is equal to the (TIMx clock / Difference). In my case TIM3 CLOCK is same as PCLK1 but it might not be same in your case so make sure you do the correction here.
In the main function, we have to start the TIMER in the Input capture interrupt mode by using
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020
- How to use Binary Semaphore in STM32 January 11, 2020
- Managing Multiple UARTs in STM32 December 18, 2019
- Multiplexer 74HC4051 and STM32 December 6, 2019
- Free RTOS Tutorial 2.0 with STM32 November 27, 2019