ADC logging with demo drive board for calibration ADC read synchronised with heater on time
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@25:f507cfd50fcc, 2019-10-09 (annotated)
- Committer:
- justinbuckland
- Date:
- Wed Oct 09 17:36:51 2019 +0000
- Revision:
- 25:f507cfd50fcc
- Parent:
- 24:899071abfc14
- Child:
- 26:323238949f9d
synchronised ADC read and heater pulses, 10x ADC read per pulse
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexStokoe | 0:362148ad8f6f | 1 | #include "mbed.h" |
justinbuckland | 16:2c563709cf09 | 2 | #include "ADS8568_ADC.h" |
justinbuckland | 25:f507cfd50fcc | 3 | // #include "FastPWM.h" |
justinbuckland | 16:2c563709cf09 | 4 | #include "MODSERIAL.h" |
AlexStokoe | 0:362148ad8f6f | 5 | |
justinbuckland | 25:f507cfd50fcc | 6 | #define MEAS_DELAY 50 // measurement delay after turning on FET (us) |
justinbuckland | 25:f507cfd50fcc | 7 | #define LOG_INTERVAL 1000 // log file interval (ms) |
justinbuckland | 16:2c563709cf09 | 8 | #define START_DELAY 1000 // pause for startup (ms) |
justinbuckland | 22:aba3ee6df08e | 9 | #define N_STEPS 100 |
justinbuckland | 25:f507cfd50fcc | 10 | #define N_READ 10 |
justinbuckland | 16:2c563709cf09 | 11 | #define BUFFER_SIZE 4096 |
AlexStokoe | 0:362148ad8f6f | 12 | |
justinbuckland | 17:25831977b98e | 13 | //UID lookup address and pointer |
justinbuckland | 17:25831977b98e | 14 | #define UID_ADDR 0x1FFF7A10 |
justinbuckland | 17:25831977b98e | 15 | unsigned long *uid = (unsigned long *) UID_ADDR; |
justinbuckland | 17:25831977b98e | 16 | |
justinbuckland | 17:25831977b98e | 17 | //UID and drive board calibration table |
justinbuckland | 17:25831977b98e | 18 | #define UID_TABLE_LENGTH 4 |
justinbuckland | 17:25831977b98e | 19 | |
justinbuckland | 17:25831977b98e | 20 | int drive_board_serial_number[UID_TABLE_LENGTH] = |
justinbuckland | 17:25831977b98e | 21 | {1, |
justinbuckland | 17:25831977b98e | 22 | 2, |
justinbuckland | 17:25831977b98e | 23 | 3, |
justinbuckland | 17:25831977b98e | 24 | 4}; |
justinbuckland | 17:25831977b98e | 25 | |
justinbuckland | 17:25831977b98e | 26 | unsigned long drive_board_uid[UID_TABLE_LENGTH][3] = |
justinbuckland | 17:25831977b98e | 27 | {{0x005B0060, 0x32375101, 0x32363531}, |
paullj | 21:4833e7fbcec7 | 28 | {0x0051003D, 0x32375114, 0x30333732}, |
paullj | 21:4833e7fbcec7 | 29 | {0x00520060, 0x32375101, 0x32363531}, |
paullj | 21:4833e7fbcec7 | 30 | {0x00570060, 0x32375101, 0x32363531}}; |
justinbuckland | 17:25831977b98e | 31 | |
justinbuckland | 19:ab2d7aa17a20 | 32 | float drive_board_cal[UID_TABLE_LENGTH][2][2] = |
paullj | 21:4833e7fbcec7 | 33 | {{{0.096724353, 10.1817431}, {0.056098807, 10.19962849}}, |
justinbuckland | 23:1d3a1d61c2b3 | 34 | {{0.0596907336847412, 10.1550084867437}, {0.0320376283698263, 10.2580153464834}}, |
justinbuckland | 25:f507cfd50fcc | 35 | {{0.01887149, 10.39360225}, {0.03115874, 10.28199855}}, |
justinbuckland | 25:f507cfd50fcc | 36 | {{0.052545339, 10.06008621}, {0.094239471, 10.11983777}}}; |
justinbuckland | 17:25831977b98e | 37 | |
justinbuckland | 16:2c563709cf09 | 38 | MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers |
justinbuckland | 16:2c563709cf09 | 39 | ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3); |
justinbuckland | 16:2c563709cf09 | 40 | I2C i2c(PB_7, PB_8); //SDA, SCL |
sophiemeredith | 11:8e6c8e654004 | 41 | Timer timer; |
justinbuckland | 16:2c563709cf09 | 42 | DigitalIn adc_busy(PA_8); //Busy interrupt sig# |
sophiemeredith | 11:8e6c8e654004 | 43 | |
justinbuckland | 16:2c563709cf09 | 44 | //Heater Control |
justinbuckland | 25:f507cfd50fcc | 45 | DigitalOut drive_1(PC_9); |
justinbuckland | 25:f507cfd50fcc | 46 | DigitalOut drive_2(PC_8); |
justinbuckland | 25:f507cfd50fcc | 47 | DigitalOut guard_1(PC_7); |
justinbuckland | 25:f507cfd50fcc | 48 | DigitalOut guard_2(PC_6); |
AlexStokoe | 0:362148ad8f6f | 49 | |
justinbuckland | 16:2c563709cf09 | 50 | //Indicator LEDs |
justinbuckland | 16:2c563709cf09 | 51 | DigitalOut hb_led(PC_13); //Green |
justinbuckland | 16:2c563709cf09 | 52 | DigitalOut led_0(PC_4); //Red |
justinbuckland | 16:2c563709cf09 | 53 | DigitalOut led_1(PC_5); //Green |
justinbuckland | 16:2c563709cf09 | 54 | |
justinbuckland | 25:f507cfd50fcc | 55 | //External LED |
justinbuckland | 25:f507cfd50fcc | 56 | DigitalOut led_ext(PB_4); |
justinbuckland | 25:f507cfd50fcc | 57 | |
justinbuckland | 16:2c563709cf09 | 58 | //User buttons |
justinbuckland | 16:2c563709cf09 | 59 | DigitalIn user_0(PB_0); |
justinbuckland | 16:2c563709cf09 | 60 | DigitalIn user_1(PB_1); |
justinbuckland | 16:2c563709cf09 | 61 | |
justinbuckland | 16:2c563709cf09 | 62 | BusOut converts(PC_0, PC_1, PC_2, PC_3); |
AlexStokoe | 0:362148ad8f6f | 63 | |
AlexStokoe | 0:362148ad8f6f | 64 | int main() { |
sophiemeredith | 11:8e6c8e654004 | 65 | int eTime; |
justinbuckland | 16:2c563709cf09 | 66 | int v[2], curr[2]; |
justinbuckland | 17:25831977b98e | 67 | double r_adc[2], r_ohm[2]; |
justinbuckland | 16:2c563709cf09 | 68 | |
justinbuckland | 16:2c563709cf09 | 69 | int i_port[2] = {0,2}; |
justinbuckland | 16:2c563709cf09 | 70 | int v_port[2] = {1,3}; |
justinbuckland | 2:23f848b21b09 | 71 | |
justinbuckland | 16:2c563709cf09 | 72 | pc.baud(115200); |
justinbuckland | 16:2c563709cf09 | 73 | adc.init(); |
AlexStokoe | 0:362148ad8f6f | 74 | |
justinbuckland | 25:f507cfd50fcc | 75 | // External LED off |
justinbuckland | 25:f507cfd50fcc | 76 | led_ext = 0; |
justinbuckland | 25:f507cfd50fcc | 77 | |
justinbuckland | 16:2c563709cf09 | 78 | // Initialsation - all heaters off |
justinbuckland | 25:f507cfd50fcc | 79 | drive_1 = 0; |
justinbuckland | 25:f507cfd50fcc | 80 | drive_2 = 0; |
justinbuckland | 25:f507cfd50fcc | 81 | guard_1 = 0; |
justinbuckland | 25:f507cfd50fcc | 82 | guard_2 = 0; |
justinbuckland | 25:f507cfd50fcc | 83 | // drive_1.prescaler(1); |
justinbuckland | 25:f507cfd50fcc | 84 | // drive_1.period_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 85 | // drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 86 | |
justinbuckland | 25:f507cfd50fcc | 87 | // guard_1.prescaler(1); |
justinbuckland | 25:f507cfd50fcc | 88 | // guard_1.period_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 89 | // guard_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 90 | |
justinbuckland | 25:f507cfd50fcc | 91 | // drive_2.prescaler(1); |
justinbuckland | 25:f507cfd50fcc | 92 | // drive_2.period_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 93 | // drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 94 | |
justinbuckland | 25:f507cfd50fcc | 95 | // guard_2.prescaler(1); |
justinbuckland | 25:f507cfd50fcc | 96 | // guard_2.period_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 97 | // guard_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 98 | |
justinbuckland | 17:25831977b98e | 99 | pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]); |
paullj | 21:4833e7fbcec7 | 100 | int i_board = -1; |
justinbuckland | 17:25831977b98e | 101 | for (int i = 0; i < UID_TABLE_LENGTH; i++) |
justinbuckland | 17:25831977b98e | 102 | { |
justinbuckland | 17:25831977b98e | 103 | if (uid[0]==drive_board_uid[i][0] && uid[1]==drive_board_uid[i][1] && uid[2]==drive_board_uid[i][2]) |
justinbuckland | 17:25831977b98e | 104 | { |
justinbuckland | 19:ab2d7aa17a20 | 105 | i_board = i; |
justinbuckland | 17:25831977b98e | 106 | i = UID_TABLE_LENGTH; |
justinbuckland | 17:25831977b98e | 107 | } |
justinbuckland | 17:25831977b98e | 108 | } |
justinbuckland | 17:25831977b98e | 109 | |
paullj | 21:4833e7fbcec7 | 110 | if (i_board != -1) pc.printf("Drive board: Board %d\n",drive_board_serial_number[i_board]); |
justinbuckland | 17:25831977b98e | 111 | else pc.printf("Drive board UID match not found\n"); |
justinbuckland | 17:25831977b98e | 112 | |
justinbuckland | 17:25831977b98e | 113 | pc.printf("iStep, eTime, I1, V1, R1_adc, R1_ohm, I2, V2, R2_adc, R2_ohm\n"); |
AlexStokoe | 0:362148ad8f6f | 114 | |
justinbuckland | 16:2c563709cf09 | 115 | wait_ms(START_DELAY); |
justinbuckland | 16:2c563709cf09 | 116 | timer.start(); |
AlexStokoe | 0:362148ad8f6f | 117 | |
justinbuckland | 25:f507cfd50fcc | 118 | for (int iDelay = 1; iDelay < 3; iDelay++) { |
justinbuckland | 24:899071abfc14 | 119 | |
sophiemeredith | 12:3f1df385d781 | 120 | for (int iStep=0; iStep<N_STEPS; iStep++) { |
justinbuckland | 16:2c563709cf09 | 121 | |
justinbuckland | 16:2c563709cf09 | 122 | eTime = timer.read_ms(); |
justinbuckland | 25:f507cfd50fcc | 123 | pc.printf("%5d, %10d,", iDelay, eTime); |
justinbuckland | 16:2c563709cf09 | 124 | |
justinbuckland | 16:2c563709cf09 | 125 | for (int iHeater=0; iHeater <2; iHeater++) { |
justinbuckland | 16:2c563709cf09 | 126 | // measure heater |
justinbuckland | 25:f507cfd50fcc | 127 | if (iHeater==0) { |
justinbuckland | 25:f507cfd50fcc | 128 | // drive_1.pulsewidth_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 129 | drive_1 = 1; |
justinbuckland | 25:f507cfd50fcc | 130 | led_0 = 1; |
justinbuckland | 25:f507cfd50fcc | 131 | led_1 = 0; |
justinbuckland | 25:f507cfd50fcc | 132 | } |
justinbuckland | 25:f507cfd50fcc | 133 | else { |
justinbuckland | 25:f507cfd50fcc | 134 | // drive_2.pulsewidth_ticks(1000); |
justinbuckland | 25:f507cfd50fcc | 135 | drive_2 = 1; |
justinbuckland | 25:f507cfd50fcc | 136 | led_0 = 0; |
justinbuckland | 25:f507cfd50fcc | 137 | led_1 = 1; |
justinbuckland | 25:f507cfd50fcc | 138 | } |
justinbuckland | 25:f507cfd50fcc | 139 | |
justinbuckland | 25:f507cfd50fcc | 140 | wait_us(MEAS_DELAY*iDelay); |
justinbuckland | 4:694f0e328a52 | 141 | |
justinbuckland | 25:f507cfd50fcc | 142 | //Average N_READ ADC cycles |
justinbuckland | 25:f507cfd50fcc | 143 | curr[iHeater] = 0; |
justinbuckland | 25:f507cfd50fcc | 144 | v[iHeater] = 0; |
justinbuckland | 25:f507cfd50fcc | 145 | for (int iRead = 0; iRead<N_READ; iRead++) { |
justinbuckland | 25:f507cfd50fcc | 146 | //Start ADC conversion |
justinbuckland | 25:f507cfd50fcc | 147 | adc.start_conversion(15); |
justinbuckland | 16:2c563709cf09 | 148 | |
justinbuckland | 25:f507cfd50fcc | 149 | //Wait until ADC is free |
justinbuckland | 25:f507cfd50fcc | 150 | while(adc_busy == 1) { |
justinbuckland | 25:f507cfd50fcc | 151 | wait_us(1); |
justinbuckland | 25:f507cfd50fcc | 152 | } |
justinbuckland | 16:2c563709cf09 | 153 | |
justinbuckland | 25:f507cfd50fcc | 154 | //Get ADC values |
justinbuckland | 25:f507cfd50fcc | 155 | adc.read_channels(); |
justinbuckland | 25:f507cfd50fcc | 156 | curr[iHeater] += adc.read_channel_result(i_port[iHeater]); |
justinbuckland | 25:f507cfd50fcc | 157 | v[iHeater] += adc.read_channel_result(v_port[iHeater]); |
justinbuckland | 25:f507cfd50fcc | 158 | } |
justinbuckland | 25:f507cfd50fcc | 159 | |
justinbuckland | 16:2c563709cf09 | 160 | //Turn off heater |
justinbuckland | 16:2c563709cf09 | 161 | if (iHeater==0) |
justinbuckland | 25:f507cfd50fcc | 162 | // drive_1.pulsewidth_ticks(0); |
justinbuckland | 25:f507cfd50fcc | 163 | drive_1 = 0; |
justinbuckland | 16:2c563709cf09 | 164 | else |
justinbuckland | 25:f507cfd50fcc | 165 | // drive_2.pulsewidth_ticks(0); |
justinbuckland | 25:f507cfd50fcc | 166 | drive_2 = 0; |
justinbuckland | 25:f507cfd50fcc | 167 | |
justinbuckland | 25:f507cfd50fcc | 168 | |
justinbuckland | 25:f507cfd50fcc | 169 | wait_us(MEAS_DELAY*iDelay); |
sophiemeredith | 14:d764e256ac6d | 170 | |
justinbuckland | 25:f507cfd50fcc | 171 | //Calculate resistance |
justinbuckland | 17:25831977b98e | 172 | r_adc[iHeater] = (float)v[iHeater]/(float)curr[iHeater]; |
paullj | 21:4833e7fbcec7 | 173 | if (i_board != -1) |
justinbuckland | 19:ab2d7aa17a20 | 174 | r_ohm[iHeater] = drive_board_cal[i_board][iHeater][0] + r_adc[iHeater]*drive_board_cal[i_board][iHeater][1]; |
justinbuckland | 17:25831977b98e | 175 | else |
justinbuckland | 17:25831977b98e | 176 | r_ohm[iHeater] = 0.0; |
justinbuckland | 17:25831977b98e | 177 | |
justinbuckland | 16:2c563709cf09 | 178 | |
justinbuckland | 16:2c563709cf09 | 179 | //Write output for iHeater |
justinbuckland | 19:ab2d7aa17a20 | 180 | pc.printf("%10d, %10d, %10.6f, %10.6f,", curr[iHeater], v[iHeater], r_adc[iHeater], r_ohm[iHeater]); |
justinbuckland | 24:899071abfc14 | 181 | |
justinbuckland | 24:899071abfc14 | 182 | //Wait before drivinng other heater |
justinbuckland | 24:899071abfc14 | 183 | wait_ms(LOG_INTERVAL/2); |
justinbuckland | 24:899071abfc14 | 184 | |
justinbuckland | 16:2c563709cf09 | 185 | } |
justinbuckland | 25:f507cfd50fcc | 186 | pc.printf("\n"); |
justinbuckland | 25:f507cfd50fcc | 187 | } |
justinbuckland | 25:f507cfd50fcc | 188 | |
justinbuckland | 16:2c563709cf09 | 189 | } |
justinbuckland | 24:899071abfc14 | 190 | |
justinbuckland | 25:f507cfd50fcc | 191 | // turn everything off |
justinbuckland | 25:f507cfd50fcc | 192 | drive_1 = 0; |
justinbuckland | 25:f507cfd50fcc | 193 | drive_2 = 0; |
justinbuckland | 25:f507cfd50fcc | 194 | led_0 = 0; |
justinbuckland | 25:f507cfd50fcc | 195 | led_1 = 0; |
justinbuckland | 25:f507cfd50fcc | 196 | |
justinbuckland | 5:67e4ee9a00dc | 197 | } |