Siemens conversion question...

leem2209

Lifetime Supporting Member
Join Date
May 2015
Location
Wirral, UK
Posts
210
Hey folks,

I have a (kind of, but not a true) random number generator that generates a number from 1 to 500. When I press a button it stores that number into another memory word (2)

I want to convert memory word 2 in order to activate the relevant bit in a datablock (DB101) which contains 500 bits of data.

EG if the button is pressed and it stores the value of 2 into MW2, then I want it to activate DB101.DBX0.2.
MW2 value = 8 then activate DB101.DBX1.0
MW2 value = 30 then activate DB101.DBX3.6
and so on.

How do I do this conversion?
 
OPN DB101 // open datablock db101

L MW2
ITD // int to double
T MD10 // bit index
LAR 1
A M0.0 // allways "1" bit
= [AR1, P#0.0]


Not tested, but something like this
 
typing error. Load all OB error blocks to CPU for reventing stop.

OPN DB101
L MW2
ITD // int to double, this is not necessary?
T MD10 // bit index, this is not necessary?
LAR 1
A M0.0 // allways "1" bit
= DBX [AR1, P#0.0]


or maybe



OPN DB101
L MW2
ITD // int to double
T MD10 // bit index

A M0.0 // allways "1" bit
= DBX [MD10]
 
typing error. Load all OB error blocks to CPU for reventing stop.

OPN DB101
L MW2
ITD // int to double, this is not necessary?
T MD10 // bit index, this is not necessary?
LAR 1
A M0.0 // allways "1" bit
= DBX [AR1, P#0.0]

Lare, this works... thank you!!! :geek:
 
OK (y)

It is useful to limit MW2 to 0 to 500. That way PLC won't go stop because of dbx-address error if MW2 somehow gets wrong number

http://www.plcdev.com/siemens_s7_indirect_addressing
http://www.plctalk.com/qanda/showthread.php?p=19321
http://www.plctalk.net/qanda/showthread.php?t=103518

With AR2 register keep in mind, that PLC uses it also, so modifing AR2 can affect to PLC program.
If you use AR2, then saving data before from AR2 and after indirect addressing moving saved value to back AR2 is good

http://s7tools.com/index.php?option...n-block-fb&catid=17:tips-and-tricks&Itemid=30
 
Last edited:
MW2 is limited to 500 anyway (uses counter to increase integer, then compare =500 to move 0 into MW2)

Just so I understand the STL code and how it works:

It's basically taking MW2, changing from integer to double word, transferring into MD10.

Then loading AR1 which will have the offset value of MD10 number of bits starting from DBX0.0....?
 
The ITD used above is misleading and wrong and should be removed.
The transfer to MD10 is redundant and should be removed.

L MW2 is all that is required prior to the address register load.
 
Last edited:
yes, for indirect addressing you can use inbuild AR1, AR2 registers or doubleword (second example)

if you write

L MW2

Then MW2 is loaded to AR1 (accu), there is no need to change type.


On indirect addressing first three bits are used for bits and all rest for words. You now set bits indirectly so MW2 have correct format without any modification (only double_word type is needed for indirect pointing)

If you want to point to whole dbw addresses without bits, then you need to multiple MW2 value with 8 (SLD 3) before indirect commands.

So

OPN DB101
L MW2
ITD // int to double
T MD10 //
L MD10
SLD3 // pointer format
MD14
L 1
T DBW [MD14]

Would have resuly moving value 1 to dbw0, dbw1 and so on dependig of MW2.


Because STL don't care data types you can also write

OPN DB101
L MW2
SLD3 // pointer format and changing to double
MD14
L 1
T DBW [MD14]


Thanks for correcting LD2. thinked to use MD10 first to indirect addressing
 
Last edited:
The purpose of the ITD instruction is to convert a signed integer to a signed double integer. The bit offset is not normally signed. Loading an integer into the accumulator (which is 32 bits wide) results in the top 16 bits being set to zero.
 
I hope it can meet your needs.

Code:
      L     B#16#0
      T     #Fill                       // Temp Byte

      CALL  "FILL"
       BVAL   :=#Fill
       RET_VAL:=#_Return
       BLK    :=P#DB101.DBX0.0 BYTE 63

      L     MW   2
      ITD   
      LAR1  

      OPN   "1..500 of Bits"
      L     DBLG
L001: T     #Count                      // Temp Int
      SET   
      =     DBX [AR1,P#0.0]
      L     #Count
      LOOP  L001

Regards.
 
I hope it can meet your needs.

Code:
      L     B#16#0
      T     #Fill                       // Temp Byte

      CALL  "FILL"
       BVAL   :=#Fill
       RET_VAL:=#_Return
       BLK    :=P#DB101.DBX0.0 BYTE 63

      L     MW   2
      ITD   
      LAR1  

      OPN   "1..500 of Bits"
      L     DBLG
L001: T     #Count                      // Temp Int
      SET   
      =     DBX [AR1,P#0.0]
      L     #Count
      LOOP  L001

Regards.

Thanks SHKODRAN but L D [person 2 # something] sent me some code that worked (see posts above)
 
Noticed you blog posting, you should correct your code if you haven't yet

With AR1 register you can do this:

OPN DB 101 // open datablock DB101
L MW 2 //bit index
LAR1 // load accu1 to AR1
A M 90.1 // allways "1" bit
= DBX [AR1,P#0.0] //indirect addressing with AR1 register and offset



Or if you want use double integer instead of AR-register (MD, DBD or temp variable)


OPN DB 101 // open datablock DB101
L MW 2 // bit index
ITD // int to double conversion
T MD 10 //transfer to MD10
A M 90.1 // allways "1" bit
= DBX [MD 10]


ON STL int to dint conversion isn't needed, STL don't check if input and ouput are same datatype. So we can shorten 2. code.


OPN DB 101 // open datablock DB101
L MW 2 // bit index
T MD 10 //transfer to MD10 for indirect addressing
A M 90.1 // allways "1" bit
= DBX [MD 10]

Address indirect move/coil needs 32-bit variable, so that is why we use AR1 or MD/DBX/temp_dint type variable
 
Lare, Thank you, but I'm done with this for now. It was for testing purposes for a project. But I will bear it in mind if I ever need this code again.

Just another question whilst I'm here; is it possible to move data from a datablock to Q register? I have 500 bits to move. Can easily move a byte, or word, or dword, but how about 500 bits at once?
 

Similar Topics

Anyone know how I can convert DTL to DATE type? This rung is trying to determine if the system is in calibration by subtracting the current date...
Replies
1
Views
580
I have converted a Siemens OP7 program to a new Delta DOP107BV. It is all working as expected except for the timer values. I am using MW36 as the...
Replies
6
Views
940
I have a nice project in Visilogic that I have been asked to re-create in Tiaportal/Siemens. (Bosses / maintenance want to standardise on one...
Replies
2
Views
1,308
Hi All, I'd like to inquire about the possibility of converting a program currently used on Delta PLC to be compatible with Siemens PLC using...
Replies
5
Views
1,421
When i try to migrate an old WinCC program in Siemens TIA V16, i get the following error: Migration failed because the project was created with a...
Replies
3
Views
6,132
Back
Top Bottom