Sign in to follow this  
Followers 0
schwarz633

Logix Convert Value to Bits

6 posts in this topic

Let's say I have a DINT tag (ptr) representing a step number (0-95) and want to have a bit representing each step. I know I can do this with another DINT tag (bits) like this: CLR bits -( )- bits.[ptr] But this is only good for values up to 32. I'm trying to avoid 96 equal instructions with 96 coils. Any suggestions?

Share this post


Link to post
Share on other sites
Hi and welcome :) - Create an array of 4 DINT's. Let's call it Step_Bits (instead of "bits" in your example above). So then you have 128 bits to use. Step_Bits[0] will contain steps 0...31, Step_Bits[1] will contain steps 32...63, etc. - Create a DINT called Array_Pointer - Create a DINT called Bit_Pointer - Create a DINT called Step_Number (ptr in your example above) - When your step changes, update Step_Number to the new value and then execute this: DIV Step_Number 32 Array_Pointer CPT Bit_Pointer Step_Number MOD 32 FLL 0 Step_Bits[0] 4 OTL Step_Bits[Array_Pointer].[bit_Pointer] The FLL is the same as your CLR, except it allows you to clear all four DINT's at once There are probably other ways to do it. That would be my best shot at it :) Edited by ASForrest

Share this post


Link to post
Share on other sites
Make your 'bits' tag a boolean array -- e.g. BOOL[96] Then use the logic you've mentioned -- CLR bits; OTE bits[ptr] Note no '.' between "bits" and "[ptr]" I suggest to preface the rung with a DTR on ptr.

Share this post


Link to post
Share on other sites
The issue there is that you can't use the CLR instruction on a BOOL array, and I haven't ever been able to find an easy way to achieve the same result. I've spent enough time beating my head against the wall with BOOL array's to know that they're always painful to work with. In almost every case I've given up and gone back to a DINT. Also, a BOOL[32] array uses a heap more memory than a DINT. I read something a while ago that explained it something like "CLX is a true 32-bit controller. This means for every tag, it reserves 32 bits whether the tag is a DINT, INT, SINT or BOOL. This is why RA recommend you use DINT's wherever possible - because it's going to reserve 32 bits of memory anyway." Ever since I watched an engineer battle for 6 hours to free up enough memory in a PLC to put in one more instruction, I've been mindful of programming efficiency. Although that was an old MS-DOS based PLC with less memory than my graphics calculator. So not really a practical issue if you've got a CLX ;)

Share this post


Link to post
Share on other sites
No. A tag defined simply as BOOL will take 32 bits of memory even though only one bit is useable. A tag defined as BOOL[32] will also take 32 bits of memory, but they can all be used. Every tag is aligned to a 32-bit boundary in memory. Arrays are packed into 32-bit chunks i.e. BOOL[32], SINT[4]. INT[2] all use the same space as a DINT. However, bear in mind that there is a penalty in program memory usage when operating on types other than DINT -- always use a DINT for an array index, even if a SINT would handle all index values. The tag Step above will use the equivalent of 3 DINT's. It may be useful to give the individual bits aliases to identify the step operation.

Share this post


Link to post
Share on other sites
Ah, fair enough. I knew there was some reason they wanted you to use DINT's but never got around to looking into the details properly :)

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