read and configure ADC using i2c-tools
3294 Views - Created 13/01/2016
I now want to use i2cget and i2cset to configure the device and read results from the ADC. Both functions take the i2c address of 68 and 69 and then expect a data address (and write value in the case of i2cset).
The data sheet refers to a configuration register but does not discuss anything in the context of data addresses such as are used with i2cget/i2cset. The data sheet also indicates that the power on default value for the configuration register will be 0x90. When I do i2cdump -y 1 0x68 I do not see any values displayed with the setting 0x90. I see all 0xFFs for byte mode or 0xDCFF if I do word mode.
Is it possible to use i2cget/i2cset to access the ADC results and set the configuration data? If so, how?
To read a value from the ADC Differential Pi you first need to write to the configuration register and then read three or four bytes depending on the selected bit rate.
In the 18-bit mode, you read 4 bytes in all of the other modes you read three bytes. Once the configuration register has been written it will take a few milliseconds for the ADC to read the voltage and write it to the registers at which point it will set a ready bit to 0. This readybit is bit 7 of the last returned byte so you need to keep performing a read from the ADC chip until the ready bit shows that the sample has been taken. You then need to combine the bytes from the last read to create the raw data value that can then be converted into a voltage. The bytes will need to be combined in different ways depending on the selected bit rate.
One of the bits in the returned value is a sign bit which shows if the value is positive or negative. In our libraries, we save this value to a variable called signbit to be used later in the voltage calculations and then set the bit to 0. The location of this bit is dependent on the bit rate so you will need to check the datasheet to find out which bit to read and then clear.
One thing to be aware of is when you perform an I2C read to get the values the ADC may not send an ACK bit with the last byte so if your software is looking for ACK bits to check if the data was received it may fail when the last byte is read.
If you understand the C programming language then it may be worth reading through the read_raw() function in our ADC Differential Pi C library so you can see how the library builds the configuration byte, sends it to the ADC and then reads back the data and combines it together.
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.