GolamMostafa:
@OP
These are my tested codes (brute force codes) which have manipulated 27-bytes data (of 2's complement form) of your array named uint8_t serialBytes[] to find the decimal value of Ch-1.
void setup()
{
Serial.begin(9600);
uint8_t serialBytes[] =
{
0xC0, 0x00, 0x00, //header 3-byte
0xFF, 0x60, 0xAD, //Ch-1 (arbitray data in 2's complement form)
0x12, 0x34, 0x56, //Ch-2
0x78, 0x9A, 0xAB, //Ch=3
0xCD, 0xEF, 0xF0, //Ch-4
0x80, 0x83, 0x98, //Ch-5
0xCC, 0xBD, 0x1C, //Ch-6
0x3B, 0xEF, 0xFE, //Ch-7
0x45, 0xDC, 0x57 //Ch-7
};
uint32_t myData[8];
for (int i = 1, j = 0; i <= 8, j < 8; i++, j++)
{
myData[j] = ((uint32_t)serialBytes[3 * i] << 16) |
(uint32_t)(serialBytes[3 * i + 1] << 8) |
(uint32_t)(serialBytes[3 * i + 2]);
}
for(int j=0; j<8; j++)
{
Serial.println(myData[j]&0x00FFFFFF, HEX);//FF60AD, 123456, FF9AAB, FFEFF0, FF8398, FFBD1C, ...
}
//decimal value of Ch-1 = myData[0] = (00)FF60AD = -12^23+(0x7F60AD) = -8388608 + 8347821 = - 40787
// .....................................
for (int i = 23; i >= 0; i--)
{
Serial.print((bitRead(myData[0], i)), BIN); //shows: 1111 1111 0110 0000 1010 1101 = FF60AD in 2's comple.
}
Serial.println();
float x1 = -1 * pow(2, 23); //getting: -12^23
Serial.println(x1, 2); //-8388608.00
uint32_t x2 = myData[0] & 0x007FFFFF;
float x3 = (float)x2;
Serial.println(x3, 2); //+8347821.00 = 7F60AD
Serial.println((x1 + x3), 2); //-40787.00 = FF60AD (2's complement form)
}
void loop()
{
}
Hi GolamMostafa,
Thank you for your help, this worked perfectly and I was able to convert the decimal values to voltage levels. I'm still new to programming and this has been a new learning curve.
void newBytes () {
uint32_t myData[8];
for (int i = 1, j = 0; i <= 8, j < 8; i++, j++)
{
myData[j] = ((uint32_t)serialBytes[3 * i] << 16) |
(uint32_t)(serialBytes[3 * i + 1] << 8) |
(uint32_t)(serialBytes[3 * i + 2]);
}
float x1 = -1 * pow(2, 23); //getting: -1*2^23
uint32_t x2 = myData[0] & 0x007FFFFF;
float x3 = (float)x2;
if (x > 8388607){
rawVoltageValue = ((x - 16777216) * 2.4) / (pow(2,23)-1);
if (filter_flag == true){
High_Pass_Filter(rawVoltageValue*1000.0);
}
else {
Serial.println(rawVoltageValue*1000.0, 5); // print voltage with 5 decimal points
}
} else {
rawVoltageValue = (x * 2.4) / (pow(2,23)-1);
if (filter_flag == true){
High_Pass_Filter(rawVoltageValue*1000.0);
}
else {
Serial.println(rawVoltageValue*1000.0, 5);
}
}
}
You were the only one who was able to help me, and I was wondering if you could answer one more question.
What would be the best way to store the voltage values for each channel in array?
e.g array[] = {voltagech1, voltagech2, voltagech3, ...}
The reason I need the array is because I need to send the data of all 8 channels to an SD card for post data processing.
Again I truly appreciate your help!