Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Tue May 08 17:08:37 2018 +0000
Revision:
13:604e6933366f
Parent:
12:1cada1fe4743
Child:
14:69cd53434783
Working code with revised cooling (if within 5 degrees will cool until near set point), has run on a row and is functional

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jrodenburg 0:a28a1035c31b 1 #include "mbed.h"
jrodenburg 0:a28a1035c31b 2 #include "LTC2487.h"
jrodenburg 0:a28a1035c31b 3 #include "MODSERIAL.h"
jrodenburg 0:a28a1035c31b 4
jrodenburg 1:0182b86f9bd4 5
jrodenburg 0:a28a1035c31b 6 namespace {
jrodenburg 0:a28a1035c31b 7 const uint8_t I2C_WRITE = 0x00;
jrodenburg 0:a28a1035c31b 8 const uint8_t I2C_READ = 0x01;
jrodenburg 0:a28a1035c31b 9
jrodenburg 12:1cada1fe4743 10 //Channel addresses (first two bits are fixed)
jrodenburg 12:1cada1fe4743 11 //0b10 EN SGL ODD A2 A1 A0
jrodenburg 12:1cada1fe4743 12 //0b10 1 1 0 0 0 0 ch0 --> 0xb0
jrodenburg 12:1cada1fe4743 13 //0b10 1 1 1 0 0 0 ch1 --> 0xb8
jrodenburg 12:1cada1fe4743 14 //0b10 1 1 0 0 0 1 ch2 --> 0xb1
jrodenburg 12:1cada1fe4743 15 //0b10 1 1 1 0 0 1 ch3 --> 0xb9
jrodenburg 12:1cada1fe4743 16
jrodenburg 12:1cada1fe4743 17
jrodenburg 12:1cada1fe4743 18 //Config Section
jrodenburg 12:1cada1fe4743 19 // EN2 IM FA FB SPD GS2 GS1 GS0
jrodenburg 12:1cada1fe4743 20 // 1 0 0 0 0 0 1 0 //Initial
jrodenburg 12:1cada1fe4743 21 // 1 0 0 0 1 0 1 1 //New Attempt with same gain (8x)
jrodenburg 12:1cada1fe4743 22 // 1 0 0 0 1 0 0 0 //New Attempt with unity gain (1x)
jrodenburg 12:1cada1fe4743 23
jrodenburg 12:1cada1fe4743 24
jrodenburg 0:a28a1035c31b 25 const uint8_t CHNL_0 = 0xb0;
jrodenburg 0:a28a1035c31b 26 const uint8_t CHNL_1 = 0xb8;
jrodenburg 0:a28a1035c31b 27 const uint8_t CHNL_2 = 0xb1;
jrodenburg 0:a28a1035c31b 28 const uint8_t CHNL_3 = 0xb9;
jrodenburg 0:a28a1035c31b 29 };
jrodenburg 0:a28a1035c31b 30
jrodenburg 0:a28a1035c31b 31 LTC2487::LTC2487 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){
jrodenburg 0:a28a1035c31b 32 addrI2C = address;
jrodenburg 0:a28a1035c31b 33 i2c.frequency(freq);
jrodenburg 0:a28a1035c31b 34 }
jrodenburg 0:a28a1035c31b 35
jrodenburg 0:a28a1035c31b 36 void LTC2487::setAddress(int address){
jrodenburg 0:a28a1035c31b 37 addrI2C = address;
jrodenburg 0:a28a1035c31b 38 }
jrodenburg 0:a28a1035c31b 39
jrodenburg 0:a28a1035c31b 40 float LTC2487::readOutput(int chnl){
jrodenburg 0:a28a1035c31b 41 char ADC_channel[1];
jrodenburg 0:a28a1035c31b 42 char ADC_data_rx[3];
jrodenburg 0:a28a1035c31b 43 char ADC_config[1];
jrodenburg 12:1cada1fe4743 44 ADC_config[0] = 0b10001000;//0x82; //0b10000010
jrodenburg 13:604e6933366f 45 char cmd[2];
jrodenburg 0:a28a1035c31b 46
jrodenburg 0:a28a1035c31b 47 //select channel to read
jrodenburg 0:a28a1035c31b 48 switch (chnl){
jrodenburg 0:a28a1035c31b 49 case 0:
jrodenburg 0:a28a1035c31b 50 ADC_channel[0] = CHNL_0;
jrodenburg 0:a28a1035c31b 51 break;
jrodenburg 0:a28a1035c31b 52 case 1:
jrodenburg 0:a28a1035c31b 53 ADC_channel[0] = CHNL_1;
jrodenburg 0:a28a1035c31b 54 break;
jrodenburg 0:a28a1035c31b 55 case 2:
jrodenburg 0:a28a1035c31b 56 ADC_channel[0] = CHNL_2;
jrodenburg 0:a28a1035c31b 57 break;
jrodenburg 0:a28a1035c31b 58 case 3:
jrodenburg 0:a28a1035c31b 59 ADC_channel[0] = CHNL_3;
jrodenburg 0:a28a1035c31b 60 break;
jrodenburg 0:a28a1035c31b 61 }
jrodenburg 0:a28a1035c31b 62
jrodenburg 13:604e6933366f 63 cmd[0] = ADC_channel[0];
jrodenburg 13:604e6933366f 64 cmd[1] = ADC_config[0];
jrodenburg 13:604e6933366f 65
jrodenburg 13:604e6933366f 66 i2c.write((addrI2C<<1)|(I2C_WRITE), cmd, 2);
jrodenburg 13:604e6933366f 67
jrodenburg 13:604e6933366f 68 /*//send message to select channel
jrodenburg 0:a28a1035c31b 69 i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_channel, 1);
jrodenburg 0:a28a1035c31b 70 //must wait, otherwise breaks...
jrodenburg 7:8a5e65e63e2a 71 wait(0.08);
jrodenburg 2:bd118a724f03 72 //send configuration (1 gain, autocalibration)
jrodenburg 0:a28a1035c31b 73 i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_config, 1);
jrodenburg 13:604e6933366f 74 //must wait, otherwise breaks...*/
jrodenburg 7:8a5e65e63e2a 75 wait(0.08);
jrodenburg 0:a28a1035c31b 76 //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK
jrodenburg 2:bd118a724f03 77 i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3);
jrodenburg 12:1cada1fe4743 78 //must wait, otherwise breaks...
jrodenburg 7:8a5e65e63e2a 79 wait(0.08);
jrodenburg 2:bd118a724f03 80 //Stitch together the bytes into a 24bit value
jrodenburg 0:a28a1035c31b 81 unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2];
jrodenburg 0:a28a1035c31b 82 //Delete SIGN bit and MSB bit and remove 6 JUNK bits
jrodenburg 1:0182b86f9bd4 83 unsigned long ADC_Result = (data&0x3fffff)>>6;
jrodenburg 1:0182b86f9bd4 84
jrodenburg 0:a28a1035c31b 85 return float(float(ADC_Result));
jrodenburg 0:a28a1035c31b 86
jrodenburg 0:a28a1035c31b 87 }