Closed
Description
This is shared with us as information taken from the industruino page.
These examples are based on the fact that most devices send data in this format:
float / ulong / long type data, Communication transmission in byte order2-1-4-3
To separate a float value into 2 registers.
unsigned int f_2uint_int1(float float_number)
{ // split the float and return first unsigned integer
union f_2uint
{
float f;
uint16_t i[2];
};
union f_2uint f_number;
f_number.f = float_number;
return f_number.i[0];
}
unsigned int f_2uint_int2(float float_number)
{ // split the float and return first unsigned integer
union f_2uint
{
float f;
uint16_t i[2];
};
union f_2uint f_number;
f_number.f = float_number;
return f_number.i[1];
}
It's used
var_float = 3.14
unsigned int reg0 = f_2uint_int2(var_float ); // split the float into 2 unsigned integers
unsigned int reg1 = f_2uint_int1(var_float );
To retrieve floating value from 2 registers.
float f_2uint_float(unsigned int uint1, unsigned int uint2) { // reconstruct the float from 2 unsigned integers
union f_2uint {
float f;
uint16_t i[2];
};
union f_2uint f_number;
f_number.i[0] = uint1;
f_number.i[1] = uint2;
return f_number.f;
}
It's used
float float_reconstructed = f_2uint_float(reg1, reg0);
To separate LONG data the code:
unsigned int l_2uint_int1(long long_number) { // split the long and return first unsigned integer
union l_2uint {
long l;
uint16_t i[2];
};
union l_2uint l_number;
l_number.l = long_number;
return l_number.i[0];
}
unsigned int l_2uint_int2(long long_number) { // split the long and return first unsigned integer
union l_2uint {
long l;
uint16_t i[2];
};
union l_2uint l_number;
l_number.l = long_number;
return l_number.i[1];
}
It's used:
long_number = 4123546
unsigned int reg0 = l_2uint_int2(long_number); // split the long into 2 unsigned integers
unsigned int reg1 = l_2uint_int1(long_number);
To rebuild LONG from two registers.:
long l_2uint_long(unsigned int uint1, unsigned int uint2) { // reconstruct the long from 2 unsigned integers
union l_2uint {
long l;
uint16_t i[2];
};
union l_2uint l_number;
l_number.i[0] = uint1;
l_number.i[1] = uint2;
return l_number.l;
}
It's used:
long long_reconstructed = l_2uint_long(reg1, reg0); // reconstruct the long from 2 unsigned integers
Metadata
Metadata
Assignees
Labels
No labels