Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2944.cpp
Go to the documentation of this file.
1 /*!
2 LTC2944: Multicell Battery Gas Gauge with Temperature, Voltage and Current Measurement
3 
4 @verbatim
5 
6 The LTC®LTC2944 measures battery charge state, battery voltage,
7 battery current and its own temperature in portable
8 product applications. The wide input voltage range allows
9 use with multicell batteries up to 60V. A precision coulomb
10 counter integrates current through a sense resistor between
11 the battery’s positive terminal and the load or charger.
12 Voltage, current and temperature are measured with an
13 internal 16-bit No Latency ΔΣ™ ADC. The measurements
14 are stored in internal registers accessible via the onboard
15 I2C/SMBus Interface
16 
17 @endverbatim
18 
19 http://www.linear.com/product/LTC2944
20 
21 http://www.linear.com/product/LTC2944#demoboards
22 
23 
24 Copyright 2018(c) Analog Devices, Inc.
25 
26 All rights reserved.
27 
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30  - Redistributions of source code must retain the above copyright
31  notice, this list of conditions and the following disclaimer.
32  - Redistributions in binary form must reproduce the above copyright
33  notice, this list of conditions and the following disclaimer in
34  the documentation and/or other materials provided with the
35  distribution.
36  - Neither the name of Analog Devices, Inc. nor the names of its
37  contributors may be used to endorse or promote products derived
38  from this software without specific prior written permission.
39  - The use of this software may or may not infringe the patent rights
40  of one or more patent holders. This license does not release you
41  from the requirement that you obtain separate licenses from these
42  patent holders to use this software.
43  - Use of the software either in source or binary form, must be run
44  on or directly connected to an Analog Devices Inc. component.
45 
46 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
47 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
48 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
50 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
52 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57 
58 //! @ingroup Power_Monitors
59 //! @{
60 //! @defgroup LTC2944 LTC2944: Multicell Battery Gas Gauge with Temperature, Voltage and Current Measurement
61 //! @}
62 
63 /*! @file
64  @ingroup LTC2944
65  Library for LTC2944 Multicell Battery Gas Gauge with Temperature, Voltage and Current Measurement
66 */
67 
68 
69 #include <Arduino.h>
70 #include <stdint.h>
71 #include "Linduino.h"
72 #include "LT_I2C.h"
73 #include "LTC2944.h"
74 #include <Wire.h>
75 
76 // Write an 8-bit code to the LTC2944.
77 int8_t LTC2944_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
78 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
79 {
80  int32_t ack;
81 
82  ack = i2c_write_byte_data(i2c_address, adc_command, code);
83  return(ack);
84 }
85 
86 
87 // Write a 16-bit code to the LTC2944.
88 int8_t LTC2944_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
89 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
90 {
91  int8_t ack;
92 
93  ack = i2c_write_word_data(i2c_address, adc_command, code);
94  return(ack);
95 }
96 
97 // Reads an 8-bit adc_code from LTC2944
98 int8_t LTC2944_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
99 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
100 {
101  int32_t ack;
102 
103  ack = i2c_read_byte_data(i2c_address, adc_command, adc_code);
104 
105  return(ack);
106 }
107 
108 // Reads a 16-bit adc_code from LTC2944
109 int8_t LTC2944_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
110 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
111 {
112  int32_t ack;
113 
114  ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
115 
116  return(ack);
117 }
118 
119 
120 float LTC2944_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
121 // The function converts the 16-bit RAW adc_code to Coulombs
122 {
123  float coulomb_charge;
124  coulomb_charge = 1000*(float)(adc_code*LTC2944_CHARGE_lsb*prescalar*50E-3)/(resistor*4096);
125  coulomb_charge = coulomb_charge*3.6f;
126  return(coulomb_charge);
127 }
128 
129 float LTC2944_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar )
130 // The function converts the 16-bit RAW adc_code to mAh
131 {
132  float mAh_charge;
133  mAh_charge = 1000*(float)(adc_code*LTC2944_CHARGE_lsb*prescalar*50E-3)/(resistor*4096);
134  return(mAh_charge);
135 }
136 
138 // The function converts the 16-bit RAW adc_code to Volts
139 {
140  float voltage;
141  voltage = ((float)adc_code/(65535))*LTC2944_FULLSCALE_VOLTAGE;
142  return(voltage);
143 }
144 
146 // The function converts the 16-bit RAW adc_code to Amperes
147 {
148  float current;
149  current = (((float)adc_code-32767)/(32767))*((float)(LTC2944_FULLSCALE_CURRENT)/resistor);
150  return(current);
151 }
152 
154 // The function converts the 16-bit RAW adc_code to Kelvin
155 {
156  float temperature;
157  temperature = adc_code*((float)(LTC2944_FULLSCALE_TEMPERATURE)/65535);
158  return(temperature);
159 }
160 
162 // The function converts the 16-bit RAW adc_code to Celcius
163 {
164  float temperature;
165  temperature = adc_code*((float)(LTC2944_FULLSCALE_TEMPERATURE)/65535) - 273.15;
166  return(temperature);
167 }
168 
169 // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register.
170 // 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.
171 int8_t LTC2944_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
172 {
173  uint8_t register_data;
174  int8_t ack = 0;
175 
176  ack |= LTC2944_read(i2c_address, register_address, &register_data);
177  register_data = register_data & (~bits_to_clear);
178  register_data = register_data | bits_to_set;
179  ack |= LTC2944_write(i2c_address, register_address, register_data);
180  return(ack);
181 }
182 
183 
184 
int8_t LTC2944_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
Definition: LTC2944.cpp:171
uint8_t i2c_address
static uint8_t adc_command
Definition: DC2071AA.ino:111
int8_t i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value)
Read a byte of data at register specified by "command", store in "value".
Definition: LT_I2C.cpp:124
const float resistor
resistor value on demo board
Definition: DC1496BB.ino:116
const float LTC2944_FULLSCALE_CURRENT
Definition: LTC2944.h:207
Header File for Linduino Libraries and Demo Code.
float LTC2944_code_to_celcius_temperature(uint16_t adc_code)
Calculate the LTC2944 temperature.
Definition: LTC2944.cpp:161
float LTC2944_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2944 charge in mAh.
Definition: LTC2944.cpp:129
int8_t LTC2944_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
Reads an 8-bit adc_code from LTC2944.
Definition: LTC2944.cpp:98
const float LTC2944_FULLSCALE_VOLTAGE
Definition: LTC2944.h:206
int8_t i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value)
Write a 16-bit word of data to register specified by "command".
Definition: LT_I2C.cpp:216
int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value)
Write a byte of data to register specified by "command".
Definition: LT_I2C.cpp:155
float LTC2944_code_to_kelvin_temperature(uint16_t adc_code)
Calculate the LTC2944 temperature.
Definition: LTC2944.cpp:153
float LTC2944_code_to_voltage(uint16_t adc_code)
Calculate the LTC2944 SENSE+ voltage.
Definition: LTC2944.cpp:137
const float LTC2944_FULLSCALE_TEMPERATURE
Definition: LTC2944.h:208
int8_t LTC2944_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
Write a 16-bit code to the LTC2944.
Definition: LTC2944.cpp:88
int8_t i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value)
Read a 16-bit word of data from register specified by "command".
Definition: LT_I2C.cpp:172
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
int8_t LTC2944_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 16-bit adc_code from LTC2944.
Definition: LTC2944.cpp:109
const float LTC2944_CHARGE_lsb
Definition: LTC2944.h:202
LTC2944: Multicell Battery Gas Gauge with Temperature, Voltage and Current Measurement.
const int16_t E
static float voltage
Definition: DC2289AA.ino:71
static uint16_t current
the current measurement from the LTC3335&#39;s counter test mode.
Definition: DC2343A.ino:114
float LTC2944_code_to_current(uint16_t adc_code, float resistor)
Calculate the LTC2944 current with a sense resistor.
Definition: LTC2944.cpp:145
static uint32_t adc_code
Definition: DC2071AA.ino:113
int8_t LTC2944_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write an 8-bit code to the LTC2944.
Definition: LTC2944.cpp:77
float LTC2944_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2944 charge in Coulombs.
Definition: LTC2944.cpp:120