#include #include #include #define mosi 23 #define miso 19 #define sclk 18 #define cs 5 #define rst 17 #define rdy 16 #define SPISPEED 1920000 // 1.92MHz std.@ 7.68/4 MHz const float Vref = 2.485; void sendAds () { unsigned long adc_val[4] = {0,0,0,0}; byte mux[4] = {0x01,0x23,0x45,0x67}; int i = 0; digitalWrite(cs, LOW); delayMicroseconds(10); //while (digitalRead(rdy)) {} //SPI.transfer(0xFE); for (i=0; i <= 3; 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(2); while (digitalRead(rdy)) {} ; 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); } digitalWrite(cs, HIGH); SPI.endTransaction(); for (i=0; i <= 3; i++) { if(adc_val[i] > 0x7fffff) { adc_val[i] = 16777216-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 ; Blynk.virtualWrite(V60, FV0); Blynk.virtualWrite(V61, FV1); Blynk.virtualWrite(V62, FV2); Blynk.virtualWrite(V63, FV3); } void setup() { Serial.begin(115200); pinMode(cs, OUTPUT); pinMode(rdy, INPUT); pinMode(rst, OUTPUT); digitalWrite(cs, LOW); 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); while (digitalRead(rdy)) {} SPI.transfer(0xFE); delay(1); while (digitalRead(rdy)) {} byte status_reg = 0x00 ; byte status_data = 0x05; SPI.transfer(0x50 | status_reg); SPI.transfer(0x00); SPI.transfer(status_data); delayMicroseconds(100); while (digitalRead(rdy)) {} byte adcon_reg = 0x02; byte adcon_data = 0x02; SPI.transfer(0x50 | adcon_reg); SPI.transfer(0x00); SPI.transfer(adcon_data); delayMicroseconds(100); while (digitalRead(rdy)) {} byte drate_reg = 0x03; byte drate_data = 0xF0; SPI.transfer(0x50 | drate_reg); SPI.transfer(0x00); SPI.transfer(drate_data); delayMicroseconds(100); while (digitalRead(rdy)) {} 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"); terminal.flush(); terminal.clear(); timer.setInterval(1000L, sendAds); timer.setInterval(1000L, sendWifi); } void loop() { timer.run(); }