Convert 24-bit ADC serial read data from 3-byte format to signed integer (int32) in Matlab
<p>I am receiving EEG data from a 24 bit ADC over serial. The ADC data is transmitting in 3 bytes from MSB to LSB. The full packet is 21 bytes:</p>

<p>The first byte is the start byte - 0xFF (255 in decimal)
Then packet number byte.
Then the next 3 bytes are the 24 bit ADC value broken into MSB LSB2 LSB1
I can parse the data fine, but re-constructing a 2's complement signed int32 number is causing issues. The values I am getting out certainly don't reflect what the ADC should be giving out.</p>

<p>Below are the lines to read and parse the 504 samples (which gives me 24 ADC values (504samples/21bytes = 24 values)). I have tried uint8 instead of uchar with similar results (when I try int8 I get a invalid specified precision error).</p>

<pre><code>comEEGSMT = serial(com,'BaudRate',3000000);
fopen(comEEGSMT);
rawData(1:504) = fread(comEEGSMT, 504, 'uchar');
fclose(comEEGSMT);
startPackets = find(rawData == 255);
bytes = rawData([startpackets+2 startpackets+3 startpackets+4]);
</code></pre>

<p>I have tried the following method to reconstruct the value:</p>

<pre><code>ADC_value = bytes(:,1)*256^2 + bytes(:,2)*256 + bytes(:,3);
</code></pre>

<p>and the following line is the formula to convert the above number to volts:</p>

<pre><code>ADC_value_volts = ADC_value*(5/3)*(1/(2^32));
</code></pre>

<p>The values are in the range of 4000 - 8000 microvolts with large jumps in value. The values SHOULD be in the range of 200 - 600 microvolts with small changes.</p>

<p>I have found other questions relating to similar issues, but have had no success trying the proposed solutions such as in the link below: </p>

<p><a href="https://uk.mathworks.com/matlabcentral/answers/137965-concatenate-3-bytes-array-of-real-time-serial-data-into-single-precision" rel="nofollow noreferrer">https://uk.mathworks.com/matlabcentral/answers/137965-concatenate-3-bytes-array-of-real-time-serial-data-into-single-precision</a></p>

<p>Any help would be very much appreciated as I've been stuck on this for quite long.</p>

<p>Thanks Mark</p>