S7 BitCast function

RMA

Member
Join Date
Sep 2004
Location
North of Hamburg, Germany
Posts
2,052
Does anybody know if there is a BitCast function available for S7 (Take an integer value from 0 - 7 and set the appropriate Bit in an Output Byte). I've dug through all the libraries, but so far haven't been able to find anything. The daft thing is, my FM352-5 has this function, but I also need it in the main program.

Alternatively, has anybody programmed this one for themselves and could give me a copy. It's going to be called quite a few times, so I'm not convinced my programming skills are good enuogh to make an efficient job of it.
 
Is this what you want?

L #Integer
LAR1
A M 0.1
= Q [AR1,P#0.0]

Just make the first '0' in the 4th line the Output byte you need, or

L #OutputByteAddress
SLD 3
L#Integer
OD
LAR1
A M0.1
= Q [AR1,P#0.0]

If you want to change the byte dynamically.

It's worth doing a limits test on both variables.
 
It's easy when you know how, isn't it!

I'm not sure why, but I'm still not very comfortable with indirect addressing on the S7, so I don't automatically think of it. I hope nothing goes wrong with the places where I have used it up to now, because I probably won't understand my own comments when I come back to it! :oops:

Thanks a lot for the help.

jvdcande,

I looked there because that was where I expected to find it and didn't recognise it from the name DECO - why can't Siemens use the same name for a function consistently!
 
Last edited:
RMA,

worst thing is: they even keep on changing the user interface with almost every new version of STEP7. This means I have to change my courseware too. It took me about one year to write the course and I have to work it over every nine months or so :rolleyes: .

Oh, what the heck. This way I'm sure to be employed for ever :p .

Kind regards,
 
Those Siemens functions can be pretty inefficient when it comes to scantime, and you are usually better off writing your own functions (the Siemens function uses a loop). I like Parrafin’s solution, but if you ever had to do the same thing with more than a byte (up to 256 bits), I would probably use a jump list.

Here is some code that should work for up to twelve bits. It looks complicated at first, but when it runs, only a few of the instructions are actually executed:


Code:
     L     P#10.0                      //We'll assume QB10, but you can determine it dynamically.
      LAR1  

      L     0                           //If you only want one bit set at a time, then clear the output byte on each scan here.
      T     QB [AR1,P#0.0]              //Remove this code if you will be resetting the outputs elsewhere.


      L     MW  1200                    //MW1200 contains the bit integer value.
      JL    Ovr
      JU    BT00
      JU    BT01
      JU    BT02
      JU    BT03
      JU    BT04
      JU    BT05
      JU    BT06
      JU    BT07
      JU    BT08
      JU    BT09
      JU    BT10
      JU    BT11
Ovr:  JU    End
BT00: A     M      2.0                  //Add your loggic here to set the bit status
      =     Q [AR1,P#0.0]
      JU    End
BT01: A     M      2.0
      =     Q [AR1,P#0.1]
      JU    End
BT02: A     M      2.0
      =     Q [AR1,P#0.2]
      JU    End
BT03: A     M      2.0
      =     Q [AR1,P#0.3]
      JU    End
BT04: A     M      2.0
      =     Q [AR1,P#0.4]
      JU    End
BT05: A     M      2.0
      =     Q [AR1,P#0.5]
      JU    End
BT06: A     M      2.0
      =     Q [AR1,P#0.6]
      JU    End
BT07: A     M      2.0
      =     Q [AR1,P#0.7]
      JU    End
BT08: A     M      2.0
      =     Q [AR1,P#1.0]
      JU    End
BT09: A     M      2.0
      =     Q [AR1,P#1.1]
      JU    End
BT10: A     M      2.0
      =     Q [AR1,P#1.2]
      JU    End
BT11: A     M      2.0
      =     Q [AR1,P#1.3]
      JU    End

End:  NOP   0
 
Thanks S7Guy, I decided to wait until this morning to read the post again, rather than answering when I read it at last night at 11 PM after a couple of glasses of a very nice Cabernet Sauvignon!

For this application, I only need the one byte to pass on to the CPU_Out Buffer DB which transfers the data to the FM352-5 to specify which of its 8 outputs I want to use. This is part of my ProTool test and commissioning picture which accesses the modules via multiplex addressing for the normal I/O.

I'm actually using a very similar structure to what you suggest in other parts of the program where I'm only having to access one of the 21 modules, for example in the fault handling routines and one major reason was precisely because of the fact that although it's a big lump of code, very little of it actually gets used on any given run through.

It has a second very important advantage in my case, as a sub-contractor, in that when I'm long gone it'll be much easier for somebody else to see what's happening, which wouldn't be case if I'd tried to be too "clever" or overly "neat".
 

Similar Topics

Hi, I have attached herewith one image which our programmer has been used in S7 1500 PLC. Now we need to use the same instructions in S7 1200 PLC...
Replies
4
Views
118
Im trying to create a level indicator for water Tank i have used the ADD function while the pump is on and level increasing everything works...
Replies
33
Views
1,020
Good morning crew! Ok my logic works but I am missing something. When the start button is pushed it should like the red light for 4sec then shut...
Replies
13
Views
419
Please see attached file. I need this program in Function Block form but I am totally lost on this. Any help would be appreciated. Thanks!
Replies
8
Views
294
The idea here is to provide for a brief rapid influx of message codes while preventing sequentially repeating the same message. i.e. if two...
Replies
23
Views
675
Back
Top Bottom