Dependencies:   PWM-Coil-driver

Fork of InjectorValveFourthMicro by Ian Wolf

Committer:
212600191
Date:
Wed Aug 16 13:51:57 2017 +0000
Revision:
1:1de97b1145f3
Parent:
0:1f9fae46bbe1
Child:
2:4425049f4174
temp;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
212600191 0:1f9fae46bbe1 1 #include "mbed.h"
212600191 1:1de97b1145f3 2
212600191 1:1de97b1145f3 3 // Read temperature from MAX1363 3-channel ADC
212600191 1:1de97b1145f3 4
212600191 1:1de97b1145f3 5 I2C i2c(PB_9 , PB_8);
212600191 1:1de97b1145f3 6
212600191 1:1de97b1145f3 7 Serial pc(SERIAL_TX, SERIAL_RX);
212600191 1:1de97b1145f3 8
212600191 1:1de97b1145f3 9 // address information for the Serenity temperature sensor device
212600191 1:1de97b1145f3 10 const int SLAVE_ADDRESS = 0x34 << 1;
212600191 1:1de97b1145f3 11
212600191 1:1de97b1145f3 12 // MAX1363 registers
212600191 1:1de97b1145f3 13 const unsigned char MAX1363_CONFIG_REG = 0x03; // single-ended inputs, read CH0-CH1 only
212600191 1:1de97b1145f3 14 const unsigned char MAX1363_SETUP_REG = 0xF2; // AIN3=output, INT REF always on
212600191 1:1de97b1145f3 15
212600191 1:1de97b1145f3 16 // MAX1363 high byte format (byte 2 is D7-D0)
212600191 1:1de97b1145f3 17 const unsigned char MAX1363_VALID_MASK = 0x90; // HIGH bit and 12/10 bit should always be 1
212600191 1:1de97b1145f3 18 const unsigned char MAX1363_HIGH_DATA_MASK = 0x0F;
212600191 1:1de97b1145f3 19
212600191 1:1de97b1145f3 20 // channel address (0-3) is in CH1/CH0 (bits 5/6)
212600191 1:1de97b1145f3 21 const unsigned char MAX1363_CHANNEL_MASK = 0x60;
212600191 1:1de97b1145f3 22 const unsigned char MAX1363_CHANNEL_SHIFT = 5;
212600191 0:1f9fae46bbe1 23
212600191 1:1de97b1145f3 24 /** conversion factors */
212600191 1:1de97b1145f3 25 const float CONV_COEFF_A3 = 4.39403;
212600191 1:1de97b1145f3 26 const float CONV_COEFF_A2 = -11.15;
212600191 1:1de97b1145f3 27 const float CONV_COEFF_A1 = 36.4955;
212600191 1:1de97b1145f3 28 const float CONV_COEFF_A0 = 266.909;
212600191 1:1de97b1145f3 29
212600191 1:1de97b1145f3 30 const int CONV_N = 12;
212600191 1:1de97b1145f3 31 const float CONV_VREF = 2.048;
212600191 1:1de97b1145f3 32
212600191 1:1de97b1145f3 33 const float CONV_K_TO_C = -272.15;
212600191 0:1f9fae46bbe1 34
212600191 1:1de97b1145f3 35 float AdcToC(unsigned int _adc)
212600191 1:1de97b1145f3 36 {
212600191 1:1de97b1145f3 37 float d = CONV_VREF * _adc / (1L << CONV_N);
212600191 1:1de97b1145f3 38 float d2 = d*d;
212600191 1:1de97b1145f3 39 float d3 = d2*d;
212600191 1:1de97b1145f3 40 float temp_k = (CONV_COEFF_A3 * d3) +
212600191 1:1de97b1145f3 41 (CONV_COEFF_A2 * d2) +
212600191 1:1de97b1145f3 42 (CONV_COEFF_A1 * d) +
212600191 1:1de97b1145f3 43 CONV_COEFF_A0;
212600191 1:1de97b1145f3 44 return temp_k + CONV_K_TO_C;
212600191 1:1de97b1145f3 45 }
212600191 0:1f9fae46bbe1 46
212600191 0:1f9fae46bbe1 47 int main() {
212600191 0:1f9fae46bbe1 48 pc.baud(250000);
212600191 1:1de97b1145f3 49
212600191 1:1de97b1145f3 50 // reserve 4 bytes for transfer
212600191 1:1de97b1145f3 51 char cmd[4];
212600191 1:1de97b1145f3 52
212600191 1:1de97b1145f3 53 // initialize the adc device
212600191 1:1de97b1145f3 54 cmd[0] = MAX1363_CONFIG_REG;
212600191 1:1de97b1145f3 55 cmd[1] = MAX1363_SETUP_REG;
212600191 1:1de97b1145f3 56 i2c.write(SLAVE_ADDRESS, cmd, 2);
212600191 1:1de97b1145f3 57
212600191 1:1de97b1145f3 58 // holds raw adc values as we convert them
212600191 1:1de97b1145f3 59 unsigned int adc[2] = {0};
212600191 1:1de97b1145f3 60 float values[2] = {0};
212600191 1:1de97b1145f3 61
212600191 0:1f9fae46bbe1 62 while (1) {
212600191 0:1f9fae46bbe1 63 wait(0.5);
212600191 1:1de97b1145f3 64
212600191 1:1de97b1145f3 65 // clear buffer, only to detect invalid data
212600191 0:1f9fae46bbe1 66 cmd[0] = 0x00;
212600191 1:1de97b1145f3 67 cmd[1] = 0x00;
212600191 1:1de97b1145f3 68 cmd[2] = 0x00;
212600191 1:1de97b1145f3 69 cmd[3] = 0x00;
212600191 1:1de97b1145f3 70
212600191 1:1de97b1145f3 71 // read 4 bytes from
212600191 1:1de97b1145f3 72 i2c.read(SLAVE_ADDRESS, cmd, 4);
212600191 1:1de97b1145f3 73
212600191 1:1de97b1145f3 74 for (int i=0; i<4; i+=2) {
212600191 1:1de97b1145f3 75 // first 4 bits contain expected bits and channel id
212600191 1:1de97b1145f3 76 if ((cmd[i] & MAX1363_VALID_MASK) == MAX1363_VALID_MASK) {
212600191 1:1de97b1145f3 77 unsigned char ch = ((cmd[i] & MAX1363_CHANNEL_MASK) >> MAX1363_CHANNEL_SHIFT);
212600191 1:1de97b1145f3 78 adc[ch] = ((int)(cmd[i] & MAX1363_HIGH_DATA_MASK) << 8) | cmd[i+1];
212600191 1:1de97b1145f3 79 values[ch] = AdcToC(adc[ch]);
212600191 1:1de97b1145f3 80 } else {
212600191 1:1de97b1145f3 81 // handle invalid data
212600191 1:1de97b1145f3 82 }
212600191 1:1de97b1145f3 83 }
212600191 1:1de97b1145f3 84
212600191 1:1de97b1145f3 85 // output temperature data
212600191 1:1de97b1145f3 86
212600191 1:1de97b1145f3 87 printf("%0.2f\t %0.2f\r\n", values[0], values[1]);
212600191 0:1f9fae46bbe1 88 }
212600191 1:1de97b1145f3 89 }
212600191 1:1de97b1145f3 90
212600191 1:1de97b1145f3 91
212600191 1:1de97b1145f3 92
212600191 1:1de97b1145f3 93
212600191 1:1de97b1145f3 94