Help on completing sketch for adc ADS1211

Hi.

I´m trying to finish the code for the adc ADS1211 from TI. 90% of the sketch is given by the user BenF and can be found here and here, from the library of the ADS1213. The code for ADS1213 is based on this one..i tried to upload it but didint get it working..the complete code for ADS1213.

But since my programming is very basic, i dont understan how can i connect the dots to make this code work

// constant defines
#define ENABLE 1
#define DISABLE 0
#define SIGNED 0
#define UNSIGNED 1
#define BIPOLAR 0
#define UNIPOLAR 1
#define SDIO 0
#define SDOUT 1
#define MSbyte 0
#define LSbyte 1
#define MSbit 0
#define LSbit 1

#define TURBO1 0
#define TURBO2 1
#define TURBO4 2
#define TURBO8 3
#define TURBO16 4

#define GAIN1 0
#define GAIN2 1
#define GAIN4 2
#define GAIN8 3
#define GAIN16 4

#define CHANNEL1 0
#define CHANNEL2 1
#define CHANNEL3 2
#define CHANNEL4 3

#define MODE_NORMAL 0
#define MODE_SELF_CALIBRATION 1
#define MODE_SYSTEM_OFFSET_CALIBRATION 2
#define MODE_SYSTEM_FULL_SCALE_CALIBRATION 3
#define MODE_PSEUDO_SYSTEM_CALIBRATION 4
#define MODE_BACKGROUND_CALIBRATION 5
#define MODE_SLEEP 6

// convenience macros
#define freq_to_data_rate(_data_freq, _turbo_rate) (F_CPU/2*(1<<_turbo_rate)/_data_freq/512-1)

// type definitions
typedef struct _cmr_type {
  uint32_t data_rate :13;
  uint32_t turbo_rate :3;
  uint32_t channel :2;
  uint32_t gain :3;
  uint32_t mode :3;
  uint32_t dsync :1;       // 1=enable, 0= disable (DRDY on read)
  uint32_t serial_out :1;  // 0=SDIO, 1=SDOUT
  uint32_t bit_order :1;   // 0=MSbit, 1=LSbit
  uint32_t byte_order :1;  // 0=MSbyte, 1=LSbyte
  uint32_t polarity :1;    // 0=bipolar, 1=unipolar
  uint32_t data_format :1; // 0=signed, 1=unsigned
  uint32_t ref_output :1;  // 0=disable, 1=enable
  uint32_t bias_output :1; // 0=disable, 1=enable
} cmr_type;

// variable declarations

static union {
  cmr_type t; 
  byte b[4];  // CMR0, CMR1, CMR2, CMR3
} cmr = {freq_to_data_rate(50,TURBO16), TURBO16, CHANNEL3, GAIN1, MODE_SELF_CALIBRATION, DISABLE, SDIO, MSbit, MSbyte, BIPOLAR, SIGNED, ENABLE, ENABLE};



void setup() {
 
  set_clock();
  Serial.begin(115200);
  DDRB |= _BV(PORTB5); // Set Serial clock as output
  reset_ADC;

}

void loop() {
  
  
}






//*******Writing ADC********
#define WRITE 0
#define READ 1

typedef struct _insr_type {
  uint8_t adr :5;        // high-bit unused
  uint8_t count :2;
  uint8_t rw :1;         // 0=write, 1=read
} insr_type;

typedef union {
    insr_type t;
    byte b;
} insr_union;

byte spi_write(byte adr, byte count, byte val[])
{
  insr_union insr;
 
  insr.t.adr = adr;
  insr.t.count = count - 1;
  insr.t.rw = WRITE;

  while (!(PIND & _BV(PIND2)));  // wait for ready to go high
  while (PIND & _BV(PIND2));     // wait for ready to go low
  
  // clock out instruction byte
  DDRB |= _BV(PORTB3); 
  delayMicroseconds(1); 
  for (byte mask = 0x80; mask; mask >>= 1) {
    if (mask & insr.b) PORTB |= _BV(PORTB3); else PORTB &= ~_BV(PORTB3);
    PORTB |= _BV(PORTB5);
    delayMicroseconds(1);
    PORTB &= ~_BV(PORTB5);
    delayMicroseconds(1);
  }
  
  // clock out bits, MSB first
  for (byte i = 0; i < count; i++) { 
    for (byte mask = 0x80; mask; mask >>= 1) {
      if (mask & val[count - i - 1]) PORTB |= _BV(PORTB3); else PORTB &= ~_BV(PORTB3);
      PORTB |= _BV(PORTB5);
      delayMicroseconds(1);
      PORTB &= ~_BV(PORTB5);
      delayMicroseconds(1);
    }
  }
  DDRB &= ~_BV(PORTB3);
}


