ADC logging with demo drive board for calibration ADC read synchronised with heater on time
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@1:7f99e938ae2a, 2018-07-23 (annotated)
- Committer:
- justinbuckland
- Date:
- Mon Jul 23 17:34:20 2018 +0000
- Revision:
- 1:7f99e938ae2a
- Parent:
- 0:362148ad8f6f
- Child:
- 2:23f848b21b09
testing for noise;
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 | |
AlexStokoe | 0:362148ad8f6f | 3 | #define ALL_CH 15 //value of convst bus to read all chanels simultaneosly |
AlexStokoe | 0:362148ad8f6f | 4 | #define IO_REF 10.17 //ref channel voltage mV (most accurate between 9-12V high supply voltage) |
AlexStokoe | 0:362148ad8f6f | 5 | |
AlexStokoe | 0:362148ad8f6f | 6 | #define CHA0_REF 6217 |
AlexStokoe | 0:362148ad8f6f | 7 | #define CHA1_REF 6196 |
AlexStokoe | 0:362148ad8f6f | 8 | #define CHB0_REF 6231 |
AlexStokoe | 0:362148ad8f6f | 9 | #define CHC0_REF 6231 |
AlexStokoe | 0:362148ad8f6f | 10 | #define CHC1_REF 6223 |
AlexStokoe | 0:362148ad8f6f | 11 | #define CHD0_REF 6240 |
AlexStokoe | 0:362148ad8f6f | 12 | #define CHD1_REF 6228 |
AlexStokoe | 0:362148ad8f6f | 13 | |
AlexStokoe | 0:362148ad8f6f | 14 | |
AlexStokoe | 0:362148ad8f6f | 15 | |
AlexStokoe | 0:362148ad8f6f | 16 | Serial pc(USBTX, USBRX); // tx, rx |
AlexStokoe | 0:362148ad8f6f | 17 | |
AlexStokoe | 0:362148ad8f6f | 18 | DigitalOut drive(p21); |
AlexStokoe | 0:362148ad8f6f | 19 | DigitalOut yLED(p27); |
AlexStokoe | 0:362148ad8f6f | 20 | DigitalOut gLED(p28); |
AlexStokoe | 0:362148ad8f6f | 21 | DigitalOut rLED(p26); |
AlexStokoe | 0:362148ad8f6f | 22 | |
AlexStokoe | 0:362148ad8f6f | 23 | AnalogIn battVolt(p19); |
AlexStokoe | 0:362148ad8f6f | 24 | AnalogIn auxVolt(p20); |
AlexStokoe | 0:362148ad8f6f | 25 | |
AlexStokoe | 0:362148ad8f6f | 26 | BusOut convt(p11, p12, p13, p14); |
AlexStokoe | 0:362148ad8f6f | 27 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
AlexStokoe | 0:362148ad8f6f | 28 | DigitalOut cs(p8); //chip select |
AlexStokoe | 0:362148ad8f6f | 29 | DigitalIn busy(p9); |
AlexStokoe | 0:362148ad8f6f | 30 | DigitalOut reset(p10); |
AlexStokoe | 0:362148ad8f6f | 31 | |
AlexStokoe | 0:362148ad8f6f | 32 | LocalFileSystem local("local"); |
AlexStokoe | 0:362148ad8f6f | 33 | |
AlexStokoe | 0:362148ad8f6f | 34 | FILE *fp = fopen("/local/TEST_LOG.csv", "w"); // Open "test_log" on the local file system for writing |
AlexStokoe | 0:362148ad8f6f | 35 | |
AlexStokoe | 0:362148ad8f6f | 36 | |
AlexStokoe | 0:362148ad8f6f | 37 | char buffer16[16]; |
AlexStokoe | 0:362148ad8f6f | 38 | int val_array[8]; |
AlexStokoe | 0:362148ad8f6f | 39 | float voltages[8]; |
AlexStokoe | 0:362148ad8f6f | 40 | const char dummy = 0; |
AlexStokoe | 0:362148ad8f6f | 41 | int i = 0; |
AlexStokoe | 0:362148ad8f6f | 42 | float cur1 = 0; |
AlexStokoe | 0:362148ad8f6f | 43 | float cur2 = 0; |
AlexStokoe | 0:362148ad8f6f | 44 | float R1 = 0; |
AlexStokoe | 0:362148ad8f6f | 45 | float R2 = 0; |
AlexStokoe | 0:362148ad8f6f | 46 | int scale_factors[8]; |
AlexStokoe | 0:362148ad8f6f | 47 | int drivetime_ms; |
AlexStokoe | 0:362148ad8f6f | 48 | |
AlexStokoe | 0:362148ad8f6f | 49 | float scale_factor = 0.0; |
AlexStokoe | 0:362148ad8f6f | 50 | char outString[100]; |
AlexStokoe | 0:362148ad8f6f | 51 | |
AlexStokoe | 0:362148ad8f6f | 52 | int readChannels (char buffer[16], int values[8], int OSR){ |
AlexStokoe | 0:362148ad8f6f | 53 | //simultaneosly samples all channels and reads into buffer |
AlexStokoe | 0:362148ad8f6f | 54 | short int val_array[8]; |
AlexStokoe | 0:362148ad8f6f | 55 | |
AlexStokoe | 0:362148ad8f6f | 56 | for (int i=0; i<OSR; i++) { |
AlexStokoe | 0:362148ad8f6f | 57 | |
AlexStokoe | 0:362148ad8f6f | 58 | //send convert signal to all channels |
AlexStokoe | 0:362148ad8f6f | 59 | convt = ALL_CH; |
AlexStokoe | 0:362148ad8f6f | 60 | wait_us(1); |
AlexStokoe | 0:362148ad8f6f | 61 | convt = 0; |
AlexStokoe | 0:362148ad8f6f | 62 | |
AlexStokoe | 0:362148ad8f6f | 63 | //SPI(like) data transfer |
AlexStokoe | 0:362148ad8f6f | 64 | cs = 0; |
AlexStokoe | 0:362148ad8f6f | 65 | spi.write(&dummy, 1, buffer, 16); |
AlexStokoe | 0:362148ad8f6f | 66 | cs=1; |
AlexStokoe | 0:362148ad8f6f | 67 | |
AlexStokoe | 0:362148ad8f6f | 68 | //loop over bytes to add channel voltage values |
AlexStokoe | 0:362148ad8f6f | 69 | for (int x=0; x<8; x++){ |
AlexStokoe | 0:362148ad8f6f | 70 | val_array[x] = buffer[2*x]<<8 | buffer16[(2*x) + 1]; |
AlexStokoe | 0:362148ad8f6f | 71 | values [x] = values[x] + val_array[x]; |
AlexStokoe | 0:362148ad8f6f | 72 | } |
AlexStokoe | 0:362148ad8f6f | 73 | } |
AlexStokoe | 0:362148ad8f6f | 74 | |
AlexStokoe | 0:362148ad8f6f | 75 | for (int y=0; y<8; y++){ |
AlexStokoe | 0:362148ad8f6f | 76 | values[y] = values[y]/OSR; |
AlexStokoe | 0:362148ad8f6f | 77 | } |
AlexStokoe | 0:362148ad8f6f | 78 | |
AlexStokoe | 0:362148ad8f6f | 79 | return 0; |
AlexStokoe | 0:362148ad8f6f | 80 | } |
AlexStokoe | 0:362148ad8f6f | 81 | |
AlexStokoe | 0:362148ad8f6f | 82 | |
AlexStokoe | 0:362148ad8f6f | 83 | int main() { |
AlexStokoe | 0:362148ad8f6f | 84 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 85 | yLED = 0; |
AlexStokoe | 0:362148ad8f6f | 86 | gLED = 0; |
AlexStokoe | 0:362148ad8f6f | 87 | |
justinbuckland | 1:7f99e938ae2a | 88 | int OSR = 5; |
AlexStokoe | 0:362148ad8f6f | 89 | |
AlexStokoe | 0:362148ad8f6f | 90 | drive = 0; |
AlexStokoe | 0:362148ad8f6f | 91 | drivetime_ms = 1; |
AlexStokoe | 0:362148ad8f6f | 92 | |
AlexStokoe | 0:362148ad8f6f | 93 | scale_factors[0] = CHA0_REF; |
AlexStokoe | 0:362148ad8f6f | 94 | scale_factors[1] = CHA1_REF; |
AlexStokoe | 0:362148ad8f6f | 95 | scale_factors[2] = CHB0_REF; |
AlexStokoe | 0:362148ad8f6f | 96 | scale_factors[3] = 0; |
AlexStokoe | 0:362148ad8f6f | 97 | scale_factors[4] = CHC0_REF; |
AlexStokoe | 0:362148ad8f6f | 98 | scale_factors[5] = CHC1_REF; |
AlexStokoe | 0:362148ad8f6f | 99 | scale_factors[6] = CHD0_REF; |
AlexStokoe | 0:362148ad8f6f | 100 | scale_factors[7] = CHD1_REF; |
AlexStokoe | 0:362148ad8f6f | 101 | |
AlexStokoe | 0:362148ad8f6f | 102 | pc.baud(115200); |
AlexStokoe | 0:362148ad8f6f | 103 | pc.printf("Test start\n\r"); |
AlexStokoe | 0:362148ad8f6f | 104 | |
AlexStokoe | 0:362148ad8f6f | 105 | //Reset ADC sequence |
AlexStokoe | 0:362148ad8f6f | 106 | reset = 1; |
AlexStokoe | 0:362148ad8f6f | 107 | wait_ms(1); |
AlexStokoe | 0:362148ad8f6f | 108 | reset = 0; |
AlexStokoe | 0:362148ad8f6f | 109 | |
AlexStokoe | 0:362148ad8f6f | 110 | //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) |
AlexStokoe | 0:362148ad8f6f | 111 | spi.format(8,2); |
AlexStokoe | 0:362148ad8f6f | 112 | spi.frequency(2000000); |
AlexStokoe | 0:362148ad8f6f | 113 | spi.set_default_write_value(0x00); |
AlexStokoe | 0:362148ad8f6f | 114 | cs = 1; |
AlexStokoe | 0:362148ad8f6f | 115 | |
AlexStokoe | 0:362148ad8f6f | 116 | //while(1) { |
AlexStokoe | 0:362148ad8f6f | 117 | rLED = 1; |
AlexStokoe | 0:362148ad8f6f | 118 | yLED = drive; |
AlexStokoe | 0:362148ad8f6f | 119 | gLED = 1; |
AlexStokoe | 0:362148ad8f6f | 120 | |
AlexStokoe | 0:362148ad8f6f | 121 | |
AlexStokoe | 0:362148ad8f6f | 122 | drive = 1; |
AlexStokoe | 0:362148ad8f6f | 123 | wait_ms(drivetime_ms); |
AlexStokoe | 0:362148ad8f6f | 124 | readChannels (buffer16, val_array, 1); |
AlexStokoe | 0:362148ad8f6f | 125 | drive = 0; |
AlexStokoe | 0:362148ad8f6f | 126 | |
AlexStokoe | 0:362148ad8f6f | 127 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 128 | pc.printf("Drive for %d ms\n", drivetime_ms); |
AlexStokoe | 0:362148ad8f6f | 129 | for (int x=0; x<8; x++){ |
justinbuckland | 1:7f99e938ae2a | 130 | voltages[x] = (float)val_array[x]*scale_factors[x]/10000; |
AlexStokoe | 0:362148ad8f6f | 131 | pc.printf("%f\n", voltages[x]); |
AlexStokoe | 0:362148ad8f6f | 132 | } |
AlexStokoe | 0:362148ad8f6f | 133 | cur1 = 2* (voltages[0] - voltages[1]); |
AlexStokoe | 0:362148ad8f6f | 134 | cur2 = 2* (voltages[2] - voltages[1]); |
AlexStokoe | 0:362148ad8f6f | 135 | |
AlexStokoe | 0:362148ad8f6f | 136 | R1 = (voltages[4]- voltages[5])/cur1; |
AlexStokoe | 0:362148ad8f6f | 137 | R2 = (voltages[6]- voltages[7])/cur2; |
AlexStokoe | 0:362148ad8f6f | 138 | |
justinbuckland | 1:7f99e938ae2a | 139 | pc.printf("V0 = %f\n", voltages[0]); |
justinbuckland | 1:7f99e938ae2a | 140 | pc.printf("V1 = %f\n", voltages[1]); |
justinbuckland | 1:7f99e938ae2a | 141 | pc.printf("V2 = %f\n", voltages[2]); |
justinbuckland | 1:7f99e938ae2a | 142 | pc.printf("V4 = %f\n", voltages[4]); |
justinbuckland | 1:7f99e938ae2a | 143 | pc.printf("V5 = %f\n", voltages[5]); |
justinbuckland | 1:7f99e938ae2a | 144 | pc.printf("V6 = %f\n", voltages[6]); |
justinbuckland | 1:7f99e938ae2a | 145 | pc.printf("V7 = %f\n", voltages[7]); |
justinbuckland | 1:7f99e938ae2a | 146 | |
AlexStokoe | 0:362148ad8f6f | 147 | |
AlexStokoe | 0:362148ad8f6f | 148 | sprintf(outString, "R1, R2, Drive for %d ms, OSR %d\n", drivetime_ms, OSR); |
AlexStokoe | 0:362148ad8f6f | 149 | fprintf(fp, outString); |
AlexStokoe | 0:362148ad8f6f | 150 | |
AlexStokoe | 0:362148ad8f6f | 151 | for (int x=0; x<20; x++) { |
AlexStokoe | 0:362148ad8f6f | 152 | drive = 1; |
AlexStokoe | 0:362148ad8f6f | 153 | wait_ms(drivetime_ms); |
AlexStokoe | 0:362148ad8f6f | 154 | readChannels (buffer16, val_array, OSR); |
AlexStokoe | 0:362148ad8f6f | 155 | drive = 0; |
AlexStokoe | 0:362148ad8f6f | 156 | |
AlexStokoe | 0:362148ad8f6f | 157 | for (int x=0; x<8; x++){ |
AlexStokoe | 0:362148ad8f6f | 158 | voltages[x] = val_array[x]*scale_factors[x]/10000; |
AlexStokoe | 0:362148ad8f6f | 159 | } |
AlexStokoe | 0:362148ad8f6f | 160 | |
AlexStokoe | 0:362148ad8f6f | 161 | cur1 = 2* (voltages[0] - voltages[1]); |
AlexStokoe | 0:362148ad8f6f | 162 | cur2 = 2* (voltages[2] - voltages[1]); |
AlexStokoe | 0:362148ad8f6f | 163 | |
justinbuckland | 1:7f99e938ae2a | 164 | // R1 = (voltages[4]- voltages[5])/cur1; |
justinbuckland | 1:7f99e938ae2a | 165 | // R2 = (voltages[6]- voltages[7])/cur2; |
justinbuckland | 1:7f99e938ae2a | 166 | R1 = cur1; |
justinbuckland | 1:7f99e938ae2a | 167 | R2 = cur2; |
AlexStokoe | 0:362148ad8f6f | 168 | sprintf(outString, "%f, %f\n", R1, R2); |
AlexStokoe | 0:362148ad8f6f | 169 | fprintf(fp, outString); |
AlexStokoe | 0:362148ad8f6f | 170 | wait_ms(100); |
AlexStokoe | 0:362148ad8f6f | 171 | |
AlexStokoe | 0:362148ad8f6f | 172 | } |
AlexStokoe | 0:362148ad8f6f | 173 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 174 | |
AlexStokoe | 0:362148ad8f6f | 175 | fclose(fp); |
AlexStokoe | 0:362148ad8f6f | 176 | |
AlexStokoe | 0:362148ad8f6f | 177 | |
AlexStokoe | 0:362148ad8f6f | 178 | // |
AlexStokoe | 0:362148ad8f6f | 179 | // pc.printf("Repeat Test?\n\rPress to increment wait time\n\r"); |
AlexStokoe | 0:362148ad8f6f | 180 | // while(1){ |
AlexStokoe | 0:362148ad8f6f | 181 | // if (pc.getc() != 0) { |
AlexStokoe | 0:362148ad8f6f | 182 | // drivetime_ms++; |
AlexStokoe | 0:362148ad8f6f | 183 | // break; |
AlexStokoe | 0:362148ad8f6f | 184 | // } |
AlexStokoe | 0:362148ad8f6f | 185 | // } |
AlexStokoe | 0:362148ad8f6f | 186 | |
AlexStokoe | 0:362148ad8f6f | 187 | //} |
AlexStokoe | 0:362148ad8f6f | 188 | } |