10 bit calculation? Help.

klima
2019-12-30
2020-01-02
  • klima - 2019-12-30

    Hello, anyone can help me to calculate 10 bit in 2 bytes ?
    [1-6] + ( [7-8] + [9-16] ) need insert value 0 - 1000 in (7-16) added first 1 - 6 bits to 7 - 16 ? how to code this in st ?


    b1,b2,b3.b4,b5,b6:bool;
    first_byte : byte;
    next_byte :byte;
    value : int; ( 0 - 1000 )

    first_byte.0 = b1;
    first_byte.1 = b2;
    first_byte.2 = b3;
    first_byte.3 = b4;
    first_byte.4 = b5;
    first_byte.5 = b6;
    ( b7 to b14 represent 0 - 1000 )

    And how to insert 0 - 1000 value start from first_byte.6 and use all(8bit) next_byte ?

     
  • dkugler - 2019-12-31

    Originally created by: D. Kugler

    declarate a WORD:

    VAR
    WordCalc : WORD;
    END_VAR

    //convert value to WORD and shift left 6 times, bits 0-5 are false now, value is in bit 6-15
    WordCalc:= SHL(TO_WORD(value),6);
    https://help.codesys.com/webapp/_cds_op ... n=3.5.15.0
    //then store uppers and lowers to your bytes:
    first_byte:= TO_BYTE(WordCalc); //the bits 8-15 are ignorred
    next_byte:= TO_BYTE(SHR(WordCalc,8)); //Shift bits 8-15 to 0-7 before writing
    //Add the bits 0-5, attention := !!
    first_byte.0 := b1;
    ...
    ...

     
  • klima - 2020-01-01

    I made a mistake with coding left to right bytes, that's why the standard approach didn't work, only switching to the 01 variable view help. Everything works as expected.

    In C Im coded like that:
    byte_a[0] = sum_of_bits | ((value >> 2) & 0xc0);
    byte_b[1] = value & 0xff;

    Working code:
    value as int.
    byte_of_bits as byte -> sum of bools (bits) from 0 to 5.
    byte_a, byte_b as byte.


    byte_a := byte_of_bits + SHL(to_byte(value),6);
    byte_b := to_byte(value);


    D. Kugler - Your code and method working great too
    Thank You for help!
    What a shame, a mistake in the coding method little endian vs big endian oh.

     

    Related

    Talk.ru: 1

  • dkugler - 2020-01-02

    Originally created by: D. Kugler

    don't be ashamed, mistakes are there, to be done and do it better at next time

     

Log in to post a comment.