ADC logging with demo drive board for calibration ADC read synchronised with heater on time
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@19:ab2d7aa17a20, 2019-09-20 (annotated)
- Committer:
- justinbuckland
- Date:
- Fri Sep 20 09:26:43 2019 +0000
- Revision:
- 19:ab2d7aa17a20
- Parent:
- 17:25831977b98e
- Child:
- 21:4833e7fbcec7
Fixed bugs in heater cal code
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 | 16:2c563709cf09 | 3 | #include "FastPWM.h" |
justinbuckland | 16:2c563709cf09 | 4 | #include "MODSERIAL.h" |
AlexStokoe | 0:362148ad8f6f | 5 | |
justinbuckland | 16:2c563709cf09 | 6 | #define MEAS_DELAY 60 // measurement delay after turning on FET (us) |
justinbuckland | 17:25831977b98e | 7 | #define LOG_INTERVAL 1000 // log file interval (ms) |
justinbuckland | 16:2c563709cf09 | 8 | #define START_DELAY 1000 // pause for startup (ms) |
justinbuckland | 17:25831977b98e | 9 | #define N_STEPS 20 |
justinbuckland | 16:2c563709cf09 | 10 | #define BUFFER_SIZE 4096 |
AlexStokoe | 0:362148ad8f6f | 11 | |
justinbuckland | 17:25831977b98e | 12 | //UID lookup address and pointer |
justinbuckland | 17:25831977b98e | 13 | #define UID_ADDR 0x1FFF7A10 |
justinbuckland | 17:25831977b98e | 14 | unsigned long *uid = (unsigned long *) UID_ADDR; |
justinbuckland | 17:25831977b98e | 15 | |
justinbuckland | 17:25831977b98e | 16 | //UID and drive board calibration table |
justinbuckland | 17:25831977b98e | 17 | #define UID_TABLE_LENGTH 4 |
justinbuckland | 17:25831977b98e | 18 | |
justinbuckland | 17:25831977b98e | 19 | int drive_board_serial_number[UID_TABLE_LENGTH] = |
justinbuckland | 17:25831977b98e | 20 | {1, |
justinbuckland | 17:25831977b98e | 21 | 2, |
justinbuckland | 17:25831977b98e | 22 | 3, |
justinbuckland | 17:25831977b98e | 23 | 4}; |
justinbuckland | 17:25831977b98e | 24 | |
justinbuckland | 17:25831977b98e | 25 | unsigned long drive_board_uid[UID_TABLE_LENGTH][3] = |
justinbuckland | 17:25831977b98e | 26 | {{0x005B0060, 0x32375101, 0x32363531}, |
justinbuckland | 17:25831977b98e | 27 | {0x00000000, 0x00000000, 0x00000000}, |
justinbuckland | 17:25831977b98e | 28 | {0x00000000, 0x00000000, 0x00000000}, |
justinbuckland | 17:25831977b98e | 29 | {0x00000000, 0x00000000, 0x00000000}}; |
justinbuckland | 17:25831977b98e | 30 | |
justinbuckland | 19:ab2d7aa17a20 | 31 | float drive_board_cal[UID_TABLE_LENGTH][2][2] = |
justinbuckland | 19:ab2d7aa17a20 | 32 | {{{0.0, 1.0}, {0.0, 1.0}}, |
justinbuckland | 19:ab2d7aa17a20 | 33 | {{0.0, 1.0}, {0.0, 1.0}}, |
justinbuckland | 19:ab2d7aa17a20 | 34 | {{0.0, 1.0}, {0.0, 1.0}}, |
justinbuckland | 19:ab2d7aa17a20 | 35 | {{0.0, 1.0}, {0.0, 1.0}}}; |
justinbuckland | 17:25831977b98e | 36 | |
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 | 16:2c563709cf09 | 45 | FastPWM drive_1(PC_9); |
justinbuckland | 16:2c563709cf09 | 46 | FastPWM drive_2(PC_8); |
justinbuckland | 16:2c563709cf09 | 47 | FastPWM guard_1(PC_7); |
justinbuckland | 16:2c563709cf09 | 48 | FastPWM 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 | 16:2c563709cf09 | 55 | //User buttons |
justinbuckland | 16:2c563709cf09 | 56 | DigitalIn user_0(PB_0); |
justinbuckland | 16:2c563709cf09 | 57 | DigitalIn user_1(PB_1); |
justinbuckland | 16:2c563709cf09 | 58 | |
justinbuckland | 16:2c563709cf09 | 59 | BusOut converts(PC_0, PC_1, PC_2, PC_3); |
AlexStokoe | 0:362148ad8f6f | 60 | |
AlexStokoe | 0:362148ad8f6f | 61 | int main() { |
sophiemeredith | 11:8e6c8e654004 | 62 | int eTime; |
justinbuckland | 16:2c563709cf09 | 63 | int v[2], curr[2]; |
justinbuckland | 17:25831977b98e | 64 | double r_adc[2], r_ohm[2]; |
justinbuckland | 16:2c563709cf09 | 65 | |
justinbuckland | 16:2c563709cf09 | 66 | int i_port[2] = {0,2}; |
justinbuckland | 16:2c563709cf09 | 67 | int v_port[2] = {1,3}; |
justinbuckland | 2:23f848b21b09 | 68 | |
justinbuckland | 16:2c563709cf09 | 69 | pc.baud(115200); |
justinbuckland | 16:2c563709cf09 | 70 | adc.init(); |
AlexStokoe | 0:362148ad8f6f | 71 | |
justinbuckland | 16:2c563709cf09 | 72 | // Initialsation - all heaters off |
justinbuckland | 16:2c563709cf09 | 73 | drive_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 74 | drive_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 75 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 76 | |
justinbuckland | 16:2c563709cf09 | 77 | guard_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 78 | guard_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 79 | guard_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 80 | |
justinbuckland | 16:2c563709cf09 | 81 | drive_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 82 | drive_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 83 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 84 | |
justinbuckland | 16:2c563709cf09 | 85 | guard_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 86 | guard_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 87 | guard_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 88 | |
justinbuckland | 17:25831977b98e | 89 | pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]); |
justinbuckland | 17:25831977b98e | 90 | int i_board = 0; |
justinbuckland | 17:25831977b98e | 91 | for (int i = 0; i < UID_TABLE_LENGTH; i++) |
justinbuckland | 17:25831977b98e | 92 | { |
justinbuckland | 17:25831977b98e | 93 | 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 | 94 | { |
justinbuckland | 19:ab2d7aa17a20 | 95 | i_board = i; |
justinbuckland | 17:25831977b98e | 96 | i = UID_TABLE_LENGTH; |
justinbuckland | 17:25831977b98e | 97 | } |
justinbuckland | 17:25831977b98e | 98 | } |
justinbuckland | 17:25831977b98e | 99 | |
justinbuckland | 17:25831977b98e | 100 | if (i_board > 0) pc.printf("Drive board: BRD%d\n",i_board); |
justinbuckland | 17:25831977b98e | 101 | else pc.printf("Drive board UID match not found\n"); |
justinbuckland | 17:25831977b98e | 102 | |
justinbuckland | 17:25831977b98e | 103 | pc.printf("iStep, eTime, I1, V1, R1_adc, R1_ohm, I2, V2, R2_adc, R2_ohm\n"); |
AlexStokoe | 0:362148ad8f6f | 104 | |
justinbuckland | 16:2c563709cf09 | 105 | wait_ms(START_DELAY); |
justinbuckland | 16:2c563709cf09 | 106 | timer.start(); |
AlexStokoe | 0:362148ad8f6f | 107 | |
sophiemeredith | 12:3f1df385d781 | 108 | for (int iStep=0; iStep<N_STEPS; iStep++) { |
justinbuckland | 16:2c563709cf09 | 109 | |
justinbuckland | 16:2c563709cf09 | 110 | eTime = timer.read_ms(); |
justinbuckland | 16:2c563709cf09 | 111 | pc.printf("%5d, %10d,", iStep, eTime); |
justinbuckland | 16:2c563709cf09 | 112 | |
justinbuckland | 16:2c563709cf09 | 113 | for (int iHeater=0; iHeater <2; iHeater++) { |
justinbuckland | 16:2c563709cf09 | 114 | // measure heater |
justinbuckland | 16:2c563709cf09 | 115 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 116 | drive_1.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 117 | else |
justinbuckland | 16:2c563709cf09 | 118 | drive_2.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 119 | wait_us(MEAS_DELAY); |
justinbuckland | 4:694f0e328a52 | 120 | |
justinbuckland | 16:2c563709cf09 | 121 | //Start ADC conversion |
justinbuckland | 16:2c563709cf09 | 122 | adc.start_conversion(15); |
justinbuckland | 16:2c563709cf09 | 123 | |
justinbuckland | 16:2c563709cf09 | 124 | //Wait until ADC is free |
justinbuckland | 16:2c563709cf09 | 125 | while(adc_busy == 1) { |
justinbuckland | 16:2c563709cf09 | 126 | wait_us(1); |
justinbuckland | 16:2c563709cf09 | 127 | } |
justinbuckland | 16:2c563709cf09 | 128 | |
justinbuckland | 16:2c563709cf09 | 129 | //Turn off heater |
justinbuckland | 16:2c563709cf09 | 130 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 131 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 132 | else |
justinbuckland | 16:2c563709cf09 | 133 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 134 | wait_us(MEAS_DELAY); |
sophiemeredith | 14:d764e256ac6d | 135 | |
justinbuckland | 16:2c563709cf09 | 136 | //Get ADC values |
justinbuckland | 16:2c563709cf09 | 137 | adc.read_channels(); |
justinbuckland | 16:2c563709cf09 | 138 | curr[iHeater] = adc.read_channel_result(i_port[iHeater]); |
justinbuckland | 16:2c563709cf09 | 139 | v[iHeater] = adc.read_channel_result(v_port[iHeater]); |
justinbuckland | 17:25831977b98e | 140 | r_adc[iHeater] = (float)v[iHeater]/(float)curr[iHeater]; |
justinbuckland | 17:25831977b98e | 141 | if (i_board > 0) |
justinbuckland | 19:ab2d7aa17a20 | 142 | r_ohm[iHeater] = drive_board_cal[i_board][iHeater][0] + r_adc[iHeater]*drive_board_cal[i_board][iHeater][1]; |
justinbuckland | 17:25831977b98e | 143 | else |
justinbuckland | 17:25831977b98e | 144 | r_ohm[iHeater] = 0.0; |
justinbuckland | 17:25831977b98e | 145 | |
justinbuckland | 16:2c563709cf09 | 146 | |
justinbuckland | 16:2c563709cf09 | 147 | //Write output for iHeater |
justinbuckland | 19:ab2d7aa17a20 | 148 | pc.printf("%10d, %10d, %10.6f, %10.6f,", curr[iHeater], v[iHeater], r_adc[iHeater], r_ohm[iHeater]); |
justinbuckland | 16:2c563709cf09 | 149 | } |
justinbuckland | 16:2c563709cf09 | 150 | pc.printf("\n"); |
justinbuckland | 9:3c5a43ce68bb | 151 | wait_ms(LOG_INTERVAL); |
justinbuckland | 16:2c563709cf09 | 152 | } |
justinbuckland | 5:67e4ee9a00dc | 153 | } |