![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ADC logging with demo drive board for calibration
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp
- Committer:
- sophiemeredith
- Date:
- 2019-06-28
- Revision:
- 13:0ec8fa0a0429
- Parent:
- 12:3f1df385d781
- Child:
- 14:d764e256ac6d
File content as of revision 13:0ec8fa0a0429:
#include "mbed.h" #define CH_A 1 // value of convst bus to read channel A only #define CH_AC 5 // value of convst bus to read channels A and C #define CH_ABCD 15 // value of convst bus to read all chanels simultaneously #define MEAS_DELAY 1000 // measurement delay after turning on FET (us) (themocycling control programme uses 50 us) #define LOG_INTERVAL 5000 // log file interval (ms) #define N_STEPS 1000 #define CAM_TRIG 20 // camera trigger pulse width (us) Serial pc(USBTX, USBRX); // tx, rx DigitalOut drive(p21); DigitalOut yLED(p27); DigitalOut gLED(p28); DigitalOut rLED(p26); DigitalOut camTrig(p24); DigitalOut mbedIO(p25); // MBED IO AnalogIn battVolt(p19); AnalogIn auxVolt(p20); BusOut convt(p11, p12, p13, p14); SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); // chip select DigitalIn busy(p9); DigitalOut reset(p10); DigitalIn userButton(p16); Timer timer; LocalFileSystem local("local"); FILE *fp = fopen("/local/TEST_LOG.csv", "w"); // Open "test_log" on the local file system for writing char outString[100]; char buffer16[16]; int val_array[8]; const char dummy = 0; int drivetime_ms = 1; int readChannels (char buffer[16], int values[8]){ //simultaneously samples and reads into buffer short int val_array[8]; //send convert signal to channels convt = CH_ABCD; wait_us(1); convt = 0; //SPI(like) data transfer cs = 0; spi.write(&dummy, 1, buffer, 16); cs=1; //loop over bytes to add channel voltage values for (int i=0; i<8; i++){ val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1]; values [i] = val_array[i]; } return 0; } int main() { int eTime; double r1; double r2; double r1_max = 0; double r1_min = 1e10; double r1_sum = 0; double r1_sum2 = 0; double r1_mean; double r1_mean2; double r1_sd; double r1_cv; double r2_max = 0; double r2_min = 1e10; double r2_sum = 0; double r2_sum2 = 0; double r2_mean; double r2_mean2; double r2_sd; double r2_cv; rLED = 0; yLED = 0; gLED = 0; drive = 0; pc.baud(115200); pc.printf("Test start\r\n"); //Reset ADC sequence reset = 1; wait_ms(1); reset = 0; //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) spi.format(8,2); spi.frequency(2000000); spi.set_default_write_value(0x00); cs = 1; rLED = 1; yLED = 0; gLED = 1; //sprintf(outString, "iSteps, eTime, R1, R2 \n"); sprintf(outString, "iSteps, eTime, I1SIG, IREF, V1POS, V1NEG, R1,I2SIG,IREF, V2POS, V2NEG, R2\n"); pc.printf("%s", outString); fprintf(fp, outString); for (int iStep=0; iStep<N_STEPS; iStep++) { eTime = timer.read_ms(); // trigger measurement drive = 1; yLED = 1; camTrig = 1; mbedIO = 1; wait_us(CAM_TRIG); camTrig = 0; mbedIO = 0; wait_us(MEAS_DELAY - CAM_TRIG); readChannels (buffer16, val_array); drive = 0; yLED = 0; r1 = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]); r2 = (double)(val_array[7]-val_array[6])/(double)(val_array[1]-val_array[2]); if (r1 < r1_min) { r1_min = r1; } if (r1 > r1_max) { r1_max = r1; } if (r2 < r2_min) { r2_min = r2; } if (r2 > r2_max) { r2_max = r2; } r1_sum = r1_sum + r1; r1_sum2 = r1_sum2 + (r1*r1); r2_sum = r2_sum + r2; r2_sum2 = r2_sum2 + (r2*r2); //printf(outString, "%10d,%10d,%10f,%10f\n", iStep, eTime, r1, r2); // log data sprintf(outString, "%5d, %10d, %5d, %5d, %5d, %5d, %f %5d, %5d, %5d, %5d, %f\n", iStep, eTime, val_array[0], val_array[1], val_array[4], val_array[5], r1, val_array[2], val_array[1], val_array[6], val_array[7], r2); pc.printf("%s", outString); fprintf(fp, outString); wait_ms(LOG_INTERVAL); iStep++; } //r1_mean = r1_sum/n_samples; //r1_mean2 = r1_sum2/n_samples; //r1_sd = sqrt(r1_mean2-(r1_mean*r1_mean)); //r1_cv = r1_sd/r1_mean; //r2_mean = r2_sum/n_samples; //r2_mean2 = r2_sum2/n_samples; //r2_sd = sqrt(r2_mean2-(r2_mean*r2_mean)); //r2_cv = r2_sd/r1_mean; //pc.printf("Statistics:\r\n"); //pc.printf("n_samples : %d\r\n", n_samples); //pc.printf("r1_mean : %f\r\n", r1_mean); //pc.printf("r1_min : %f\r\n", r1_min); //pc.printf("r1_max : %f\r\n", r1_max); //pc.printf("r1_sd : %f\r\n", r1_sd); //pc.printf("r1_cv : %f\r\n", r1_cv); //pc.printf("r2_mean : %f\r\n", r2_mean); //pc.printf("r2_min : %f\r\n", r2_min); //pc.printf("r2_max : %f\r\n", r2_max); //pc.printf("r2_sd : %f\r\n", r2_sd); //.printf("r2_cv : %f\r\n", r2_cv); rLED = 0; fclose(fp); }