Function read_raw without sign
The ADC Differential Pi is an Analogue to Digital converter for the Raspberry Pi
03/08/2016
Posted by:
mbrod
I want to store raw data in a database. But it seemed that the function read_raw does not distinguish between positive and negative values. For example: Suppling negativ and positiv voltage the codeadc.set_pga(1)print (adc.read_raw(1))print (adc.read_voltage(1))delivers130360-0.011093750000000124and1303752.0370625In my understanding of two's complement negativ values should deliver raw date above 131071 with 18 bit resolution, right?How can I achieve integer values, which distinguish between negative and positive values?
03/08/2016
Posted by:
andrew
I have added a function called get_signbit() to the Python and Python 3 libraries that will return the sign bit for the previous sample. The sign bit is the bit which the ADC returns to show if the value is positive or negative. If you call adc.read_raw(1) and then call adc.getsignbit() it will return 0 if the read_raw value was positive or 1 if it was negative.When the raw value is read from the ADC chip the location of the sign bit changes depending on what bit rate is being used. The library records the value of the sign bit into a variable before setting it to 0 so the value can be used by the read_voltage function. If you want to store the raw value in your database then you can use the get_signbit() function to find out if the value was positive or negative and then use that to update a bit within your integer before saving it to the database.If you want to update the sign bit to restore the value as it came out of the ADC then you will need to change the bit in the position as follows:18 bit: sign bit = bit 1716 bit: sign bit = bit 1514 bit: sign bit = bit 1312 bit: sign bit = bit 11
04/08/2016
Posted by:
mbrod
Fine!Now code:Bitres=pow(2,17)signed_int=adc.read_raw(1)-adc.get_signbit()*Bitresprint (signed_int)print (adc.read_voltage(1))delivers-69-0.00013281250000002354and650.000126953125That's perfect to store in a database. Signed int needs only 3 Byte vs. Decimal 5 Byte. May be important when saving secondly data over long time.Thank you for your fast answer and updating the library!
Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.