Using Arduino Uno with ADS1256

Hi,
I'm trying to read data from the ADS1256 and save it into a 1 dimensional array. I'm using a function called GetValues as shown below. The read_values() function inside reads the ADC count for each of the 4 pins and save it into the array ADC_Value. Normally, ADC_Value[0] should correspond to read_values(0) which is the ADC count for channel 0 but I keep getting a mixed array. read_value(0) is being stored in ADC_Value[1] and read_value(1) is being stored in ADC_Value[2], read_value(2) --> ADC_Value[3] and read_value(3) --> ADC_Value[0]. I 'm not sure why I keep getting a shift in my array. I implemented the same routine with a Rpi and it works fine. Could someone please explain to me how to pass a function in arduino because I thought Arduino was basically C. I'm not sure what I did wrong in this function.

void GetValues(uint32_t *ADC_Value){

uint32_t new_ADC_Value1[4] = {0,0,0,0};

for (i = 0;i<4;i++){

ADC_Value = read_values(i);

  • }*
    }

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Please post a copy of your complete code.

Thanks.. Tom... :slight_smile:

uint32_t read_three_values(uint8_t ch) {

uint8_t mode = 1;

  //datasheet page 21 at the bottom gives the timing
  uint32_t adc_val1;
  uint32_t adc_val2;
  uint32_t adc_val3;
  uint32_t buf[3] = {0,0,0};
  waitforDRDY(); // Wait until DRDY is LOW original
  SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1));
  digitalWriteFast(10, LOW); //Pull SS Low to Enable Communications with ADS1247
  //delayMicroseconds(5); // RD: Wait 25ns for ADC12xx to get ready
 // waitforDRDY(); // Wait until DRDY is LOW

  //now change the mux register
//  SPI.transfer(WREG | MUX); // send 1st command byte, address of the register
//  SPI.transfer(0x00);   // send 2nd command byte, write only one register
  if (mode == 1){
    switch(ch){
      case 0:
    //  waitforDRDY();
        digitalWriteFast(10, LOW); //Pull SS Low to Enable Communications with ADS1247

      
          SPI.transfer(WREG | MUX); // send 1st command byte, address of the register
          SPI.transfer(0x00);   // send 2nd command byte, write only one register
          SPI.transfer((0 << 4) | 1);     //pins registers 2 and 3
            digitalWriteFast(10, HIGH); //Pull SS Low to Enable Communications with ADS1247

          //delay(1);
          //Serial.println("Using channel 0-1 ");
         // delayMicroseconds(15);
         // Serial.println(MUX);
          break;
          
      case 1:
      waitforDRDY();
          SPI.transfer(WREG | MUX); // send 1st command byte, address of the register
          SPI.transfer(0x00);   // send 2nd command byte, write only one register
          SPI.transfer((2 << 4) | 3);     //pins registers 2 and 3
          delayMicroseconds(15);
          break;
          
      case 2:
     // waitforDRDY();
        digitalWriteFast(10, LOW); //Pull SS Low to Enable Communications with ADS1247

          SPI.transfer(WREG | MUX); // send 1st command byte, address of the register
          SPI.transfer(0x00);   // send 2nd command byte, write only one register
          SPI.transfer((4 << 4) | 5);     //pins registers 2 and 3
            digitalWriteFast(10, HIGH); //Pull SS Low to Enable Communications with ADS1247

         // delayMicroseconds(15);
          break;
          //delayMicroseconds(15);
      case 3:
      //waitforDRDY();
        digitalWriteFast(10, LOW); //Pull SS Low to Enable Communications with ADS1247

          SPI.transfer(WREG | MUX); // send 1st command byte, address of the register
          SPI.transfer(0x00);   // send 2nd command byte, write only one register
          SPI.transfer((6 << 4) | 7);     //pins registers 2 and 3
            digitalWriteFast(10, HIGH); //Pull SS Low to Enable Communications with ADS1247

          //delayMicroseconds(15);
          break;
          delayMicroseconds(15);

    }
        
  }else if(mode == 0){
    SPI.transfer((ch << 4) | (1 << 3));     //pins registers 2 and
    
    
    
    }
//    Serial.print("ch ");
//    Serial.println(ch);
//  Serial.print("First MUX Reg ");
//    Serial.println(GetRegisterValue(MUX));
  //now we need to sync
  //need to delay by 4x SPI clock = 2.35 uS (t1)
  //to be safe 5 uS
  delayMicroseconds(2);
  SPI.transfer(SYNC);
delayMicroseconds(10);
  //again delay by t1
 // delayMicroseconds(5);
  //send wakeup
  SPI.transfer(WAKEUP);
  delayMicroseconds(10);

  //then delay one more time by t1 before rdata
 // delayMicroseconds(1);

  SPI.transfer(RDATA); //Issue RDATA
  delayMicroseconds(7);

  //This is the reading in the Data register from whatever the mux was set to the last
  //time this function was called. By default, it is configured to leave that value at 0

  buf[0] = SPI.transfer(NOP);
  delayMicroseconds(10);
  buf[1] = SPI.transfer(NOP);
  //adc_val1 |= SPI.transfer(NOP);
  delayMicroseconds(10);
  buf[2] = SPI.transfer(NOP);
  delayMicroseconds(10);
          adc_val1 = (buf[0]<<16) & 0xFF0000;
        adc_val1 |= (buf[1]<<8) & 0xFF00;
        adc_val1 |= (buf[2]) & 0xFF;

  //now wait for the next dataready
  //waitforDRDY(); // Wait until DRDY is LOW
  //delayMicroseconds(5);


  digitalWriteFast(10, HIGH);
  SPI.endTransaction();


        if (adc_val1 & 0x800000){
            adc_val1 &= 0xF000000;
        }
