top of page
Search
Writer's pictureIvaylo Fiziev

Practical use case: Frequency counter

As the name suggests this is a device that measures frequency. It counts the number of pulses in a periodic signal per second. Internally they use a counter that increments on each pulse. When the preset period (1s for example) for the frequency counter elapses, the value of the counter is stored and the counter is reset to zero. The stored value is provided on output so it can be used by some other logic.

The building blocks seem quite familiar, aren't they? A counter, a timer and some logic on top. It is a pretty good candidate for a script:

FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_INPUT
	clk : BOOL;				// clock signal
	pt : TIME := TIME#1s;		// preset time (1 sec)
END_VAR
VAR_OUTPUT
	out : DINT;				// frequency
END_VAR
VAR
	timer : TON_TIME;			// TON timer 
	trigger : R_TRIG;			// positive edge trigger
	pulses : DINT;			// number of pulses detected
END_VAR
BEGIN
#trigger(CLK:=#clk);			// detect positive edge
#timer(IN:=true, PT:=#pt);		// start timer

if #trigger.Q then			// edge detected?
	#pulses := #pulses + 1;	// increment counter
end_if;

if #timer.Q then				// time elapsed?
	#out := #pulses;			// store result
	#pulses := 0;				// reset counter
	#timer(IN:=false);		// reset timer
end_if;
END_FUNCTION_BLOCK

Of course the accuracy of measurement is limited by the sampling rate (LB update rate) and this is the biggest drawback to using this script. With a sampling rate of 100ms you only have ten executions per second and half of them can detect pulses. This means that you can detect frequencies of 5Hz or less... If you use a sampling rate of 10ms then 50Hz is the top.

This probably won't be of any use in a real world scenario.

Still it is good to know that the script is not the limit here. One day we will probably do better.


See you!





25 views0 comments

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
bottom of page