semf
FrequencyIn

General

The semf::FrequencyIn class is used for the measurement of period and frequency of an input signal. For this purpose it has two methods periodInMs() and frequency(). It depends on a user-definable (or semf-provided) hardware interface class semf::InputCapture which provides the connection to the hardware input of the microcontroller to be monitored.

The semf::InputCapture class captures input signals and records the corresponding timestamps in memory. When a rising (or falling or both, depending on configuration) edge occurs, the value of the internal timestamp is captured and stored. These timestamps are used by semf::FrequencyIn for determining the period or the frequency of the input signal. The elapsed time between two successive edge events represents an entire period. The hardware timers can easily be configured either by e.g. stm-cube or by calling the hardware functions to configure the input and can for example be set to register a rising edge, falling edge, or both.

Definition

Declare and define a class that inherits from semf::InputCapture class and redefine some of its functions which will be used in semf::FrequencyIn. The main goal of this class is to connect the interface of the semf library with the hardware abstraction layer of the microcontroller. In the following example we consider a Stm32F4 microcontroller.

Initialization

The first step for setting up the semf::FrequencyIn object is to initialize a semf::app::Timer hardware module and configure one of its channels to an input capture mode (usually done in HW configuration, for example cubeMX). The second step is to create an semf::InputCapture object referencing the timer handle and the preferred timer channel. This semf::InputCapture object is then passed to the semf::FrequencyIn object. The last parameter to initialize is the timer resolution via setTicksPerSecond(). This value must correspond to the hardware configuration.

In this example we consider the hardware timer module TIM2 and its first channel TIM_CHANNEL_1, which has been set to input capture mode.

#include "semf/hardwareabstraction/stm32/stm32f4inputcapture.h"
#include "main.h"
extern TIM_HandleTypeDef htim2; // Hardware handler which is predefined in 'main.c' file.
// Interface class for the timer.
semf::Stm32F4InputCapture inputcapture(htim2, TIM_CHANNEL_1);
// Objectsemf class that handles the frequency calculations.
semf::FrequencyIn freqIn(inputcapture);
// This value must be the same as timer input frequency, e.g. 1MHz.
freqIn.setTicksPerSecond(1000000);
Class for measuring the frequency on a capture input.
Definition: frequencyin.h:22

Usage

freqIn.start();
while (1)
{
if (freqIn.frequency() > 10000) // 10kHz
{
// do something ...
}
else
{
// do something else ...
}
}
freqIn.stop();