Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2990.cpp
Go to the documentation of this file.
1 /*!
2 LTC2990: 14-bit Quad I2C Voltage, Current, and Temperature Monitor
3 
4 @verbatim
5 
6 The LTC2990 is used to monitor system temperatures, voltages and currents.
7 Through the I2C serial interface, the four 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 LTC2990 flexibility to be incorporated in
13 various systems needing temperature, voltage or current data. The LTC2990 fits
14 well in systems needing sub-millivolt 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/LTC2990
20 
21 http://www.linear.com/product/LTC2990#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 LTC2990 LTC2990: 14-bit Quad I2C Voltage, Current, and Temperature Monitor
61 //! @}
62 
63 /*! @file
64  @ingroup LTC2990
65  Library for LTC2990: 14-bit Quad 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 "LTC2990.h"
73 #include <Wire.h>
74 
75 // Reads a 14-bit adc_code from LTC2990.
76 int8_t LTC2990_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 LTC2990 but enforces a maximum timeout.
90 // Similar to LTC2990_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 LTC2990 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 LTC2990_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  *data_valid = 0;
99 
100  for (timer_count = 0; timer_count < timeout; timer_count++)
101  {
102 
103  ack |= LTC2990_register_read(i2c_address, LTC2990_STATUS_REG, &reg_data); //! 1)Read status register until correct data valid bit is set
104 
105  if ((ack) || (((reg_data>>status_bit)&0x1)==1))
106  {
107  break;
108  }
109  delay(1);
110  }
111  ack |= LTC2990_adc_read(i2c_address, msb_register_address, &(*adc_code), &(*data_valid)); //! 2) It's either valid or it's timed out, we read anyways
112  if (*data_valid !=1)
113  {
114  return (1);
115  }
116  return(ack);
117 }
118 
119 // Reads new data (even after a mode change) by flushing old data and waiting for the data_valid bit to be set.
120 // This function simplifies adc reads when modes are changing. For example, if V1-V2 changes from temperature mode
121 // to differential voltage mode, the data in the register may still correspond to the temperature reading immediately
122 // after the mode change. Flushing one reading and waiting for a new reading guarantees fresh data is received.
123 // If the timeout is reached without valid data (*data_valid=1) the function exits.
124 int8_t LTC2990_adc_read_new_data(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout)
125 {
126  int8_t ack = 0;
127  uint8_t status_bit;
128  status_bit = msb_register_address/2-1;
129  ack |= LTC2990_adc_read_timeout(i2c_address, msb_register_address, adc_code, data_valid, timeout, status_bit); //! 1) Throw away old data
130 
131  ack |= LTC2990_adc_read_timeout(i2c_address, msb_register_address, adc_code, data_valid, timeout,status_bit); //! 2) Read new data
132 
133  return(ack);
134 }
135 
136 // Reads an 8-bit register from the LTC2990 using the standard repeated start format.
137 int8_t LTC2990_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
138 {
139  int8_t ack = 0;
140 
141  ack = i2c_read_byte_data(i2c_address, register_address, register_data);
142  return(ack);
143 }
144 
145 // Write one byte to an LTC2990 register.
146 // Writes to an 8-bit register inside the LTC2990 using the standard I2C repeated start format.
147 int8_t LTC2990_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
148 {
149  int8_t ack = 0;
150 
151  ack = i2c_write_byte_data(i2c_address, register_address, register_data);
152  return(ack);
153 }
154 
155 // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register.
156 // 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.
157 int8_t LTC2990_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
158 {
159  uint8_t register_data;
160  int8_t ack = 0;
161 
162  ack |= LTC2990_register_read(i2c_address, register_address, &register_data); //! 1) Read register
163  register_data = register_data & (~bits_to_clear); //! 2) Clear bits that were set to be cleared
164  register_data = register_data | bits_to_set;
165  ack |= LTC2990_register_write(i2c_address, register_address, register_data); //! 3) Write to register with the cleared bits
166  return(ack);
167 }
168 
169 // Calculates the LTC2990 single-ended input voltages
170 float LTC2990_code_to_single_ended_voltage(int16_t adc_code, float LTC2990_single_ended_lsb)
171 {
172  float voltage;
173  int16_t sign = 1;
174  if (adc_code >> 14)
175  {
176  adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
177  sign = -1;
178  }
179  adc_code = (adc_code & 0x3FFF);
180  voltage = ((float) adc_code) * LTC2990_single_ended_lsb * sign; //! 2) Convert code to voltage from lsb
181  return (voltage);
182 }
183 
184 // Calculates the LTC2990 Vcc voltage
185 float LTC2990_code_to_vcc_voltage(int16_t adc_code, float LTC2990_single_ended_lsb)
186 {
187  float voltage;
188  int16_t sign = 1;
189  if (adc_code >> 14)
190  {
191  adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
192  sign = -1;
193  }
194 
195  voltage = (((float) adc_code) * LTC2990_single_ended_lsb * sign) + 2.5; //! 2) Convert code to Vcc Voltage from single-ended lsb
196  return (voltage);
197 }
198 
199 // Calculates the LTC2990 differential input voltage.
200 float LTC2990_code_to_differential_voltage(int16_t adc_code, float LTC2990_differential_lsb)
201 {
202  float voltage;
203  int16_t sign = 1;
204  if (adc_code >> 14)
205  {
206  adc_code = (adc_code ^ 0x7FFF) + 1; //! 1)Converts two's complement to binary
207  sign = -1;
208  }
209  voltage = ((float) adc_code) * LTC2990_differential_lsb * sign; //! 2) Convert code to voltage form differential lsb
210  return (voltage);
211 }
212 
213 // Calculates the LTC2990 temperature
214 float LTC2990_temperature(int16_t adc_code, float LTC2990_temperature_lsb, boolean unit)
215 {
216  float temperature;
217  adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
218  if (!unit) //! 2)Checks to see if it's Kelvin
219  {
220  if (adc_code >>12)
221  {
222  adc_code = (adc_code | 0xE000); //! Sign extend if it's not Kelvin (Celsius)
223  }
224  }
225  temperature = ((float) adc_code) * LTC2990_temperature_lsb; //! 3) Converts code to temperature from temperature lsb
226 
227  return (temperature);
228 }
229 
230 // Calculates the LTC2990 diode voltage
231 float LTC2990_code_to_diode_voltage(int16_t adc_code, float LTC2990_diode_voltage_lsb)
232 {
233  float voltage;
234  adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
235  voltage = ((float) adc_code) * LTC2990_diode_voltage_lsb; //! 2) Convert code to voltage from diode voltage lsb
236  return (voltage);
237 }
uint8_t i2c_address
int8_t LTC2990_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 LTC2990.
Definition: LTC2990.cpp:76
#define LTC2990_STATUS_REG
Indicates BUSY state and conversion status.
Definition: LTC2990.h:170
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
float LTC2990_code_to_single_ended_voltage(int16_t adc_code, float LTC2990_single_ended_lsb)
Calculates the LTC2990 single-ended input voltages.
Definition: LTC2990.cpp:170
Header File for Linduino Libraries and Demo Code.
int8_t LTC2990_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: LTC2990.cpp:124
float LTC2990_code_to_vcc_voltage(int16_t adc_code, float LTC2990_single_ended_lsb)
Calculates the LTC2990 Vcc voltage.
Definition: LTC2990.cpp:185
LTC2990: 14-bit ADC Quad I2C voltage, current, and temperature monitor.
int8_t LTC2990_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: LTC2990.cpp:157
int8_t LTC2990_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
Write one byte to an LTC2990 register.
Definition: LTC2990.cpp:147
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 LTC2990_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
Reads an 8-bit register from the LTC2990 using the standard repeated start format.
Definition: LTC2990.cpp:137
int8_t LTC2990_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 LTC2990 but enforces a maximum timeout.
Definition: LTC2990.cpp:93
long timeout
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 LTC2990_temperature(int16_t adc_code, float LTC2990_temperature_lsb, boolean unit)
Calculates the LTC2990 temperature.
Definition: LTC2990.cpp:214
float LTC2990_code_to_differential_voltage(int16_t adc_code, float LTC2990_differential_lsb)
Calculates the LTC2990 differential input voltage.
Definition: LTC2990.cpp:200
static float voltage
Definition: DC2289AA.ino:71
float LTC2990_code_to_diode_voltage(int16_t adc_code, float LTC2990_diode_voltage_lsb)
Calculates the LTC2990 diode voltage.
Definition: LTC2990.cpp:231
static uint32_t adc_code
Definition: DC2071AA.ino:113