Teste semf
Kontakt

FrequencyIn

General

The FrequencyIn class has as its main purpose the measurement of period and frequency of an input signal. For this purpose it has two methods periodInMS() and frequency(). It is dependent on a user-definable (or semf-provided) hardware interface class InputCapture, which provides the connection to the hardware input of the microcontroller to be monitored.

The 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 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 be easily configured either by e.g. stm-cube or by calling the hardware functions for configuring 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 InputCapture class and redefine some of its functions, which will be used in 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 an stm32f4 microcontroller.

Initialization

The first step for setting up the FrequencyIn object is to initialize a 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 creating an InputCapture object referencing the timer handle and the preferred timer channel.This InputCapture object is then passed to the FrequencyIn Object (by registerInzerface()). 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 "HardwareAbstraction/Stm32/stm32f4inputcapture.h"  
#include "Components/Input/frequencyin.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;
// Registering the interface class to FrequencyIn object.
freqIn.registerInterface(inputcapture);
// This value must be the same as timer input frequency, e.g. 1MHz.
freqIn.setTicksPerSecond(1000000);

Usage

freqIn.start();

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

zurück zur Dokumentation