ADC logging with demo drive board for calibration
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@16:2c563709cf09, 2019-09-13 (annotated)
- Committer:
- justinbuckland
- Date:
- Fri Sep 13 10:29:44 2019 +0000
- Revision:
- 16:2c563709cf09
- Parent:
- 15:a08527d78ffa
- Child:
- 17:25831977b98e
Initial version, logs I, V, R every 5s with non-simultaneous drive of both heaters
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 | 16:2c563709cf09 | 7 | #define LOG_INTERVAL 5000 // log file interval (ms) |
justinbuckland | 16:2c563709cf09 | 8 | #define START_DELAY 1000 // pause for startup (ms) |
justinbuckland | 16:2c563709cf09 | 9 | #define N_STEPS 1000 |
justinbuckland | 16:2c563709cf09 | 10 | #define BUFFER_SIZE 4096 |
AlexStokoe | 0:362148ad8f6f | 11 | |
justinbuckland | 16:2c563709cf09 | 12 | MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers |
justinbuckland | 16:2c563709cf09 | 13 | ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3); |
justinbuckland | 16:2c563709cf09 | 14 | I2C i2c(PB_7, PB_8); //SDA, SCL |
sophiemeredith | 11:8e6c8e654004 | 15 | Timer timer; |
justinbuckland | 16:2c563709cf09 | 16 | DigitalIn adc_busy(PA_8); //Busy interrupt sig# |
sophiemeredith | 11:8e6c8e654004 | 17 | |
justinbuckland | 16:2c563709cf09 | 18 | //Heater Control |
justinbuckland | 16:2c563709cf09 | 19 | FastPWM drive_1(PC_9); |
justinbuckland | 16:2c563709cf09 | 20 | FastPWM drive_2(PC_8); |
justinbuckland | 16:2c563709cf09 | 21 | FastPWM guard_1(PC_7); |
justinbuckland | 16:2c563709cf09 | 22 | FastPWM guard_2(PC_6); |
AlexStokoe | 0:362148ad8f6f | 23 | |
justinbuckland | 16:2c563709cf09 | 24 | //Indicator LEDs |
justinbuckland | 16:2c563709cf09 | 25 | DigitalOut hb_led(PC_13); //Green |
justinbuckland | 16:2c563709cf09 | 26 | DigitalOut led_0(PC_4); //Red |
justinbuckland | 16:2c563709cf09 | 27 | DigitalOut led_1(PC_5); //Green |
justinbuckland | 16:2c563709cf09 | 28 | |
justinbuckland | 16:2c563709cf09 | 29 | //User buttons |
justinbuckland | 16:2c563709cf09 | 30 | DigitalIn user_0(PB_0); |
justinbuckland | 16:2c563709cf09 | 31 | DigitalIn user_1(PB_1); |
justinbuckland | 16:2c563709cf09 | 32 | |
justinbuckland | 16:2c563709cf09 | 33 | BusOut converts(PC_0, PC_1, PC_2, PC_3); |
AlexStokoe | 0:362148ad8f6f | 34 | |
AlexStokoe | 0:362148ad8f6f | 35 | int main() { |
sophiemeredith | 11:8e6c8e654004 | 36 | int eTime; |
justinbuckland | 16:2c563709cf09 | 37 | int v[2], curr[2]; |
justinbuckland | 16:2c563709cf09 | 38 | double r[2]; |
justinbuckland | 16:2c563709cf09 | 39 | |
justinbuckland | 16:2c563709cf09 | 40 | int i_port[2] = {0,2}; |
justinbuckland | 16:2c563709cf09 | 41 | int v_port[2] = {1,3}; |
justinbuckland | 2:23f848b21b09 | 42 | |
justinbuckland | 16:2c563709cf09 | 43 | pc.baud(115200); |
justinbuckland | 16:2c563709cf09 | 44 | adc.init(); |
AlexStokoe | 0:362148ad8f6f | 45 | |
justinbuckland | 16:2c563709cf09 | 46 | // Initialsation - all heaters off |
justinbuckland | 16:2c563709cf09 | 47 | drive_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 48 | drive_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 49 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 50 | |
justinbuckland | 16:2c563709cf09 | 51 | guard_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 52 | guard_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 53 | guard_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 54 | |
justinbuckland | 16:2c563709cf09 | 55 | drive_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 56 | drive_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 57 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 58 | |
justinbuckland | 16:2c563709cf09 | 59 | guard_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 60 | guard_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 61 | guard_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 62 | |
justinbuckland | 16:2c563709cf09 | 63 | pc.printf("iStep, eTime, I1, V1, R1, I2, V2, R2\n"); |
AlexStokoe | 0:362148ad8f6f | 64 | |
justinbuckland | 16:2c563709cf09 | 65 | wait_ms(START_DELAY); |
justinbuckland | 16:2c563709cf09 | 66 | timer.start(); |
AlexStokoe | 0:362148ad8f6f | 67 | |
sophiemeredith | 12:3f1df385d781 | 68 | for (int iStep=0; iStep<N_STEPS; iStep++) { |
justinbuckland | 16:2c563709cf09 | 69 | |
justinbuckland | 16:2c563709cf09 | 70 | eTime = timer.read_ms(); |
justinbuckland | 16:2c563709cf09 | 71 | pc.printf("%5d, %10d,", iStep, eTime); |
justinbuckland | 16:2c563709cf09 | 72 | |
justinbuckland | 16:2c563709cf09 | 73 | for (int iHeater=0; iHeater <2; iHeater++) { |
justinbuckland | 16:2c563709cf09 | 74 | // measure heater |
justinbuckland | 16:2c563709cf09 | 75 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 76 | drive_1.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 77 | else |
justinbuckland | 16:2c563709cf09 | 78 | drive_2.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 79 | wait_us(MEAS_DELAY); |
justinbuckland | 4:694f0e328a52 | 80 | |
justinbuckland | 16:2c563709cf09 | 81 | //Start ADC conversion |
justinbuckland | 16:2c563709cf09 | 82 | adc.start_conversion(15); |
justinbuckland | 16:2c563709cf09 | 83 | |
justinbuckland | 16:2c563709cf09 | 84 | //Wait until ADC is free |
justinbuckland | 16:2c563709cf09 | 85 | while(adc_busy == 1) { |
justinbuckland | 16:2c563709cf09 | 86 | wait_us(1); |
justinbuckland | 16:2c563709cf09 | 87 | } |
justinbuckland | 16:2c563709cf09 | 88 | |
justinbuckland | 16:2c563709cf09 | 89 | //Turn off heater |
justinbuckland | 16:2c563709cf09 | 90 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 91 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 92 | else |
justinbuckland | 16:2c563709cf09 | 93 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 94 | wait_us(MEAS_DELAY); |
sophiemeredith | 14:d764e256ac6d | 95 | |
justinbuckland | 16:2c563709cf09 | 96 | //Get ADC values |
justinbuckland | 16:2c563709cf09 | 97 | adc.read_channels(); |
justinbuckland | 16:2c563709cf09 | 98 | curr[iHeater] = adc.read_channel_result(i_port[iHeater]); |
justinbuckland | 16:2c563709cf09 | 99 | v[iHeater] = adc.read_channel_result(v_port[iHeater]); |
justinbuckland | 16:2c563709cf09 | 100 | r[iHeater] = (float)v[iHeater]/(float)curr[iHeater]; |
justinbuckland | 16:2c563709cf09 | 101 | |
justinbuckland | 16:2c563709cf09 | 102 | //Write output for iHeater |
justinbuckland | 16:2c563709cf09 | 103 | pc.printf("%10d, %10d, %10.6f", curr[iHeater], v[iHeater], r[iHeater]); |
justinbuckland | 16:2c563709cf09 | 104 | } |
justinbuckland | 16:2c563709cf09 | 105 | pc.printf("\n"); |
justinbuckland | 9:3c5a43ce68bb | 106 | wait_ms(LOG_INTERVAL); |
justinbuckland | 16:2c563709cf09 | 107 | } |
justinbuckland | 5:67e4ee9a00dc | 108 | } |