| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

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