Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2942.cpp
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 
8 
9 @endverbatim
10 
11 http://www.linear.com/product/LTC2942
12 http://www.linear.com/product/LTC2941
13 
14 http://www.linear.com/product/LTC2942#demoboards
15 http://www.linear.com/product/LTC2941#demoboards
16 
17 
18 Copyright 2018(c) Analog Devices, Inc.
19 
20 All rights reserved.
21 
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24  - Redistributions of source code must retain the above copyright
25  notice, this list of conditions and the following disclaimer.
26  - Redistributions in binary form must reproduce the above copyright
27  notice, this list of conditions and the following disclaimer in
28  the documentation and/or other materials provided with the
29  distribution.
30  - Neither the name of Analog Devices, Inc. nor the names of its
31  contributors may be used to endorse or promote products derived
32  from this software without specific prior written permission.
33  - The use of this software may or may not infringe the patent rights
34  of one or more patent holders. This license does not release you
35  from the requirement that you obtain separate licenses from these
36  patent holders to use this software.
37  - Use of the software either in source or binary form, must be run
38  on or directly connected to an Analog Devices Inc. component.
39 
40 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
41 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
42 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
44 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
46 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 */
51 
52 //! @ingroup Power_Monitors
53 //! @{
54 //! @defgroup LTC2942 LTC2942: Battery Gas Gauge with Temperature, Voltage Measurement
55 //! @}
56 
57 /*! @file
58  @ingroup LTC2942
59  Library for LTC2942 Battery Gas Gauge with Temperature, Voltage Measurement
60 */
61 
62 
63 #include <Arduino.h>
64 #include <stdint.h>
65 #include "Linduino.h"
66 #include "LT_I2C.h"
67 #include "LTC2942.h"
68 #include <Wire.h>
69 
70 // Write an 8-bit code to the LTC2942.
71 int8_t LTC2942_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
72 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
73 {
74  int32_t ack;
75 
76  ack = i2c_write_byte_data(i2c_address, adc_command, code);
77  return(ack);
78 }
79 
80 
81 // Write a 16-bit code to the LTC2942.
82 int8_t LTC2942_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
83 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
84 {
85  int8_t ack;
86 
87  ack = i2c_write_word_data(i2c_address, adc_command, code);
88  return(ack);
89 }
90 
91 // Reads an 8-bit adc_code from LTC2942
92 int8_t LTC2942_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
93 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
94 {
95  int32_t ack;
96 
97  ack = i2c_read_byte_data(i2c_address, adc_command, adc_code);
98 
99  return(ack);
100 }
101 
102 // Reads a 16-bit adc_code from LTC2942
103 int8_t LTC2942_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
104 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
105 {
106  int32_t ack;
107 
108  ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
109 
110  return(ack);
111 }
112 
113 
114 float LTC2942_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
115 // The function converts the 16-bit RAW adc_code to Coulombs
116 {
117  float coulomb_charge;
118  coulomb_charge = 1000*(float)(adc_code*LTC2942_CHARGE_lsb*prescalar*50E-3)/(resistor*128);
119  coulomb_charge = coulomb_charge*3.6f;
120  return(coulomb_charge);
121 }
122 
123 float LTC2942_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar )
124 // The function converts the 16-bit RAW adc_code to mAh
125 {
126  float mAh_charge;
127  mAh_charge = 1000*(float)(adc_code*LTC2942_CHARGE_lsb*prescalar*50E-3)/(resistor*128);
128  return(mAh_charge);
129 }
130 
132 // The function converts the 16-bit RAW adc_code to Volts
133 {
134  float voltage;
135  voltage = ((float)adc_code/(65535))*LTC2942_FULLSCALE_VOLTAGE;
136  return(voltage);
137 }
138 
139 
141 // The function converts the 16-bit RAW adc_code to Kelvin
142 {
143  float temperature;
144  temperature = adc_code*((float)(LTC2942_FULLSCALE_TEMPERATURE)/65535);
145  return(temperature);
146 }
147 
149 // The function converts the 16-bit RAW adc_code to Celcius
150 {
151  float temperature;
152  temperature = adc_code*((float)(LTC2942_FULLSCALE_TEMPERATURE)/65535) - 273.15;
153  return(temperature);
154 }
155 
156 // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register.
157 // 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.
158 int8_t LTC2942_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
159 {
160  uint8_t register_data;
161  int8_t ack = 0;
162 
163  ack |= LTC2942_read(i2c_address, register_address, &register_data);
164  register_data = register_data & (~bits_to_clear);
165  register_data = register_data | bits_to_set;
166  ack |= LTC2942_write(i2c_address, register_address, register_data);
167  return(ack);
168 }
169 
170 
171 
float LTC2942_code_to_voltage(uint16_t adc_code)
Calculate the LTC2942 SENSE+ voltage.
Definition: LTC2942.cpp:131
uint8_t i2c_address
LTC2942: Battery Gas Gauge with Temperature, Voltage Measurement.
const float LTC2942_FULLSCALE_TEMPERATURE
Definition: LTC2942.h:243
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
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
Header File for Linduino Libraries and Demo Code.
const float LTC2942_CHARGE_lsb
Definition: LTC2942.h:239
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
int8_t LTC2942_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
Definition: LTC2942.cpp:158
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
const float LTC2942_FULLSCALE_VOLTAGE
Definition: LTC2942.h:242
float LTC2942_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in Coulombs.
Definition: LTC2942.cpp:114
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
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.
float LTC2942_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in mAh.
Definition: LTC2942.cpp:123
const int16_t E
static float voltage
Definition: DC2289AA.ino:71
float LTC2942_code_to_celcius_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
Definition: LTC2942.cpp:148
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
static uint32_t adc_code
Definition: DC2071AA.ino:113
float LTC2942_code_to_kelvin_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
Definition: LTC2942.cpp:140