Im reading data off a ADC (ADS1256).
This results in the following data, with (hardware) ch0 connected to 3.3V, ch1 is on GND and the rest is floating:
ch0: 90702 / ch1: 5538205 / ch2: 12131 / ch3: 75446 / ch4: 77493 / ch5: 78029 / ch6: 77114 / ch7: 82250
So for some reason ch7 gets displayed as ch0, ch0 displayed as ch1 etc.
for (i = 0; i < 8; i ++) //Read 8 channels from ads1256
{
adc_val = 0;
while (digitalRead(rdy)) {} ;
SPI.beginTransaction(SPISettings(1250000, MSBFIRST, SPI_MODE1)); // start SPI
delayMicroseconds(10);
// writereg(mux_reg, i << 4 | i + 1 ); // differential input
writereg(mux_reg, i << 4 | 1 << 3 ); // not differential (p.31)
while (digitalRead(rdy)) {} ;
SPI.transfer(1); // command for: READ DATA (p.34)
delayMicroseconds(10); // read three bytes (adc-value)
adc_val = SPI.transfer(0) << 8;
adc_val |= SPI.transfer(0);
adc_val <<= 8;
adc_val |= SPI.transfer(0);
delayMicroseconds(10);
SPI.endTransaction();
Serial.print("ch");
Serial.print(i);
Serial.print(": ");
Serial.print(adc_val);
Serial.print(" / ");
delay(10);
}
Serial.println();
However, if I simply fix "i" to 0 in the register:
writereg(mux_reg, 0 << 4 | 1 << 3 );
It will be read correctly as ch.0
Or I can set for (i = 0; i < 1; i ++)
And ch0 will be correct... but i want the other channels too...
Or I can set for (i = 0; i < 2; i ++)
And ch0 will be displayed as ch1 and ch1 will be displayed as ch0. So the shift starts as soon as there is more than 1 channel.
Since I print the current "i" to get the channelnumber itself... and that obvisously is zero at the first reading... why am i not getting that data on ch0 but shifted to the right as soon as I set "i < 2" or more then 2
Complete 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
Analog ch0 (input 0,1)
Analog ch1 (input 2,3)
Analog ch2 (input 4,5)
(Analog ch3 (input 6,7))
*/
#define cs 10 // chip select
#define rdy 9 // data ready, input
#define rst 8 // may omit
// ** asd1256 Register map and data to write **
#define status_reg 0 // address (datasheet p. 30)
//#define status_data 0x05 // = 101 = Auto-Cal, Buffer off, DRDY
#define status_data 0x07 // = 111 = Auto-Cal, Buffer on, DRDY
#define mux_reg 1
// mux_data to be changed while running, to select input ports
#define adcon_reg 2
#define adcon_data 0x20 // 0 01 00 100 => clk_out(default), sensor detect OFF, gain 1 (p.31)
#define drate_reg 3
#define drate_data 0x23 // => 10 SPS (samples/secons)
#include <SPI.h>
byte i; // general use
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);
// now set working parameters
writereg(status_reg, status_data); // parameters defined on top
writereg(adcon_reg, adcon_data);
writereg(drate_reg, drate_data);
}
void writereg(byte reg_num, byte data) // write to a single register
{
while (digitalRead(rdy)) {} // wait for ready_line to go low
SPI.beginTransaction(SPISettings(1250000, MSBFIRST, SPI_MODE1)); // start SPI
// delayMicroseconds(10);
// SPI.transfer(0xFE);
delayMicroseconds(100);
SPI.transfer(0x50 | reg_num); // dataseheet p.34
SPI.transfer(0x00); // 2nd command byte
SPI.transfer(data); // write the databyte to the register
delayMicroseconds(10);
SPI.endTransaction();
}
void loop()
{
long adc_val; // store reading
for (i = 0; i < 8; i ++) //Read 8? channels from ads1256
{
adc_val = 0;
while (digitalRead(rdy)) {} ;
SPI.beginTransaction(SPISettings(1250000, MSBFIRST, SPI_MODE1)); // start SPI
delayMicroseconds(10);
// writereg(mux_reg, i << 4 | i + 1 ); // positive to analog 0,2,4,6, Neg to 1,3,5,7
writereg(mux_reg, i << 4 | 1 << 3 ); // 1 << 3 == 1000 -> not differential
while (digitalRead(rdy)) {} ;
SPI.transfer(1); // command for: READ DATA (p.34)
delayMicroseconds(10); // read three bytes (adc-value)
adc_val = SPI.transfer(0) << 8;
adc_val |= SPI.transfer(0);
adc_val <<= 8;
adc_val |= SPI.transfer(0);
delayMicroseconds(10);
SPI.endTransaction();
Serial.print("ch");
Serial.print(i);
Serial.print(": ");
Serial.print(adc_val);
Serial.print(" / ");
delay(10);
}
Serial.println();
}