Bit Addressing in integers

swtroelofs
2010-02-09
2010-03-16
  • swtroelofs - 2010-02-09

    I've got a question about bit addressing in Codesys 2.3.9.18:

    I would like to use the following code:

    iTest : int;
    wTest : word;
    FOR iTest := 1 TO 10 DO
       wTest.iTest := TRUE;
    END_FOR
    

    This returns the error:
    Integer number or symbolic constant expected

    The following example does work:

    FOR iTest := 1 TO 10 DO
       wTest.1   := TRUE;
    END_FOR
    

    Is it possible to use something like 'wTest.iTest'?

    (and yes, it is possible to use an ARRAY OF BOOL to store a large collection of booleans, but this uses a lot more memory)

     
  • Rolf-Geisler - 2010-02-09

    I only could suggest to use a function, which gets the bit number as an input.

    http://www.geisler-controls.de I've published the library BinUtils. Use the function PutBit_W out of it :

    iTest : int;
    wTest : word;
    FOR iTest := 1 TO 10 DO
       wTest := PutBit_W (wTest, iTest, TRUE); (* to set the bit iTest *)
       wTest := PutBit_W (wTest, iTest, FALSE); (* to clear the bit iTest *)
    END_FOR
    

    This will work.

     
  • swtroelofs - 2010-02-10

    Thanks for your reply. You put me on the right track. A library I'm already using (util.lib) also has a function PUTBIT. The program works now.

     
  • spfeif - 2010-03-16

    To add you can also use constants as bit modifiers but you also have to select in the OPTIONS-BUILD-REPLACE CONSTANTS. So you can do the following:

    options :word;
    var constant
     OPTION1 : word := 0;
     OPTION2 : word := 1;
     ON          : BOOL := TRUE;
     OFF         : BOOL := False;
    end_var
    options.OPTION1 := ON;
    options.OPTION2 := OFF;
    
     

Log in to post a comment.