Write registers and BITs i2C within a library

mrpioneer
2021-07-12
2021-07-16
  • mrpioneer - 2021-07-12

    Hello everybody,

    I need a brief explanation.
    I would like to create an i2C library in which I have to write individual bits in different registers.

    Specifically, this means

    Register (0x01)
    Bit 4

    Possibly can someone write me a short code how this works.

    I have the following methods available within the library.
    write
    write8
    writebits

    Thanks in advance.

     
  • mrpioneer - 2021-07-16

    write8

    write8(1, 16#40); If i understand it correct should this write BIT 4 in register 01?


    write

    HoldCmd :BYTE :=16#F4;
    write(ADR(HoldCmd),1);
    This will write a byte into register 16 # F4.
    But what bits?
    All to 1 or all to 0 or something completely different?


    writeBits

    But unfortunately I have no idea how to use this command.

    I would be very grateful for a short description

    Input usiRegister USINT register
    usiFirstBit USINT highest value bit to write (0..7)
    usiLength USINT number of bits to write (1..usiFirstBit+1)
    usiValue USINT value of the bits to set (0..2^usiLength-1)


    Thanks in advance.

    BR
    MR. Pioneer

     
  • Ingo

    Ingo - 2021-07-16

    Hi!
    This actually can be very easy, when I got your question right πŸ˜‰. I would intuitively use Write8, as you did.
    The interface is roughly:

    Write8(usiRegister, usiValue)
    

    So, to write your bit 4 to register 1, you need to call s.th. like this:

    // when you start counting with 1
    // => 0000 1000
    Write8(1, 16#8);
    
    // when you start counting with 0
    // => 0001 0000
    Write8(1, 16#10);
    

    Alternatively, you can write it in binary form, instead of hex:

    // when you start counting with 1
    Write8(1, 2#00001000);
    
    // when you start counting with 0
    Write8(1, 2#00010000);
    
     

Log in to post a comment.