Single bit energized test

I like that one. Clean and compact and pretty easy to understand if you understand binary representations of numbers. It also allows handling of zero as either good or bad (no more than one bit set versus one and only one bit set) simply by including or removing the EQU of the initial value to zero at the end. And in MOST plcs you could use all the bits in the word if you wanted to and it would work correctly.

Keith
 
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.

Your first solution worked but it is not a general case. It is very application specific. The next person may want to do a similar thing with 6 bits or some other number of bits. Are we to have a thread for each case? Are people expected to re-discover a solution each time? NO! This is what separates humans from some intelligent animals.

The other thing that was disappointing is that no one got my hint. I used two words "bit hacks". Do a search for "bit hacks". If you can't remember the specific algorithm then remember "bit hacks" so you can find the algorithm.

python code
Code:
d=0b0100_1100     # find the least significant bit set of d
print(bin(d))
lsb=d&-d               # only the least significant bit is set.
print(bin(lsb))

output
Code:
0b1001100
0b100

if only interested in the 4 lsb then 'and' with 0b1111.
No loops. No need for a subroutine. very efficient and general at least up to the maximum word length
 
My second solution (admit it is a loop) can take any number of bits only caveats is a large number of iterations may increase scan time considerably and in that platform you cannot jump out of a for next loop (but you could use conditional jumps to exit the loop at the first test for more than 1 bit). the original solution would not take much coding for up to 8 bits. I really don't see the issue with any of the solutions posted here. It sounds like people are trying to trash ideas in favour of their own rather than submit them as an alternative. I noticed that DR's last solution missed the problem of negative numbers but decided not to **** on his solution before he realised. I call that being diplomatic.
 
DR's last solution missed the problem of negative numbers but decided not to **** on his solution before he realised. I call that being diplomatic.


I call it that, and kind, too; thanks!


Maybe I should have considered the old adage: "Better to keep your mouth shut and be thought a fool, than to open it and remove all doubt;)."
 
DR: I consider you a proficient programmer and would not trash your code, your thought process is exactly what a good programmer needs and like many on here I find most of the solutions helpful, not for me currently but we can all learn. If you noticed in my last post, on Mitsubishi, you cannot jump out of a for next loop the compiler does not let you, also you cannot configure the number of iterations using a variable, I know it sucks but there it is, You could create your own by using conditional jumps & of course use a variable as the number of iterations.
Probably find that is how they do the for next loop when compiled.
 
Here is the bit-hack algorithm corrected to include handling of the sign bit, for 16-bits:

  1. Rung 0000: Copy B3:45 bits to N7:255
  2. Rung 0001: Detect cases where number of 1-bits in N7:255, and therefore in B3:45, is other than unity

A similar approach will work for 32-bit DINTs.


I agree with parky that we should not be trashing any given approach.


I like this (Lare's) bit solution, but then, as the "bitboy," I am comfortable with bit-hacks and knowing I will understand it thirteen fortnights hence. Considering the obscurity of what is being done here and the difficulty of generating cogent (as defined by the OP) comments, perhaps one of the loop, or NEQ, or NxN XIO/XIC grid algorithms, would be a better choice.

xxx.png
 
Just to keep it simple... if I had 8 alarm bits all doing the same thing (moving 37 to N7:49) I would just make B3:45 an alarm word and anytime the word was greater than 1 I would make the move, if you dont have a HMI or code thats looking at each bit its easier to just look at the word
 
Genius The original post is a little different, it compares 4 bits so if more than one is on then it sends the code to an alarm word for example: if bit 0 has been selected & any of bits 1-3 is on then this is the alarm. I suppose it's a bit like having 4 selector switches (On/Off) and if more than one is turned on alarm.
This started to be 4 possibly 8 combinations, however, it turned into a competition long code versus word comparisons versus loops (bit checks) and bit tests on 32 or more bits. The one problem is I don't think any of the contributors here really know why the alarm is an integer again I think it's down to OP not posting all relevant information and of course as we all do mis-read posts have done it a number of times lol.
 
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.đź“š


I looked this over and this seems to be the best solution. The example I posted is actual code from my train project and there are two subroutines that need this check. I have 4 areas of track that the subroutine would operate. The bit set tells the routine which section of track to operate. I did this rather then duplicating code four times. One routine will power up the train and detect if the train is moving forward or backwards and depending on another bit that is set, will toggle the track power until the train is moving in the desired direction. Lionel trains can move in either direction but its not possible for the PLC to maintain memory bits of the trains next move since some trains will change states once track power has been off for so many seconds. Others will maintain their forward - reverse cycle no matter how long it sits on the track.


There is planned code where I will need to apply this principal to more then four bits. The track has sections that are known as "block occupancy detectors" which detect if a train is in a section of track. The mainline (which is the complete loop of track) has 8 block detectors, and its possible for the train to be occupying two of them. (overlapping) In this case I would not activate an alarm code but set a flag that the routine will deal with.



Attached is my track Layout. "B" is the block detectors, There are actually 10, but two of them are not part of the main line. "TP" is track sections where I can energize or de-energize track power, "OS" are optical (Photoeye) sensors which protect the track switches and are used to sense the train direction for when a train enters/exits the side or team track. SW are the track switches which are controlled by the PLC.


Also attached is my HMI screen of the layout The green bars indicate that a train is occupying that section of track and when a section of track is energized, the blue bar turns white.


Oh, and I am not too concerned about "Bubba" I doubt I would ever be rolling out a PLC controlled model railway layout to any other company :)




Mike

Track Layout.jpg 0413201842 (Medium).jpg
 
Not related to your PLC, however, while looking at testing for number of bits in a 32 bit word using GXWorks there is a function that counts the number of bits it is called DSUM so it takes a 32 bit tag and counts the number of bits that are true so only needs one instruction i.e. AND True [DSUM MyTag, Result] result contains the number of bits that are true. I have been programming Mitsubishi for over 30 years and never used it until now, just goes to show that you only use the instructions applicable to the process you are programming 👨🏻‍🏫
 

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,088
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,732
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,415
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,872
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,061
Back
Top Bottom