![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
Diff: LTC2487/LTC2487.cpp
- Revision:
- 0:a28a1035c31b
- Child:
- 1:0182b86f9bd4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LTC2487/LTC2487.cpp Mon Jan 22 20:11:52 2018 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" +#include "LTC2487.h" +#include "MODSERIAL.h" + +namespace { + const uint8_t I2C_WRITE = 0x00; + const uint8_t I2C_READ = 0x01; + + //Channel addresses + //0b10110000 ch0 --> 0xb0 + //0b10111000 ch1 --> 0xb8 + //0b10110001 ch2 --> 0xb1 + //0b10111001 ch3 --> 0xb9 + const uint8_t CHNL_0 = 0xb0; + const uint8_t CHNL_1 = 0xb8; + const uint8_t CHNL_2 = 0xb1; + const uint8_t CHNL_3 = 0xb9; +}; + +LTC2487::LTC2487 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){ + addrI2C = address; + i2c.frequency(freq); +} + +void LTC2487::setAddress(int address){ + addrI2C = address; +} + +float LTC2487::readOutput(int chnl){ + char ADC_channel[1]; + char ADC_data_rx[3]; + char ADC_config[1]; + ADC_config[0] = 0x82; + + //select channel to read + switch (chnl){ + case 0: + ADC_channel[0] = CHNL_0; + break; + case 1: + ADC_channel[0] = CHNL_1; + break; + case 2: + ADC_channel[0] = CHNL_2; + break; + case 3: + ADC_channel[0] = CHNL_3; + break; + } + + //send message to select channel + i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_channel, 1); + //must wait, otherwise breaks... + wait(0.3); + //send configuration (1 gain, autocalibration + i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_config, 1); + //must wait, otherwise breaks... + wait(0.3); + //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK + i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3); + //Stich togethor the bytes into a 24bit value + unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2]; + //Delete SIGN bit and MSB bit and remove 6 JUNK bits + unsigned long ADC_Result = (data&0x3fffff)>>6; + + return float(float(ADC_Result)); + +}