Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Mon May 07 03:38:15 2018 +0000
Revision:
12:1cada1fe4743
Parent:
7:8a5e65e63e2a
Child:
13:604e6933366f
Working version, if all else fails go to this file, it controls everything with a 4s period and GUI control

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 0:a28a1035c31b 45
jrodenburg 0:a28a1035c31b 46 //select channel to read
jrodenburg 0:a28a1035c31b 47 switch (chnl){
jrodenburg 0:a28a1035c31b 48 case 0:
jrodenburg 0:a28a1035c31b 49 ADC_channel[0] = CHNL_0;
jrodenburg 0:a28a1035c31b 50 break;
jrodenburg 0:a28a1035c31b 51 case 1:
jrodenburg 0:a28a1035c31b 52 ADC_channel[0] = CHNL_1;
jrodenburg 0:a28a1035c31b 53 break;
jrodenburg 0:a28a1035c31b 54 case 2:
jrodenburg 0:a28a1035c31b 55 ADC_channel[0] = CHNL_2;
jrodenburg 0:a28a1035c31b 56 break;
jrodenburg 0:a28a1035c31b 57 case 3:
jrodenburg 0:a28a1035c31b 58 ADC_channel[0] = CHNL_3;
jrodenburg 0:a28a1035c31b 59 break;
jrodenburg 0:a28a1035c31b 60 }
jrodenburg 0:a28a1035c31b 61
jrodenburg 0:a28a1035c31b 62 //send message to select channel
jrodenburg 0:a28a1035c31b 63 i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_channel, 1);
jrodenburg 0:a28a1035c31b 64 //must wait, otherwise breaks...
jrodenburg 7:8a5e65e63e2a 65 wait(0.08);
jrodenburg 2:bd118a724f03 66 //send configuration (1 gain, autocalibration)
jrodenburg 0:a28a1035c31b 67 i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_config, 1);
jrodenburg 0:a28a1035c31b 68 //must wait, otherwise breaks...
jrodenburg 7:8a5e65e63e2a 69 wait(0.08);
jrodenburg 0:a28a1035c31b 70 //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK
jrodenburg 2:bd118a724f03 71 i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3);
jrodenburg 12:1cada1fe4743 72 //must wait, otherwise breaks...
jrodenburg 7:8a5e65e63e2a 73 wait(0.08);
jrodenburg 2:bd118a724f03 74 //Stitch together the bytes into a 24bit value
jrodenburg 0:a28a1035c31b 75 unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2];
jrodenburg 0:a28a1035c31b 76 //Delete SIGN bit and MSB bit and remove 6 JUNK bits
jrodenburg 1:0182b86f9bd4 77 unsigned long ADC_Result = (data&0x3fffff)>>6;
jrodenburg 1:0182b86f9bd4 78
jrodenburg 0:a28a1035c31b 79 return float(float(ADC_Result));
jrodenburg 0:a28a1035c31b 80
jrodenburg 0:a28a1035c31b 81 }