LTC2945 ported

Dependencies:   mbed

Committer:
odtulumbedder
Date:
Wed Aug 23 10:49:21 2017 +0000
Revision:
0:6be57f391716
Child:
2:7e390bcce297
initial problematic code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
odtulumbedder 0:6be57f391716 1 #include "mbed.h"
odtulumbedder 0:6be57f391716 2 #include <I2C.h>
odtulumbedder 0:6be57f391716 3 #include "LT_I2C.h"
odtulumbedder 0:6be57f391716 4 #include "LTC2945.h"
odtulumbedder 0:6be57f391716 5
odtulumbedder 0:6be57f391716 6 I2C i2c(p28, p27);
odtulumbedder 0:6be57f391716 7 // sda, scl
odtulumbedder 0:6be57f391716 8
odtulumbedder 0:6be57f391716 9 Serial pc(USBTX, USBRX);
odtulumbedder 0:6be57f391716 10
odtulumbedder 0:6be57f391716 11 #define LTC2945_I2C_ADDRESS 0xD4
odtulumbedder 0:6be57f391716 12
odtulumbedder 0:6be57f391716 13 #define ASSERT(x) if (! (x)) { pc.printf("%s:%d %s failed!\n", __FILE__, __LINE__, #x); }
odtulumbedder 0:6be57f391716 14
odtulumbedder 0:6be57f391716 15 int main() {
odtulumbedder 0:6be57f391716 16 ASSERT(lt_i2c_init_attach(&i2c) == 0);
odtulumbedder 0:6be57f391716 17
odtulumbedder 0:6be57f391716 18 const float LTC2945_DELTA_SENSE_lsb = 2.5006105E-05; //!< Typical Delta lsb weight in volts
odtulumbedder 0:6be57f391716 19 const float LTC2945_VIN_lsb = 2.5006105E-02; //!< Typical VIN lsb weight in volts
odtulumbedder 0:6be57f391716 20 const float LTC2945_Power_lsb = 6.25305E-07; //!< Typical POWER lsb weight in V^2
odtulumbedder 0:6be57f391716 21
odtulumbedder 0:6be57f391716 22 int32_t power_code;
odtulumbedder 0:6be57f391716 23 uint16_t current_code;
odtulumbedder 0:6be57f391716 24 uint16_t VIN_code;
odtulumbedder 0:6be57f391716 25 uint8_t adc_command;
odtulumbedder 0:6be57f391716 26 int8_t ack;
odtulumbedder 0:6be57f391716 27 float resistor;
odtulumbedder 0:6be57f391716 28 float power;
odtulumbedder 0:6be57f391716 29 float current;
odtulumbedder 0:6be57f391716 30 float VIN;
odtulumbedder 0:6be57f391716 31
odtulumbedder 0:6be57f391716 32 while (true) {
odtulumbedder 0:6be57f391716 33 adc_command = LTC2945_SENSE_MONITOR | LTC2945_CONTINUOUS_MODE; // Builds commands to set LTC2945 to continuous mode
odtulumbedder 0:6be57f391716 34 ack |= LTC2945_write(LTC2945_I2C_ADDRESS, LTC2945_CONTROL_REG, adc_command); // Sets the LTC2945 to continuous mode
odtulumbedder 0:6be57f391716 35 ASSERT(ack == 0);
odtulumbedder 0:6be57f391716 36 resistor = .01; // Resistor Value On Demo Board
odtulumbedder 0:6be57f391716 37
odtulumbedder 0:6be57f391716 38 ack |= LTC2945_read_24_bits(LTC2945_I2C_ADDRESS, LTC2945_POWER_MSB2_REG, &power_code); // Reads the ADC registers that contains V^2
odtulumbedder 0:6be57f391716 39 ASSERT(ack == 0);
odtulumbedder 0:6be57f391716 40 power = LTC2945_code_to_power(power_code, resistor, LTC2945_Power_lsb); // Calculates power from power code, resistor value and power lsb
odtulumbedder 0:6be57f391716 41
odtulumbedder 0:6be57f391716 42 ack |= LTC2945_read_12_bits(LTC2945_I2C_ADDRESS, LTC2945_DELTA_SENSE_MSB_REG, &current_code); // Reads the voltage code across sense resistor
odtulumbedder 0:6be57f391716 43 ASSERT(ack == 0);
odtulumbedder 0:6be57f391716 44 current = LTC2945_code_to_current(current_code, resistor, LTC2945_DELTA_SENSE_lsb); // Calculates current from current code, resistor value and current lsb
odtulumbedder 0:6be57f391716 45
odtulumbedder 0:6be57f391716 46 ack |= LTC2945_read_12_bits(LTC2945_I2C_ADDRESS, LTC2945_VIN_MSB_REG, &VIN_code); // Reads VIN voltage code
odtulumbedder 0:6be57f391716 47 ASSERT(ack == 0);
odtulumbedder 0:6be57f391716 48 VIN = LTC2945_VIN_code_to_voltage(VIN_code, LTC2945_VIN_lsb); // Calculates VIN voltage from VIN code and lsb
odtulumbedder 0:6be57f391716 49
odtulumbedder 0:6be57f391716 50 pc.printf("power: %f, current: %f, Vin: %f\n", power, current, VIN);
odtulumbedder 0:6be57f391716 51 wait_ms(500);
odtulumbedder 0:6be57f391716 52 }
odtulumbedder 0:6be57f391716 53 }