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);
}