Bit Mask operation

  • What is the purpose you you wanting to do this? We may be able to help you find a different solution.

    I have a register, R[200] in which I set alarms.

    Then I use this register to find which alarms are activated.

    When R[200] == 1, I want to call a sub-program to display a notification.

    When R[200] == 2 (10), I want to stop the program

    When R[200] == 3 (11), I want to stop the program

    In my program I will write something like below:

    Code
    IF (R[200] AND 1) THEN JMP LBL[10:Warning]
    ELSEIF (R[200] AND 2) THEN JMP LBL[11:STOP]
    ENDIF

    I don't know if it's really clear..


    If the previous code doesn't work I think it will be possible to use the operator DIV and MOD to check if a bit is set !

    Code
    R[1] = R[200] DIV 2
    R[1] = R[1] MOD 2
    IF R[1] == 0 => bit 2 is equal to 0
    IF R[1] == 1 => bit 2 is equal to 1 

    I am going to try that :smiling_face:

  • Hi



    Somewhere in your program you set R200 to a value, lets say R[200]=1

    Then, somewhere else you ask the questions about the value of R200


    IF R[200] = 1 , JMP LBL (30)

    IF R[200] =2 , JMP LBL (33)


    So, in LBL(33) you do something and from there you go home or jump back to other part of the program


    If you want to stop the program you could


    LBL 11

    !Stop program

    !Wait R111= 999


    You program will wait forever until an arbitrary register = 999 or you can wait for an input that you will never use OR send an output to the PLC to stop you

    Retired but still helping

  • Your program is only hard if the system can have multiple alarms at once. If this is the case then you could just do the following:


    Code
    1=Bad Thing
    2=Worse Thing
    4=Really bad thing
    8=Worst thing possible


    At the start of your program you would need to set R[200]=0. Then when ever a new alarm is added it would be R[200]=R[200]+Current alarm number

  • A few other options to consider that may be easier for other people to read and troubleshoot your code.


    Option 1: is to use Group Outputs. A group output creates an integer, up to 16 bits. You can map the same bits to DO's or Flags. Then you have the option to look at each bit or the total integer value.


    Option 2: Forget using integers or registers. Instead just reserve a block of Flags or DO's (boolean IO type). Each flag represents 1 alarm and can be set or examined independently. If you wanted to scan through all of them you could do so with a For Loop, or an incrementing BG Logic program.


    My honest opinion is that using individual Flags or DO's would be much easier for another programmer to make sense of then to come up with a complex round-a-bout bitmask operation.

Advertising from our partners