>Working, with library!< Interfacing ADS1213 22-bit ADC

I decided to write a library for the ADS1213. I think it could benefit many people who are looking for a multiple-channel, very accurate external ADC as there are a lot of projects out there that need fast and accurate analog to digital conversions and the ADS1213 is a really powerful ADC with a lot of functions.
It includes some functions to be able to easily set the various settings without having to define the bytes by hand. It also includes a function to be able to change channel really quickly, and a function to read the signed output in either 1, 2 or 3 bytes.

Any bugs found or suggestions for improvement are of course welcome. [EDIT 4 may '11: bugfixed version uploaded, offset binary/two's complement option was written to the wrong bit]

The library is attached and includes a readme file which documents all functions in more detail and an example which reads signed values of all ports sequentially, as well as the datasheet itself. Place it in the library folder of your Arduino installation.

The library can be easily adjusted to be used with other TI ADC chips.

It's possible to use multiple ADS1213 chips by using separate Chip Select pins.

The library allows for different clock speeds and different pins (while still using direct port manipulation of course). The restrictions are:
SCLK pin can be from pin 8 to pin 13
IO pin can be from pin 8 to pin 13
DRDY pin can be from pin 2 to pin 7
CS pin can be any pin and is OPTIONAL (if not used (you pull CS LOW) use 0)

The constructor:
ADS1213(clockspeed in MHz (e.g. 2 or 1.6), Offset Binary true/false, SCLK pin, IO pin, DRDY pin, CS pin (0 if not in use))
Clock speed automatically adjusts the timings. It's conservative and can be cranked up for faster transfer. Reading will be the first thing failing when cranked up too high. Reset is probably even more sensitive to it and should be adjusted by hand in ADS1213.cpp for the real clock frequency when over-cranking.

Writing:
write(address, number of bytes, (name of) array with bytes)

Easy writing to the Command Register:
CMRwrite(channel, mode, gain, Turbo Mode Rate, Decimation Ratio)
See datasheet p.19-21 for explanation about mode, gain, TMR and DR. All are set using decimal numbers, except for mode which is set by bytes (e.g. B001 = self calibration, 0 is normal mode). Gain and TMR can be 1,2,4,8 and 16. Decimation Ratio can be from 19 to 8000 (= the number of samples taken for each conversion result).

Change the channel:
channel(number of channel)
Channel can be from 1 to 4.

Reading if Offset binary = true: ( - full scale = 0, mid range = maximum value / 2, + full scale = maximum value)
read(start address, number of bytes, (optional) synchronize)
Returns an unsigned long. If synchronize is left out, the function automatically waits for DRDY to go high and low again. If 'false', it does not wait (if you've already accounted for it in your own code).

Reading if Offset binary = false (using two's complement, - full scale = - min. value, mid range = 0, + full scale = + max. value):
readSigned(number of bytes)
Returns a signed long. When only reading 1 or 2 bytes instead of 3, the maximum and minimum values will be closer to 0 than when reading 3 bytes because the bytes get placed at the end of the long. When reading 1 byte the resolution is very low (8 bits), when reading 2 bytes the resolution is already very good for most projects (16 bits).

reset(): Resets the chip. Happens automatically at initialization.

Here's a sample sketch which cycles through all channels:

// This sketch demonstrates the use of the ADS1213 library by first initializing
// it with the neccesary parameters, then reading it every half second while cycling through the channels.

#include <ADS1213.h>

ADS1213 ADC_ext(2,false,13,11,2,0);
// clock speed MHz, true=offset binary (false=two's complement (can be negative)), SCLK pin, IO pin, DRDY pin, CS pin (0 if not in use)

void setup() {
  Serial.begin(115200);
  ADC_ext.CMRwrite(3,B001,1,1,255);
  // channel 3, mode 001 (self-calibration), gain 1, TMR 1, DR 255
  Serial.println("Done with setup");
}

void loop() {
  static byte Channel=1;
  Serial.print(Channel,DEC); Serial.print(" ch ");
  Serial.println(ADC_ext.readSigned(3),DEC); // Read and print the signed value
  Channel += 1;
  if (Channel>4) Channel = 1;
  ADC_ext.channel(Channel); // Set the current (incremented) channel by using the channel() function
  delay(500);
}

ADS1213.rar (911 KB)