Technical Forum

STEP 7 / STEP 7 Lite

Random function in STEP 7

Thread Starter: ILYAZ   Started: 6/23/2008 7:49 AM   Replies: 7

« Previous thread Next thread »
Page 1 of 1 (8 items)
  6/23/2008, 7:49 AM
Joined 11/13/2006
Last visit: 4/17/2014
Posts: 27
Rating:
Rated: Very Good [2 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hello!

Can any one tell me how to use a random funtion in STEP 7.Actually in my application i have to turn on 06 valves randomely.Is there any way to generated any random number from 0--->6.Please help me in this regard.
waiting for your positive responses.

ILYAZ
Top
  6/23/2008, 11:44 AM
Joined 11/2/2006
Last visit: 6/23/2008
Posts: 3
Rating:
Rated: Excellent [5 out of 5 / rated 1 time(s)]. (1) 
Rated: Excellent [5 out of 5 / rated 1 time(s)]. (1) 

Hello ILYAZ.
This ist an example in SCL for Randomgenerator.
You CALL this Function example in the OB35.

FUNCTION FC1:VOID

TITLE = 'Zufallszahl zwischen 0 und HiLimit ermitteln'

NAME    : RANDOM
(*---------------------Eingangsvariablen/INPUT---------------------------*)
VAR_INPUT
    HiLimit :   INT ; // Obergrenze des Wertebereichs der Zufallszahl
END_VAR
(*---------------------Ausgangsvariablen/OUTPUT---------------------------*)
VAR_OUTPUT
    Random  :   INT ; // erzeugte Zufallszahl
END_VAR
(*---------------------Lokale Variablen/local Tags----------------------------*)
VAR_TEMP
    TimeTck :   TIME; // aktuelle Systemzeit
END_VAR
(*---------------------End Of Deklaration--------------------------*)
BEGIN
// Systemzeit lesen /read systemtime
TimeTck := TIME_TCK(); // TIME
// Zufallszahl ermitteln /calculate random number
Random := ABS(DINT_TO_INT(TIME_TO_DINT(TimeTck))) MOD HiLimit ;
END_FUNCTION

kind regards TIMBER

Top
Thank you for this post from:
  6/23/2008, 12:46 PM
Joined 11/13/2006
Last visit: 4/17/2014
Posts: 27
Rating:
Rated: Very Good [2 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
gringringrin

thanks a lot Timber!!!.It really worked.
best wishes

Regards,

ILYAZ
Top
  7/3/2008, 8:59 AM
Joined 3/1/2007
Last visit: 10/18/2009
Posts: 16
Rating:
Rated: Above avarage [3 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hi all,
This problem deals with what I need exactly right now. I need to turn some random values like reading some values from the transmitters. I will use this for the simulation in WinCC. But I need some codes for STL.
Edit: I can call SFC64 for TIME_TCK and load it to a merker but I couldnt find the following number converting steps. TimetoDINT?



=== Edited by trigger @ 7/3/2008 9:18 AM [GMT ] ===


Top
  7/3/2008, 9:25 AM
Joined 3/1/2007
Last visit: 10/18/2009
Posts: 16
Rating:
Rated: Above avarage [3 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
I found out the following note in Simatic Help:
Note:A TIME data type is stored as a signed integer number of milliseconds in 2's complement format.
So I think I dont need to convert TIME to DINT since it is alread signed INT? I will try the other steps and write down the result here later.
Top
  7/3/2008, 9:39 AM
Joined 3/1/2007
Last visit: 10/18/2009
Posts: 16
Rating:
Rated: Above avarage [3 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Hi again,
I used the following code in OB1 and that worked. The main principle here is the MOD part. I didnt think it before. I can use it in any place and in any combination to generate the numbers I want.
Thank you very much for the code...

      CALL  "TIME_TCK"     //SFC64
       RET_VAL:=MD200
      L     MD   200
      ABS  
      T     MD   200
      A     "pulse_1"    //generate number every 1 sec (CPU clock memory merker is M99 for me)
      JCN   END
      L     MD   200
      L     5    //High limit
      MOD  
      T     MD   204     //random number here
END:  NOP   0


Top
  7/8/2008, 10:54 AM
Joined 3/1/2007
Last visit: 10/18/2009
Posts: 16
Rating:
Rated: Above avarage [3 out of 5 / rated 1 time(s)]. (1) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hi all,
You can also use WinCC tag simulator (from tools). It is very useful.You can generate random numbers easily by defining low and high limits. What you have to do is just to link the tag generated in WinCC to tag simulator.
Top
  6/29/2010, 12:56 PM
Joined 6/29/2010
Last visit: 1/7/2014
Posts: 12
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
There is another example with less system CPU load:
STL example:

Network1:
          A        FirstRun     //any bit in bit memory or data block
          JC      nIni            //skip initialisation, if allready performed

          CALL  "TIME_TCK"     // CALL   SFC64
            RET_VAL:=
Backup
Network2:
nIni:   L         Backup      //Word
         L         111
         *I
         L         123
         +I
         L         11111
         MOD
         T         RETURN
         T         Backup

Total - 11 instructions, to execute, with no CALL's, except initialisation moment.
I used http://en.wikipedia.org/wiki/Multiply-with-carry
Top
Thank you for this post from:
Page 1 of 1 (8 items)
Actions