Recently I was asked for such an example from customers so I decided to make a post out of it. Interestingly they struggle implementing this simple logic.
As you might imagine I will use our powerful SCL script for this. Once you have the proper tools it gets really easy.
Here is a picture of what we need to achieve:
To implement this behavior I will use two built-in FBs.
A pulse timer (TP_TIME)
A falling edge trigger (F_TRIG)
The provisioned time of the timer should match the pulse width. And since it is a pulse timer its output will stay raised (true) until the time elapses. When the timer output is lowered (false) we can invert the value of the output signal and reset the timer. To detect this event I will rely on the falling edge trigger.
Here is how the code looks:
FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_OUTPUT
out : BOOL; // output
END_VAR
VAR
timer : TP_TIME; // pulse timer
trigger : F_TRIG; // FE trigger
END_VAR
BEGIN
#timer(IN:=true, PT:=T#500ms); // start the timer
#trigger(CLK:=#timer.Q); // detect FE on timer output
if (#trigger.Q) then // FE detected ?
#timer(IN:=false); // reset timer
end_if;
END_FUNCTION_BLOCK
Really simple, isn't it?
To test the code just paste it in the SCL Editor, connect a signal to the output and save the changes.
I hope this will serve as an example to everyone who needs similar logic implemented in Process Simulate.
Enjoy!
Comments