Siemens S7 Flip-Flop written in SCL. Possible?

MrTysk

Member
Join Date
Sep 2007
Location
Lessebo
Posts
9
Hello.
I was looking a bít at the other threads about flip-flops. Thing is I want to make my own FC working as a flip-flop so I don't have to write the same code everytime I need to use one in the program. 1 push on 1 push off.
I was trying to write a positive edge detection in scl with help from the other threads but I can't get the edge detection to work.

All help how to set up the code in this block would be appreciated.
 
Something along the lines of

On a positive edge
If FlipFlop = 0 then FlipFlop = 1
Else FlipFlop = 0
Endif

This is a basic idea I don't have SCL but know basic idea.

Another place to look would be www.oscat.de open source programming concepts. They have a siemens file. I downloaded it before but it is all SCL so I can't view it. May be something helpfull in there.
 
Post your SCL code. As there is no memory storage in a FC, you will have to pass the storage bit for your edge detection as an in/out variable.
 
Here's an example FC

Code:
FUNCTION FC99: void
VAR_INPUT
        bRisingEdgeTrigger:BOOL;
end_var
VAR_in_out
    bToggle:BOOL;
    bEdgeStore:BOOL;    
end_var
;
IF bRisingEdgeTrigger AND NOT bEdgestore THEN bToggle:=NOT bToggle; END_IF;
bEdgeStore:=bRisingEdgeTrigger;
    
END_FUNCTION
 
Code:
Toggle:=Input AND NOT LastInput;
LastInput:=Input;
Run := Run XOR Toggle;
If you have R_Trig then
Code:
VAR
    R_TRIG0  :  R_TRIG;  (* Declare an R_TRIG instance *)
END_VAR

Run := Run XOR R_TRIG0(CLK:=Input); (* Flip the state *)
OT but related. I would prefer to have this:

Code:
Run := Run XOR R_TRIG(Input);
No extra declaration required. I hate having to assign parameters too like the IEC makes you do. The compiler should be smart enough to allocate a memory state for each instance of R_TRIG instead of having to declare it.

The general case should have been

Code:
R_TRIG(boolean expression)
where the boolean expression could be of any complexity.
 
Last edited:
Well I did it like this;

FUNCTION FC521 : VOID

VAR_INPUT
InP : BOOL;
END_VAR

VAR_OUTPUT
Out : BOOL;
END_VAR

BEGIN
IF InP THEN
IF Out THEN
Out := FALSE;
ELSE
OUT := TRUE;
END_IF;
END_IF;

END_FUNCTION

And then I put a -(P)- at InP in the whenenver I use the fc in my program. Works like a charm. Can't understand why I didn't think of that before.
Im pretty new on scl so there's much to learn. And thanks again for the replies. I learned some new things from that too.=)
 
I use a counter to do the same thing. Set the counter to reset when accum=2. Each time the button is pressed, the counter will alternate between 0 and 1. Then use 'equal' compare instructions to test whether it is flipped (=1) or flopped (=0). This is ladder logic but it can be done in SCL too.
 
Well I did it like this..... Works like a charm.

.... for now !

Your function contains two fundamental coding errors.

1. You are testing the state of an FC output before it has been conditioned.

2. The FC output is not being written all the time.
 
For logic code in SCL, avoid IF THEN ELSE if possible. Write logic as you would in LAD or FBD:
What I mean is, if you write logic like this:
var1 := var2 AND var3 AND NOT (var4 OR var5) ; etc...
then you dont have to worry about initialising variables. And as a bonus, the code is tighter and more readable (to my opinion).

Code:
FUNCTION FC_flip_flop : VOID
VAR_INPUT
    bi_input : BOOL; 
END_VAR
 
VAR_IN_OUT
    bi_oneshot_mem : BOOL ;
END_VAR
 
VAR_OUTPUT
    bi_toggle : BOOL; 
END_VAR
 
bi_toggle := bi_toggle XOR (bi_input AND NOT bi_oneshot_mem) ;
bi_oneshot_mem := bi_input ;
 
END_FUNCTION
 
Last edited:
Ouch. You are right LD.

FUNCTION FC_flip_flop : VOID
VAR_INPUT
bi_input : BOOL;
END_VAR
VAR_IN_OUT
bi_oneshot_mem : BOOL ;
bi_toggle : BOOL;
END_VAR
bi_toggle := bi_toggle XOR (bi_input AND NOT bi_oneshot_mem) ;
bi_oneshot_mem := bi_input ;
END_FUNCTION
 

Similar Topics

This is what I got from ABB Control Builder Help file about RS flip flop: How to Use the Function Block The RS bistable flip-flop, is a latch...
Replies
6
Views
6,665
I know this has been answered before and I have searched the archives but can't find it. What is the best (easiest) way to implement a simple...
Replies
2
Views
5,096
I was wondering if siemens has a Flip/Flop function. Basically I would like to turn on/off an output with the same input. For example P.B.1 is...
Replies
5
Views
2,734
Hi, I'm setting up a modbus master on an S7-300. It seems to work in OB1 but not when I use it in OB35. Does anyone have any ideas why? Could...
Replies
10
Views
91
The past week we received a new piece of equipment from Germany which utilizes siemens controls. Typically in our company we use A.B. controls for...
Replies
12
Views
322
Back
Top Bottom