In this tutorial, we’ll learn how to measure frequency by using microcontrollers. Unfortunately, microcontrollers are not equipped with frequency-measurement functions. So, for this project to be a success, we’ll need to interface multiple microcontroller peripherals before we can sufficiently measure the frequency of an external signal.
Two of the most common methods for measuring the frequency of an external frequency using microcontrollers are:
1. Reciprocal method. The simplest way to carry out this method is to detect the two rising edges of a frequency wave. At each rising edge, you’ll need to take a timestamp to figure out the exact time period in between (1 / time period is the frequency). However, this is not an easy task for a do-it-yourself project.
2. External signal counter. This method involves running a counter on the external frequency signal to detect the number of positive edges within a fixed amount of time. The frequency can be obtained by dividing the number of counts by the time.
However, if we increase the time (say, to 10 seconds), then the frequency = n. of counts / 10.
Using the STM32 microcontroller
For this project, we’ll use the STM32F0 discovery board, which is 3.3 volts tolerant. This means any external signal that’s greater than 3.3v must be decreased to match the microcontroller. Some of the STM32 input/output pins are up to 5 volts tolerant.
It’s possible to use them but, ideally, the signal should be the same tolerance level as the operating microcontroller.
Circuit diagram
You’ll require an external frequency generator for this project. We’ll debug the code using Keil’s µVision IDE and view the real-time results on the IDE simulator.
We’ll use STM32 TIMER2 to record the number of counts (by using the counter) from the internal clock between the two rising edges of the external signal. The is similar to the external signal counter method with a slight alteration. Instead of counting the external signal, we’ll use the internal clock, counting between the two rising edges of the external signal.
The code
First, you’ll need to configure the input pin and connect it to the timer. Below is the configuration function.
The STM32 libraries are in the form of structures. We’ll import two structures for this configuration (GPIO and NVIC non-volatile vector interrupt). The TIMER2 clock is enabled first. TIMER2 is connected to the Advanced peripheral bus1.
Next, the GPIO port-A clock must be enabled and the GPIO structure parameters should be defined. The GPIO-A1 is then selected, with the speed set to 50MHz. The pin is pulled up and the last pin mode should be set to the alternate function (AF). AF means the pin is neither input nor output — its function alternates depending on if assigned to the DMA, TIMER, RCC, or CCP, and so on.
The pin AF can be found in the microcontroller’s datasheet. For this project, we’re using GPIOA pin#1 in conjunction with the TIMER2. The statement, GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2), assigns GPIOA pin#1 AF (which is assigned to the TIMER2). Lastly, the TIMER2 global interrupt is activated with the highest priority.
The TIMER configuration is somewhat complex. First, the TIMER channel is assigned to the TIMER structure. The polarity is set to the rising edge. Essentially, the TIMER is made sensitive to the rising edge. The ICSelection’s TIM Input 1, 2, 3, or 4 are selected and connected to the IC1, IC2, IC3, or IC4, respectively.
The prescaler is set to TIM_ICPSC_DIV1, which is capture performed each time the edge is detected on the capture input. The above diagram should help with the TIMER configurations — follow the TIMx_CH2 path. The TIMER2 CC2 (capture compare 2) module will be activated with the TIMER itself.
The “while (1)” loop above takes control of the system and performs nothing.
Where’s the frequency calculation?
The frequency is calculated in the interrupt handler routine because we defined the interrupt for the TIMER.
The duty cycle is given by:
We’ll use the same formula for our routine. The TIMER2 counter will count the number of steps during an active period and store it in the CCP1 register. The number of counts between the two rising edges is stored in the CCP2, which is equivalent to the period of the signal.
The above calculation is carried out in the TIMER2 interrupt handler.
First, we must clear the interrupt bit in the IRQ. Then, using the Tim_GetCapture2(Tim2) function, it’s possible to read the period of the signal from the CCP2 register. But check to ensure the period is valid. If so, extract the count in active time by reading the CCP1 register.
After you’ve calculated the number of counts in a period, it’s easy to figure out the frequency. Simply divide the TIMER clock with the number of counts in a period. In our case, the IC2Value is variable.
Here’s a basic graphical explanation…
Note the output of the variables (the duty cycle and frequency) in the emulator of Keil’s ARM IDE. The frequency calculation depends on several variables, so it’s ideal to check the values in an emulator first and refine the system if necessary.
There are certain limitations where it’s not possible to measure a frequency that’s greater than the clock frequency of the timer/counter. Using a prescaler and postscaler somewhat widens this measurement range.
Where to buy the parts?
You may also like:
Filed Under: Microcontroller Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.