Temp setting is updated by a thread. Main loop grabs, prints, and manages temperature feedback
Fork of Temperature_Reading1 by
main.cpp@2:2e95521439cb, 2017-08-29 (annotated)
- Committer:
- iwolf32
- Date:
- Tue Aug 29 14:35:13 2017 +0000
- Revision:
- 2:2e95521439cb
- Parent:
- 1:1de97b1145f3
Update Threading version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
212600191 | 0:1f9fae46bbe1 | 1 | #include "mbed.h" |
iwolf32 | 2:2e95521439cb | 2 | #include "rtos.h" |
212600191 | 1:1de97b1145f3 | 3 | |
212600191 | 1:1de97b1145f3 | 4 | // Read temperature from MAX1363 3-channel ADC |
212600191 | 1:1de97b1145f3 | 5 | |
212600191 | 1:1de97b1145f3 | 6 | I2C i2c(PB_9 , PB_8); |
212600191 | 1:1de97b1145f3 | 7 | |
iwolf32 | 2:2e95521439cb | 8 | DigitalOut Relay(D7); |
iwolf32 | 2:2e95521439cb | 9 | |
212600191 | 1:1de97b1145f3 | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
212600191 | 1:1de97b1145f3 | 11 | |
iwolf32 | 2:2e95521439cb | 12 | float x=0; |
iwolf32 | 2:2e95521439cb | 13 | double SetTemp=0; |
iwolf32 | 2:2e95521439cb | 14 | Thread UpdatePressure_t; |
iwolf32 | 2:2e95521439cb | 15 | |
212600191 | 1:1de97b1145f3 | 16 | // address information for the Serenity temperature sensor device |
212600191 | 1:1de97b1145f3 | 17 | const int SLAVE_ADDRESS = 0x34 << 1; |
212600191 | 1:1de97b1145f3 | 18 | |
212600191 | 1:1de97b1145f3 | 19 | // MAX1363 registers |
212600191 | 1:1de97b1145f3 | 20 | const unsigned char MAX1363_CONFIG_REG = 0x03; // single-ended inputs, read CH0-CH1 only |
212600191 | 1:1de97b1145f3 | 21 | const unsigned char MAX1363_SETUP_REG = 0xF2; // AIN3=output, INT REF always on |
212600191 | 1:1de97b1145f3 | 22 | |
212600191 | 1:1de97b1145f3 | 23 | // MAX1363 high byte format (byte 2 is D7-D0) |
212600191 | 1:1de97b1145f3 | 24 | const unsigned char MAX1363_VALID_MASK = 0x90; // HIGH bit and 12/10 bit should always be 1 |
212600191 | 1:1de97b1145f3 | 25 | const unsigned char MAX1363_HIGH_DATA_MASK = 0x0F; |
212600191 | 1:1de97b1145f3 | 26 | |
212600191 | 1:1de97b1145f3 | 27 | // channel address (0-3) is in CH1/CH0 (bits 5/6) |
212600191 | 1:1de97b1145f3 | 28 | const unsigned char MAX1363_CHANNEL_MASK = 0x60; |
212600191 | 1:1de97b1145f3 | 29 | const unsigned char MAX1363_CHANNEL_SHIFT = 5; |
212600191 | 0:1f9fae46bbe1 | 30 | |
212600191 | 1:1de97b1145f3 | 31 | /** conversion factors */ |
212600191 | 1:1de97b1145f3 | 32 | const float CONV_COEFF_A3 = 4.39403; |
212600191 | 1:1de97b1145f3 | 33 | const float CONV_COEFF_A2 = -11.15; |
212600191 | 1:1de97b1145f3 | 34 | const float CONV_COEFF_A1 = 36.4955; |
212600191 | 1:1de97b1145f3 | 35 | const float CONV_COEFF_A0 = 266.909; |
212600191 | 1:1de97b1145f3 | 36 | |
212600191 | 1:1de97b1145f3 | 37 | const int CONV_N = 12; |
212600191 | 1:1de97b1145f3 | 38 | const float CONV_VREF = 2.048; |
212600191 | 1:1de97b1145f3 | 39 | |
212600191 | 1:1de97b1145f3 | 40 | const float CONV_K_TO_C = -272.15; |
212600191 | 0:1f9fae46bbe1 | 41 | |
212600191 | 1:1de97b1145f3 | 42 | float AdcToC(unsigned int _adc) |
212600191 | 1:1de97b1145f3 | 43 | { |
212600191 | 1:1de97b1145f3 | 44 | float d = CONV_VREF * _adc / (1L << CONV_N); |
212600191 | 1:1de97b1145f3 | 45 | float d2 = d*d; |
212600191 | 1:1de97b1145f3 | 46 | float d3 = d2*d; |
212600191 | 1:1de97b1145f3 | 47 | float temp_k = (CONV_COEFF_A3 * d3) + |
212600191 | 1:1de97b1145f3 | 48 | (CONV_COEFF_A2 * d2) + |
212600191 | 1:1de97b1145f3 | 49 | (CONV_COEFF_A1 * d) + |
212600191 | 1:1de97b1145f3 | 50 | CONV_COEFF_A0; |
212600191 | 1:1de97b1145f3 | 51 | return temp_k + CONV_K_TO_C; |
212600191 | 1:1de97b1145f3 | 52 | } |
iwolf32 | 2:2e95521439cb | 53 | //------------------------------------------------------------------ |
iwolf32 | 2:2e95521439cb | 54 | //Function for Getting Temperature Setting |
iwolf32 | 2:2e95521439cb | 55 | void TempSetInit(){ |
iwolf32 | 2:2e95521439cb | 56 | char buffer[]={'0','0','0','0','0','\0'}; |
iwolf32 | 2:2e95521439cb | 57 | char ch; |
212600191 | 0:1f9fae46bbe1 | 58 | |
iwolf32 | 2:2e95521439cb | 59 | while(pc.readable()==0){ |
iwolf32 | 2:2e95521439cb | 60 | } |
iwolf32 | 2:2e95521439cb | 61 | if (pc.readable()==1){ |
iwolf32 | 2:2e95521439cb | 62 | for (int index=0; index<5; index=index+1){ |
iwolf32 | 2:2e95521439cb | 63 | ch=pc.getc(); |
iwolf32 | 2:2e95521439cb | 64 | buffer[index]=ch; |
iwolf32 | 2:2e95521439cb | 65 | } |
iwolf32 | 2:2e95521439cb | 66 | SetTemp= atof(buffer); |
iwolf32 | 2:2e95521439cb | 67 | |
iwolf32 | 2:2e95521439cb | 68 | } |
iwolf32 | 2:2e95521439cb | 69 | } |
iwolf32 | 2:2e95521439cb | 70 | void TempSet(){ |
iwolf32 | 2:2e95521439cb | 71 | while(1){ |
iwolf32 | 2:2e95521439cb | 72 | char buffer[]={'0','0','0','0','0','\0'}; |
iwolf32 | 2:2e95521439cb | 73 | char ch; |
iwolf32 | 2:2e95521439cb | 74 | |
iwolf32 | 2:2e95521439cb | 75 | while(pc.readable()==0){ |
iwolf32 | 2:2e95521439cb | 76 | } |
iwolf32 | 2:2e95521439cb | 77 | if (pc.readable()==1){ |
iwolf32 | 2:2e95521439cb | 78 | wait(0.1); |
iwolf32 | 2:2e95521439cb | 79 | for (int index=0; index<5; index=index+1){ |
iwolf32 | 2:2e95521439cb | 80 | ch=pc.getc(); |
iwolf32 | 2:2e95521439cb | 81 | buffer[index]=ch; |
iwolf32 | 2:2e95521439cb | 82 | } |
iwolf32 | 2:2e95521439cb | 83 | SetTemp= atof(buffer); |
iwolf32 | 2:2e95521439cb | 84 | } |
iwolf32 | 2:2e95521439cb | 85 | } |
iwolf32 | 2:2e95521439cb | 86 | } |
iwolf32 | 2:2e95521439cb | 87 | //------------------------------------------------------------------ |
212600191 | 0:1f9fae46bbe1 | 88 | int main() { |
iwolf32 | 2:2e95521439cb | 89 | pc.baud(115200); |
212600191 | 1:1de97b1145f3 | 90 | |
212600191 | 1:1de97b1145f3 | 91 | // reserve 4 bytes for transfer |
212600191 | 1:1de97b1145f3 | 92 | char cmd[4]; |
212600191 | 1:1de97b1145f3 | 93 | |
212600191 | 1:1de97b1145f3 | 94 | // initialize the adc device |
212600191 | 1:1de97b1145f3 | 95 | cmd[0] = MAX1363_CONFIG_REG; |
212600191 | 1:1de97b1145f3 | 96 | cmd[1] = MAX1363_SETUP_REG; |
212600191 | 1:1de97b1145f3 | 97 | i2c.write(SLAVE_ADDRESS, cmd, 2); |
212600191 | 1:1de97b1145f3 | 98 | |
212600191 | 1:1de97b1145f3 | 99 | // holds raw adc values as we convert them |
212600191 | 1:1de97b1145f3 | 100 | unsigned int adc[2] = {0}; |
212600191 | 1:1de97b1145f3 | 101 | float values[2] = {0}; |
212600191 | 1:1de97b1145f3 | 102 | |
iwolf32 | 2:2e95521439cb | 103 | //Get initial Temperature Setting |
iwolf32 | 2:2e95521439cb | 104 | TempSetInit(); |
iwolf32 | 2:2e95521439cb | 105 | |
iwolf32 | 2:2e95521439cb | 106 | //Continuously check for new temp settings |
iwolf32 | 2:2e95521439cb | 107 | UpdatePressure_t.set_priority(osPriorityHigh); |
iwolf32 | 2:2e95521439cb | 108 | UpdatePressure_t.start(TempSet); |
iwolf32 | 2:2e95521439cb | 109 | |
212600191 | 0:1f9fae46bbe1 | 110 | while (1) { |
iwolf32 | 2:2e95521439cb | 111 | wait(0.05); |
212600191 | 1:1de97b1145f3 | 112 | |
212600191 | 1:1de97b1145f3 | 113 | // clear buffer, only to detect invalid data |
212600191 | 0:1f9fae46bbe1 | 114 | cmd[0] = 0x00; |
212600191 | 1:1de97b1145f3 | 115 | cmd[1] = 0x00; |
212600191 | 1:1de97b1145f3 | 116 | cmd[2] = 0x00; |
212600191 | 1:1de97b1145f3 | 117 | cmd[3] = 0x00; |
212600191 | 1:1de97b1145f3 | 118 | |
212600191 | 1:1de97b1145f3 | 119 | // read 4 bytes from |
212600191 | 1:1de97b1145f3 | 120 | i2c.read(SLAVE_ADDRESS, cmd, 4); |
212600191 | 1:1de97b1145f3 | 121 | |
212600191 | 1:1de97b1145f3 | 122 | for (int i=0; i<4; i+=2) { |
212600191 | 1:1de97b1145f3 | 123 | // first 4 bits contain expected bits and channel id |
212600191 | 1:1de97b1145f3 | 124 | if ((cmd[i] & MAX1363_VALID_MASK) == MAX1363_VALID_MASK) { |
212600191 | 1:1de97b1145f3 | 125 | unsigned char ch = ((cmd[i] & MAX1363_CHANNEL_MASK) >> MAX1363_CHANNEL_SHIFT); |
212600191 | 1:1de97b1145f3 | 126 | adc[ch] = ((int)(cmd[i] & MAX1363_HIGH_DATA_MASK) << 8) | cmd[i+1]; |
212600191 | 1:1de97b1145f3 | 127 | values[ch] = AdcToC(adc[ch]); |
212600191 | 1:1de97b1145f3 | 128 | } else { |
212600191 | 1:1de97b1145f3 | 129 | // handle invalid data |
212600191 | 1:1de97b1145f3 | 130 | } |
212600191 | 1:1de97b1145f3 | 131 | } |
212600191 | 1:1de97b1145f3 | 132 | |
iwolf32 | 2:2e95521439cb | 133 | //Temperature control |
iwolf32 | 2:2e95521439cb | 134 | x=values[1]; |
iwolf32 | 2:2e95521439cb | 135 | if (x<SetTemp){ |
iwolf32 | 2:2e95521439cb | 136 | Relay=0; |
iwolf32 | 2:2e95521439cb | 137 | } |
iwolf32 | 2:2e95521439cb | 138 | if (x>SetTemp+1){ |
iwolf32 | 2:2e95521439cb | 139 | Relay=1; |
iwolf32 | 2:2e95521439cb | 140 | } |
iwolf32 | 2:2e95521439cb | 141 | printf("%0.2f\t %0.2f\t %0.2f\r\n", SetTemp, values[0], values[1]); |
212600191 | 0:1f9fae46bbe1 | 142 | } |
212600191 | 1:1de97b1145f3 | 143 | } |