Single bit energized test

Like I said there are more than one way to skin a cat, I think people are looking too deep into this, The code I put up there is based on a selection, I would doubt that anybody will need more that a few selections, however, I do agree that this would not be the solution for more than a few selections. From the post it appears that the bit selection is for running certain routines one at a time based on some way of selecting those routines, how many are you likely to use ?.
I suppose if you have 32 bits then just loop through if you find more than one bit true then alarm.
 
I think Parky's first solution is good enough to solve his problem but what if there were 32 bits and only one can be set? Then Parky's solution is not very good.

What bothers me about the forum needs to rediscover things every few years. This problem or one similar to it has been solved before. I used many bit hack routines for writing chess or Othello programs decades ago.


Here is download section, where is some usually asked PLC codes.
But as this is free forum, no one is saving there.
 
The beauty about the code is it can be expanded to many bits (given scan time) just by altering the for next loop value so increasing it to 64 for testing 64 bits it is that simple, by the way I have not included a check for the process status before the purists comment but that is just an extra little bit of code.
 
I think Parky's first solution is good enough to solve his problem but what if there were 32 bits and only one can be set? Then Parky's solution is not very good.


The first response in this thread proposed a bitsum approach, suitable for a loop and any number of bits; loop sample code was posted almost a dozen posts before this.



The OP's original criteria are cleaner and better; those have a subjective component, of course. For a non-loop approach, doing N integer NEQs (posts #4 and #5 here) is cleaner, IMNSHO, than NxN XICs/XIOs for N>1 or 2; it is also better, if CPU usage is the metric, beyond some value for N (break-even is around half a dozen for MicroLogix 1xxx, so not much less than the OPs expected end case of eight bits).


A few weeks ago there was a thread that ended up being mainly about when to use brute-force (actually in-line) vs. looping algorithms. Looping was generally roundly rejected; some would do it in a few cases and with specific restrictions; one post said hours of extra coding a reliable in-line approach, vs. a riskier "cute" approach, was worth it for the potential reduction in downtime.



What bothers me about the forum needs to rediscover things every few years.


I think you meant weeks;); we do often see links to previous threads as first or second responses.
 
You can also copy bits to int or dint,


Then subtrack one from original int-value


AND these values.
If result is zero, there is only one bit true on bits.
If result is <> zero, several bits are on


https://iq.opengenus.org/detect-if-a-number-is-power-of-2-using-bitwise-operators/


Useful, maybe o_O

but Bubba would not undestand this code.đź“š

Beautiful solution and Bubba can read same comment you wrote:
"If result is zero, there is only one bit true on bits.
If result is <> zero, several bits are on"
 
You can also copy bits to int or dint,


Then subtrack one from original int-value


AND these values.
If result is zero, there is only one bit true on bits.
If result is <> zero, several bits are on


https://iq.opengenus.org/detect-if-a-number-is-power-of-2-using-bitwise-operators/


Useful, maybe o_O

but Bubba would not undestand this code.đź“š


There it is.


Although the OP would also have to check for 0 - i.e. no bits are 1 - as the initial value: 0 - 1 => -1; -1 AND 0 => 0.


And no Bubba in the OP's case, unless OP is a Bubba.
 
Last edited:
One way I simplify alarms and conditions is to check if things are OK, then set an OK bit.

Then, you only need one rung to MOV 37 checking each mode and each OK bit.


I have that in my main ladder (#2) I set an Ok to run bit with the conditions that there is no Emergency Stop, unit is in automatic mode, no alarm present. I did not post the whole program because it is large.
 
The more I look at this, the more I think it belongs in a (sub-)routine: the main reasons are the [NEQ N7:36 0] instructions duplicated in so many rungs.


That test could either make optional the call to the routine, [NEQ N7:36 0 JSR U:n], or the call could be unconditional and the inverse test could be used to return early from the routine, [EQU N7:36 0 RET], after some initialization (e.g. [MOV 0 N7:49]).


It is a subroutine, I am using N7:36 as the sequence step for this subroutine. The reason this snip of code does not have my "ok to run" bit check is the routine would not be called if that bit is false.
 
I thought it would help to post the entire program as it is now (its still being developed) but it exceeds the fourms limit of 300Kb.



I will look at these replies in detail later, thank you all for the replies.


Mike
 
Let's put this into perspective, The OP wanted ideas on how to code a particular type of function in an efficient way, this contained 4 bits (possibly expanding to 8 bits), some solutions have been put forward, however, it seems yet again it's turning into a contest of who can write the function in the least amount of code, all the what if etc... Yes I agree it's good to submit different ideas and possibly enhance on those ideas, it may not particularly help the OP any more but will give others looking through these posts ideas going forward, what seems to be happening again is a slogging match between members (yes I probably find myself being caught up in it as well). Programmers tend to be highly strung and it sure shows, I have been in this industry for over 35 years, do not claim to be the best, however, I have shown to be able to do my job right, have extensive experience in all areas of controls and done work for many well known blue chip companies. I have worked closely with production teams and done extensive training for plant engineers. I have great respect for many of the members here people like Peter N, Ron B and a number of others. Keep the ideas rolling in but let's keep it in context.
 
It's not necessary, but here is an empirical confirmation of Lare's latest algorithm for 8 bits.


Four instructions (SUB, AND, EQU, NEQ); five with the MVM from B3:45 to N7:whatever; no loop. Sweet.


Cleaner AND better.

xxx.png
 

Similar Topics

Hi, I've got a Micro820 that I am using to create some menus and handle an alarm buzzer for a Panelview 800 HMI. I also need it to communicate...
Replies
4
Views
2,070
I'm writing a function block using Omron CX-Programmer for a CJ2M CPU. The FB is for interfacing with Slice I/O counter cards. The counter cards...
Replies
17
Views
9,623
Hi all, I have an application where the plant PLC is communicating via Ethernet/IP to 3 different field PLC's. The Field PLC's are all Micrologix...
Replies
6
Views
3,412
Hi, I'm talking to a ControlLogix PLC using CIP over E/IP, and I'm having trouble accessing single bits inside another type (such as an integer)...
Replies
5
Views
3,851
Hi all, someone knows if is possible in SCL extract the single bit of a WORD or DOUBLE WORD defined as VAR_TEMP?? In other word I'd like to...
Replies
2
Views
7,031
Back
Top Bottom