Hi, recently i tried to interface ADS1299 using Arduino Due. The ADS1299 send only 8 bit data to Arduino make it display in serial monitor only 0 to 255 value. Basically what i tried to do here is i want to take the first 4 8-bit value and combine it into a new one long integer and display it to serial monitor. I tried to make it into array. But still don't really understand it. I am new to Arduino and I need to complete this project in this mean time.
I found some coding that tried to do similar things as I tried to do. But I can't fully understand the coding yet. The coding said something related to memcpy and pointer.
Basically what i tried to do here is i want to take the first 4 8-bit value and combine it into a new one long integer and display it to serial monitor.
Sorry. You are not even in the right city, let alone the right ballpark.
If you are getting 4 bytes from your sensor, you want to use a union containing an unsigned long and a 4 byte array:
union stuff
{
unsigned long value;
byte bytes[4];
};
Create an instance of the struct, populate the byte array, and use the long value.
union stuff
{
unsigned long value;
byte bytes[4];
};
A union is a variable on a certain memory location that can be accessed in multiple ways. IN the above example of PaulS the union stuff takes 4 bytes and can be accessed through the unsigned long value and through the array bytes[]. If you had
union stuff
{
unsigned long value;
byte bytes[4];
int x;
};
the variable x (which is only 2 bytes) would share (part of) the same memory locations as the array of 4 bytes or the unsigned long of 4 bytes.
By writing the 4 bytes you have to the union through the byte array the fill up the memory of the union. Then you access those 4 bytes as a long .
That is approx the working of the union.
Another way to pack 4 bytes in one unsigned long is as follows:
unsigned long value = byte0;
value = value * 256 + byte1; // effectively shift the first byte 8 bit positions
value = value * 256 + byte2;
value = value * 256 + byte3;
char textCopy[][4] =
{
{ '1', '9', '2', '.' } // 0
, { '1', '6', '8', '.' } // 1
, { '0', '0', '0', '.' } // 2
, { '0', '0', '1', '.' } // 3
};
// utility function given the address of an array and its length reverses the
// array contents
void memrev(uint8_t* const p, size_t c)
{
uint8_t* lhs = p - 1;
uint8_t* rhs = p + c;
uint8_t t;
while ( ++lhs < --rhs )
{
t = *lhs;
*lhs = *rhs;
*rhs = t;
}
}
...
// CODE FRAGMENT
// Arduino Ateml based processors are little endian, meaning the least
// significant byte of a multi-byte data type is at the lowest address occupied
// by the data type -
//
// copy the first 4 bytes of the array 'textCopy' into the uint32_t value 'v'
// 'memcpy' returns the destination provided it as the address of the array to
// reverse in this case it should be 4 bytes (the size of 'uint32_t') which the
// same length as the value 'v'.
size_t cb = sizeof(textCopy[0]);
uint32_t v;
memrev((uint8_t*)memcpy(&v, &textCopy[0], cb), cb);
Serial.print(v);
Hello-
Please note that the ads1299 (and ads1294, ads1296, ads1298) measure data as 24-bit SIGNED integers, not 32-bit integers. The solution is to bit-shift the 3 bytes to be the most significant bytes of a 32-bit singed integer (padded 8-bits, so that the signed bit is preserved). You can then bit-shift the 32-bit signed value by 8-bits to get the intended 24-bit value:
long var = (adc[1] << 24) + (adc[2] << 16) + (add[3] << 8);
var = var >> 8;
If you are interested in Arduino code for communicating with the ads129n series, you may find this page interesting: http://www.mccauslandcenter.sc.edu/CRNL/ads1298
The sample Arduino Sketch simply converts the ads129n SPI signals to a bluetooth or USB serial communication for display using the example Processing or Matlab scripts. However, the Sketch does identify that the ads129n is working correctly and sets up the device, so it would be very easy to have the Arduino do a lot more (the page includes links for other similar projects).