The Technology Partnership / Mbed 2 deprecated MEMSPCR_Control_Program_V2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define CH_A 1     // value of convst bus to read channel A only
00004 #define CH_AC 5     // value of convst bus to read channels A and C
00005 #define CH_ABCD 15 // value of convst bus to read all chanels simultaneously
00006 
00007 Serial pc(USBTX, USBRX); // tx, rx
00008 
00009 DigitalOut drive(p21);
00010 DigitalOut yLED(p27);
00011 DigitalOut gLED(p28);
00012 DigitalOut rLED(p26);
00013 
00014 AnalogIn battVolt(p19);
00015 AnalogIn auxVolt(p20);
00016 
00017 BusOut convt(p11, p12, p13, p14);
00018 SPI spi(p5, p6, p7); // mosi, miso, sclk
00019 DigitalOut cs(p8);  //chip select
00020 DigitalIn busy(p9);
00021 DigitalOut reset(p10);
00022 
00023 char buffer16[16];
00024 int val_array[8];
00025 const char dummy = 0;
00026 int drivetime_ms;
00027 
00028 char outString[100];
00029 
00030 int readChannels (char buffer[16], int values[8]){
00031     //simultaneously samples and reads into buffer
00032     short int val_array[8];
00033              
00034     //send convert signal to channels
00035     convt = CH_AC;
00036     wait_us(1);
00037     convt = 0;
00038                 
00039     //SPI(like) data transfer
00040     cs = 0;
00041     spi.write(&dummy, 1, buffer, 16);
00042     cs=1;
00043        
00044     //loop over bytes to add channel voltage values
00045     for (int i=0; i<8; i++){
00046         val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1];
00047         values [i] = val_array[i];   
00048         }
00049             
00050     return 0;
00051     }
00052     
00053 
00054 int main() {
00055     int n_samples = 10000;
00056     double r;
00057     double r_max = 0;
00058     double r_min = 1e10;
00059     double r_sum = 0;
00060     double r_sum2 = 0;
00061     double r_mean;
00062     double r_mean2;
00063     double r_sd;
00064     double r_cv;
00065      
00066     rLED = 0;
00067     yLED = 0;
00068     gLED = 0;
00069     
00070     drive = 0;
00071     drivetime_ms = 1;
00072        
00073     pc.baud(115200);
00074     pc.printf("Test start\r\n");
00075     
00076     //Reset ADC sequence
00077     reset = 1;
00078     wait_ms(1);
00079     reset = 0;
00080     
00081     //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) 
00082     spi.format(8,2);
00083     spi.frequency(2000000);
00084     spi.set_default_write_value(0x00);
00085     cs = 1;
00086 
00087     rLED = 1;
00088     yLED = 0;
00089     gLED = 1;
00090     
00091     sprintf(outString, "I1SIG, I1REF, V1POS, V1NEG,        R\r\n");
00092     pc.printf("%s", outString);
00093     
00094     for (int x=0; x<n_samples; x++) {
00095         drive = 1;
00096         yLED = 1;
00097         wait_ms(drivetime_ms);
00098         readChannels (buffer16, val_array);
00099         drive = 0;
00100         yLED = 0;
00101         
00102         r = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]);
00103         if (r < r_min) { r_min = r; }
00104         if (r > r_max) { r_max = r; }
00105         r_sum = r_sum + r;
00106         r_sum2 = r_sum2 + (r*r);
00107 
00108         sprintf(outString, "%5d\t %5d\t %5d\t %5d\t %f\r\n", val_array[0], val_array[1], val_array[4], val_array[5], r);
00109         pc.printf("%s", outString); 
00110         wait_ms(1000);
00111         
00112         }
00113 
00114     r_mean = r_sum/n_samples;
00115     r_mean2 = r_sum2/n_samples;
00116     r_sd = sqrt(r_mean2-(r_mean*r_mean));
00117     r_cv = r_sd/r_mean;
00118 
00119 //    pc.printf("Statistics:\r\n"); 
00120 //    pc.printf("n_samples : %d\r\n", n_samples); 
00121 //    pc.printf("r_mean    : %f\r\n", r_mean); 
00122 //    pc.printf("r_min     : %f\r\n", r_min); 
00123 //    pc.printf("r_max     : %f\r\n", r_max); 
00124 //    pc.printf("r_sd      : %f\r\n", r_sd); 
00125 //    pc.printf("r_cv      : %f\r\n", r_cv);  
00126 
00127     rLED = 0;
00128     
00129  }