//********Reading ADC********
void spi_read(byte adr, byte count, byte val[], boolean sync)
{
  insr_union insr;
 
  insr.t.adr = adr;
  insr.t.count = count - 1;
  insr.t.rw = READ;
 
  if (sync) {
    while (!(PIND & _BV(PIND2)));  // wait for ready to go high
    while (PIND & _BV(PIND2));     // wait for ready to go low
  }
  
  // clock out instruction byte
  DDRB |= _BV(PORTB3); 
  delayMicroseconds(1); 
  for (byte mask = 0x80; mask; mask >>= 1) {
    if (mask & insr.b) PORTB |= _BV(PORTB3); else PORTB &= ~_BV(PORTB3);
    PORTB |= _BV(PORTB5);
    delayMicroseconds(1);
    PORTB &= ~_BV(PORTB5);
    delayMicroseconds(1);
  }
  
  DDRB &= ~_BV(PORTB3);
  PORTB &= ~_BV(PORTB3);
  
  // clock in bits, MSB first
  for (byte i = 0; i < count ; i++) {
    val[count-i-1] = 0;
    for (byte j = 0; j < 8; j++) {
      PORTB |= _BV(PORTB5);
      delayMicroseconds(1);
      val[count-i-1] <<= 1;
      if (PINB & _BV(PINB3)) val[count-i-1] |= 1;
      PORTB &= ~_BV(PORTB5);
      delayMicroseconds(1);
    }
  }
}



//*******Reseatting the ADC*******

void reset_ADC() {

  PORTB |= _BV(PORTB5);
  delayMicroseconds(32+1);  // 256 x Xin
  PORTB &= ~_BV(PORTB5);
  delayMicroseconds(1);
  PORTB |= _BV(PORTB5);
  delayMicroseconds(64+1);  // 512 x Xin
  PORTB &= ~_BV(PORTB5);
  delayMicroseconds(1);
  PORTB |= _BV(PORTB5);
  delayMicroseconds(128+1); // 1024 x Xin
  PORTB &= ~_BV(PORTB5);
  
  }
  
//********Clock***********

  // configure timer2 for 8MHz output on PD3
  void set_clock() {
    
  TCCR2B = _BV(CS20);  // prescaler = F_CPU/1
  OCR2A = 2;  // clear timer at 0 count, f = F_CPU/2
  TCCR2A = _BV(COM2B0) | _BV(WGM21);  // toggle PD3 on compare match
  DDRD |= _BV(PORTD3);  // start clock output on XIN/PD3
  
  }

In void setup():
-Set clock, to use clock from arduino PIND3
-Serialbegin...
-Set serialclock as output
-Reset adc

In the setup i also need to write something i suppose it has something to do with this

static union {
  cmr_type t; 
  byte b[4];  // CMR0, CMR1, CMR2, CMR3
} cmr = {freq_to_data_rate(50,TURBO16), TURBO16, CHANNEL3, GAIN1, MODE_SELF_CALIBRATION, DISABLE, SDIO, MSbit, MSbyte, BIPOLAR, SIGNED, ENABLE, ENABLE};

where i choose the options i want to be write, maybe using spi_write?

In void loop()
-I need to read from the ADC using spi_read..but i dont have a clue how can get the values..

I appreciate any help..

Thanks.

From what i tested, the ADS1213 Library from Murdock works fine with ADS1211. Just a tip for ADS1211 users..

Did you finally get it working? Did you use an externa crystal?