LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

SPI + ADS1256 24-bit ADC

I am currently trying out myRIO and SPI communication.

I have tested my code on an arduino DUE and want to use the ADS1256 on the myRIO.

 

Anyone know how to do that?

 

/* ADS1256
    CLK  - pin 13
    DIN  - pin 11 (MOSI)
    DOUT - pin 12 (MISO)
    CS   - pin 10
    DRDY - pin 9
    RESET- pin 8 (or tie HIGH?)
    DVDD - 3V3
    DGND - GND
*/

#define cs 10 // chip select
#define rdy 9 // data ready, input
#define rst 8 // may omit

#define SPISPEED 1000000
              // 1250000

#include <SPI.h>

void setup()
{
  Serial.begin(115200);
  
  pinMode(cs, OUTPUT);
  digitalWrite(cs, LOW); // tied low is also OK.
  pinMode(rdy, INPUT);
  pinMode(rst, OUTPUT);
  digitalWrite(rst, LOW);
  delay(1); // LOW at least 4 clock cycles of onboard clock. 100 microsecons is enough
  digitalWrite(rst, HIGH); // now reset to deafult values
  
  delay(500);
  SPI.begin(); //start the spi-bus
  delay(500);

  //init

  // digitalWrite(cs, LOW);
  while (digitalRead(rdy)) {}  // wait for ready_line to go low
  SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
  delayMicroseconds(10);

  //Reset to Power-Up Values (FEh)
  SPI.transfer(0xFE);
  delayMicroseconds(100);
  
  byte status_reg = 0 ;  // address (datasheet p. 30)
  //PGA SETTING
  //1 ±5V
  //2 ±2.5V
  //4 ±1.25V
  //8 ±0.625V
  //16 ±312.5mV
  //32 ±156.25mV
  //64 ±78.125mV
  //byte status_data = 0x01; //status: Most Significant Bit First, Auto-Calibration Disabled, Analog Input Buffer Disabled 
  byte status_data = 0b00000110;
  SPI.transfer(0x50 | status_reg);
  SPI.transfer(0x00);   // 2nd command byte, write one register only
  SPI.transfer(status_data);   // write the databyte to the register
  delayMicroseconds(10);
  

  byte adcon_reg = 2; //A/D Control Register (Address 02h)
  byte adcon_data = 0x20; // 0 01 00 000 => Clock Out Frequency = fCLKIN, Sensor Detect OFF, gain 1
  SPI.transfer(0x50 | adcon_reg);
  SPI.transfer(0x00);   // 2nd command byte, write one register only
  SPI.transfer(adcon_data);   // write the databyte to the register
  delayMicroseconds(10);


  // Set sampling rate
  byte drate_reg = 3;
  byte drate_data = 0b10000010; // 100SPS
  SPI.transfer(0x50 | drate_reg);
  SPI.transfer(0x00);
  SPI.transfer(drate_data);
  delayMicroseconds(10);

  SPI.transfer(0xF0);
  delay(5000);
  
// digitalWrite(cs, HIGH);
  
  Serial.println("configured, starting");
}


void loop()
{
  unsigned long adc_val =0;
  float mV = 0;

  // digitalWrite(cs, LOW);
  SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
  delayMicroseconds(10);

  while (digitalRead(rdy)) {} ;

  byte data = 0b00000001; DIFF 1
  SPI.transfer(0x50 | 1); // MUX register
  SPI.transfer(0x00);   // 2nd command byte, write one register only
  SPI.transfer(data);   // write the databyte to the register
  delayMicroseconds(10);

  //SYNC command 1111 1100
  SPI.transfer(0xFC);
  delayMicroseconds(10);
  
  //WAKEUP 0000 0000
  SPI.transfer(0x00);
  delayMicroseconds(10);
  
  SPI.transfer(0x01); // Read Data 0000  0001 (01h)
  delayMicroseconds(10);
  
  adc_val = SPI.transfer(0);
  adc_val <<= 8; //shift to left
  adc_val |= SPI.transfer(0);
  adc_val <<= 8;
  adc_val |= SPI.transfer(0);
  
  delayMicroseconds(10);
  
  // digitalWrite(cs, HIGH);
  SPI.endTransaction();

  //The ADS1255/6 output 24 bits of data in Binary Two's
  //Complement format. The LSB has a weight of
  //2VREF/(PGA(223 − 1)). A positive full-scale input produces
  //an output code of 7FFFFFh and the negative full-scale
  //input produces an output code of 800000h. 
  if(adc_val > 0x7fffff){ //if MSB == 1
    adc_val = (16777215ul - adc_val) + 1; //do 2's complement
    //Serial.print("-");
  }
  else
  {
    //Serial.print("+");
  }
  mV = adc_val*0.000596046; // 8388607 = +5.000v / 
  Serial.println(mV);

  delay(100);
}

 

kls
0 Kudos
Message 1 of 10
(5,785 Views)

You can use the SPI Express VI

 

Here is also an example with the FPGA:

http://www.labvolution.com/myrio-fpga-spi-communications/

0 Kudos
Message 2 of 10
(5,757 Views)

Tnx for the info.

I am looking at it now.

 

Here is my New start-up, config and run

 

Power-Up Reset
1. 0xFE
 
Config
1. 0x50 + 0x00 + 0x06 == Buff enable, Auto-Calibration enable
2. 0x51 + 0x00 + 0x01 == DIFF, +AIN0, -AIN1
2. 0x52 + 0x00 + 0x20 == A/D Control Register - PGA1,Default Clock
3. 0x53 + 0x00 + 0x82 == Data Rate
4. 0xFC
 
Run
1. 0x01 == Read Data
kls
0 Kudos
Message 3 of 10
(5,745 Views)

New start-up, config and run is wrong on previous post. Working on it

kls
0 Kudos
Message 4 of 10
(5,742 Views)

Did you succeed?
I have been looking for related documents, if possible can you share your program?

0 Kudos
Message 5 of 10
(4,020 Views)

Sorry for the delay. I have not been working on it for some time, but got some of the codes to work.

I can see if I find it again and send you a link.

kls
0 Kudos
Message 6 of 10
(3,929 Views)

It's ok~
thank you very much .
Looking forward to your news~

0 Kudos
Message 7 of 10
(3,921 Views)

Sorry for the delay.

It is not a loot but a start.

kls
0 Kudos
Message 8 of 10
(3,851 Views)

Hello, thank you very much for your program, but I use spi. But IS ok, thank you~ I want to know that asd1256 has an i2c version?

0 Kudos
Message 9 of 10
(3,833 Views)

I dont know, sorry

kls
0 Kudos
Message 10 of 10
(3,807 Views)