| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC2942.h
Go to the documentation of this file.
1 /*!
2 LTC2942: Battery Gas Gauge with Temperature, Voltage Measurement.
3 LTC2941: Battery Gas Gauge with I2C Interface.
4
5 @verbatim
6
7 The LTC®2942 measures battery charge state, battery
8 voltage and chip temperature in handheld PC and portable
9 product applications. Its operating range is perfectly suited
10 for single-cell Li-Ion batteries. A precision coulomb counter
11 integrates current through a sense resistor between the
12 battery’s positive terminal and the load or charger. Battery
13 voltage and on-chip temperature are measured with an
14 internal 14-bit No Latency ∆∑™ ADC. The three measured
15 quantities (charge, voltage and temperature) are stored in
16 internal registers accessible via the onboard SMBus/I2C
17 interface.
18
19 I2C DATA FORMAT (MSB FIRST):
20
21 Data Out:
22 Byte #1 Byte #2 Byte #3
23
24 START SA6 SA5 SA4 SA3 SA2 SA1 SA0 W SACK C7 C6 C5 C4 C3 C2 C1 C0 SACK D7 D6 D5 D4 D3 D2 D1 D0 SACK STOP
25
26 Data In:
27 Byte #1 Byte #2 Byte #3
28
29 START SA6 SA5 SA4 SA3 SA2 SA1 SA0 W SACK C7 C6 C5 C4 C3 C2 C1 C0 SACK Repeat Start SA6 SA5 SA4 SA3 SA2 SA1 SA0 R SACK
30
31 Byte #4 Byte #5
32 MSB LSB
33 D15 D14 D13 D12 D11 D10 D9 D8 MACK D7 D6 D5 D4 D3 D2 D1 D0 MNACK STOP
34
35 START : I2C Start
36 Repeat Start: I2c Repeat Start
37 STOP : I2C Stop
38 SAx : I2C Address
39 SACK : I2C Slave Generated Acknowledge (Active Low)
40 MACK : I2C Master Generated Acknowledge (Active Low)
41 MNACK : I2c Master Generated Not Acknowledge
42 W : I2C Write (0)
43 R : I2C Read (1)
44 Cx : Command Code
45 Dx : Data Bits
46 X : Don't care
47
48
49
50 Example Code:
51
52 Read charge, current, and voltage.
53
54 adc_command = LTC2942_SENSE_MONITOR | LTC2942_AUTOMATIC_MODE; // Builds commands to set LTC2942 to automatic mode
55 ack |= LTC2942_write(LTC2942_I2C_ADDRESS, LTC2942_CONTROL_REG, adc_command); // Sets the LTC2942 to automatic mode
56
57 resistor = .1; // Resistor Value On Demo Board
58
59 ack |= LTC2942_read_16_bits(LTC2942_I2C_ADDRESS, LTC2942_CHARGE_MSB_REG, &charge_code); // Reads the ADC registers that contains charge value
60 charge = LTC2942_code_to_coulombs(charge_code, resistor, prescalarValue); // Calculates charge from charge code, resistor and prescalar
61
62 ack |= LTC2942_read_16_bits(LTC2942_I2C_ADDRESS, LTC2942_CURRENT_MSB_REG, &current_code); // Reads the voltage code across sense resistor
63 current = LTC2942_code_to_current(current_code, resistor); // Calculates current from current code, resistor value.
64
65 ack |= LTC2942_read_16_bits(LTC2942_I2C_ADDRESS, LTC2942_VOLTAGE_MSB_REG, &voltage_code); // Reads voltage voltage code
66 VIN = LTC2942_VIN_code_to_voltage(voltage_code); // Calculates VIN voltage from VIN code and lsb
67
68
69 @endverbatim
70
71 http://www.linear.com/product/LTC2942
72 http://www.linear.com/product/LTC2941
73
74 http://www.linear.com/product/LTC2942#demoboards
75 http://www.linear.com/product/LTC2941#demoboards
76
77
78 Copyright 2018(c) Analog Devices, Inc.
79
80 All rights reserved.
81
82 Redistribution and use in source and binary forms, with or without
83 modification, are permitted provided that the following conditions are met:
84 - Redistributions of source code must retain the above copyright
85 notice, this list of conditions and the following disclaimer.
86 - Redistributions in binary form must reproduce the above copyright
87 notice, this list of conditions and the following disclaimer in
88 the documentation and/or other materials provided with the
89 distribution.
90 - Neither the name of Analog Devices, Inc. nor the names of its
91 contributors may be used to endorse or promote products derived
92 from this software without specific prior written permission.
93 - The use of this software may or may not infringe the patent rights
94 of one or more patent holders. This license does not release you
95 from the requirement that you obtain separate licenses from these
96 patent holders to use this software.
97 - Use of the software either in source or binary form, must be run
98 on or directly connected to an Analog Devices Inc. component.
99
100 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
101 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
102 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
103 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
104 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
105 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
106 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
107 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
108 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
109 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110 */
111
112 /*! @file
113 @ingroup LTC2942
114 Header for LTC2942: Battery Gas Gauge with Temperature, Voltage Measurement
115 */
116
117
118 #ifndef LTC2942_H
119 #define LTC2942_H
120
121 #include <Wire.h>
122
123
124 /*!
125 | LTC2942 I2C Address Assignment | Value |
126 | :-------------------------------- | :---: |
127 | LTC2942_I2C_ADDRESS | 0xC8 |
128 */
129
130 /*! @name LTC2942 I2C Address
131 @{ */
132
133 #define LTC2942_I2C_ADDRESS 0x64
134 #define LTC2942_I2C_ALERT_RESPONSE 0x0C
135 //! @}
136
137
138 /*!
139 | Name | Value |
140 | :------------------------------------------------ | :---: |
141 | LTC2942_STATUS_REG | 0x00 |
142 | LTC2942_CONTROL_REG | 0x01 |
143 | LTC2942_ACCUM_CHARGE_MSB_REG | 0x02 |
144 | LTC2942_ACCUM_CHARGE_LSB_REG | 0x03 |
145 | LTC2942_CHARGE_THRESH_HIGH_MSB_REG | 0x04 |
146 | LTC2942_CHARGE_THRESH_HIGH_LSB_REG | 0x05 |
147 | LTC2942_CHARGE_THRESH_LOW_MSB_REG | 0x06 |
148 | LTC2942_CHARGE_THRESH_LOW_LSB_REG | 0x07 |
149 | LTC2942_VOLTAGE_MSB_REG | 0x08 |
150 | LTC2942_VOLTAGE_LSB_REG | 0x09 |
151 | LTC2942_VOLTAGE_THRESH_HIGH_REG | 0x0A |
152 | LTC2942_VOLTAGE_THRESH_LOW_REG | 0x0B |
153 | LTC2942_TEMPERATURE_MSB_REG | 0x0C |
154 | LTC2942_TEMPERATURE_LSB_REG | 0x0D |
155 | LTC2942_TEMPERATURE_THRESH_HIGH_REG | 0x0E |
156 | LTC2942_TEMPERATURE_THRESH_LOW_REG | 0x0F |
157
158 */
159
160 /*! @name Registers
161 @{ */
162 // Registers
163 #define LTC2942_STATUS_REG 0x00
164 #define LTC2942_CONTROL_REG 0x01
165 #define LTC2942_ACCUM_CHARGE_MSB_REG 0x02
166 #define LTC2942_ACCUM_CHARGE_LSB_REG 0x03
167 #define LTC2942_CHARGE_THRESH_HIGH_MSB_REG 0x04
168 #define LTC2942_CHARGE_THRESH_HIGH_LSB_REG 0x05
169 #define LTC2942_CHARGE_THRESH_LOW_MSB_REG 0x06
170 #define LTC2942_CHARGE_THRESH_LOW_LSB_REG 0x07
171 #define LTC2942_VOLTAGE_MSB_REG 0x08
172 #define LTC2942_VOLTAGE_LSB_REG 0x09
173 #define LTC2942_VOLTAGE_THRESH_HIGH_REG 0x0A
174 #define LTC2942_VOLTAGE_THRESH_LOW_REG 0x0B
175 #define LTC2942_TEMPERATURE_MSB_REG 0x0C
176 #define LTC2942_TEMPERATURE_LSB_REG 0x0D
177 #define LTC2942_TEMPERATURE_THRESH_HIGH_REG 0x0E
178 #define LTC2942_TEMPERATURE_THRESH_LOW_REG 0x0F
179 //! @}
180
181 /*!
182 | Command Codes | Value |
183 | :-------------------------------------------- | :-------: |
184 | LTC2942_AUTOMATIC_MODE | 0xC0 |
185 | LTC2942_MANUAL_VOLTAGE | 0x80 |
186 | LTC2942_MANUAL_TEMPERATURE | 0x40 |
187 | LTC2942_SLEEP_MODE | 0x00 |
188 | LTC2942_PRESCALAR_M_1 | 0x00 |
189 | LTC2942_PRESCALAR_M_2 | 0x08 |
190 | LTC2942_PRESCALAR_M_4 | 0x10 |
191 | LTC2942_PRESCALAR_M_8 | 0x18 |
192 | LTC2942_PRESCALAR_M_16 | 0x20 |
193 | LTC2942_PRESCALAR_M_32 | 0x28 |
194 | LTC2942_PRESCALAR_M_64 | 0x30 |
195 | LTC2942_PRESCALAR_M_128 | 0x31 |
196 | LTC2942_ALERT_MODE | 0x04 |
197 | LTC2942_CHARGE_COMPLETE_MODE | 0x02 |
198 | LTC2942_DISABLE_ALCC_PIN | 0x00 |
199 | LTC2942_SHUTDOWN_MODE | 0x01 |
200 */
201
202 /*! @name Command Codes
203 @{ */
204 // Command Codes
205 #define LTC2942_AUTOMATIC_MODE 0xC0
206 #define LTC2942_MANUAL_VOLTAGE 0x80
207 #define LTC2942_MANUAL_TEMPERATURE 0x40
208 #define LTC2942_SLEEP_MODE 0x00
209
210 #define LTC2942_PRESCALAR_M_1 0x00
211 #define LTC2942_PRESCALAR_M_2 0x08
212 #define LTC2942_PRESCALAR_M_4 0x10
213 #define LTC2942_PRESCALAR_M_8 0x18
214 #define LTC2942_PRESCALAR_M_16 0x20
215 #define LTC2942_PRESCALAR_M_32 0x28
216 #define LTC2942_PRESCALAR_M_64 0x30
217 #define LTC2942_PRESCALAR_M_128 0x38
218
219 #define LTC2942_ALERT_MODE 0x04
220 #define LTC2942_CHARGE_COMPLETE_MODE 0x02
221 #define LTC2942_DISABLE_ALCC_PIN 0x00
222
223 #define LTC2942_SHUTDOWN_MODE 0x01
224
225 //! @}
226
227 /*!
228 | Conversion Constants | Value |
229 | :------------------------------------------------ | :------: |
230 | LTC2942_CHARGE_lsb | 0.085 mAh|
231 | LTC2942_VOLTAGE_lsb | 366.2 uV|
232 | LTC2942_TEMPERATURE_lsb | 0.586 C|
233 | LTC2942_FULLSCALE_VOLTAGE | 6 V|
234 | LTC2942_FULLSCALE_TEMPERATURE | 600 K|
235
236 */
237 /*! @name Conversion Constants
238 @{ */
239 const float LTC2942_CHARGE_lsb = 0.085E-3;
240 const float LTC2942_VOLTAGE_lsb = .3662E-3;
241 const float LTC2942_TEMPERATURE_lsb = 0.25;
242 const float LTC2942_FULLSCALE_VOLTAGE = 6;
243 const float LTC2942_FULLSCALE_TEMPERATURE = 600;
244 //! @}
245
246 //! @}
247
248
249 //! Write an 8-bit code to the LTC2942.
250 //! @return The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
251 int8_t LTC2942_write(uint8_t i2c_address, //!< Register address for the LTC2942
252 uint8_t adc_command, //!< The "command byte" for the LTC2942
253 uint8_t code //!< Value that will be written to the register.
254 );
255
256 //! Write a 16-bit code to the LTC2942.
257 //! @return The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
258 int8_t LTC2942_write_16_bits(uint8_t i2c_address, //!< Register address for the LTC2942
259 uint8_t adc_command, //!< The "command byte" for the LTC2942
260 uint16_t code //!< Value that will be written to the register.
261 );
262
263
264 //! Reads an 8-bit adc_code from LTC2942
265 //! @return The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
266 int8_t LTC2942_read(uint8_t i2c_address, //!< Register address for the LTC2942
267 uint8_t adc_command, //!< The "command byte" for the LTC2942
268 uint8_t *adc_code //!< Value that will be read from the register.
269 );
270
271 //! Reads a 16-bit adc_code from LTC2942
272 //! @return The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
273 int8_t LTC2942_read_16_bits(uint8_t i2c_address, //!< Register address for the LTC2942
274 uint8_t adc_command, //!< The "command byte" for the LTC2942
275 uint16_t *adc_code //!< Value that will be read from the register.
276 );
277
278
279 //! Calculate the LTC2942 charge in Coulombs
280 //! @return Returns the Coulombs of charge in the ACR register.
281 float LTC2942_code_to_coulombs(uint16_t adc_code, //!< The RAW ADC value
282 float resistor, //!< The sense resistor value
283 uint16_t prescalar //!< The prescalar value
284 );
285
286 //! Calculate the LTC2942 charge in mAh
287 //! @return Returns the Coulombs of charge in the ACR register.
288 float LTC2942_code_to_mAh(uint16_t adc_code, //!< The RAW ADC value
289 float resistor, //!< The sense resistor value
290 uint16_t prescalar //!< The prescalar value
291 );
292
293 //! Calculate the LTC2942 SENSE+ voltage
294 //! @return Returns the SENSE+ Voltage in Volts
295 float LTC2942_code_to_voltage(uint16_t adc_code //!< The RAW ADC value
296 );
297
298
299 //! Calculate the LTC2942 temperature
300 //! @return Returns the temperature in Kelvin
301 float LTC2942_code_to_kelvin_temperature(uint16_t adc_code //!< The RAW ADC value
302 );
303
304 //! Calculate the LTC2942 temperature
305 //! @return Returns the temperature in Celcius
306 float LTC2942_code_to_celcius_temperature(uint16_t adc_code //!< The RAW ADC value
307 );
308
309 #endif // LTC2942_H
LTC2942_read
int8_t LTC2942_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
Reads an 8-bit adc_code from LTC2942.
Definition: LTC2942.cpp:92
LTC2942_TEMPERATURE_lsb
const float LTC2942_TEMPERATURE_lsb
Definition: LTC2942.h:241
i2c_address
uint8_t i2c_address
LTC2942_FULLSCALE_TEMPERATURE
const float LTC2942_FULLSCALE_TEMPERATURE
Definition: LTC2942.h:243
adc_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
LTC2942_write
int8_t LTC2942_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write an 8-bit code to the LTC2942.
Definition: LTC2942.cpp:71
resistor
const float resistor
resistor value on demo board
Definition: DC1496BB.ino:116
LTC2942_CHARGE_lsb
const float LTC2942_CHARGE_lsb
Definition: LTC2942.h:239
LTC2942_write_16_bits
int8_t LTC2942_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
Write a 16-bit code to the LTC2942.
Definition: LTC2942.cpp:82
LTC2942_FULLSCALE_VOLTAGE
const float LTC2942_FULLSCALE_VOLTAGE
Definition: LTC2942.h:242
LTC2942_code_to_mAh
float LTC2942_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in mAh.
Definition: LTC2942.cpp:123
LTC2942_code_to_celcius_temperature
float LTC2942_code_to_celcius_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
Definition: LTC2942.cpp:148
LTC2942_code_to_voltage
float LTC2942_code_to_voltage(uint16_t adc_code)
Calculate the LTC2942 SENSE+ voltage.
Definition: LTC2942.cpp:131
LTC2942_code_to_coulombs
float LTC2942_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in Coulombs.
Definition: LTC2942.cpp:114
LTC2942_VOLTAGE_lsb
const float LTC2942_VOLTAGE_lsb
Definition: LTC2942.h:240
LTC2942_read_16_bits
int8_t LTC2942_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 16-bit adc_code from LTC2942.
Definition: LTC2942.cpp:103
LTC2942_code_to_kelvin_temperature
float LTC2942_code_to_kelvin_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
Definition: LTC2942.cpp:140
adc_code
static uint32_t adc_code
Definition: DC2071AA.ino:113