Move Bits Into Byte W/ Siemens

Bratt formerly known as Klaus.
If you have Step7 v5.2 you can set the adress priority. So if you rightclick the block folder and select object properties you can change the priority from "hardcoded" absolut adress to symbol programming. So Peter the problem isent really a problem just a matter of settings. ;)
 
Last edited:
BooIn [YourFC] ByteOut

So if you want to code this block the code could look as follows

Declare BooIn as a Pointer in the In field
Declare ByteOut as a byte in the Out field
Declare DBnumber as a int in the Tempfield

L P##BooIn //Load Booin as a pointer

l W [ar1.p#0.0] //Loads the dbnumber
t #DBnumber //Transfers to temparea

OPN DB [#dbnumber]// Opens the db

L D [AR1,P#2.0] //Loads the pointerinformation
LAR1 //In adressregister1

L B [AR1,P#0.0] //Loads a byte of whatever info you entered in BooIn
T #ByteOut //Transfers to a byte out

You can remove the part about the dbnumber if you dont move dataareabits but it will work even if you not are moving databits since the memory information also is carried in the pointer.
 
Last edited:
Thanks Bratt! I haven't tested it live yet but your idea works great as code. You make it look so simple. I'm definately going to need to learn STL if I can't get away from Siemens.

There is a typo if anyone tries to cut & past it.
l W [ar1.p#0.0] //Loads the dbnumber
has a "." in "ar1.p#" that should be a ",".

Note: Step 7 version 5.1 will also set the adress priority but 5.2 has more options.
 
Final Result

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 Bratt(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.

Code:
// 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
 
This may have helped with the byte swap too.

Format

CAW

Description

CAW reverses the sequence of bytes in ACCU 1-L. The result is stored in the low word of accumulator 1. The high word of accumulator 1 and accumulator 2 remain unchanged.

Status word

BR CC 1 CC 0 OV OS OR STA RLO /FC
writes: - - - - - - - - -
Example

STL Explanation
L MW10 //Load the value of MW10 into ACCU 1.
CAW //Reverse the sequence of bytes in ACCU 1-L.
T MW20 //Transfer the result to MW20.
Contents ACCU1-H-H ACCU1-H-L ACCU1-L-H ACCU1-L-L
before execution of CAW value A value B value C value D
after execution of CAW value A value B value D value C
 
Sorry guys I have been reading thru this and I am having a hard time following it. I have a byte of information starting at DB36.dbx81.0 that I would like to copy to Db501.dbx0.0 Would I use this style code.


Declare BooIn as a Pointer in the In field
Declare ByteOut as a byte in the Out field
Declare DBnumber as a int in the Tempfield

L P##BooIn //Load Booin as a pointer

l W [ar1.p#0.0] //Loads the dbnumber
t #DBnumber //Transfers to temparea

OPN DB [#dbnumber]// Opens the db

L D [AR1,P#2.0] //Loads the pointerinformation
LAR1 //In adressregister1

L B [AR1,P#0.0] //Loads a byte of whatever info you entered in BooIn
T #ByteOut //Transfers to a byte out


And when I call this FB200 would the BooIn be DB36.dbx81.0 and the Byte out be DB501.dbb0 I guess the part that throws me off the most is the OPN DB [#DB_Number]

I have little to no knowledge of STL. And would like create an FB that I can use whenever to move bytes of information around.

Thanks,
-Joel
 
Just to Confuse...SCL option AT

To be able to access a declared variable with a different data type, you can define views of the variable or of ranges within the variables using the "AT" keyword. A view is visible only locally in the block; it is not included in the interface. A view can be used like any other variable in the block. It inherits all the properties of the variable that it references; only the data type is new.

To be able to access a declared variable with a different data type, you can define views of the variable or of ranges within the variables using the "AT" keyword. A view is visible only locally in the block; it is not included in the interface. A view can be used like any other variable in the block. It inherits all the properties of the variable that it references; only the data type is new.

Works like a union in C

VAR_IN_OUT
bytein: ARRAY[0..1] OF BYTE;
individual AT BYTEin: ARRAY[0..15] OF BOOL;
END_VAR
 
That seems easy enough, but what would I have to do to create a FB that I can specify the starting point of a byte and a byte to place it in? Also what does the OPN actually do? Thanks again guys.
 
OPN - opens a data block after which any data instructions are related to data in that data block, therefore if you can

Code:
OPN DB6
L   DBW1
T   DBW7

as opposed to

Code:
 L   DB6.DBW1
 T   DB6.DBW7

It takes less memory and scan time if your going to working on data in the same data block.

In your example, your indirectly addressing the data block to open, if #dbnumber = 10, the you would be openning DB10.

Be aware that if you do the following

Code:
OPN DB9
  L   DB6.DBW1

After the second instruction DB6 will be open and not DB9.





As far as the FB is concerned.

IN parameter INByte as Byte

OUT, TEMP or STAT parameter DESTByte as Byte


Inside the block

L #INByte
T #DESTByte
 
JOLTRON said:
That seems easy enough, but what would I have to do to create a FB that I can specify the starting point of a byte and a byte to place it in?

Why would you want to write a function block that performs the same function as a load/transfer in STL, or a MOVE in ladder - especially when that function block will contain the same instructions, namely a load/transfer ?

Note that if you use a function block you will need an associated instance data block - this would be better suited to a function (FC) ?

Here's the code for reference:

Code:
FUNCTION FC 1 : VOID
TITLE =
VERSION : 0.1

VAR_INPUT
  byteIn : BYTE ; 
END_VAR
VAR_OUTPUT
  byteOut : BYTE ; 
END_VAR
BEGIN
NETWORK
TITLE =
	  L	 #byteIn; 
	  T	 #byteOut;  
END_FUNCTION
 
I thought MOVE in ladder would move an entire word, and all i need to move is a byte so I was looking to find a way to do that. I was planning on using an FB. Is there an item in ladder that can move a single byte???
 
Alright I guess I never noticed that you could send info to and from an FC. This should work. Now if anyone has any advice and how to take an array of a random number of characters and then spitting out only the last five that would be great. Thanks for all your help so far. I really appreciate it!!
 
Can you be more specific about your array problem ?

NB move works fine for bytes:

bytemove.JPG
 
Last edited:
I completely forgot about the move command, I think I originally tried SFC20 and had issues with that. At least now I have a very basic idea of STL, so I can play with it some more.

As for more details on the array issue:
We have lot numbers that are read in from a barcode reader. These numbers come in as a string then converted to an array of characters ranging from 6 to about 14 characters long. On our final product we are to print the last 5 digits of the lot number. So I was looking to create a FC that would take in an array of characters then spit out the last five digits.

Anything else you need?
 

Similar Topics

I am reading 32 bits from a DCS system into a DINT tag. I need to move this data into 4 INT tags. 1st 8 bits to one tag, next 8 bits to another...
Replies
3
Views
1,761
Hi I'm trying to move bits in to a word. I'm wanting to move %10.0 - %10.13 (Onboard Inputs) in to %MW1 I've tried moving one bit in to another...
Replies
1
Views
1,441
Hi I'm trying to move bits in to a word. I'm wanting to move %10.0 - %10.13 (Onboard Inputs) in to %MW1 I've tried moving one bit in to...
Replies
4
Views
3,399
Hey. I need some input on how to solve this task i am struggeling with. In the picture I attatched to this post, i tried to display what my task...
Replies
40
Views
16,459
I am using Unity Pro XL 4.1 Is there an easy way to move a Boolean value to every bit of an Integer? There is a function for moving a boolean to...
Replies
1
Views
5,816
Back
Top Bottom