Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC4151.cpp
Go to the documentation of this file.
1 /*!
2 LTC4151: High Voltage I2C Current and Voltage Monitor
3 
4 @verbatim
5 
6 The LTC4151 is a high side power monitor that operates over a wide voltage range
7 of 7V to 80V. In default operation mode, the onboard 12-bit ADC continuously
8 measures high side current, input voltage and an external voltage. Data is
9 reported through the I2C interface when polled by a host. The LTC4151 can also
10 perform on-demand measurement in a snapshot mode. The LTC4151 features a
11 dedicated shutdown pin to reduce power consumption. The LTC4151-1/LTC4151-2
12 feature split I2C data pins to drive opto-isolators. The data out on the
13 LTC4151-1 is inverted while that on the LTC4151-2 is not.
14 
15 @endverbatim
16 
17 http://www.linear.com/product/LTC4151
18 
19 http://www.linear.com/product/LTC4151#demoboards
20 
21 
22 Copyright 2018(c) Analog Devices, Inc.
23 
24 All rights reserved.
25 
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28  - Redistributions of source code must retain the above copyright
29  notice, this list of conditions and the following disclaimer.
30  - Redistributions in binary form must reproduce the above copyright
31  notice, this list of conditions and the following disclaimer in
32  the documentation and/or other materials provided with the
33  distribution.
34  - Neither the name of Analog Devices, Inc. nor the names of its
35  contributors may be used to endorse or promote products derived
36  from this software without specific prior written permission.
37  - The use of this software may or may not infringe the patent rights
38  of one or more patent holders. This license does not release you
39  from the requirement that you obtain separate licenses from these
40  patent holders to use this software.
41  - Use of the software either in source or binary form, must be run
42  on or directly connected to an Analog Devices Inc. component.
43 
44 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
45 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
46 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
48 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
50 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55 
56 //! @ingroup Power_Monitors
57 //! @{
58 //! @defgroup LTC4151 LTC4151: High Voltage I2C Current and Voltage Monitor
59 //! @}
60 
61 /*! @file
62  @ingroup LTC4151
63  Library for LTC4151: High Voltage I2C Current and Voltage Monitor
64 */
65 
66 #include <Arduino.h>
67 #include "Linduino.h"
68 #include "LT_I2C.h"
69 #include "LTC4151.h"
70 #include <Wire.h>
71 
72 // Write one byte to an LTC4151 register.
73 int8_t LTC4151_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
74 {
75  int32_t ack;
76 
77  ack = i2c_write_byte_data(i2c_address, adc_command, code);
78 
79  return(ack);
80 }
81 
82 // Reads a 12-bit value from LTC4151
83 int8_t LTC4151_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
84 {
85  int32_t ack = 0;
86 
87  ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
88 
89  *adc_code >>= 4;
90 
91  return(ack);
92 }
93 
94 // Note that the following functions are somewhat contrived, and the units are arbitrary. LTC4151_ADIN_voltage could be given an LSB
95 // weight (LTC4151_adin_lsb) in volts instead of mV, and the returned voltage will be in volts instead of mV.
96 // Similarly, the LTC4151_sense_current function could be written to accept an LSB weight in amps or milliamps.
97 // That would eliminate the extra floating point divide operation based on the sense resistor value.
98 
99 // Calculates the LTC4151 sense current in Amps given "resistor" value in ohms and "LTC4151_sense_lsb" LSB weight in volts.
101 {
102  float voltage, current;
103  voltage = (float)adc_code * LTC4151_sense_lsb; //! 1) Convert code to voltage from sense lsb
104  current = voltage / resistor; //! 2) Calculate Current, I = V/R
105  return (current);
106 }
107 
108 // Calculates the LTC4151 V_IN voltage given "LTC_vin_lsb" LSB weight in volts
110 {
111  float voltage;
112  voltage = (float)adc_code * LTC4151_vin_lsb; //! 1) Convert code to voltage from VIN lsb
113  return (voltage);
114 }
115 
116 // Calculates the LTC4151 ADIN voltage in mV given "LTC4151_adin_lsb" LSB weight in mV
118 {
119  float voltage;
120  voltage = (float)adc_code * LTC4151_adin_lsb; //! 1) Convert Code to voltage from ADIN lsb
121  return (voltage);
122 }
uint8_t i2c_address
static uint8_t adc_command
Definition: DC2071AA.ino:111
const float LTC4151_adin_lsb
Typical ADIN lsb weight in mV.
Definition: DC1208A.ino:110
const float resistor
resistor value on demo board
Definition: DC1496BB.ino:116
Header File for Linduino Libraries and Demo Code.
float LTC4151_code_to_ADIN_voltage(uint16_t adc_code, float LTC4151_adin_lsb)
Calculates the LTC4151 ADIN voltage in mV given "LTC4151_adin_lsb" LSB weight in mV.
Definition: LTC4151.cpp:117
int8_t LTC4151_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write one byte to an LTC4151 register.
Definition: LTC4151.cpp:73
const float LTC4151_sense_lsb
Typical sense lsb weight in volts.
Definition: DC1208A.ino:108
LTC4151: High Voltage I2C Current and Voltage Monitor.
int8_t LTC4151_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 12-bit value from LTC4151.
Definition: LTC4151.cpp:83
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
float LTC4151_code_to_vin_voltage(uint16_t adc_code, float LTC4151_vin_lsb)
Calculates the LTC4151 V_IN voltage given "LTC_vin_lsb" LSB weight in volts.
Definition: LTC4151.cpp:109
const float LTC4151_vin_lsb
Typical Vin lsb weight in volts.
Definition: DC1208A.ino:109
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 LTC4151_code_to_sense_current(uint16_t adc_code, float resistor, float LTC4151_sense_lsb)
Calculates the LTC4151 sense current in Amps given "resistor" value in ohms and "LTC4151_sense_lsb" L...
Definition: LTC4151.cpp:100
static float voltage
Definition: DC2289AA.ino:71
static uint16_t current
the current measurement from the LTC3335&#39;s counter test mode.
Definition: DC2343A.ino:114
static uint32_t adc_code
Definition: DC2071AA.ino:113