Justin Rodenburg / Mbed 2 deprecated TCTF_Control_Main

Dependencies:   MODSERIAL mbed

Fork of TCTF_Control_Main by Rivian Irvine Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LTC2487.cpp Source File

LTC2487.cpp

00001 #include "mbed.h"
00002 #include "LTC2487.h"
00003 #include "MODSERIAL.h"
00004 
00005 
00006 namespace {
00007     const uint8_t I2C_WRITE  = 0x00;
00008     const uint8_t I2C_READ   = 0x01;
00009     
00010     //Channel addresses (first two bits are fixed)
00011     //0b10  EN  SGL ODD A2  A1  A0
00012     //0b10  1   1   0   0   0   0   ch0 --> 0xb0
00013     //0b10  1   1   1   0   0   0   ch1 --> 0xb8
00014     //0b10  1   1   0   0   0   1   ch2 --> 0xb1
00015     //0b10  1   1   1   0   0   1   ch3 --> 0xb9
00016     
00017 
00018     //Config Section
00019     //      EN2 IM  FA  FB  SPD GS2 GS1 GS0
00020     //      1   0   0   0   0   0   1   0       //Initial
00021     //      1   0   0   0   1   0   1   1       //New Attempt with same gain (8x)
00022     //      1   0   0   0   1   0   0   0       //New Attempt with unity gain (1x)
00023     
00024     
00025     const uint8_t CHNL_0     = 0xb0;
00026     const uint8_t CHNL_1     = 0xb8;
00027     const uint8_t CHNL_2     = 0xb1;
00028     const uint8_t CHNL_3     = 0xb9;
00029 };
00030 
00031 LTC2487::LTC2487 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){
00032     addrI2C = address;
00033     i2c.frequency(freq);
00034 }
00035 
00036 void LTC2487::setAddress(int address){
00037     addrI2C = address;
00038 }
00039 
00040 float LTC2487::writePort(int chnl){
00041     char ADC_channel[1];
00042     char ADC_config[1];
00043     ADC_config[0] = 0b10001000;//0x82; //0b10000010
00044     char cmd[2];
00045         
00046     //select channel to read
00047     switch (chnl){
00048         case 0:
00049             ADC_channel[0] = CHNL_0;
00050             break;
00051         case 1:
00052             ADC_channel[0] = CHNL_1;
00053             break;
00054         case 2:
00055             ADC_channel[0] = CHNL_2;
00056             break;
00057         case 3:
00058             ADC_channel[0] = CHNL_3;
00059             break;
00060     } 
00061     
00062     cmd[0] = ADC_channel[0];
00063     cmd[1] = ADC_config[0];
00064     
00065     i2c.write((addrI2C<<1)|(I2C_WRITE), cmd, 2);
00066 }
00067 
00068 float LTC2487::read(){
00069     char ADC_data_rx[3];
00070     
00071     //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK
00072     i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3);
00073     //Stitch together the bytes into a 24bit value
00074     unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2];
00075     //Delete SIGN bit and MSB bit and remove 6 JUNK bits
00076     unsigned long ADC_Result = (data&0x3fffff)>>6;  
00077         
00078     return float(float(ADC_Result));
00079 
00080 }