![](/media/cache/profiles/eb1feb5ab28dec83652dd1e5c673f256.jpg.50x50_q85.jpg)
LTC2945 ported
main.cpp@3:2ab78e7e8518, 2017-08-25 (annotated)
- Committer:
- odtulumbedder
- Date:
- Fri Aug 25 14:42:14 2017 +0000
- Revision:
- 3:2ab78e7e8518
- Parent:
- 2:7e390bcce297
I added a few comments, and cleaned up the code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
odtulumbedder | 3:2ab78e7e8518 | 1 | /** |
odtulumbedder | 3:2ab78e7e8518 | 2 | * Canberk Sönmez, |
odtulumbedder | 3:2ab78e7e8518 | 3 | * code is taken from the official tutorial for Linduino platform. |
odtulumbedder | 3:2ab78e7e8518 | 4 | * |
odtulumbedder | 3:2ab78e7e8518 | 5 | * I couldn't get power measurement working. |
odtulumbedder | 3:2ab78e7e8518 | 6 | */ |
odtulumbedder | 3:2ab78e7e8518 | 7 | |
odtulumbedder | 3:2ab78e7e8518 | 8 | #include <mbed.h> |
odtulumbedder | 0:6be57f391716 | 9 | #include <I2C.h> |
odtulumbedder | 0:6be57f391716 | 10 | #include "LT_I2C.h" |
odtulumbedder | 0:6be57f391716 | 11 | #include "LTC2945.h" |
odtulumbedder | 0:6be57f391716 | 12 | |
odtulumbedder | 3:2ab78e7e8518 | 13 | /* initialize I2C communications object */ |
odtulumbedder | 3:2ab78e7e8518 | 14 | I2C i2c(p28, p27); |
odtulumbedder | 3:2ab78e7e8518 | 15 | /* sda, scl */ |
odtulumbedder | 0:6be57f391716 | 16 | |
odtulumbedder | 3:2ab78e7e8518 | 17 | /* LTC2945 I2C address, it's shifted 1 bit right, |
odtulumbedder | 3:2ab78e7e8518 | 18 | * as it's given in 8-bit form in the datasheet, |
odtulumbedder | 3:2ab78e7e8518 | 19 | * and the library does shifting again, causing |
odtulumbedder | 3:2ab78e7e8518 | 20 | * communication problems |
odtulumbedder | 3:2ab78e7e8518 | 21 | * 0xDE, since ADR0 and ADR1 are both grounded. */ |
odtulumbedder | 2:7e390bcce297 | 22 | const int ltc2945_addr = 0xDE>>1; |
odtulumbedder | 0:6be57f391716 | 23 | |
odtulumbedder | 3:2ab78e7e8518 | 24 | /* ASSERT macro, reports debugging information if |
odtulumbedder | 3:2ab78e7e8518 | 25 | * the condition inside paranthesis is not satisfied. */ |
odtulumbedder | 3:2ab78e7e8518 | 26 | #define ASSERT(x) if (! (x)) { printf("%s:%d %s failed!\n", \ |
odtulumbedder | 3:2ab78e7e8518 | 27 | __FILE__, __LINE__, #x); } |
odtulumbedder | 2:7e390bcce297 | 28 | |
odtulumbedder | 3:2ab78e7e8518 | 29 | /* |
odtulumbedder | 3:2ab78e7e8518 | 30 | * Program entry point. |
odtulumbedder | 3:2ab78e7e8518 | 31 | */ |
odtulumbedder | 3:2ab78e7e8518 | 32 | int main() { |
odtulumbedder | 3:2ab78e7e8518 | 33 | /* initialize ported LT_I2C library and check if it's fine */ |
odtulumbedder | 3:2ab78e7e8518 | 34 | ASSERT(lt_i2c_init_attach(&i2c) == LT_I2C_INIT_FINE); |
odtulumbedder | 0:6be57f391716 | 35 | |
odtulumbedder | 3:2ab78e7e8518 | 36 | /* change I2C frequency, to have it working with optocoupled circuit */ |
odtulumbedder | 3:2ab78e7e8518 | 37 | i2c.frequency(1000); |
odtulumbedder | 0:6be57f391716 | 38 | |
odtulumbedder | 3:2ab78e7e8518 | 39 | /* typical LSB values, LSB is the smallest change in the value per least |
odtulumbedder | 3:2ab78e7e8518 | 40 | * significant bit. */ |
odtulumbedder | 3:2ab78e7e8518 | 41 | // typical deltaSense LSB weight in volts |
odtulumbedder | 3:2ab78e7e8518 | 42 | const float LTC2945_DELTA_SENSE_lsb = 2.5006105E-05; |
odtulumbedder | 3:2ab78e7e8518 | 43 | // typical Vin LSB in volts |
odtulumbedder | 3:2ab78e7e8518 | 44 | const float LTC2945_VIN_lsb = 2.5006105E-02; |
odtulumbedder | 3:2ab78e7e8518 | 45 | // typical POWER LSB in V^2 |
odtulumbedder | 3:2ab78e7e8518 | 46 | const float LTC2945_Power_lsb = 6.25305E-07; |
odtulumbedder | 0:6be57f391716 | 47 | |
odtulumbedder | 3:2ab78e7e8518 | 48 | // power code, which is read as an integer and converted later |
odtulumbedder | 0:6be57f391716 | 49 | int32_t power_code; |
odtulumbedder | 3:2ab78e7e8518 | 50 | // current code (same as above) |
odtulumbedder | 0:6be57f391716 | 51 | uint16_t current_code; |
odtulumbedder | 3:2ab78e7e8518 | 52 | // Vin code (same as above) |
odtulumbedder | 0:6be57f391716 | 53 | uint16_t VIN_code; |
odtulumbedder | 3:2ab78e7e8518 | 54 | // adc command is constructed and sent to the LTC2945 |
odtulumbedder | 0:6be57f391716 | 55 | uint8_t adc_command; |
odtulumbedder | 3:2ab78e7e8518 | 56 | // ACKnowledge bit, received if the operation is successful |
odtulumbedder | 0:6be57f391716 | 57 | int8_t ack; |
odtulumbedder | 3:2ab78e7e8518 | 58 | // resistance value |
odtulumbedder | 0:6be57f391716 | 59 | float resistor; |
odtulumbedder | 3:2ab78e7e8518 | 60 | // calculated power value |
odtulumbedder | 0:6be57f391716 | 61 | float power; |
odtulumbedder | 3:2ab78e7e8518 | 62 | // calculated current value |
odtulumbedder | 0:6be57f391716 | 63 | float current; |
odtulumbedder | 3:2ab78e7e8518 | 64 | // calculated VIN value |
odtulumbedder | 0:6be57f391716 | 65 | float VIN; |
odtulumbedder | 0:6be57f391716 | 66 | |
odtulumbedder | 3:2ab78e7e8518 | 67 | // the resistance value (calibrate if necessary) |
odtulumbedder | 3:2ab78e7e8518 | 68 | resistor = 0.26518f; |
odtulumbedder | 3:2ab78e7e8518 | 69 | |
odtulumbedder | 3:2ab78e7e8518 | 70 | // we are in an infinite loop, continuously print values |
odtulumbedder | 0:6be57f391716 | 71 | while (true) { |
odtulumbedder | 3:2ab78e7e8518 | 72 | ack = 0; // never forget resetting ack |
odtulumbedder | 3:2ab78e7e8518 | 73 | // build adc_command, set LTC2945 in continuous mode |
odtulumbedder | 3:2ab78e7e8518 | 74 | adc_command = LTC2945_SENSE_MONITOR | LTC2945_CONTINUOUS_MODE; |
odtulumbedder | 3:2ab78e7e8518 | 75 | /* send command by LTC2945_write command. |
odtulumbedder | 3:2ab78e7e8518 | 76 | * we use |= here not &= to accumulate success state |
odtulumbedder | 3:2ab78e7e8518 | 77 | * as in the API 0 means successful, and 1 means failure. |
odtulumbedder | 3:2ab78e7e8518 | 78 | * if we have ACKnowledge, then we are successful */ |
odtulumbedder | 3:2ab78e7e8518 | 79 | ack |= LTC2945_write(ltc2945_addr, LTC2945_CONTROL_REG, adc_command); |
odtulumbedder | 2:7e390bcce297 | 80 | |
odtulumbedder | 3:2ab78e7e8518 | 81 | /* read LTC2945_POWER_MSB2_REG register, containing power code |
odtulumbedder | 3:2ab78e7e8518 | 82 | * (it's a 24-bit register) */ |
odtulumbedder | 3:2ab78e7e8518 | 83 | ack |= LTC2945_read_24_bits( |
odtulumbedder | 3:2ab78e7e8518 | 84 | ltc2945_addr, |
odtulumbedder | 3:2ab78e7e8518 | 85 | LTC2945_POWER_MSB2_REG, |
odtulumbedder | 3:2ab78e7e8518 | 86 | &power_code); |
odtulumbedder | 3:2ab78e7e8518 | 87 | /* and convert it to power, using LTC2945_code_to_power */ |
odtulumbedder | 3:2ab78e7e8518 | 88 | power = LTC2945_code_to_power(power_code, resistor, LTC2945_Power_lsb); |
odtulumbedder | 2:7e390bcce297 | 89 | |
odtulumbedder | 3:2ab78e7e8518 | 90 | /* read LTC2945_DELTA_SENSE_MSB_REG register, containing deltaSENSE |
odtulumbedder | 3:2ab78e7e8518 | 91 | * voltage code (it's a 12-bit register) */ |
odtulumbedder | 3:2ab78e7e8518 | 92 | ack |= LTC2945_read_12_bits( |
odtulumbedder | 3:2ab78e7e8518 | 93 | ltc2945_addr, |
odtulumbedder | 3:2ab78e7e8518 | 94 | LTC2945_DELTA_SENSE_MSB_REG, |
odtulumbedder | 3:2ab78e7e8518 | 95 | ¤t_code); |
odtulumbedder | 3:2ab78e7e8518 | 96 | /* and convert it to current using resistance |
odtulumbedder | 3:2ab78e7e8518 | 97 | * via LTC2945_code_to_current */ |
odtulumbedder | 3:2ab78e7e8518 | 98 | current = LTC2945_code_to_current( |
odtulumbedder | 3:2ab78e7e8518 | 99 | current_code, |
odtulumbedder | 3:2ab78e7e8518 | 100 | resistor, |
odtulumbedder | 3:2ab78e7e8518 | 101 | LTC2945_DELTA_SENSE_lsb); |
odtulumbedder | 3:2ab78e7e8518 | 102 | |
odtulumbedder | 3:2ab78e7e8518 | 103 | /* read LTC2945_VIN_MSB_REG register, containing Vin voltage code |
odtulumbedder | 3:2ab78e7e8518 | 104 | * (it's a 12-bit register) */ |
odtulumbedder | 3:2ab78e7e8518 | 105 | ack |= LTC2945_read_12_bits( |
odtulumbedder | 3:2ab78e7e8518 | 106 | ltc2945_addr, |
odtulumbedder | 3:2ab78e7e8518 | 107 | LTC2945_VIN_MSB_REG, |
odtulumbedder | 3:2ab78e7e8518 | 108 | &VIN_code); |
odtulumbedder | 3:2ab78e7e8518 | 109 | /* and convert it to voltage directly */ |
odtulumbedder | 3:2ab78e7e8518 | 110 | VIN = LTC2945_VIN_code_to_voltage(VIN_code, LTC2945_VIN_lsb); |
odtulumbedder | 3:2ab78e7e8518 | 111 | |
odtulumbedder | 3:2ab78e7e8518 | 112 | /* we must have ack = 0 if all the operations above are successful! */ |
odtulumbedder | 0:6be57f391716 | 113 | ASSERT(ack == 0); |
odtulumbedder | 2:7e390bcce297 | 114 | |
odtulumbedder | 3:2ab78e7e8518 | 115 | /* print it, only 2 digits after dot */ |
odtulumbedder | 3:2ab78e7e8518 | 116 | printf("power: %.2f, current: %.2f, Vin: %.2f\n", power, current, VIN); |
odtulumbedder | 3:2ab78e7e8518 | 117 | |
odtulumbedder | 3:2ab78e7e8518 | 118 | /* wait 500 ms */ |
odtulumbedder | 2:7e390bcce297 | 119 | wait_ms(500); |
odtulumbedder | 0:6be57f391716 | 120 | } |
odtulumbedder | 0:6be57f391716 | 121 | } |