Sign in to follow this  
Followers 0
DesertDog

Help Moving Bits into Byte

8 posts in this topic

Using Siemens Step 7, I and wanting to move 8 sequential bits into a Byte using the label instead if the direct address so that adding/removing variables will not cause me to need to readdress everything. Is there any simple way to do this. The bits are defined in the STAT section of a function block and I want to simply move them into a Byte. I have done this many times with GE & Melsec but cant find a simple way with Step in it 7.

Share this post


Link to post
Share on other sites
Im not exactly sure what you want to do. Please correct me if im wrong. One way to adress bits in a byte symbolically are by using stl and adressregister 1. This only works inside the fb call. Now you can add stat variables without have to readressing the bits. lar1 p#yourbyte a [ar1,p#0.0] // Accessing the bits a [ar1,p#0.1] a [ar1,p#0.2] a [ar1,p#0.3] a [ar1,p#0.4] a [ar1,p#0.5] a [ar1,p#0.6] a [ar1,p#0.7] Another way is to use tempvariables. Lets say you symbolicly name 8 temp bits first in the temp stack. Then you can move over the temp bits to a statbyte in at the end of the block call by using the move instruction. This you can use in lad,fbd and stl. L lb 0 //Loads temp byte 0 T Yourbyte

Share this post


Link to post
Share on other sites
BoolIn is the frist Bit of 8 to be used and Byte out is a byte with the bit pattern of the 8 bits. Reversing this would also be useful but I should be able to figure that out once I understand this way. Even if it has to be STL can it be made into a FC to use? Example: -----------------|-------|----                  |       |           BooIn  |       |  ByteOut                  |       |                  |_______| OR -----------------|-------|----                  |       |          ByteIn  |       |                  |       |          BoolOut |       |                    |_______|

Share this post


Link to post
Share on other sites
Yes its possible to do the above described FC block. One Bool in one Byte out and you will get all your 8 bits i a byte. Even if your bits start for expample at Db7.dbx5.4 and ends at Db7.dbx6.3. Let me know if you want to do it and i push you in the right direction.

Share this post


Link to post
Share on other sites
I would like to do this very much. Also I want to use the Labels instead of the addresses so adding to the data blocks won't mess things up.

Share this post


Link to post
Share on other sites
Here we go then...... Declare your lsb bit in as a a in variable in pointer format Declare your byte as a out byte variable Declare a tempvariable a dword i here call it DBnumber Hmmmm the pointer format is 6 bytes long how do we access it inside the fc since siemens only can handle 4 bytes a dword at the most We let the adressregisters help us Inside the fc call L #BitIn //Loads the information you entered at the BitIn bone in accumulator1 lar1 //Loads the accumulator1 in adressregister 1 Now we can access all the 6 bytes in the pointer. The first 2 bytes contains the dbnumber so remember if you want to move bits in a db the pointer in must be declared with the db number example db1.dbx5.5. You can write in errorhandeling in the fc but i wont do that here. You can ofcourse move any bits with this code Inputs outputs memory bits. If you are planning not to move dbbits you can remove that part of the code. l w [ar1.p#0.0] //Loads the first word of the pointer the dbnumber t #DBnumber //transfers to a dbnumber opn db [dbnumber] //Opens the db in the db register //This will work even if you dont move bits in a db The last 4 bytes contains your adress and your memory information L D [AR1,P#2.0] //Loads the information in adressregister1 LAR1 L B [AR1,P#0.0] // Loads a byte of whatever information you entered in the bit in pointer T #ByteOut //Moves to your byte

Share this post


Link to post
Share on other sites
L p##BitIn //!!Almost missed the Bitin must be loaded as a pointer!!! l W [ar1.p#0.0] t #DBnumber OPN DB [#dbnumber] L D [AR1,P#2.0] LAR1 L B [AR1,P#0.0] T #ByteOut Not so much code right?? Stl is very powerful ones you get to know it. Me myself is a ladder guy but ladder cant do it all in a Siemens plc. You might think that this is wrong but look at any windows program in a dissambler and you will see similarity to Stl. This means when you code in stl you will get very efficient code. And efficient code means less cycle time. All your lad or fbd code will get compiled to stl code before being downloded to the plc. The best should probaly be to code all of the program i Stl but i think that its much easier to locate faults in the logic with the lad format. And i know for a fact that people with only basic knowledge of plc programming dont want to see Stl code when they open a project. As earlier members of this forum surely stated the Siemes manuals isent the best but i think they get better and better. But do you want to learn Stl buy a book by Hans Berger! Ive read these two Automating with STEP 7 in STL and SCL and Automating with STEP 7 in LAD and FBD and i can isure you they were worth every penny. Please forgive me for any grammar or spelling errors English is not my mother tongue.

Share this post


Link to post
Share on other sites
For those who have been following this thread: I have used what I found here and tweaked it to be exactly what I want. I originally asked for a Byte output so I could avoid the low byte - high byte issue but after playing with the code I just included code to fix the bit order and used a double word. I am using this to move alarm bits into a double word for easy detection (not zero) and TouchScreen Communication transfer. There was another problem with the original code where the pointer was not read into the address register causing a program fault. Special thanks to Klaus who's original code this is based upon. The following code will create a FC that will input a start bit and move 32 bits into an output double word retaining the bit order. // Read In Pointer info      L     P##BoolIn                   // Load BoolIn as a Pointer      LAR1                              // Into Address Register // Determine & Open Data Block      L     W [AR1,P#0.0]               // Loads the DB Number      T     #DB_Number                  // Transfer Data Block Number To Temp Area      OPN   DB [#DB_Number]             // Opens the Data Block // Read Bit Data into Accumulator      L     D [AR1,P#2.0]               // Loads a DWord of Data starting with BoolIn      LAR1                              // Into Address Register 1       // Swap Byte 0 And 1 of Output (LW 2 & 3) To retain bit order      L     B [AR1,P#0.0]               // Load Byte With First Byte In      T     LB     3                    // Save Byte to Second Byte of Output      L     B [AR1,P#1.0]               // Load Byte With Second Byte In      T     LB     2                    // Save Byte to First Byte of Output      L     B [AR1,P#2.0]               // Load Byte With Third Byte In      T     LB     5                    // Save Byte to Fourth Byte of Output      L     B [AR1,P#3.0]               // Load Byte With Fourth Byte In      T     LB     4                    // Save Byte to Third Byte of Output // Transfer data out      L     #TempOut                    // Loads the reordered temp output word      T     #DWordOut                   // Save to output DWord

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0