Heater for threaded program

Dependents:   LEX_Threaded_Programming

Committer:
omatthews
Date:
Wed Jul 17 13:55:33 2019 +0000
Revision:
2:7f15386fcc90
Parent:
1:4435d407d827
Child:
3:313711a66929
Child:
7:59ece353eea2
Commit for testing ADC error;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:4e33cc8171f4 1 /*------------------------------------------------------------------------------
omatthews 0:4e33cc8171f4 2 Library code file for interface to Heater
omatthews 0:4e33cc8171f4 3 Date: 16/07/2018
omatthews 0:4e33cc8171f4 4
omatthews 0:4e33cc8171f4 5
omatthews 0:4e33cc8171f4 6 ------------------------------------------------------------------------------*/
omatthews 1:4435d407d827 7 #include "mbed.h"
omatthews 1:4435d407d827 8 #include "MODSERIAL.h"
omatthews 0:4e33cc8171f4 9 #include "Heater.h"
omatthews 0:4e33cc8171f4 10 #include "ADS8568_ADC.h"
omatthews 0:4e33cc8171f4 11
omatthews 0:4e33cc8171f4 12 extern ADS8568_ADC adc;
omatthews 0:4e33cc8171f4 13 extern float scale_factors[8];
omatthews 0:4e33cc8171f4 14 extern Timer timer;
omatthews 1:4435d407d827 15 extern DigitalIn adc_busy;
omatthews 1:4435d407d827 16 extern MODSERIAL pc;
omatthews 1:4435d407d827 17
omatthews 0:4e33cc8171f4 18
omatthews 0:4e33cc8171f4 19
omatthews 0:4e33cc8171f4 20 Heater::Heater(int i_port, int v_port, DigitalOut drive, float R_set)
omatthews 1:4435d407d827 21 :R_set(R_set),i_port(i_port),v_port(v_port),drive(drive){}
omatthews 0:4e33cc8171f4 22
omatthews 0:4e33cc8171f4 23
omatthews 2:7f15386fcc90 24 void Heater::read()
omatthews 0:4e33cc8171f4 25 {
omatthews 1:4435d407d827 26 //Reads R and then resets the drive back to its previous value
omatthews 1:4435d407d827 27 int i = 0;
omatthews 1:4435d407d827 28 int drive_prev = drive;
omatthews 1:4435d407d827 29 drive = 1;
omatthews 1:4435d407d827 30 wait_us(10);
omatthews 1:4435d407d827 31 adc.start_conversion(ALL_CH);
omatthews 1:4435d407d827 32 while(adc_busy == 1)
omatthews 1:4435d407d827 33 {
omatthews 1:4435d407d827 34 wait_us(1);
omatthews 1:4435d407d827 35 i++;
omatthews 1:4435d407d827 36 }
omatthews 2:7f15386fcc90 37
omatthews 2:7f15386fcc90 38 drive = drive_prev;
omatthews 2:7f15386fcc90 39
omatthews 0:4e33cc8171f4 40 adc.read_channels();
omatthews 1:4435d407d827 41
omatthews 1:4435d407d827 42 //pc.printf("conversion took %d us\n", i );
omatthews 1:4435d407d827 43 i=0;
omatthews 1:4435d407d827 44
omatthews 1:4435d407d827 45
omatthews 1:4435d407d827 46 curr = adc.read_channel_result(i_port)/scale_factors[i_port];
omatthews 0:4e33cc8171f4 47 v = adc.read_channel_result(v_port)/scale_factors[v_port];
omatthews 2:7f15386fcc90 48 if (curr > 0) R = float(v)/float(curr);
omatthews 0:4e33cc8171f4 49 }
omatthews 0:4e33cc8171f4 50
omatthews 2:7f15386fcc90 51
omatthews 2:7f15386fcc90 52
omatthews 2:7f15386fcc90 53
omatthews 0:4e33cc8171f4 54 void Heater::hold(int hold_time)
omatthews 0:4e33cc8171f4 55 {
omatthews 0:4e33cc8171f4 56 int end_time = timer.read_ms() + hold_time;
omatthews 1:4435d407d827 57 pc.printf("end time is %d \n",end_time);
omatthews 1:4435d407d827 58 float R_avg = 0;
omatthews 1:4435d407d827 59 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 60 {
omatthews 0:4e33cc8171f4 61 drive = 1;
omatthews 0:4e33cc8171f4 62 wait_us(MEAS_DELAY);
omatthews 2:7f15386fcc90 63 read();
omatthews 1:4435d407d827 64 R_avg = ((N_ROLL_AVG-1)*R_avg + R)/N_ROLL_AVG;
omatthews 1:4435d407d827 65 if (R_avg > R_set)
omatthews 0:4e33cc8171f4 66 {
omatthews 0:4e33cc8171f4 67 drive = 0;
omatthews 1:4435d407d827 68 wait_us(2*MEAS_DELAY);
omatthews 0:4e33cc8171f4 69 }
omatthews 1:4435d407d827 70
omatthews 0:4e33cc8171f4 71 }
omatthews 1:4435d407d827 72 }
omatthews 1:4435d407d827 73
omatthews 0:4e33cc8171f4 74
omatthews 0:4e33cc8171f4 75 void Heater::Set_R_set(float R) {R_set = R;}
omatthews 0:4e33cc8171f4 76
omatthews 2:7f15386fcc90 77 int Heater::Get_i() {return curr;}
omatthews 2:7f15386fcc90 78 int Heater::Get_v() {return v;}
omatthews 2:7f15386fcc90 79
omatthews 0:4e33cc8171f4 80 float Heater::Get_R() {return R;}
omatthews 0:4e33cc8171f4 81
omatthews 1:4435d407d827 82 void Heater::turn_on () {drive = 1;}
omatthews 1:4435d407d827 83
omatthews 1:4435d407d827 84 void Heater::turn_off () {drive = 0;}