//Serial.print("adc_val1 ");
//Serial.println(adc_val1);


//  val1 = adc_val1;
//
//  val2 = adc_val2;
//
//  val3 = adc_val3;

  return adc_val1;
  
}


//library files

volatile int DRDY_state = HIGH;

void waitforDRDY() {
  while (DRDY_state) {
    continue;
  }
  noInterrupts();
  DRDY_state = HIGH;
  interrupts();
}

//Interrupt function
void DRDY_Interuppt() {
  DRDY_state = LOW;
}

long GetRegisterValue(uint8_t regAdress) {
  uint8_t bufr;
  digitalWriteFast(10, LOW);
  delayMicroseconds(10);
  SPI.transfer(RREG | regAdress); // send 1st command byte, address of the register
  SPI.transfer(0x00);     // send 2nd command byte, read only one register
  delayMicroseconds(10);
  bufr = SPI.transfer(NOP); // read data of the register
  delayMicroseconds(10);
  digitalWriteFast(10, HIGH);
  //digitalWrite(_START, LOW);
  SPI.endTransaction();
  return bufr;

}

void SendCMD(uint8_t cmd) {
  waitforDRDY();
  SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with 4Mhz clock, MSB first, SPI Mode0
  digitalWriteFast(10, LOW);
  delayMicroseconds(10);
  SPI.transfer(cmd);
  delayMicroseconds(10);
  digitalWriteFast(10, HIGH);
  SPI.endTransaction();
}

void Reset() {
  SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with  clock, MSB first, SPI Mode1
  digitalWriteFast(10, LOW);


  
digitalWriteFast(ADS_RST_PIN, HIGH);
delay(200);
  digitalWriteFast(ADS_RST_PIN, LOW);
  delay(200);
  digitalWriteFast(ADS_RST_PIN, HIGH);
//  delayMicroseconds(10);
//  SPI.transfer(RESET); //Reset
//  delay(2); //Minimum 0.6ms required for Reset to finish.
//  SPI.transfer(SDATAC); //Issue SDATAC
//  delayMicroseconds(100);

  
  digitalWriteFast(10, HIGH);
  SPI.endTransaction();
}

void SetRegisterValue(uint8_t regAdress, uint8_t regValue) {

  uint8_t regValuePre = GetRegisterValue(regAdress);
  if (regValue != regValuePre) {
    //digitalWrite(_START, HIGH);
    delayMicroseconds(10);
    waitforDRDY();
    SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with SPI_SPEED, MSB first, SPI Mode1
    digitalWriteFast(10, LOW);
    delayMicroseconds(10);
    SPI.transfer(WREG | regAdress); // send 1st command byte, address of the register
    SPI.transfer(0x00);   // send 2nd command byte, write only one register
    SPI.transfer(regValue);         // write data (1 Byte) for the register
    delayMicroseconds(10);
    digitalWriteFast(10, HIGH);
    //digitalWrite(_START, LOW);
    if (regValue != GetRegisterValue(regAdress)) {   //Check if write was succesfull
      Serial.print("Write to Register 0x");
      Serial.print(regAdress, HEX);
      Serial.println(" failed!");
    }
    else {
      Serial.println("success");
    }
    SPI.endTransaction();

  }

}


 
//uint32_t set_new_val[4]={0,0,0,0};
void GetAll(uint32_t *ADC_Value){
      SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1));

  digitalWriteFast(10, LOW);

uint8_t mode = 1;//set up mode || common mode --> 8 channels diff mode --> 4 channels
uint8_t chan_num;
uint8_t i;
if (mode == 0){
   chan_num = 8;
  
  }else if (mode == 1){
    chan_num = 4;
    
    }

    uint32_t new_ADC_Value1[4] = {0,0,0,0};
   // Serial.print("chan num ");
//Serial.println(chan_num);

    for (i = 0;i<chan_num;i++){
      
      ADC_Value[i] = read_three_values(i);
      Serial.print("i ");
      Serial.println(i);
      Serial.print("ADC Value ");
      Serial.print(read_three_values(i));
      Serial.print(" ");
      Serial.print("ADC_VALEU[i] --> ");
      Serial.print(ADC_Value[i]);
      Serial.println(" ");
      }
    //  uint32_t new_val[4]={0,0,0,0};

//  if (mode == 1 ){
//      //uint32_t new_val[4]={0,0,0,0};
//  for (uint8_t i =0;i<4;i++){
//    new_val[i] = read_three_values(i);
//
//    //set_new_val[i] = new_val[i];
//    
//    }
//    }else if (mode == 0){
//
//
//          // uint32_t new_val[8]={0,0,0,0,0,0,0,0};
//  for (uint8_t i =0;i<8;i++){
//
//    
//    new_val[i] = read_three_values(i);
    
    
 //   }
      
      
      
    //  }
    
//    return new_val[0];
    SPI.endTransaction();

digitalWriteFast(10, HIGH);
    }