#include #include #include #define mosi 23 #define miso 19 #define sclk 18 #define cs 5 #define rst 17 #define rdy 16 #define SPISPEED 1000000 // 1.92MHz std.@ 7.68/4 MHz const float Vref = 2.485; void sendAds () { unsigned long adc_val[8] = {0,0,0,0,0,0,0,0}; byte mux[8] = {0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78}; int i = 0; SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); digitalWrite(cs, LOW); delayMicroseconds(1); for (i=0; i <= 7; i++) { byte channel = mux[i]; while (digitalRead(rdy)) {} ; SPI.transfer(0x50 | 0x01); SPI.transfer(0x00); SPI.transfer(channel); delayMicroseconds(2); SPI.transfer(0xFC); delayMicroseconds(2); SPI.transfer(0x00); delayMicroseconds(250); SPI.transfer(0x01); delayMicroseconds(7); adc_val[i] = SPI.transfer(0); adc_val[i] <<= 8; adc_val[i] |= SPI.transfer(0); adc_val[i] <<= 8; adc_val[i] |= SPI.transfer(0); delayMicroseconds(1); } digitalWrite(cs, HIGH); SPI.endTransaction(); for (i=0; i <= 7; i++) { if(adc_val[i] > 0x7fffff) { adc_val[i] = 16777216ul-adc_val[i]; } } double FV0 = adc_val[0] * 2 * Vref / 8388607 ; double FV1 = adc_val[1] * 2 * Vref / 8388607 ; double FV2 = adc_val[2] * 2 * Vref / 8388607 ; double FV3 = adc_val[3] * 2 * Vref / 8388607 ; double FV4 = adc_val[4] * 2 * Vref / 8388607 ; double FV5 = adc_val[5] * 2 * Vref / 8388607 ; double FV6 = adc_val[6] * 2 * Vref / 8388607 ; double FV7 = adc_val[7] * 2 * Vref / 8388607 ; delay(1); } void setup() { Serial.begin(115200); delay(100); pinMode(cs, OUTPUT); digitalWrite(cs, LOW); pinMode(rdy, INPUT); pinMode(rst, OUTPUT); digitalWrite(rst, LOW); delay(1); digitalWrite(rst, HIGH); delay(500); SPI.begin(); delay(500); digitalWrite(cs, LOW); while (digitalRead(rdy)) {} SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); digitalWrite(cs, LOW); delayMicroseconds(10); SPI.transfer(0xFE); delay(10); byte status_reg = 0x00 ; byte status_data = 0x07; SPI.transfer(0x50 | status_reg); SPI.transfer(0x00); SPI.transfer(status_data); delayMicroseconds(10); byte adcon_reg = 0x02; byte adcon_data = 0x00; SPI.transfer(0x50 | adcon_reg); SPI.transfer(0x00); SPI.transfer(adcon_data); delayMicroseconds(10); byte drate_reg = 0x03; byte drate_data = 0xF0; SPI.transfer(0x50 | drate_reg); SPI.transfer(0x00); SPI.transfer(drate_data); delayMicroseconds(10); SPI.transfer(0xF0); delay(400); digitalWrite(cs, HIGH); SPI.endTransaction(); while (!Serial && (millis () <= 5000)); Serial.println("configured, starting"); Serial.println(""); Serial.println("A0,A1,A2,A3,A4,A5,A6,A7"); terminal.flush(); terminal.clear(); timer.setInterval(1000L, sendAds); } void loop() { timer.run(); }