ADC logging with demo drive board for calibration ADC read synchronised with heater on time
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@3:447b0de4295a, 2018-07-31 (annotated)
- Committer:
- justinbuckland
- Date:
- Tue Jul 31 09:18:58 2018 +0000
- Revision:
- 3:447b0de4295a
- Parent:
- 2:23f848b21b09
- Child:
- 4:694f0e328a52
Temperature logging for TCR and resistance calibration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexStokoe | 0:362148ad8f6f | 1 | #include "mbed.h" |
AlexStokoe | 0:362148ad8f6f | 2 | |
justinbuckland | 2:23f848b21b09 | 3 | #define CH_A 1 // value of convst bus to read channel A only |
justinbuckland | 2:23f848b21b09 | 4 | #define CH_AC 5 // value of convst bus to read channels A and C |
justinbuckland | 2:23f848b21b09 | 5 | #define CH_ABCD 15 // value of convst bus to read all chanels simultaneously |
AlexStokoe | 0:362148ad8f6f | 6 | |
AlexStokoe | 0:362148ad8f6f | 7 | Serial pc(USBTX, USBRX); // tx, rx |
AlexStokoe | 0:362148ad8f6f | 8 | |
AlexStokoe | 0:362148ad8f6f | 9 | DigitalOut drive(p21); |
AlexStokoe | 0:362148ad8f6f | 10 | DigitalOut yLED(p27); |
AlexStokoe | 0:362148ad8f6f | 11 | DigitalOut gLED(p28); |
AlexStokoe | 0:362148ad8f6f | 12 | DigitalOut rLED(p26); |
AlexStokoe | 0:362148ad8f6f | 13 | |
AlexStokoe | 0:362148ad8f6f | 14 | AnalogIn battVolt(p19); |
AlexStokoe | 0:362148ad8f6f | 15 | AnalogIn auxVolt(p20); |
AlexStokoe | 0:362148ad8f6f | 16 | |
AlexStokoe | 0:362148ad8f6f | 17 | BusOut convt(p11, p12, p13, p14); |
AlexStokoe | 0:362148ad8f6f | 18 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
AlexStokoe | 0:362148ad8f6f | 19 | DigitalOut cs(p8); //chip select |
AlexStokoe | 0:362148ad8f6f | 20 | DigitalIn busy(p9); |
AlexStokoe | 0:362148ad8f6f | 21 | DigitalOut reset(p10); |
AlexStokoe | 0:362148ad8f6f | 22 | |
AlexStokoe | 0:362148ad8f6f | 23 | char buffer16[16]; |
AlexStokoe | 0:362148ad8f6f | 24 | int val_array[8]; |
AlexStokoe | 0:362148ad8f6f | 25 | const char dummy = 0; |
AlexStokoe | 0:362148ad8f6f | 26 | int drivetime_ms; |
AlexStokoe | 0:362148ad8f6f | 27 | |
AlexStokoe | 0:362148ad8f6f | 28 | char outString[100]; |
AlexStokoe | 0:362148ad8f6f | 29 | |
justinbuckland | 2:23f848b21b09 | 30 | int readChannels (char buffer[16], int values[8]){ |
justinbuckland | 2:23f848b21b09 | 31 | //simultaneously samples and reads into buffer |
AlexStokoe | 0:362148ad8f6f | 32 | short int val_array[8]; |
justinbuckland | 2:23f848b21b09 | 33 | |
justinbuckland | 2:23f848b21b09 | 34 | //send convert signal to channels |
justinbuckland | 2:23f848b21b09 | 35 | convt = CH_AC; |
justinbuckland | 2:23f848b21b09 | 36 | wait_us(1); |
justinbuckland | 2:23f848b21b09 | 37 | convt = 0; |
justinbuckland | 2:23f848b21b09 | 38 | |
justinbuckland | 2:23f848b21b09 | 39 | //SPI(like) data transfer |
justinbuckland | 2:23f848b21b09 | 40 | cs = 0; |
justinbuckland | 2:23f848b21b09 | 41 | spi.write(&dummy, 1, buffer, 16); |
justinbuckland | 2:23f848b21b09 | 42 | cs=1; |
justinbuckland | 2:23f848b21b09 | 43 | |
justinbuckland | 2:23f848b21b09 | 44 | //loop over bytes to add channel voltage values |
justinbuckland | 2:23f848b21b09 | 45 | for (int i=0; i<8; i++){ |
justinbuckland | 2:23f848b21b09 | 46 | val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1]; |
justinbuckland | 2:23f848b21b09 | 47 | values [i] = val_array[i]; |
AlexStokoe | 0:362148ad8f6f | 48 | } |
AlexStokoe | 0:362148ad8f6f | 49 | |
AlexStokoe | 0:362148ad8f6f | 50 | return 0; |
AlexStokoe | 0:362148ad8f6f | 51 | } |
AlexStokoe | 0:362148ad8f6f | 52 | |
AlexStokoe | 0:362148ad8f6f | 53 | |
AlexStokoe | 0:362148ad8f6f | 54 | int main() { |
justinbuckland | 3:447b0de4295a | 55 | int n_samples = 10000; |
justinbuckland | 2:23f848b21b09 | 56 | double r; |
justinbuckland | 2:23f848b21b09 | 57 | double r_max = 0; |
justinbuckland | 2:23f848b21b09 | 58 | double r_min = 1e10; |
justinbuckland | 2:23f848b21b09 | 59 | double r_sum = 0; |
justinbuckland | 2:23f848b21b09 | 60 | double r_sum2 = 0; |
justinbuckland | 2:23f848b21b09 | 61 | double r_mean; |
justinbuckland | 2:23f848b21b09 | 62 | double r_mean2; |
justinbuckland | 2:23f848b21b09 | 63 | double r_sd; |
justinbuckland | 2:23f848b21b09 | 64 | double r_cv; |
justinbuckland | 2:23f848b21b09 | 65 | |
AlexStokoe | 0:362148ad8f6f | 66 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 67 | yLED = 0; |
AlexStokoe | 0:362148ad8f6f | 68 | gLED = 0; |
AlexStokoe | 0:362148ad8f6f | 69 | |
AlexStokoe | 0:362148ad8f6f | 70 | drive = 0; |
AlexStokoe | 0:362148ad8f6f | 71 | drivetime_ms = 1; |
justinbuckland | 2:23f848b21b09 | 72 | |
AlexStokoe | 0:362148ad8f6f | 73 | pc.baud(115200); |
justinbuckland | 2:23f848b21b09 | 74 | pc.printf("Test start\r\n"); |
AlexStokoe | 0:362148ad8f6f | 75 | |
AlexStokoe | 0:362148ad8f6f | 76 | //Reset ADC sequence |
AlexStokoe | 0:362148ad8f6f | 77 | reset = 1; |
AlexStokoe | 0:362148ad8f6f | 78 | wait_ms(1); |
AlexStokoe | 0:362148ad8f6f | 79 | reset = 0; |
AlexStokoe | 0:362148ad8f6f | 80 | |
AlexStokoe | 0:362148ad8f6f | 81 | //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) |
AlexStokoe | 0:362148ad8f6f | 82 | spi.format(8,2); |
AlexStokoe | 0:362148ad8f6f | 83 | spi.frequency(2000000); |
AlexStokoe | 0:362148ad8f6f | 84 | spi.set_default_write_value(0x00); |
AlexStokoe | 0:362148ad8f6f | 85 | cs = 1; |
AlexStokoe | 0:362148ad8f6f | 86 | |
AlexStokoe | 0:362148ad8f6f | 87 | rLED = 1; |
justinbuckland | 2:23f848b21b09 | 88 | yLED = 0; |
AlexStokoe | 0:362148ad8f6f | 89 | gLED = 1; |
AlexStokoe | 0:362148ad8f6f | 90 | |
justinbuckland | 2:23f848b21b09 | 91 | sprintf(outString, "I1SIG, I1REF, V1POS, V1NEG, R\r\n"); |
justinbuckland | 2:23f848b21b09 | 92 | pc.printf("%s", outString); |
AlexStokoe | 0:362148ad8f6f | 93 | |
justinbuckland | 2:23f848b21b09 | 94 | for (int x=0; x<n_samples; x++) { |
AlexStokoe | 0:362148ad8f6f | 95 | drive = 1; |
justinbuckland | 2:23f848b21b09 | 96 | yLED = 1; |
AlexStokoe | 0:362148ad8f6f | 97 | wait_ms(drivetime_ms); |
justinbuckland | 2:23f848b21b09 | 98 | readChannels (buffer16, val_array); |
AlexStokoe | 0:362148ad8f6f | 99 | drive = 0; |
justinbuckland | 2:23f848b21b09 | 100 | yLED = 0; |
justinbuckland | 2:23f848b21b09 | 101 | |
justinbuckland | 2:23f848b21b09 | 102 | r = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]); |
justinbuckland | 2:23f848b21b09 | 103 | if (r < r_min) { r_min = r; } |
justinbuckland | 2:23f848b21b09 | 104 | if (r > r_max) { r_max = r; } |
justinbuckland | 2:23f848b21b09 | 105 | r_sum = r_sum + r; |
justinbuckland | 2:23f848b21b09 | 106 | r_sum2 = r_sum2 + (r*r); |
AlexStokoe | 0:362148ad8f6f | 107 | |
justinbuckland | 3:447b0de4295a | 108 | 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); |
justinbuckland | 2:23f848b21b09 | 109 | pc.printf("%s", outString); |
justinbuckland | 2:23f848b21b09 | 110 | wait_ms(1000); |
AlexStokoe | 0:362148ad8f6f | 111 | |
AlexStokoe | 0:362148ad8f6f | 112 | } |
justinbuckland | 2:23f848b21b09 | 113 | |
justinbuckland | 2:23f848b21b09 | 114 | r_mean = r_sum/n_samples; |
justinbuckland | 2:23f848b21b09 | 115 | r_mean2 = r_sum2/n_samples; |
justinbuckland | 2:23f848b21b09 | 116 | r_sd = sqrt(r_mean2-(r_mean*r_mean)); |
justinbuckland | 2:23f848b21b09 | 117 | r_cv = r_sd/r_mean; |
justinbuckland | 2:23f848b21b09 | 118 | |
justinbuckland | 3:447b0de4295a | 119 | // pc.printf("Statistics:\r\n"); |
justinbuckland | 3:447b0de4295a | 120 | // pc.printf("n_samples : %d\r\n", n_samples); |
justinbuckland | 3:447b0de4295a | 121 | // pc.printf("r_mean : %f\r\n", r_mean); |
justinbuckland | 3:447b0de4295a | 122 | // pc.printf("r_min : %f\r\n", r_min); |
justinbuckland | 3:447b0de4295a | 123 | // pc.printf("r_max : %f\r\n", r_max); |
justinbuckland | 3:447b0de4295a | 124 | // pc.printf("r_sd : %f\r\n", r_sd); |
justinbuckland | 3:447b0de4295a | 125 | // pc.printf("r_cv : %f\r\n", r_cv); |
justinbuckland | 2:23f848b21b09 | 126 | |
AlexStokoe | 0:362148ad8f6f | 127 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 128 | |
justinbuckland | 2:23f848b21b09 | 129 | } |