Author Topic: MSB LSB  (Read 15967 times)

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
MSB LSB
« on: April 11, 2012, 03:39:19 AM »
Hi,

Can you please assist.

I'm reading some modbus values to DM and whilst I can calculate the right answer with a calculator I cannot get the maths right in  the PLC.

The data is stored in two registers say 40001 and 40002,

If I work in hex the data is represented as 3 and A660  respectively, if I input this into my hex calculator as 3A660 adding the MSB and LSB then change to decimal I get 239200 which is 239.2Volts - fine.

Because the DM's are 16 bit, in the PLC I read 3 and -22944, even if I read the data into the system 32 bit variables as 3 and 42592 I cannot get the maths right to convert back to my required 239200.

I don't need the full three decimal places so will probably divide the number by 100 again so I can store the final value back into DM so my HMI can read the data so temporary use of the 32bit variables would be nice as I have around ten values to convert....

Cheers  Marcus


garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:MSB LSB
« Reply #1 on: April 11, 2012, 09:21:21 AM »
Marcus,

Welcome to fun with trying to build a 32bit value from two 16bit pieces.  TBASIC treats everything in DM[] as 16 bit signed integers.

More accurately, TBASIC, only works with 32bit signed values.  Fortunately, the TRiLog guys have thrown in some support to help deal with the assembling a 32 bit value from 16bit values in the form of SETHIGH.  A similar problem is how to disassemble a 32 bit value into 16bit words is solved with GETHIGH.

The following example shows how to assemble a 32bit value from a pair of 16 bit chunks.  The first version is what I'd recommend, the second version shows how it could be done w/o the use of SETTHIGH.


' Assemble32b - build 32bit value in A from two 16bit
'  words stored in DM[1..2]
'
'
' Value in DM[1] is the least significant word
' Value in DM[2] is the most significant word

'
' If DM[1] is greater than or equal to &h8000 it will be treated as a negative number
'  and sign extended when assigned to the 32bit variable A.  So if DM[1] was &H8001, then
'  A will become &HFFFF8001 after the assignment.
'
' The SETHIGH16 statement will overwrite the most significant 16 bits of A with the value held
'  in DM[2].
'
' The order of the following statements is critical...
'

A = DM[1]????????????' least significant word (with sign extension)?????????
SETHIGH16 A,DM[2]??????' most significant word.

' This code is how it could be done without the
'    use of the SETHIGH function.
'
'
B = DM[2]               ' most significant word, first
B = B * &H10000         ' 16bit left shift to get msw into position
B = B + (DM[1] & &HFFFF)   ' least significant word placed
                           '   without messing up the msw

[/color]

Gary D
« Last Edit: April 11, 2012, 09:40:39 AM by garysdickinson »

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
Re:MSB LSB
« Reply #2 on: April 11, 2012, 12:53:00 PM »
Gary,

Thank you for this.  Again your input is much appreciated.

Cheers