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 ?
first_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 ?
Originally created by: D. Kugler
declarate a WORD:
WordCalc : WORD;
//convert value to WORD and shift left 6 times, bits 0-5 are false now, value is in bit 6-15
https://help.codesys.com/webapp/_cds_op ... n=22.214.171.124
//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;
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 = sum_of_bits | ((value >> 2) & 0xc0);
byte_b = value & 0xff;
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.
don't be ashamed, mistakes are there, to be done and do it better at next time
Log in to post a comment.