Ian Wolf
/
InjectorValveFourthMicro1
Fork of InjectorValveFourthMicro by
main.cpp@2:4425049f4174, 2017-08-31 (annotated)
- Committer:
- iwolf32
- Date:
- Thu Aug 31 13:57:01 2017 +0000
- Revision:
- 2:4425049f4174
- Parent:
- 1:1de97b1145f3
- Child:
- 3:0f441a513e2c
No character constraints;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
212600191 | 0:1f9fae46bbe1 | 1 | #include "mbed.h" |
iwolf32 | 2:4425049f4174 | 2 | #include <string> |
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); |
iwolf32 | 2:4425049f4174 | 7 | DigitalOut Relay(D7); |
iwolf32 | 2:4425049f4174 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
212600191 | 1:1de97b1145f3 | 9 | |
iwolf32 | 2:4425049f4174 | 10 | std::string str; |
iwolf32 | 2:4425049f4174 | 11 | char ch; |
iwolf32 | 2:4425049f4174 | 12 | double SetTemp=0; |
iwolf32 | 2:4425049f4174 | 13 | float x=0; |
212600191 | 1:1de97b1145f3 | 14 | |
212600191 | 1:1de97b1145f3 | 15 | // address information for the Serenity temperature sensor device |
212600191 | 1:1de97b1145f3 | 16 | const int SLAVE_ADDRESS = 0x34 << 1; |
212600191 | 1:1de97b1145f3 | 17 | |
212600191 | 1:1de97b1145f3 | 18 | // MAX1363 registers |
212600191 | 1:1de97b1145f3 | 19 | const unsigned char MAX1363_CONFIG_REG = 0x03; // single-ended inputs, read CH0-CH1 only |
212600191 | 1:1de97b1145f3 | 20 | const unsigned char MAX1363_SETUP_REG = 0xF2; // AIN3=output, INT REF always on |
212600191 | 1:1de97b1145f3 | 21 | |
212600191 | 1:1de97b1145f3 | 22 | // MAX1363 high byte format (byte 2 is D7-D0) |
212600191 | 1:1de97b1145f3 | 23 | const unsigned char MAX1363_VALID_MASK = 0x90; // HIGH bit and 12/10 bit should always be 1 |
212600191 | 1:1de97b1145f3 | 24 | const unsigned char MAX1363_HIGH_DATA_MASK = 0x0F; |
212600191 | 1:1de97b1145f3 | 25 | |
212600191 | 1:1de97b1145f3 | 26 | // channel address (0-3) is in CH1/CH0 (bits 5/6) |
212600191 | 1:1de97b1145f3 | 27 | const unsigned char MAX1363_CHANNEL_MASK = 0x60; |
212600191 | 1:1de97b1145f3 | 28 | const unsigned char MAX1363_CHANNEL_SHIFT = 5; |
212600191 | 0:1f9fae46bbe1 | 29 | |
212600191 | 1:1de97b1145f3 | 30 | /** conversion factors */ |
212600191 | 1:1de97b1145f3 | 31 | const float CONV_COEFF_A3 = 4.39403; |
212600191 | 1:1de97b1145f3 | 32 | const float CONV_COEFF_A2 = -11.15; |
212600191 | 1:1de97b1145f3 | 33 | const float CONV_COEFF_A1 = 36.4955; |
212600191 | 1:1de97b1145f3 | 34 | const float CONV_COEFF_A0 = 266.909; |
212600191 | 1:1de97b1145f3 | 35 | |
212600191 | 1:1de97b1145f3 | 36 | const int CONV_N = 12; |
212600191 | 1:1de97b1145f3 | 37 | const float CONV_VREF = 2.048; |
212600191 | 1:1de97b1145f3 | 38 | |
212600191 | 1:1de97b1145f3 | 39 | const float CONV_K_TO_C = -272.15; |
212600191 | 0:1f9fae46bbe1 | 40 | |
212600191 | 1:1de97b1145f3 | 41 | float AdcToC(unsigned int _adc) |
212600191 | 1:1de97b1145f3 | 42 | { |
212600191 | 1:1de97b1145f3 | 43 | float d = CONV_VREF * _adc / (1L << CONV_N); |
212600191 | 1:1de97b1145f3 | 44 | float d2 = d*d; |
212600191 | 1:1de97b1145f3 | 45 | float d3 = d2*d; |
212600191 | 1:1de97b1145f3 | 46 | float temp_k = (CONV_COEFF_A3 * d3) + |
212600191 | 1:1de97b1145f3 | 47 | (CONV_COEFF_A2 * d2) + |
212600191 | 1:1de97b1145f3 | 48 | (CONV_COEFF_A1 * d) + |
212600191 | 1:1de97b1145f3 | 49 | CONV_COEFF_A0; |
212600191 | 1:1de97b1145f3 | 50 | return temp_k + CONV_K_TO_C; |
212600191 | 1:1de97b1145f3 | 51 | } |
212600191 | 0:1f9fae46bbe1 | 52 | |
iwolf32 | 2:4425049f4174 | 53 | void callback(){ |
iwolf32 | 2:4425049f4174 | 54 | ch=pc.getc(); |
iwolf32 | 2:4425049f4174 | 55 | str+=ch; |
iwolf32 | 2:4425049f4174 | 56 | SetTemp= atof(str.c_str()); |
iwolf32 | 2:4425049f4174 | 57 | return; |
iwolf32 | 2:4425049f4174 | 58 | } |
iwolf32 | 2:4425049f4174 | 59 | |
iwolf32 | 2:4425049f4174 | 60 | |
212600191 | 0:1f9fae46bbe1 | 61 | int main() { |
iwolf32 | 2:4425049f4174 | 62 | pc.baud(115200); |
iwolf32 | 2:4425049f4174 | 63 | |
iwolf32 | 2:4425049f4174 | 64 | pc.attach(&callback); |
212600191 | 1:1de97b1145f3 | 65 | |
212600191 | 1:1de97b1145f3 | 66 | // reserve 4 bytes for transfer |
212600191 | 1:1de97b1145f3 | 67 | char cmd[4]; |
212600191 | 1:1de97b1145f3 | 68 | |
212600191 | 1:1de97b1145f3 | 69 | // initialize the adc device |
212600191 | 1:1de97b1145f3 | 70 | cmd[0] = MAX1363_CONFIG_REG; |
212600191 | 1:1de97b1145f3 | 71 | cmd[1] = MAX1363_SETUP_REG; |
212600191 | 1:1de97b1145f3 | 72 | i2c.write(SLAVE_ADDRESS, cmd, 2); |
212600191 | 1:1de97b1145f3 | 73 | |
212600191 | 1:1de97b1145f3 | 74 | // holds raw adc values as we convert them |
212600191 | 1:1de97b1145f3 | 75 | unsigned int adc[2] = {0}; |
212600191 | 1:1de97b1145f3 | 76 | float values[2] = {0}; |
212600191 | 1:1de97b1145f3 | 77 | |
212600191 | 0:1f9fae46bbe1 | 78 | while (1) { |
iwolf32 | 2:4425049f4174 | 79 | //Clear string holding the input from serial |
iwolf32 | 2:4425049f4174 | 80 | str.clear(); |
iwolf32 | 2:4425049f4174 | 81 | |
iwolf32 | 2:4425049f4174 | 82 | wait(1); |
212600191 | 1:1de97b1145f3 | 83 | |
212600191 | 1:1de97b1145f3 | 84 | // clear buffer, only to detect invalid data |
212600191 | 0:1f9fae46bbe1 | 85 | cmd[0] = 0x00; |
212600191 | 1:1de97b1145f3 | 86 | cmd[1] = 0x00; |
212600191 | 1:1de97b1145f3 | 87 | cmd[2] = 0x00; |
212600191 | 1:1de97b1145f3 | 88 | cmd[3] = 0x00; |
212600191 | 1:1de97b1145f3 | 89 | |
212600191 | 1:1de97b1145f3 | 90 | // read 4 bytes from |
212600191 | 1:1de97b1145f3 | 91 | i2c.read(SLAVE_ADDRESS, cmd, 4); |
212600191 | 1:1de97b1145f3 | 92 | |
212600191 | 1:1de97b1145f3 | 93 | for (int i=0; i<4; i+=2) { |
212600191 | 1:1de97b1145f3 | 94 | // first 4 bits contain expected bits and channel id |
212600191 | 1:1de97b1145f3 | 95 | if ((cmd[i] & MAX1363_VALID_MASK) == MAX1363_VALID_MASK) { |
212600191 | 1:1de97b1145f3 | 96 | unsigned char ch = ((cmd[i] & MAX1363_CHANNEL_MASK) >> MAX1363_CHANNEL_SHIFT); |
212600191 | 1:1de97b1145f3 | 97 | adc[ch] = ((int)(cmd[i] & MAX1363_HIGH_DATA_MASK) << 8) | cmd[i+1]; |
212600191 | 1:1de97b1145f3 | 98 | values[ch] = AdcToC(adc[ch]); |
212600191 | 1:1de97b1145f3 | 99 | } else { |
212600191 | 1:1de97b1145f3 | 100 | // handle invalid data |
212600191 | 1:1de97b1145f3 | 101 | } |
212600191 | 1:1de97b1145f3 | 102 | } |
iwolf32 | 2:4425049f4174 | 103 | x=values[1]; |
iwolf32 | 2:4425049f4174 | 104 | if (x<SetTemp){ |
iwolf32 | 2:4425049f4174 | 105 | Relay=0; |
iwolf32 | 2:4425049f4174 | 106 | } |
iwolf32 | 2:4425049f4174 | 107 | if (x>SetTemp+1){ |
iwolf32 | 2:4425049f4174 | 108 | Relay=1; |
iwolf32 | 2:4425049f4174 | 109 | } |
212600191 | 1:1de97b1145f3 | 110 | // output temperature data |
iwolf32 | 2:4425049f4174 | 111 | printf("%0.2f\t %0.2f\t %0.2f\r\n", SetTemp, values[0], values[1]); |
iwolf32 | 2:4425049f4174 | 112 | |
212600191 | 0:1f9fae46bbe1 | 113 | } |
212600191 | 1:1de97b1145f3 | 114 | } |