Community
cancel
Showing results for 
Search instead for 
Did you mean: 
KSels
New Contributor II
1,011 Views

ADS1256 JavaScript

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 //...

Tags (1)
0 Kudos
6 Replies
idata
Community Manager
84 Views

Hi KimLorentz,

 

 

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

 

 

-Sergio

 

KSels
New Contributor II
84 Views

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...----------------//////////////////////

idata
Community Manager
84 Views

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 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/ 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

 

idata
Community Manager
84 Views

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

 

 

-Sergio

 

KSels
New Contributor II
84 Views

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.

idata
Community Manager
84 Views

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

 

 

-Sergio

 

Reply