About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

Function read_raw without sign

1512 Views - Created 03/08/2016

03/08/2016

Posted by:
mbrod

Users Avatar

Location:
Frankfurt am Main

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 code

adc.set_pga(1)
print (adc.read_raw(1))
print (adc.read_voltage(1))

delivers
130360
-0.011093750000000124

and
130375
2.0370625

In 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

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

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 17
16 bit: sign bit = bit 15
14 bit: sign bit = bit 13
12 bit: sign bit = bit 11

AB Electronics UK Tech Support

04/08/2016

Posted by:
mbrod

Users Avatar

Location:
Frankfurt am Main

Fine!

Now code:

Bitres=pow(2,17)
signed_int=adc.read_raw(1)-adc.get_signbit()*Bitres
print (signed_int)
print (adc.read_voltage(1))

delivers

-69
-0.00013281250000002354

and

65
0.000126953125

That'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.