|
| 1 | +// Code from: https://build.particle.io/libs/LTC2943/1.0.0/tab/LTC2943.cpp |
| 2 | +// Modified by Byran Huang 2024 |
| 3 | +// anyon_e project |
| 4 | + |
| 5 | +/* |
| 6 | +Copyright 2011(c) Analog Devices, Inc. |
| 7 | +
|
| 8 | +All rights reserved. |
| 9 | +
|
| 10 | +Redistribution and use in source and binary forms, with or without modification, |
| 11 | +are permitted provided that the following conditions are met: |
| 12 | + - Redistributions of source code must retain the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer. |
| 14 | + - Redistributions in binary form must reproduce the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer in |
| 16 | + the documentation and/or other materials provided with the |
| 17 | + distribution. |
| 18 | + - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | + contributors may be used to endorse or promote products derived |
| 20 | + from this software without specific prior written permission. |
| 21 | + - The use of this software may or may not infringe the patent rights |
| 22 | + of one or more patent holders. This license does not release you |
| 23 | + from the requirement that you obtain separate licenses from these |
| 24 | + patent holders to use this software. |
| 25 | + - Use of the software either in source or binary form, must be run |
| 26 | + on or directly connected to an Analog Devices Inc. component. |
| 27 | +
|
| 28 | +THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 29 | +INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A |
| 30 | +PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | +
|
| 32 | +IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 33 | +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY |
| 34 | +RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 35 | +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 36 | +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 37 | +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | +*/ |
| 39 | + |
| 40 | +#include <Arduino.h> |
| 41 | +#include <Wire.h> |
| 42 | +#include "LTC2943.h" |
| 43 | + |
| 44 | + |
| 45 | +// Write an 8-bit code to the LTC2943. |
| 46 | +int8_t LTC2943_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code) |
| 47 | +// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge. |
| 48 | +{ |
| 49 | + int32_t ack; |
| 50 | + |
| 51 | + Wire.beginTransmission(i2c_address); |
| 52 | + Wire.write(adc_command); |
| 53 | + Wire.write(code); |
| 54 | + ack = Wire.endTransmission(); |
| 55 | + return(ack); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// Write a 16-bit code to the LTC2943. |
| 60 | +int8_t LTC2943_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code) |
| 61 | +// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge. |
| 62 | +{ |
| 63 | + int8_t ack; |
| 64 | + union |
| 65 | + { |
| 66 | + uint8_t b[2]; |
| 67 | + uint16_t w; |
| 68 | + } data; |
| 69 | + data.w = code; |
| 70 | + Wire.beginTransmission(i2c_address); |
| 71 | + Wire.write(adc_command); |
| 72 | + Wire.write(data.b[1]); |
| 73 | + Wire.write(data.b[0]); |
| 74 | + ack = Wire.endTransmission(); |
| 75 | + return(ack); |
| 76 | +} |
| 77 | + |
| 78 | +// Reads an 8-bit adc_code from LTC2943 |
| 79 | +int8_t LTC2943_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code) |
| 80 | +// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge. |
| 81 | +{ |
| 82 | + int32_t ack; |
| 83 | + Wire.beginTransmission(i2c_address); |
| 84 | + Wire.write(adc_command); |
| 85 | + Wire.endTransmission(false); |
| 86 | + ack=Wire.requestFrom(i2c_address,1,false); |
| 87 | + *adc_code=Wire.read(); |
| 88 | +// ack=Wire.endTransmission(); |
| 89 | + return(ack); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +// Reads a 16-bit adc_code from LTC2943 |
| 94 | +int8_t LTC2943_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code) |
| 95 | +// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge. |
| 96 | +{ |
| 97 | + Wire.beginTransmission(i2c_address); |
| 98 | + Wire.write(adc_command); // Send register address |
| 99 | + Wire.endTransmission(false); |
| 100 | + |
| 101 | + Wire.requestFrom(i2c_address, 2); // Request 2 bytes from the register |
| 102 | + uint8_t highByte = Wire.read(); |
| 103 | + uint8_t lowByte = Wire.read(); |
| 104 | + *adc_code = (highByte << 8) | lowByte; // Combine high and low byte |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +// Reads alert protocol 8-bit response from LTC2943 |
| 109 | +int8_t LTC2943_ara_protocol(uint8_t i2c_alert_address, uint8_t *response) |
| 110 | +{ |
| 111 | + |
| 112 | + int32_t ack; |
| 113 | + Wire.beginTransmission(i2c_alert_address); |
| 114 | + Wire.endTransmission(false); |
| 115 | + ack= Wire.requestFrom(i2c_alert_address,1,false); |
| 116 | + *response=Wire.read(); |
| 117 | + //ack=Wire.endTransmission(); |
| 118 | + return(ack); |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +float LTC2943_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar) |
| 123 | +// The function converts the 16-bit RAW adc_code to Coulombs |
| 124 | +{ |
| 125 | + float coulomb_charge; |
| 126 | + coulomb_charge = 1000*(float)(adc_code*LTC2943_CHARGE_lsb*prescalar*50E-3)/(resistor*4096); |
| 127 | + coulomb_charge = coulomb_charge*3.6f; |
| 128 | + return(coulomb_charge); |
| 129 | +} |
| 130 | + |
| 131 | +float LTC2943_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar ) |
| 132 | +// The function converts the 16-bit RAW adc_code to mAh |
| 133 | +{ |
| 134 | + float mAh_charge; |
| 135 | + mAh_charge = 1000*(float)(adc_code*LTC2943_CHARGE_lsb*prescalar*50E-3)/(resistor*4096); |
| 136 | + return(mAh_charge); |
| 137 | +} |
| 138 | + |
| 139 | +float LTC2943_code_to_voltage(uint16_t adc_code) |
| 140 | +// The function converts the 16-bit RAW adc_code to Volts |
| 141 | +{ |
| 142 | + float voltage; |
| 143 | + voltage = ((float)adc_code/(65535))*LTC2943_FULLSCALE_VOLTAGE; |
| 144 | + return(voltage); |
| 145 | +} |
| 146 | + |
| 147 | +float LTC2943_code_to_current(uint16_t adc_code, float resistor) |
| 148 | +// The function converts the 16-bit RAW adc_code to Amperes |
| 149 | +{ |
| 150 | + float current; |
| 151 | + current = (((float)adc_code-32767)/(32767))*((float)(LTC2943_FULLSCALE_CURRENT)/resistor); |
| 152 | + return(current); |
| 153 | +} |
| 154 | + |
| 155 | +float LTC2943_code_to_kelvin_temperature(uint16_t adc_code) |
| 156 | +// The function converts the 16-bit RAW adc_code to Kelvin |
| 157 | +{ |
| 158 | + float temperature; |
| 159 | + temperature = adc_code*((float)(LTC2943_FULLSCALE_TEMPERATURE)/65535); |
| 160 | + return(temperature); |
| 161 | +} |
| 162 | + |
| 163 | +float LTC2943_code_to_celcius_temperature(uint16_t adc_code) |
| 164 | +// The function converts the 16-bit RAW adc_code to Celcius |
| 165 | +{ |
| 166 | + float temperature; |
| 167 | + temperature = adc_code*((float)(LTC2943_FULLSCALE_TEMPERATURE)/65535) - 273.15; |
| 168 | + return(temperature); |
| 169 | +} |
| 170 | + |
| 171 | +// Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register. |
| 172 | +// bits_to_clear will be inverted and bitwise AND'd with the register so that every location with a 1 will result in a 0 in the register. |
| 173 | +int8_t LTC2943_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear) |
| 174 | +{ |
| 175 | + uint8_t register_data; |
| 176 | + int8_t ack = 0; |
| 177 | + |
| 178 | + ack |= LTC2943_read(i2c_address, register_address, ®ister_data); |
| 179 | + register_data = register_data & (~bits_to_clear); |
| 180 | + register_data = register_data | bits_to_set; |
| 181 | + ack |= LTC2943_write(i2c_address, register_address, register_data); |
| 182 | + return(ack); |
| 183 | +} |
0 commit comments