| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC2945.cpp
Go to the documentation of this file.
1 /*!
2 LTC2945: 12-Bit Wide Range Power Monitor
3
4 @verbatim
5
6 The LTC2945 is a rail-to-rail system monitor that measures current, voltage, and
7 power. It features an operating range of 2.7V to 80V and includes a shunt
8 regulator for supplies above 80V to allow flexibility in the selection of input
9 supply. The current measurement range of 0V to 80V is independent of the input
10 supply. An onboard 0.75% accurate 12-bit ADC measures load current, input
11 voltage and an auxiliary external voltage. A 24-bit power value is generated by
12 digitally multiplying the measured 12-bit load current and input voltage data.
13 Minimum and maximum values are stored and an overrange alert with programmable
14 thresholds minimizes the need for software polling. Data is reported via a
15 standard I2C interface. Shutdown mode reduces power consumption to 20uA.
16
17 @endverbatim
18
19 http://www.linear.com/product/LTC2945
20
21 http://www.linear.com/product/ltc2945#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 LTC2945 LTC2945: 12-Bit Wide Range Power Monitor
61 //! @}
62
63 /*! @file
64 @ingroup LTC2945
65 Library for LTC2945 12-Bit Wide Range Power Monitor
66 */
67
68 #include <Arduino.h>
69 #include <stdint.h>
70 #include "Linduino.h"
71 #include "LT_I2C.h"
72 #include "LTC2945.h"
73 #include <Wire.h>
74
75 // Write an 8-bit code to the LTC2945.
76 int8_t LTC2945_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
77 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
78 {
79 int32_t ack;
80
81 ack = i2c_write_byte_data(i2c_address, adc_command, code);
82
83 return ack;
84
85 }
86
87 // Write a 16-bit code to the LTC2945.
88 int8_t LTC2945_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 // Write a 24-bit code to the LTC2945.
98 int8_t LTC2945_write_24_bits(uint8_t i2c_address, uint8_t adc_command, int32_t code)
99 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
100 {
101 int8_t ack;
102
103 LT_union_int32_4bytes data;
104 data.LT_int32 = code;
105
106 ack = i2c_write_block_data(i2c_address, adc_command, (uint8_t) 3, data.LT_byte);
107
108 return(ack);
109 }
110
111 // Reads an 8-bit adc_code from LTC2945
112 int8_t LTC2945_read(uint8_t i2c_address, uint8_t adc_command, uint8_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_byte_data(i2c_address, adc_command, adc_code);
118
119 return ack;
120 }
121
122 // Reads a 12-bit adc_code from LTC2945
123 int8_t LTC2945_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
124 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
125 {
126 // 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)
127 // Then, shift by 4 bits and return in *adc_code
128 int32_t ack;
129
130 ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
131
132 *adc_code >>= 4;
133 return ack;
134 }
135
136 // Reads a 16-bit adc_code from LTC2945
137 int8_t LTC2945_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
138 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
139 {
140 int32_t ack;
141
142 ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
143
144 return ack;
145 }
146
147 // Reads a 24-bit adc_code from LTC2945
148 int8_t LTC2945_read_24_bits(uint8_t i2c_address, uint8_t adc_command, int32_t *adc_code)
149 // The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
150 {
151 int8_t ack;
152
153 LT_union_int32_4bytes data;
154
155 ack = i2c_read_block_data(i2c_address, adc_command, (uint8_t)3, data.LT_byte);
156
157 *adc_code = 0x0FFFFFF & data.LT_int32;
158 return(ack);
159 }
160
161 // Calculate the LTC2945 VIN voltage
162 float LTC2945_VIN_code_to_voltage(uint16_t adc_code, float LTC2945_VIN_lsb)
163 // Returns the VIN Voltage in Volts
164 {
165 float voltage;
166 voltage = (float)adc_code*LTC2945_VIN_lsb; //! 1) Calculate voltage from code and lsb
167 return(voltage);
168 }
169
170 // Calculate the LTC2945 ADIN voltage
171 float LTC2945_ADIN_code_to_voltage(uint16_t adc_code, float LTC2945_ADIN_lsb)
172 // Returns the ADIN Voltage in Volts
173 {
174 float adc_voltage;
175 adc_voltage = (float)adc_code*LTC2945_ADIN_lsb; //! 1) Calculate voltage from code and ADIN lsb
176 return(adc_voltage);
177 }
178
179 // Calculate the LTC2945 current with a sense resistor
180 float LTC2945_code_to_current(uint16_t adc_code, float resistor, float LTC2945_DELTA_SENSE_lsb)
181 // Returns the LTC2945 current
182 {
183 float voltage, current;
184 voltage = (float)adc_code*LTC2945_DELTA_SENSE_lsb; //! 1) Calculate voltage from ADC code and delta sense lsb
185 current = voltage/resistor; //! 2) Calculate current, I = V/R
186 return(current);
187 }
188
189 // Calculate the LTC2945 power
190 float LTC2945_code_to_power(int32_t adc_code, float resistor, float LTC2945_Power_lsb)
191 // Returns The LTC2945 power
192 {
193 float voltage, power;
194 voltage = (float)adc_code*LTC2945_Power_lsb; //! 1) Calculate V^2 from ADC code and power lsb
195 power = voltage/resistor; //! 2) Calculate Power, P = V^2/R
196 return(power);
197 }
198
199 // Calculate the LTC2945 power with the ADIN
200 float LTC2945_code_to_ADIN_power(int32_t adc_code, float resistor, float LTC2945_ADIN_DELTA_SENSE_lsb)
201 // Returns the LTC2945 power with the ADIN
202 {
203 float voltage, power;
204 voltage=(float)adc_code*LTC2945_ADIN_DELTA_SENSE_lsb; //! 1) Calculate V^2 from ADC code and ADIN delta sense lsb
205 power = voltage/resistor; //! 2) Calculate Power, P = V^2/R
206 return(power);
207 }
i2c_address
uint8_t i2c_address
adc_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
i2c_read_block_data
int8_t i2c_read_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
Read a block of data, starting at register specified by "command" and ending at (command + length - 1...
Definition: LT_I2C.cpp:244
LT_union_int32_4bytes::LT_byte
uint8_t LT_byte[4]
4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer ...
Definition: Linduino.h:112
LTC2945_write_16_bits
int8_t LTC2945_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
Write a 16-bit code to the LTC2945.
Definition: LTC2945.cpp:88
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
LTC2945_code_to_ADIN_power
float LTC2945_code_to_ADIN_power(int32_t adc_code, float resistor, float LTC2945_ADIN_DELTA_SENSE_lsb)
Calculate the LTC2945 power with the ADIN.
Definition: LTC2945.cpp:200
resistor
const float resistor
resistor value on demo board
Definition: DC1496BB.ino:116
LTC2945_code_to_current
float LTC2945_code_to_current(uint16_t adc_code, float resistor, float LTC2945_DELTA_SENSE_lsb)
Calculate the LTC2945 current with a sense resistor.
Definition: LTC2945.cpp:180
Linduino.h
Header File for Linduino Libraries and Demo Code.
LTC2945_read_16_bits
int8_t LTC2945_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 16-bit adc_code from LTC2945.
Definition: LTC2945.cpp:137
LTC2945_ADIN_lsb
const float LTC2945_ADIN_lsb
Typical ADIN lsb weight in volts.
Definition: DC1697A.ino:112
LTC2945_read
int8_t LTC2945_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
Reads an 8-bit adc_code from LTC2945.
Definition: LTC2945.cpp:112
adc_voltage
static float adc_voltage
Definition: DC2071AA.ino:115
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LTC2945_write
int8_t LTC2945_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write an 8-bit code to the LTC2945.
Definition: LTC2945.cpp:76
LTC2945_code_to_power
float LTC2945_code_to_power(int32_t adc_code, float resistor, float LTC2945_Power_lsb)
Calculate the LTC2945 power.
Definition: LTC2945.cpp:190
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
LTC2945_Power_lsb
const float LTC2945_Power_lsb
Typical POWER lsb weight in V^2.
Definition: DC1697A.ino:115
LT_union_int32_4bytes::LT_int32
int32_t LT_int32
32-bit signed integer to be converted to four bytes
Definition: Linduino.h:110
LTC2945_ADIN_code_to_voltage
float LTC2945_ADIN_code_to_voltage(uint16_t adc_code, float LTC2945_ADIN_lsb)
Calculate the LTC2945 ADIN voltage.
Definition: LTC2945.cpp:171
LTC2945_VIN_code_to_voltage
float LTC2945_VIN_code_to_voltage(uint16_t adc_code, float LTC2945_VIN_lsb)
Calculate the LTC2945 VIN voltage.
Definition: LTC2945.cpp:162
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.
LTC2945_ADIN_DELTA_SENSE_lsb
const float LTC2945_ADIN_DELTA_SENSE_lsb
Typical sense lsb weight in V^2 *ADIN_lsb * DELTA_SENSE_lsb.
Definition: DC1697A.ino:116
i2c_write_block_data
int8_t i2c_write_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
Write a block of data, starting at register specified by "command" and ending at (command + length - ...
Definition: LT_I2C.cpp:321
LTC2945_VIN_lsb
const float LTC2945_VIN_lsb
Typical VIN lsb weight in volts.
Definition: DC1697A.ino:114
LT_union_int32_4bytes
This union splits one int32_t (32-bit signed integer) or uint32_t (32-bit unsigned integer) four uint...
Definition: Linduino.h:108
LTC2945_read_12_bits
int8_t LTC2945_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 12-bit adc_code from LTC2945.
Definition: LTC2945.cpp:123
LTC2945_DELTA_SENSE_lsb
const float LTC2945_DELTA_SENSE_lsb
Typical Delta lsb weight in volts.
Definition: DC1697A.ino:113
LTC2945_read_24_bits
int8_t LTC2945_read_24_bits(uint8_t i2c_address, uint8_t adc_command, int32_t *adc_code)
Reads a 24-bit adc_code from LTC2945.
Definition: LTC2945.cpp:148
voltage
static float voltage
Definition: DC2289AA.ino:71
current
static uint16_t current
the current measurement from the LTC3335&#39;s counter test mode.
Definition: DC2343A.ino:114
LTC2945.h
LTC2945: 12-Bit Wide Range Power Monitor.
adc_code
static uint32_t adc_code
Definition: DC2071AA.ino:113
LTC2945_write_24_bits
int8_t LTC2945_write_24_bits(uint8_t i2c_address, uint8_t adc_command, int32_t code)
Write a 24-bit code to the LTC2945.
Definition: LTC2945.cpp:98