-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLTC2946.cpp
More file actions
259 lines (203 loc) · 9.27 KB
/
Copy pathLTC2946.cpp
File metadata and controls
259 lines (203 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*!
LTC2946: 12-Bit Wide Range Power, Charge and Energy Monitor
@verbatim
The LTC®2946 is a rail-to-rail system monitor that measures
current, voltage, power, charge and energy. It features an
operating range of 2.7V to 100V and includes a shunt regulator
for supplies above 100V. The current measurement common mode
range of 0V to 100V is independent of the input supply.
A 12-bit ADC measures load current, input voltage and an
auxiliary external voltage. Load current and internally
calculated power are integrated over an external clock or
crystal or internal oscillator time base for charge and energy.
An accurate time base allows the LTC2946 to provide measurement
accuracy of better than ±0.6% for charge and ±1% for power and
energy. Minimum and maximum values are stored and an overrange
alert with programmable thresholds minimizes the need for software
polling. Data is reported via a standard I2C interface.
Shutdown mode reduces power consumption to 15uA.
@endverbatim
http://www.linear.com/product/LTC2946
http://www.linear.com/product/ltc2946#demoboards
REVISION HISTORY
$Revision: 2041 $
$Date: 2013-10-15 16:22:28 -0700 (Tue, 15 Oct 2013) $
Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.
The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community. Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
*/
//! @defgroup LTC2946 LTC2946: 12-Bit Wide Range Power, Charge and Energy Monitor
/*! @file
@ingroup LTC2946
Library for LTC2946 12-Bit Wide Range Power, Charge and Energy Monitor
*/
#include <Arduino.h>
#include <stdint.h>
#include "Linduino.h"
#include "LT_I2C.h"
#include "LTC2946.h"
#include <Wire.h>
// Write an 8-bit code to the LTC2946.
int8_t LTC2946_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int32_t ack;
ack = i2c_write_byte_data(i2c_address, adc_command, code);
return ack;
}
// Write a 16-bit code to the LTC2946.
int8_t LTC2946_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
ack = i2c_write_word_data(i2c_address, adc_command, code);
return(ack);
}
// Write a 24-bit code to the LTC2946.
int8_t LTC2946_write_24_bits(uint8_t i2c_address, uint8_t adc_command, uint32_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
LT_union_int32_4bytes data;
data.LT_int32 = code;
ack = i2c_write_block_data(i2c_address, adc_command, (uint8_t) 3, data.LT_byte);
return(ack);
}
int8_t LTC2946_write_32_bits(uint8_t i2c_address, uint8_t adc_command, uint32_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
LT_union_int32_4bytes data;
data.LT_int32 = code;
ack = i2c_write_block_data(i2c_address, adc_command, (uint8_t) 4, data.LT_byte);
return(ack);
}
// Reads an 8-bit adc_code from LTC2946
int8_t LTC2946_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int32_t ack;
ack = i2c_read_byte_data(i2c_address, adc_command, adc_code);
return ack;
}
// Reads a 12-bit adc_code from LTC2946
int8_t LTC2946_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
// Use union type defined in Linduino.h to combine two uint8_t's (8-bit unsigned integers) into one uint16_t (unsigned 16-bit integer)
// Then, shift by 4 bits and return in *adc_code
int32_t ack;
ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
*adc_code >>= 4;
return ack;
}
// Reads a 16-bit adc_code from LTC2946
int8_t LTC2946_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int32_t ack;
ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
return ack;
}
// Reads a 24-bit adc_code from LTC2946
int8_t LTC2946_read_24_bits(uint8_t i2c_address, uint8_t adc_command, uint32_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
LT_union_int32_4bytes data;
ack = i2c_read_block_data(i2c_address, adc_command, (uint8_t)3, data.LT_byte);
*adc_code = 0x0FFFFFF & data.LT_int32;
return(ack);
}
// Reads a 32-bit adc_code from LTC2946
int8_t LTC2946_read_32_bits(uint8_t i2c_address, uint8_t adc_command, uint32_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
LT_union_int32_4bytes data;
ack = i2c_read_block_data(i2c_address, adc_command, (uint8_t) 4, data.LT_byte);
*adc_code = 0xFFFFFFFF & data.LT_int32;
return(ack);
}
// Calculate the LTC2946 VIN voltage
float LTC2946_VIN_code_to_voltage(uint16_t adc_code, float LTC2946_VIN_lsb)
// Returns the VIN Voltage in Volts
{
float voltage;
voltage = (float)adc_code*LTC2946_VIN_lsb; //! 1) Calculate voltage from code and lsb
return(voltage);
}
// Calculate the LTC2946 ADIN voltage
float LTC2946_ADIN_code_to_voltage(uint16_t adc_code, float LTC2946_ADIN_lsb)
// Returns the ADIN Voltage in Volts
{
float adc_voltage;
adc_voltage = (float)adc_code*LTC2946_ADIN_lsb; //! 1) Calculate voltage from code and ADIN lsb
return(adc_voltage);
}
// Calculate the LTC2946 current with a sense resistor
float LTC2946_code_to_current(uint16_t adc_code, float resistor, float LTC2946_DELTA_SENSE_lsb)
// Returns the LTC2946 current in Amps
{
float voltage, current;
voltage = (float)adc_code*LTC2946_DELTA_SENSE_lsb; //! 1) Calculate voltage from ADC code and delta sense lsb
current = voltage/resistor; //! 2) Calculate current, I = V/R
return(current);
}
// Calculate the LTC2946 power
float LTC2946_code_to_power(int32_t adc_code, float resistor, float LTC2946_Power_lsb)
// Returns The LTC2946 power in Watts
{
float power;
power = (float)adc_code*LTC2946_Power_lsb/resistor; //! 1) Calculate Power using Power lsb and resistor
return(power);
}
// Calculate the LTC2946 energy
float LTC2946_code_to_energy(int32_t adc_code,float resistor, float LTC2946_Power_lsb, float LTC2946_TIME_lsb)
// Returns the LTC2946 energy in Joules
{
float energy_lsb, energy;
energy_lsb=(float)(LTC2946_Power_lsb/resistor)*65536*LTC2946_TIME_lsb; //! 1) Calculate Energy lsb from Power lsb and Time lsb
energy = adc_code*energy_lsb; //! 2) Calculate Energy using Energy lsb and adc code
return(energy);
}
// Calculate the LTC2946 Coulombs
float LTC2946_code_to_coulombs(int32_t adc_code, float resistor, float LTC2946_DELTA_SENSE_lsb, float LTC2946_TIME_lsb)
// Returns the LTC2946 Coulombs
{
float coulomb_lsb, coulombs;
coulomb_lsb=(float)(LTC2946_DELTA_SENSE_lsb/resistor)*16*LTC2946_TIME_lsb; //! 1) Calculate Coulomb lsb Current lsb and Time lsb
coulombs = adc_code*coulomb_lsb; //! 2) Calculate Coulombs using Coulomb lsb and adc code
return(coulombs);
}
//Calculate the LTC2946 Time in Seconds
float LTC2946_code_to_time(float time_code, float LTC2946_TIME_lsb)
{
float seconds;
seconds = LTC2946_TIME_lsb * time_code;
return seconds;
}