6 Replies Latest reply on Aug 9, 2016 1:06 PM by Intel Corporation

    ADS1256 JavaScript

    KimLorentz

      I am working on an ADS1256 that is controlled true SPI.

      I got it working on Arduino code but need help with the JavaScript versjon, does not know how to make it work.

       

      Here is my Arduino code:

       

      /* ADS1256, datasheet: http://www.ti.com/lit/ds/sbas288j/sbas288j.pdf
        compare: https://github.com/Flydroid/ADS12xx-Library/blob/master/ads12xx.cpp
          connections to Atmega328 (UNO)
      
          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   STATUS  0x00 //Status Control Register 0
      #define   MUX     0x01 //Multiplexer Control Register 0
      #define   ADCON   0x02 //A/D Control Register 0
      #define   DRATE   0x03 //A/D Data Rate Control Register 0
      #define   IO      0X04 //GPIO Control Register 0
      #define   OFC0    0x05 //Offset Calibration Coefficient Register 1
      #define   OFC1    0x06 //Offset Calibration Coefficient Register 2
      #define   OFC2    0x07 //Offset Calibration Coefficient Register 2
      #define   FSC0    0x08 //Full scale Callibration Coefficient Register 0
      #define   FSC1    0x09 //Full scale Callibration Coefficient Register 1
      #define   FSC2    0x0A //Full scale Callibration Coefficient Register 2
      
      
      /*STATUS - Status Control Register 0 ( see p30)*/
      /* BIT7 - BIT6 -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* ID   - ID   -  ID     -  ID     -  ORDER  -  ACAL   -  BUFEN  -  DRDY */
      #define STATUS_RESET 0x01 // Reset STATUS Register
      /*Bits 7 - 4 ID3, ID2, ID1, ID0 Factory Programmed Identification Bits(Read Only)*/
      /*ORDER1:0  Data Output Bit Order*/
      #define ORDER_MSB B00000000 // Most significant Bit first (default)
      #define ORDER_LSB B00001000 // Least significant Bit first
      /*Input data is always shifted in most significant byte and bit first. Output data is always shifted out most significant
      byte first. The ORDER bit only controls the bit order of the output data within the byte.*/
      /*ACAL1:0 Auto Calibration*/
      #define ACAL_OFF B00000000 // Auto Calibration Disabled (default)
      #define ACAL_ON  B00000100 // Auto Calibration Enabled
      /*When Auto-Calibration is enabled, self-calibration begins at the completion of the WREG command that changes
      the PGA (bits 0-2 of ADCON register), DR (bits 7-0 in the DRATE register) or BUFEN (bit 1 in the STATUS register)
      values.*/
      /*BUFEN1:0 Analog Input Buffer Enable*/
      #define BUFEN_OFF B00000000 //Buffer Disabled (default)
      #define BUFEN_ON  B00000010 //Buffer Enabled
      /*DRDY1:0 Data Ready (Read Only) Duplicates the state of the DRDY pin*/
      
      
      /* MUX - Multiplexer Control Register 0 (see p31 - bring together with bitwise OR | */
      /* BIT7  - BIT6  -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* PSEL3 - PSEL2 -  PSEL1  -  PSEL0  -  NSEL3  -  NSEL2   - NSEL1   - NSEL0 */
      #define MUX_RESET 0x01      // Reset MUX0 Register
      /* PSEL3:0 Positive input channel selection bits */
      #define P_AIN0 B00000000 //(default)
      #define P_AIN1 B00010000
      #define P_AIN2 B00100000
      #define P_AIN3 B00110000
      #define P_AIN4 B01000000
      #define P_AIN5 B01010000
      #define P_AIN6 B01100000
      #define P_AIN7 B01110000
      #define P_AINCOM B10000000
      /* NSEL3:0 Negativ input channel selection bits */
      #define N_AIN0 B00000000
      #define N_AIN1 B00000001 //(default)
      #define N_AIN2 B00000010
      #define N_AIN3 B00000011
      #define N_AIN4 B00000100
      #define N_AIN5 B00000101
      #define N_AIN6 B00000110
      #define N_AIN7 B00000111
      #define N_AINCOM B00001000
      
      
      // Differential settings
      #define DIFF0_1 B00000001
      #define DIFF2_3 B00100011
      #define DIFF4_5 B01000101
      #define DIFF6_7 B01100111
      
      
      /*ADCON - A/D Control Register 0 ( see p31)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* 0    - CLK1   -  CLK0   -  SDCS1  -  SDCS0  -  PGA2   -  PGA1   -  PAG0 */
      #define ADCON_RESET 0x20 // Reset ADCON Register
      /*CLK2:0 D0/CLKOUT Clock Out Rate Setting*/
      #define CLK_OFF B00000000 //Clock Out off
      #define CLK_1   B00100000 //Clock Out Frequency = fCLKIN (default)
      #define CLK_2   B01000000 //Clock Out Frequency = fCLKIN/2
      #define CLK_4   B01100000 //Clock Out Frequency = fCLKIN/4
      /*When not using CLKOUT, it is recommended that it be turned off. These bits can only be reset using the RESET pin.*/
      /*SDCS2:0 Sensor Detection Current Sources*/
      #define SDCS_OFF B00000000 //Sensor Detect Off (default)
      #define SDCS_05  B00001000 //Sensor Detect Current 0.5?A
      #define SDCS_2   B00010000 //Sensor Detect Current 2?A
      #define SDCS_10  B00011000 //Sensor Detect Current 10?A
      /*The Sensor Detect Current Sources can be activated to verify the integrity of an external sensor supplying a signal to the
      ADS1255/6. A shorted sensor produces a very small signal while an open-circuit sensor produces a very large signal.*/
      /*PGA3:0 Programmable Gain Amplifier Setting*/
      #define PGA_1   B00000000 //(default)
      #define PGA_2   B00000001
      #define PGA_4   B00000010
      #define PGA_8   B00000011
      #define PGA_16  B00000100
      #define PGA_32  B00000101
      #define PGA_64  B00000110
      
      
      #define PGAmulti_1    0.0000005960464832810452   //PGA-1  ±5V        /16777215
      #define PGAmulti_2    0.0000002980232416405226   //PGA-2  ±2.5V      /16777215
      #define PGAmulti_4    0.0000001490116208202613   //PGA-4  ±1.25V     /16777215
      #define PGAmulti_8    0.00000007450581041013064  //PGA-8  ±0.625V    /16777215
      #define PGAmulti_16   0.00000003725290520506532  //PGA-16 ±312.5mV   /16777215
      #define PGAmulti_32   0.00000001862645260253266  //PGA-32 ±156.25mV  /16777215
      #define PGAmulti_64   0.000000009313226301266331 //PGA-64 ±78.125mV  /16777215
      
      
      /*DRATE - A/D Data Rate Register 0 ( see p32)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* DR7  - DR6    -  DR5    -  DR4    -  DR3    -  DR2    -  DR1    -  DR0 */
      #define DRATE_RESET 0xF0 // Reset DRATE Register
      /*DR7:0 Data Rate Setting*/
      #define DR_30000 B11110000 //30.000 SPS (default)
      #define DR_15000 B11100000 //15.000 SPS
      #define DR_7500  B11010000 //7.500 SPS
      #define DR_3750  B11000000 //3.750 SPS
      #define DR_2000  B10110000 //2.000 SPS
      #define DR_1000  B10100001 //1.000 SPS
      #define DR_500   B10010010 //500 SPS
      #define DR_100   B10000010 //100 SPS
      #define DR_60    B01110010 //60 SPS
      #define DR_50    B01100011 //50 SPS
      #define DR_30    B01010011 //30 SPS
      #define DR_25    B01000011 //25 SPS
      #define DR_15    B00110011 //15 SPS
      #define DR_10    B00100011 //10 SPS
      #define DR_5     B00010011 //5 SPS
      #define DR2_5    B00000011 //2,5 SPS
      
      
      /*IO - GPIO Control Register 0 ( see p32)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* DIR3 - DIR2   -  DIR1   -  DIR0   -  DIO3   -  DIO2   -  DIO1   -  DIO0 */
      #define IO_RESET 0xE0 // Reset IO Register
      /*DIR3 - Digital I/O Direction for Pin D3*/
      #define DIR3_OUT B00000000 //D3 is an output
      #define DIR_IN   B10000000 //D3 is an input (default)
      /*DIR2 - Digital I/O Direction for Pin D3*/
      #define DIR2_OUT B00000000 //D2 is an output
      #define DIR2_IN  B01000000 //D2 is an input (default)
      /*DIR1 - Digital I/O Direction for Pin D3*/
      #define DIR1_OUT B00000000 //D1 is an output
      #define DIR1_IN  B001ßß000 //D1 is an input (default)
      /*DIR0 - Digital I/O Direction for Pin D3*/
      #define DIR0_OUT B00000000 //D0/CLKOUT is an output
      #define DIR0_IN  B00010000 //D0/CLKOUT is an input (default)
      /*DIO3:0 Status of Digital I/O, Read Only*/
      
      
      /* SPI COMMAND DEFINITIONS (p34) */
      /*SYSTEM CONTROL */
      #define   WAKEUP  0x00  //Exit Sleep Mode
      #define   STANDBY 0xFD  //Enter Sleep Mode
      #define   SYNC    0xFC  //Synchornize the A/D Conversion
      #define   RESET   0xFE  //Reset To Power UP values
      #define   NOP     0xFF  //No operation
      /*DATA READ*/
      #define   RDATA   0x01  //Read data once
      #define   RDATAC  0x03  //Read data continously
      #define   SDATAC  0x0F  //Stop reading data continously
      /*READ REGISTER */
      #define   RREG    0x10  //Read From Register
      #define   WREG    0x50  //Write To Register
      /*Calibration */
      #define   SYSOCAL 0xF3  //System Offset Calibration
      #define   SYSGCAL 0xF2  //System Gain Calibration
      #define   SELFCAL 0xF0  //Self offset then gain calibration
      #define   SELFOCAL 0xF1 //Self offset calibration
      #define   SELFGCAL 0xF2 //Self gain calibration
      
      
      #define cs 10 // chip select
      #define rdy 9 // data ready, input
      #define rst 8 // may omit
      
      #define SPISPEED 1250000
                    // 1700000
      
      
      #include <SPI.h>
      
      byte i;
      
      void setup()
      {
        Serial.begin(9600);
        
        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(RESET);
        //delayMicroseconds(100);
        
      //-------------------------------
      // Status setup
        //PGA SETTING
        Serial.println("Status Data setup started");
        byte status_data = 0x01; //status: Most Significant Bit First, Auto-Calibration Disabled, Analog Input Buffer Disabled 
        SPI.transfer(0x50 | STATUS);
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        SPI.transfer(status_data);   // write the databyte to the register
        Serial.println("Status Data setup complete");
        delayMicroseconds(10);
      //-------------------------------
        
      //-------------------------------
      // ADCON setup
        //byte adcon_data = 0x00; // 0 00 00 000
        Serial.println("ADCON Data setup started");
        SPI.transfer(0x50 | ADCON);
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        SPI.transfer(PGA_1);   // write the databyte to the register
        Serial.println("ADCON Data setup ended");
        delayMicroseconds(10);
      //--------------------------------
      
      //--------------------------------
      // Data Rate setup
        Serial.println("DRATE Data setup started");
        SPI.transfer(0x50 | DRATE);
        SPI.transfer(0x00);
        SPI.transfer(DR_10);
        Serial.println("DRATE Data setup complete");
        delayMicroseconds(10);
      //--------------------------------
      
      //--------------------------------
      //Self-calibration full
        Serial.println("Full Self-cal setup started");
        SPI.transfer(SELFCAL);
        delayMicroseconds(1300);
        Serial.println("Full Self-cal setup complete");
      //--------------------------------
      
        digitalWrite(cs, HIGH);
        
        Serial.println("configured, starting");
      }
      
      
      void Self_cal_full()
      {
        Serial.println("Full Self-cal setup started");
        SPI.transfer(SELFCAL);
        delayMicroseconds(1300);
        Serial.println("Full Self-cal setup complete");
      }
      
      void Self_cal_offset()
      {
        Serial.println("Self offset calibration setup started");
        SPI.transfer(SELFOCAL);
        delayMicroseconds(1300);
        Serial.println("Self offset calibration setup complete");
      }
      
      void Self_cal_gain()
      {
        Serial.println("Self gain calibration setup started");
        SPI.transfer(SELFGCAL);
        delayMicroseconds(1300);
        Serial.println("Self gain calibration setup complete");
      }
      
      void ReadReg_STATUS()
      {
        byte reg_val;
        digitalWrite(cs, LOW);
        SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
        delayMicroseconds(10);
      
        while (digitalRead(rdy)) {} ;
      
        SPI.transfer(0x10);
        delayMicroseconds(10);
      
        //SYNC command 1111 1100
        SPI.transfer(SYNC);
        delayMicroseconds(10);
        
        //WAKEUP 0000 0000
        SPI.transfer(WAKEUP);
        delayMicroseconds(10);
      
        reg_val = SPI.transfer(0);
        reg_val <<= 8; //shift to left
        reg_val |= SPI.transfer(0);
        reg_val <<= 8;
        reg_val |= SPI.transfer(0);
        Serial.print("HEX: "); Serial.println(reg_val,HEX);
        Serial.print("BIN: "); Serial.println(reg_val,BIN);
      
        delayMicroseconds(10);
        
        digitalWrite(cs, HIGH);
        SPI.endTransaction();
      }
      
      void AIN_0_COM()
      {
        unsigned long adc_val =0; // store reading
      
        digitalWrite(cs, LOW);
        SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
        delayMicroseconds(10);
        //The most efficient way to cycle through the inputs is to
        //change the multiplexer setting (using a WREG command
        //to the multiplexer register MUX) immediately after DRDY
        //goes low. Then, after changing the multiplexer, restart the
        //conversion process by issuing the SYNC and WAKEUP
        //commands, and retrieve the data with the RDATA
        //command. 
        while (digitalRead(rdy)) {} ;
      
        //byte channel = 0;
        //byte data = (channel << 4) | (1 << 3); //AIN-channel and AINCOM
        //byte data = 1;
        SPI.transfer(0x50 | 1); // MUX register (0101 rrrr)
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        //SPI.transfer(AIN0_COM);   // write the databyte to the register
        SPI.transfer(P_AIN0 + N_AINCOM);
        delayMicroseconds(10);
      
        //SYNC command 1111 1100
        SPI.transfer(SYNC);
        delayMicroseconds(10);
        
        //WAKEUP 0000 0000
        SPI.transfer(WAKEUP);
        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("AIN0_COM: "); Serial.print(adc_val*PGAmulti_1,5); Serial.println(" Volt");
        //Serial.println(adc_val*PGAmulti_1*1000,2);
      }
      
      void AIN_1_COM()
      {
        unsigned long adc_val =0; // store reading
      
        digitalWrite(cs, LOW);
        SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
        delayMicroseconds(10);
        //The most efficient way to cycle through the inputs is to
        //change the multiplexer setting (using a WREG command
        //to the multiplexer register MUX) immediately after DRDY
        //goes low. Then, after changing the multiplexer, restart the
        //conversion process by issuing the SYNC and WAKEUP
        //commands, and retrieve the data with the RDATA
        //command. 
        while (digitalRead(rdy)) {} ;
      
        //byte channel = 0;
        //byte data = (channel << 4) | (1 << 3); //AIN-channel and AINCOM
        //byte data = 1;
        SPI.transfer(0x50 | 1); // MUX register
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        //SPI.transfer(AIN0_COM);   // write the databyte to the register
        SPI.transfer(P_AIN1 + N_AINCOM);
        delayMicroseconds(10);
      
        //SYNC command 1111 1100
        SPI.transfer(SYNC);
        delayMicroseconds(10);
        
        //WAKEUP 0000 0000
        SPI.transfer(WAKEUP);
        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("AIN1_COM: "); Serial.print(adc_val*PGAmulti_1,5); Serial.println(" Volt");
      }
      
      void DIFFdata0_1()
      {
        unsigned long adc_val =0; // store reading
      
        digitalWrite(cs, LOW);
        SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
        delayMicroseconds(10);
        //The most efficient way to cycle through the inputs is to
        //change the multiplexer setting (using a WREG command
        //to the multiplexer register MUX) immediately after DRDY
        //goes low. Then, after changing the multiplexer, restart the
        //conversion process by issuing the SYNC and WAKEUP
        //commands, and retrieve the data with the RDATA
        //command. 
        while (digitalRead(rdy)) {} ;
      
        //byte channel = 0;
        //byte data = (channel << 4) | (1 << 3); //AIN-channel and AINCOM
        //byte data = 1;
        SPI.transfer(0x50 | 1); // MUX register
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        SPI.transfer(DIFF0_1);   // write the databyte to the register
        delayMicroseconds(10);
      
        //SYNC command 1111 1100
        SPI.transfer(SYNC);
        delayMicroseconds(10);
        
        //WAKEUP 0000 0000
        SPI.transfer(WAKEUP);
        delayMicroseconds(10);
          
        SPI.transfer(RDATA); // 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);
        Serial.println(adc_val, HEX);
        
        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("DIFF0_1: "); Serial.print(adc_val*PGAmulti_1,5); Serial.println(" Volt");
      }
      
      void DIFFdata2_3()
      {
        unsigned long adc_val =0; // store reading
      
        digitalWrite(cs, LOW);
        SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
        delayMicroseconds(10);
        //The most efficient way to cycle through the inputs is to
        //change the multiplexer setting (using a WREG command
        //to the multiplexer register MUX) immediately after DRDY
        //goes low. Then, after changing the multiplexer, restart the
        //conversion process by issuing the SYNC and WAKEUP
        //commands, and retrieve the data with the RDATA
        //command. 
        while (digitalRead(rdy)) {} ;
      
        SPI.transfer(0x50 | 1); // MUX register
        SPI.transfer(0x00);   // 2nd command byte, write one register only
        SPI.transfer(DIFF2_3);   // write the databyte to the register
        delayMicroseconds(10);
      
        //SYNC command 1111 1100
        SPI.transfer(SYNC);
        delayMicroseconds(10);
        
        //WAKEUP 0000 0000
        SPI.transfer(WAKEUP);
        delayMicroseconds(10);
          
        SPI.transfer(RDATA); // 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();
        Serial.println(adc_val);
      
        //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("DIFF2_3: "); Serial.print(adc_val*PGAmulti_1,5); Serial.println(" Volt");
      }
      
      void loop()
      {
        
      //  while (Serial.available() > 0)
      //  {   
        //AIN_0_COM();
        DIFFdata0_1();
        //delay(100);
        //ReadReg_STATUS();
        delay(200);
        digitalWrite(cs, HIGH);
      //  }
        
      }
      
      
      /*  ------  NOTES ------
      Buffer OFF  AGND - 0.1v to AVDD + 0.1v
      Buffer ON AGND      to AVDD - 2.0v
      
      
      Gain error 1, buffer ON   +/-0.005%
      Gain error 64, buffer ON  +/-0.03%
      
      
      Gain drift 1  +/- 100nV/C
      Gain drift 64 +/- 4nV/C
      
      
      DVDD = 3.3v
      AVDD = 5.0v
      
      
      The PGA is controlled by the ADCON register.
      Recalibrating the A/D converter after changing the PGA
      setting is recommended
      
      
      ---Calibration Notes---
      
      OFC mode = -8,388,608 to 8,388,607
      FSC mode = 0 to 16,777,215
      
      Perform self-calibration after power-up and reset.
      Takes up to 1,2sec to perform a self-cal.
      
      
      SELFOCAL = Self offset calibration    Time = 0,3 to 800,3ms
      SELFGCAL = Self gain calibration    Time = 0,4 to 827,0ms
      SELFCAL  = Self offset then gain calibration  Time = 0,5 to 1227,2ms
      
      
      ...Perform SELFCAL...
      Command = F0h = 1111 0000
      Address = 09h
      
      ---System Calibration---
      
      SYSOCAL  = System offset calibration - supply zero input diff signal.
      SYSGCAL  = System gain calibration   - Supply full-scale input signal.
      
      ---Auto-Calibration---
      
      Performed everytime data rate, PGA setting or Buffer data changes(WREG). 
      */
      

       

      Here is my starting JavaScript code:

      STATUS = 0x00; //Status Control Register 0
      MUX    = 0x01; //Multiplexer Control Register 0
      ADCON  = 0x02; //A/D Control Register 0
      DRATE  = 0x03; //A/D Data Rate Control Register 0
      IO     = 0X04; //GPIO Control Register 0
      OFC0   = 0x05; //Offset Calibration Coefficient Register 1
      OFC1   = 0x06; //Offset Calibration Coefficient Register 2
      OFC2   = 0x07; //Offset Calibration Coefficient Register 2
      FSC0   = 0x08; //Full scale Callibration Coefficient Register 0
      FSC1   = 0x09; //Full scale Callibration Coefficient Register 1
      FSC2   = 0x0A; //Full scale Callibration Coefficient Register 2
      
      
      /*STATUS - Status Control Register 0 ( see p30)*/
      /* BIT7 - BIT6 -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* ID   - ID   -  ID     -  ID     -  ORDER  -  ACAL   -  BUFEN  -  DRDY */
      STATUS_RESET = 0x01; // Reset STATUS Register
      /*Bits 7 - 4 ID3, ID2, ID1, ID0 Factory Programmed Identification Bits(Read Only)*/
      /*ORDER1:0  Data Output Bit Order*/
      ORDER_MSB = 'B00000000'; // Most significant Bit first (default)
      ORDER_LSB = 'B00001000'; // Least significant Bit first
      /*Input data is always shifted in most significant byte and bit first. Output data is always shifted out most significant
      byte first. The ORDER bit only controls the bit order of the output data within the byte.*/
      /*ACAL1:0 Auto Calibration*/
      ACAL_OFF = 'B00000000'; // Auto Calibration Disabled (default)
      ACAL_ON  = 'B00000100'; // Auto Calibration Enabled
      /*When Auto-Calibration is enabled, self-calibration begins at the completion of the WREG command that changes
      the PGA (bits 0-2 of ADCON register), DR (bits 7-0 in the DRATE register) or BUFEN (bit 1 in the STATUS register)
      values.*/
      /*BUFEN1:0 Analog Input Buffer Enable*/
      BUFEN_OFF = 'B00000000'; //Buffer Disabled (default)
      BUFEN_ON  = 'B00000010'; //Buffer Enabled
      /*DRDY1:0 Data Ready (Read Only) Duplicates the state of the DRDY pin*/
      
      
      /* MUX - Multiplexer Control Register 0 (see p31 - bring together with bitwise OR | */
      /* BIT7  - BIT6  -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* PSEL3 - PSEL2 -  PSEL1  -  PSEL0  -  NSEL3  -  NSEL2   - NSEL1   - NSEL0 */
      MUX_RESET = 0x01;      // Reset MUX0 Register
      /* PSEL3:0 Positive input channel selection bits */
      P_AIN0   = 'B00000000'; //(default)
      P_AIN1   = 'B00010000';
      P_AIN2   = 'B00100000';
      P_AIN3   = 'B00110000';
      P_AIN4   = 'B01000000';
      P_AIN5   = 'B01010000';
      P_AIN6   = 'B01100000';
      P_AIN7   = 'B01110000';
      P_AINCOM = 'B10000000';
      /* NSEL3:0 Negativ input channel selection bits */
      N_AIN0   = 'B00000000';
      N_AIN1   = 'B00000001'; //(default)
      N_AIN2   = 'B00000010';
      N_AIN3   = 'B00000011';
      N_AIN4   = 'B00000100';
      N_AIN5   = 'B00000101';
      N_AIN6   = 'B00000110';
      N_AIN7   = 'B00000111';
      N_AINCOM = 'B00001000';
      
      
      // Differential settings
      DIFF0_1  = 'B00000001';
      DIFF2_3  = 'B00100011';
      DIFF4_5  = 'B01000101';
      DIFF6_7  = 'B01100111';
      
      
      /*ADCON - A/D Control Register 0 ( see p31)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* 0    - CLK1   -  CLK0   -  SDCS1  -  SDCS0  -  PGA2   -  PGA1   -  PAG0 */
      ADCON_RESET = 0x20; // Reset ADCON Register
      /*CLK2:0 D0/CLKOUT Clock Out Rate Setting*/
      CLK_OFF     = 'B00000000'; //Clock Out off
      CLK_1       = 'B00100000'; //Clock Out Frequency = fCLKIN (default)
      CLK_2       = 'B01000000'; //Clock Out Frequency = fCLKIN/2
      CLK_4       = 'B01100000'; //Clock Out Frequency = fCLKIN/4
      /*When not using CLKOUT, it is recommended that it be turned off. These bits can only be reset using the RESET pin.*/
      /*SDCS2:0 Sensor Detection Current Sources*/
      SDCS_OFF    = 'B00000000'; //Sensor Detect Off (default)
      SDCS_05     = 'B00001000'; //Sensor Detect Current 0.5?A
      SDCS_2      = 'B00010000'; //Sensor Detect Current 2?A
      SDCS_10     = 'B00011000'; //Sensor Detect Current 10?A
      /*The Sensor Detect Current Sources can be activated to verify the integrity of an external sensor supplying a signal to the
      ADS1255/6. A shorted sensor produces a very small signal while an open-circuit sensor produces a very large signal.*/
      /*PGA3:0 Programmable Gain Amplifier Setting*/
      PGA_1       = 'B00000000'; //(default)
      PGA_2       = 'B00000001';
      PGA_4       = 'B00000010';
      PGA_8       = 'B00000011';
      PGA_16      = 'B00000100';
      PGA_32      = 'B00000101';
      PGA_64      = 'B00000110';
      
      
      PGAmulti_1  =  0.0000005960464832810452;   //PGA-1  ±5V        /16777215
      PGAmulti_2  =  0.0000002980232416405226;  //PGA-2  ±2.5V      /16777215
      PGAmulti_4  =  0.0000001490116208202613;   //PGA-4  ±1.25V     /16777215
      PGAmulti_8  =  0.00000007450581041013064;  //PGA-8  ±0.625V    /16777215
      PGAmulti_16 =  0.00000003725290520506532;  //PGA-16 ±312.5mV   /16777215
      PGAmulti_32 =  0.00000001862645260253266;  //PGA-32 ±156.25mV  /16777215
      PGAmulti_64 =  0.000000009313226301266331; //PGA-64 ±78.125mV  /16777215
      
      
      /*DRATE - A/D Data Rate Register 0 ( see p32)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* DR7  - DR6    -  DR5    -  DR4    -  DR3    -  DR2    -  DR1    -  DR0 */
      DRATE_RESET = 0xF0; // Reset DRATE Register
      /*DR7:0 Data Rate Setting*/
      DR_30000    = 'B11110000'; //30.000 SPS (default)
      DR_15000    = 'B11100000'; //15.000 SPS
      DR_7500     = 'B11010000'; //7.500 SPS
      DR_3750     = 'B11000000'; //3.750 SPS
      DR_2000     = 'B10110000'; //2.000 SPS
      DR_1000     = 'B10100001'; //1.000 SPS
      DR_500      = 'B10010010'; //500 SPS
      DR_100      = 'B10000010'; //100 SPS
      DR_60       = 'B01110010'; //60 SPS
      DR_50       = 'B01100011'; //50 SPS
      DR_30       = 'B01010011'; //30 SPS
      DR_25       = 'B01000011'; //25 SPS
      DR_15       = 'B00110011'; //15 SPS
      DR_10       = 'B00100011'; //10 SPS
      DR_5        = 'B00010011'; //5 SPS
      DR2_5       = 'B00000011'; //2,5 SPS
      
      
      /*IO - GPIO Control Register 0 ( see p32)*/
      /* BIT7 - BIT6   -  BIT5   -  BIT4   -  BIT3   -  BIT2   -  BIT1   -  BIT0 */
      /* DIR3 - DIR2   -  DIR1   -  DIR0   -  DIO3   -  DIO2   -  DIO1   -  DIO0 */
      IO_RESET    = 0xE0; // Reset IO Register
      /*DIR3 - Digital I/O Direction for Pin D3*/
      DIR3_OUT    = 'B00000000'; //D3 is an output
      DIR_IN      = 'B10000000'; //D3 is an input (default)
      /*DIR2 - Digital I/O Direction for Pin D3*/
      DIR2_OUT    = 'B00000000'; //D2 is an output
      DIR2_IN     = 'B01000000'; //D2 is an input (default)
      /*DIR1 - Digital I/O Direction for Pin D3*/
      DIR1_OUT    = 'B00000000'; //D1 is an output
      DIR1_IN     = 'B001ßß000'; //D1 is an input (default)
      /*DIR0 - Digital I/O Direction for Pin D3*/
      DIR0_OUT    = 'B00000000'; //D0/CLKOUT is an output
      DIR0_IN     = 'B00010000'; //D0/CLKOUT is an input (default)
      /*DIO3:0 Status of Digital I/O, Read Only*/
      
      
      /* SPI COMMAND DEFINITIONS (p34) */
      /*SYSTEM CONTROL */
      WAKEUP      = 0x00;  //Exit Sleep Mode
      STANDBY     = 0xFD;  //Enter Sleep Mode
      SYNC        = 0xFC;  //Synchornize the A/D Conversion
      RESET       = 0xFE;  //Reset To Power UP values
      NOP         = 0xFF;  //No operation
      /*DATA READ*/
      RDATA       = 0x01;  //Read data once
      RDATAC      = 0x03;  //Read data continously
      SDATAC      = 0x0F;  //Stop reading data continously
      /*READ REGISTER */
      RREG        = 0x10;  //Read From Register
      WREG        = 0x50;  //Write To Register
      /*Calibration */
      SYSOCAL     = 0xF3;         //System Offset Calibration
      SYSGCAL     = 0xF2;         //System Gain Calibration
      SELFCAL     = 0xF0;         //Self offset then gain calibration
      SELFOCAL    = 0xF1;        //Self offset calibration
      SELFGCAL    = 0xF2;        //Self gain calibration
      
      
      function delay(milliseconds) 
      {
          var startTime = Date.now();
          while (Date.now() - startTime < milliseconds);
      }
      
      
      var m = require('mraa'); //require mraa
      
      
      // helper function to go from hex val to dec
      function char(x) { 
        return parseInt(x, 16); 
      }
      
      var x = new m.Spi(0);
      var buf = new Buffer(4);
      buf[0] = char('0xf4');
      buf[1] = char('0x2e');
      buf[2] = char('0x3e');
      buf[3] = char('0x4e');
      var buf2 = x.write(buf);
      console.log("Sent: " + buf.toString('hex') + "  Received: " + buf2.toString('hex'));
      
        • 1. Re: ADS1256 JavaScript
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi KimLorentz,

          What problems do you have specifically with your code, do you see any error messages? How are you compiling the code?

          -Sergio
           

          • 2. Re: ADS1256 JavaScript
            KimLorentz

            dont have any errors, the question is how to send command over SPI and how to read from the ADS1256.

             

             

            This is the code found on Intels demo codes,

             

             

            Demo from Intel XDK IoT

             var m = require('mraa'); //require mraa 
              
            // helper function to go from hex val to dec  
            function char {  
              return parseInt(x, 16);  
            }  
              
            var x = new m.Spi(0);  
            var buf = new Buffer(4);  
            buf[0] = char('0xf4');  
            buf[1] = char('0x2e');  
            buf[2] = char('0x3e');  
            buf[3] = char('0x4e');  
            var buf2 = x.write(buf);  
            console.log("Sent: " + buf.toString('hex') + "  Received: " + buf2.toString('hex'));
            

             

             

             

            I dont know how to change it to what the arduino code version does.

            how do I do that?

             

            For exemple like this

            //////////////////////----------------...Start-up command "run ones" start...----------------//////////////////////
            
            // Set SPI CS to LOW
            // Set SPI speed to: 1250000
            // Set SPI mode to: MODE1
            // Set SPI Bite to: MSBFIRST
                   
            //PGA SETTING  
              Serial.println("Status Data setup started");  
              byte status_data = 0x01; //status: Most Significant Bit First, Auto-Calibration Disabled, Analog Input Buffer Disabled   
              SPI.transfer(0x50 | STATUS);  
              SPI.transfer(0x00);   // 2nd command byte, write one register only  
              SPI.transfer(status_data);   // write the databyte to the register  
              Serial.println("Status Data setup complete");  
              delayMicroseconds(10);  
            //-------------------------------  
                
            //-------------------------------  
            // ADCON setup   
              Serial.println("ADCON Data setup started");  
              SPI.transfer(0x50 | ADCON);  
              SPI.transfer(0x00);   // 2nd command byte, write one register only  
              SPI.transfer(PGA_1);   // write the databyte to the register  
              Serial.println("ADCON Data setup ended");  
              delayMicroseconds(10);  
            //--------------------------------  
              
            //--------------------------------  
            // Data Rate setup  
              Serial.println("DRATE Data setup started");  
              SPI.transfer(0x50 | DRATE);  
              SPI.transfer(0x00);  
              SPI.transfer(DR_10);  
              Serial.println("DRATE Data setup complete");  
              delayMicroseconds(10);  
            //--------------------------------  
              
            //--------------------------------  
            //Self-calibration full  
              Serial.println("Full Self-cal setup started");  
              SPI.transfer(SELFCAL);  
              delayMicroseconds(1300);  
              Serial.println("Full Self-cal setup complete");  
            //--------------------------------  
              
            //Set SPI CS to HIGH  
                
              Serial.println("configured, starting");  
            }  
            
            //////////////////////----------------...Start-up command "run ones" end...----------------//////////////////////
            
            
            
            //////////////////////----------------...Command "run every 100ms" start...----------------//////////////////////
            
            
            void DIFFdata0_1()  
            {  
              unsigned long adc_val =0; // store reading  
              
              digitalWrite(cs, LOW);  
              SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI  
              delayMicroseconds(10);  
              //The most efficient way to cycle through the inputs is to  
              //change the multiplexer setting (using a WREG command  
              //to the multiplexer register MUX) immediately after DRDY  
              //goes low. Then, after changing the multiplexer, restart the  
              //conversion process by issuing the SYNC and WAKEUP  
              //commands, and retrieve the data with the RDATA  
              //command.   
              while (digitalRead(rdy)) {} ;  
              
              //byte channel = 0;  
              //byte data = (channel << 4) | (1 << 3); //AIN-channel and AINCOM  
              //byte data = 1;  
              SPI.transfer(0x50 | 1); // MUX register  
              SPI.transfer(0x00);   // 2nd command byte, write one register only  
              SPI.transfer(DIFF0_1);   // write the databyte to the register  
              delayMicroseconds(10);  
              
              //SYNC command 1111 1100  
              SPI.transfer(SYNC);  
              delayMicroseconds(10);  
                
              //WAKEUP 0000 0000  
              SPI.transfer(WAKEUP);  
              delayMicroseconds(10);  
                  
              SPI.transfer(RDATA); // 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);  
              Serial.println(adc_val, HEX);  
                
              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("DIFF0_1: "); Serial.print(adc_val*PGAmulti_1,5); Serial.println(" Volt");  
            }  
            
            //////////////////////----------------...Command "run every 100ms" end...----------------//////////////////////
            
            • 3. Re: ADS1256 JavaScript
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              The functions used to read and write are described in the node documentation. A very useful document that has node.js templates is https://software.intel.com/en-us/node-js-templates-for-intel-xdk-iot-edition .

              There are no sample codes for this code specifically, but if you want to change this Arduino code to Javascript then the best approach would be to use the MRAA API for Javascript. http://iotdk.intel.com/docs/master/mraa/node/ . There you can see the functions you can use, so based on the Arduino functions, look for the appropriate Javascript function and begin building your code from there.

              -Sergio
               

              • 4. Re: ADS1256 JavaScript
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Do you still need help with this thread, were you able to change your code?

                -Sergio
                 

                • 5. Re: ADS1256 JavaScript
                  KimLorentz

                  Was away on course so have not been doing any coding this week.

                  But still not able to understand how the coding works and have not been able to make it work.

                  • 6. Re: ADS1256 JavaScript
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation

                    Keep us updated on your progress. If you have any specific questions about the MRAA API don’t hesitate to contact us.

                    -Sergio