ADC logging with demo drive board for calibration

Dependencies:   mbed MODSERIAL FastPWM ADS8568_ADC

main.cpp

Committer:
justinbuckland
Date:
2019-01-30
Revision:
7:2d695116d636
Parent:
6:75b09f0bcbd9
Child:
8:325f68c1e3d2

File content as of revision 7:2d695116d636:

#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

Serial pc(USBTX, USBRX); // tx, rx

DigitalOut drive(p21);
DigitalOut yLED(p27);
DigitalOut gLED(p28);
DigitalOut rLED(p26);

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

char buffer16[16];
int val_array[8];
const char dummy = 0;
int drivetime_ms = 1;

char outString[100];

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 n_samples = 20;
    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;
     
    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, "I1SIG, I1REF, V1POS, V1NEG,         R1  I2SIG, I2REF, V2POS, V2NEG,         R2\r\n");
    pc.printf("%s", outString);
    
    for (int x=0; x<n_samples; x++) {
        drive = 1;
        yLED = 1;
        wait_ms(drivetime_ms);
        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; }
        r1_sum = r1_sum + r1;
        r1_sum2 = r1_sum2 + (r1*r1);

        sprintf(outString, "%5d\t %5d\t %5d\t %5d\t %f  %5d\t %5d\t %5d\t %5d\t %f\r\n", 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); 
        wait_ms(1000);
        
        }

    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;

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

    rLED = 0;
    
 }