Author Topic: 16 BIT TO 32 BIT  (Read 9159 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
16 BIT TO 32 BIT
« on: January 23, 2020, 12:58:32 PM »
Is there a simple way to add two 16 bit values to make it into a 32 bit value.
Example I need RELAY[1]  and RELAY[2] to be a 32bit value instead of two separate 16bit values

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re: 16 BIT TO 32 BIT
« Reply #1 on: January 23, 2020, 03:10:14 PM »
Lorne,

I can think of 3 reasonable approaches:
  • Use the SetHigh statement to build a 32 bit variable in A:
    A = RELAY[2]
    SETHIGH16 A,RELAY[1]
  • Build a 32 bit variable in A
    A=RELAY[1] * &h10000            ‘ shift left 16 bits
    A=A | (RELAY[2] | &h0000ffff)   ‘ OR in least significant 16 bits
  • Take advantage of the fact that the DM memory can be accessed as both 16 bit and 32 bit values.  In "C-like" programming languages this is an example of a "union" data structure. This code will build a 32-bit value in DM32[1]:
    DM[1]=RELAY[1]
    DM[2]=RELAY[2]
Try them in the simulator. Pay close attention to where the bits from RELAY[1] and RELAY[2] end up in the 32 bit value.  Set the most significant bit of each group of relays and verify that you get the correct pattern in the 32-bit variable.

Gary Dickinson
« Last Edit: January 23, 2020, 09:01:03 PM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re: 16 BIT TO 32 BIT
« Reply #2 on: January 24, 2020, 06:38:24 AM »
Gary again thanks for the reply I had used DM[1] = RELAY[1] and DM[2] = RELAY[2] with the result being DM32[1] before I posted then I found out the problem is the simulator.
Do you or anyone else know how to solve the issue of the simulator stopping and causing you to have to close down the software and reboot only for it to work for another 5 to 1o minutes. This only seems to happen on the computer with Windows 10 I never experienced this on my computer with Windows XP

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: 16 BIT TO 32 BIT
« Reply #3 on: January 24, 2020, 12:01:57 PM »

I can think of 3 reasonable approaches:
  • Use the SetHigh statement to build a 32 bit variable in A:
    A = RELAY[2]
    SETHIGH16 A,RELAY[1]
  • Build a 32 bit variable in A
    A=RELAY[1] * &h10000            ‘ shift left 16 bits
    A=A | (RELAY[2] | &h0000ffff)   ‘ OR in least significant 16 bits
  • Take advantage of the fact that the DM memory can be accessed as both 16 bit and 32 bit values.  In "C-like" programming languages this is an example of a "union" data structure. This code will build a 32-bit value in DM32[1]:
    DM[1]=RELAY[1]
    DM[2]=RELAY[2]
Try them in the simulator. Pay close attention to where the bits from RELAY[1] and RELAY[2] end up in the 32 bit value.  Set the most significant bit of each group of relays and verify that you get the correct pattern in the 32-bit variable.

Gary Dickinson

Great answers to the question that Lorne posted, Gary!

The 3rd approach is very efficient but just need to remember that when DM32[1] is formed by two adjacent 16-bit DM[1] and DM[2], the upper 16-bit is contained in DM[1] and lower 16-bit is contained in DM[2] (known as the Big Endian representation).  So in order for the relay #1 to 32 to form a 32-bit number the correct assignment is as follow:

DM[2] = RELAY[1]  ' the lower 16-bit
DM[1] = RELAY[2]  ' the upper 16-bit
A = DM32[1]


« Last Edit: January 24, 2020, 12:12:39 PM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3170
    • View Profile
    • Internet Programmable PLCs
Re: 16 BIT TO 32 BIT
« Reply #4 on: January 24, 2020, 12:10:58 PM »
Do you or anyone else know how to solve the issue of the simulator stopping and causing you to have to close down the software and reboot only for it to work for another 5 to 1o minutes. This only seems to happen on the computer with Windows 10 I never experienced this on my computer with Windows XP

We have been using Windows 10 for a few years already and do not experience simulator stopping issue. Do note that TRiLOGI is written to run on 32-bit Java JRE and does tend to encounter problems when it is running on 64-bit Java.

In the latest iTRiLOGI 6.6 and 7.2 the 32-bit Java JRE is automatically installed and invoked by the TL66.exe and TL32.exe.  If you have older version of TRiLOGI you may want to consider upgrading them which also resolves the problems that could be caused by incompatible JRE.  If the Java update does remove critical files on the 32-bit java then you need to re-run the SetupTL7.exe program to re-install the correct JRE.

Also whenever your PC prompt you to update the (usually 64-bit) JRE, make sure that if you are given an option, do not allow it to "search and destroy" older java on the PC. The 32-bit JRE installed in the TRiLOGI folder will only be used by TRiLOGI and would not affect your other PC program that may need to use the latest 64-bit JRE.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re: 16 BIT TO 32 BIT
« Reply #5 on: January 24, 2020, 12:25:38 PM »
Thanks for resolving this issue.
I did not realize that I needed to reverse the position of DM[1] & DM[2]
My problem is that over the years there was no real standard for the order of which 16 bits was the least significant group of 16 bits betwee various PLC manuafactures. So I guess its save to say that if you look at say DM32[1] then the least significant group of 16 bits is DM[2] which correlates to DM32[1] = DM[1] - DM[2] in that order.