| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC2991.cpp
Go to the documentation of this file.
1 /*!
2 LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor
3
4 @verbatim
5
6 The LTC2991 is used to monitor system temperatures, voltages and currents.
7 Through the I2C serial interface, the eight monitors can individually measure
8 supply voltages and can be paired for differential measurements of current sense
9 resistors or temperature sensing transistors. Additional measurements include
10 internal temperature and internal VCC. The internal 10ppm reference minimizes
11 the number of supporting components and area required. Selectable address and
12 configurable functionality give the LTC2991 flexibility to be incorporated in
13 various systems needing temperature, voltage or current data. The LTC2991 fits
14 well in systems needing submillivolt voltage resolution, 1% current measurement
15 and 1 degree Celsius temperature accuracy or any combination of the three.
16
17 @endverbatim
18
19 http://www.linear.com/product/LTC2991
20
21 http://www.linear.com/product/LTC2991#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 LTC2991 LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor
61 //! @}
62
63 /*! @file
64 @ingroup LTC2991
65 Library for LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor
66 */
67
68 #include <Arduino.h>
69 #include <stdint.h>
70 #include "Linduino.h"
71 #include "LT_I2C.h"
72 #include "LTC2991.h"
73 #include <Wire.h>
74
75 // Reads a 14-bit adc_code from LTC2991.
76 int8_t LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid)
77 {
78 int8_t ack = 0;
79 uint16_t code;
80 ack = i2c_read_word_data(i2c_address, msb_register_address, &code);
81
82 *data_valid = (code >> 15) & 0x01; // Place Data Valid Bit in *data_valid
83
84 *adc_code = code & 0x7FFF; // Removes data valid bit to return proper adc_code value
85
86 return(ack);
87 }
88
89 // Reads a 14-bit adc_code from the LTC2991 but enforces a maximum timeout.
90 // Similar to LTC2991_adc_read except it repeats until the data_valid bit is set, it fails to receive an I2C acknowledge, or the timeout (in milliseconds)
91 // expires. It keeps trying to read from the LTC2991 every millisecond until the data_valid bit is set (indicating new data since the previous
92 // time this register was read) or until it fails to receive an I2C acknowledge (indicating an error on the I2C bus).
93 int8_t LTC2991_adc_read_timeout(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout, uint8_t status_bit)
94 {
95 int8_t ack = 0;
96 uint8_t reg_data;
97 uint16_t timer_count; // Timer count for data_valid
98
99 for (timer_count = 0; timer_count < timeout; timer_count++)
100 {
101
102 if (status_bit<8)
103 {
104 ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_LOW_REG, &reg_data); //! 1)Read status register until correct data valid bit is set
105 }
106 else
107 {
108 ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_HIGH_REG, &reg_data); //! 1)Read status register until correct data valid bit is set
109 if (status_bit==8)
110 {
111 status_bit =1;
112 }
113 else
114 {
115 status_bit = 0;
116 }
117 }
118
119 if ((ack) || (((reg_data>>status_bit)&0x1)==1))
120 {
121 break;
122 }
123
124 delay(1);
125 }
126
127 ack |= LTC2991_adc_read(i2c_address, msb_register_address, &(*adc_code), &(*data_valid)); //! 2) It's either valid or it's timed out, we read anyways
128 if (*data_valid !=1)
129 {
130 Serial.println("Data not valid");
131 Serial.println(*data_valid);
132 return (1);
133 }
134 return(ack);
135 }
136
137 // Reads new data (even after a mode change) by flushing old data and waiting for the data_valid bit to be set.
138 // This function simplifies adc reads when modes are changing. For example, if V1-V2 changes from temperature mode
139 // to differential voltage mode, the data in the register may still correspond to the temperature reading immediately
140 // after the mode change. Flushing one reading and waiting for a new reading guarantees fresh data is received.
141 // If the timeout is reached without valid data (*data_valid=1) the function exits.
142 int8_t LTC2991_adc_read_new_data(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout)
143 {
144 int8_t ack = 0;
145
146 ack |= LTC2991_adc_read_timeout(i2c_address, msb_register_address, &(*adc_code), &(*data_valid), timeout, ((msb_register_address/2) - 0x05)); //! 1) Throw away old data
147 ack |= LTC2991_adc_read_timeout(i2c_address, msb_register_address, &(*adc_code), &(*data_valid), timeout, ((msb_register_address/2) - 0x05)); //! 2) Read new data
148
149 return(ack);
150 }
151
152 // Reads an 8-bit register from the LTC2991 using the standard repeated start format.
153 int8_t LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
154 {
155 int8_t ack = 0;
156
157 ack = i2c_read_byte_data(i2c_address, register_address, register_data);
158 return(ack);
159 }
160
161 // Write one byte to an LTC2991 register.
162 // Writes to an 8-bit register inside the LTC2991 using the standard I2C repeated start format.
163 int8_t LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
164 {
165 int8_t ack = 0;
166
167 ack = i2c_write_byte_data(i2c_address, register_address, register_data);
168 return(ack);
169 }
170
171 // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register.
172 // 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.
173 int8_t LTC2991_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
174 {
175 uint8_t register_data;
176 int8_t ack = 0;
177
178 ack |= LTC2991_register_read(i2c_address, register_address, &register_data); //! 1) Read register
179 register_data = register_data & (~bits_to_clear); //! 2) Clear bits that were set to be cleared
180 register_data = register_data | bits_to_set;
181 ack |= LTC2991_register_write(i2c_address, register_address, register_data); //! 3) Write to register with the cleared bits
182 return(ack);
183 }
184
185 // Calculates the LTC2991 single-ended input voltages
186 float LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
187 {
188 float voltage;
189 int16_t sign = 1;
190 if (adc_code >> 14)
191 {
192 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
193 sign = -1;
194 }
195 adc_code = (adc_code & 0x3FFF);
196 voltage = ((float) adc_code) * LTC2991_single_ended_lsb * sign; //! 2) Convert code to voltage from lsb
197 return (voltage);
198 }
199
200 // Calculates the LTC2991 Vcc voltage
201 float LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
202 {
203 float voltage;
204 int16_t sign = 1;
205 if (adc_code >> 14)
206 {
207 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
208 sign = -1;
209 }
210
211 voltage = (((float) adc_code) * LTC2991_single_ended_lsb * sign) + 2.5; //! 2) Convert code to Vcc Voltage from single-ended lsb
212 return (voltage);
213 }
214
215 // Calculates the LTC2991 differential input voltage.
216 float LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb)
217 {
218 float voltage;
219 int16_t sign = 1;
220 if (adc_code >> 14)
221 {
222 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1)Converts two's complement to binary
223 sign = -1;
224 }
225 voltage = ((float) adc_code) * LTC2991_differential_lsb * sign; //! 2) Convert code to voltage form differential lsb
226 return (voltage);
227 }
228
229 // Calculates the LTC2991 temperature
230 float LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit)
231 {
232 float temperature;
233 adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
234 if (!unit) //! 2)Checks to see if it's Kelvin
235 {
236 if (adc_code >>12)
237 {
238 adc_code = (adc_code | 0xE000); //! Sign extend if it's not Kelvin (Celsius)
239 }
240 }
241 temperature = ((float) adc_code) * LTC2991_temperature_lsb; //! 3) Converts code to temperature from temperature lsb
242
243 return (temperature);
244 }
245
246 // Calculates the LTC2991 diode voltage
247 float LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb)
248 {
249 float voltage;
250 adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
251 voltage = ((float) adc_code) * LTC2991_diode_voltage_lsb; //! 2) Convert code to voltage from diode voltage lsb
252 return (voltage);
253 }
i2c_address
uint8_t i2c_address
sign
bool sign
Definition: Precision_Voltage_Source.ino:77
LTC2991_register_set_clear_bits
int8_t LTC2991_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
Used to set and clear bits in a control register.
Definition: LTC2991.cpp:173
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
LTC2991_adc_read_timeout
int8_t LTC2991_adc_read_timeout(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout, uint8_t status_bit)
Reads a 14-bit adc_code from the LTC2991 but enforces a maximum timeout.
Definition: LTC2991.cpp:93
LTC2991_code_to_vcc_voltage
float LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
Calculates the LTC2991 Vcc voltage.
Definition: LTC2991.cpp:201
Linduino.h
Header File for Linduino Libraries and Demo Code.
LTC2991_adc_read
int8_t LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid)
Reads a 14-bit adc_code from LTC2991.
Definition: LTC2991.cpp:76
LTC2991_code_to_diode_voltage
float LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb)
Calcultates the LTC2991 diode voltage.
Definition: LTC2991.cpp:247
LTC2991_register_write
int8_t LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
Write one byte to an LTC2991 register.
Definition: LTC2991.cpp:163
LTC2991_code_to_differential_voltage
float LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb)
Calculates the LTC2991 differential input voltage.
Definition: LTC2991.cpp:216
LTC2991_code_to_single_ended_voltage
float LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
Calculates the LTC2991 single-ended input voltages.
Definition: LTC2991.cpp:186
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
LTC2991_STATUS_LOW_REG
#define LTC2991_STATUS_LOW_REG
Data_Valid Bits(V1 Through V8)
Definition: LTC2991.h:172
timeout
long timeout
Definition: EVAL-AD7124-8.ino:144
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.
LTC2991_adc_read_new_data
int8_t LTC2991_adc_read_new_data(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout)
Reads new data (even after a mode change) by flushing old data and waiting for the data_valid bit to ...
Definition: LTC2991.cpp:142
LTC2991_register_read
int8_t LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
Reads an 8-bit register from the LTC2991 using the standard repeated start format.
Definition: LTC2991.cpp:153
voltage
static float voltage
Definition: DC2289AA.ino:71
LTC2991_temperature
float LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit)
Calculates the LTC2991 temperature.
Definition: LTC2991.cpp:230
LTC2991.h
LTC2991: 14-bit ADC octal I2C voltage, current, and temperature monitor.
LTC2991_STATUS_HIGH_REG
#define LTC2991_STATUS_HIGH_REG
Data_valid bits.
Definition: LTC2991.h:173
adc_code
static uint32_t adc_code
Definition: DC2071AA.ino:113