Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ltc2991_test by
LTC2991.cpp
00001 /*! 00002 LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor 00003 00004 @verbatim 00005 00006 The LTC2991 is used to monitor system temperatures, voltages and currents. 00007 Through the I2C serial interface, the eight monitors can individually measure 00008 supply voltages and can be paired for differential measurements of current sense 00009 resistors or temperature sensing transistors. Additional measurements include 00010 internal temperature and internal VCC. The internal 10ppm reference minimizes 00011 the number of supporting components and area required. Selectable address and 00012 configurable functionality give the LTC2991 flexibility to be incorporated in 00013 various systems needing temperature, voltage or current data. The LTC2991 fits 00014 well in systems needing submillivolt voltage resolution, 1% current measurement 00015 and 1 degree Celsius temperature accuracy or any combination of the three. 00016 00017 @endverbatim 00018 00019 http://www.linear.com/product/LTC2991 00020 00021 http://www.linear.com/product/LTC2991#demoboards 00022 00023 REVISION HISTORY 00024 $Revision: 3659 $ 00025 $Date: 2015-07-01 10:19:20 -0700 (Wed, 01 Jul 2015) $ 00026 00027 Copyright (c) 2013, Linear Technology Corp.(LTC) 00028 All rights reserved. 00029 00030 Redistribution and use in source and binary forms, with or without 00031 modification, are permitted provided that the following conditions are met: 00032 00033 1. Redistributions of source code must retain the above copyright notice, this 00034 list of conditions and the following disclaimer. 00035 2. Redistributions in binary form must reproduce the above copyright notice, 00036 this list of conditions and the following disclaimer in the documentation 00037 and/or other materials provided with the distribution. 00038 00039 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00040 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00041 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00042 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00043 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00044 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00045 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00046 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00047 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00048 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00049 00050 The views and conclusions contained in the software and documentation are those 00051 of the authors and should not be interpreted as representing official policies, 00052 either expressed or implied, of Linear Technology Corp. 00053 00054 The Linear Technology Linduino is not affiliated with the official Arduino team. 00055 However, the Linduino is only possible because of the Arduino team's commitment 00056 to the open-source community. Please, visit http://www.arduino.cc and 00057 http://store.arduino.cc , and consider a purchase that will help fund their 00058 ongoing work. 00059 */ 00060 00061 //! @defgroup LTC2991 LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor 00062 00063 /*! @file 00064 @ingroup LTC2991 00065 Library for LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor 00066 */ 00067 00068 #include <stdint.h> 00069 #include "Linduino.h " 00070 #include "LT_I2C.h " 00071 #include "LTC2991.h " 00072 #include "mbed.h" 00073 00074 //Serial pc2(USBTX, USBRX, 115200); 00075 00076 LTC2991::LTC2991() { 00077 lti2c = new LT_I2C(); 00078 } 00079 00080 LTC2991::LTC2991(PinName i2c_sda_, PinName i2c_scl_) { 00081 lti2c = new LT_I2C(i2c_sda_, i2c_scl_); 00082 } 00083 00084 // Reads a 14-bit adc_code from LTC2991. 00085 int8_t LTC2991::LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid) 00086 { 00087 int8_t ack = 0; 00088 uint16_t code; 00089 ack = lti2c->i2c_read_word_data(i2c_address, msb_register_address, &code); 00090 00091 00092 *data_valid = (code >> 15) & 0x01; // Place Data Valid Bit in *data_valid 00093 00094 *adc_code = code & 0x7FFF; // Removes data valid bit to return proper adc_code value 00095 00096 //pc2.printf("i2c_read_word_data result: raw code: %0x%X, ack %d\n", code, ack); 00097 00098 return(ack); 00099 } 00100 00101 // Reads a 14-bit adc_code from the LTC2991 but enforces a maximum timeout. 00102 // 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) 00103 // expires. It keeps trying to read from the LTC2991 every millisecond until the data_valid bit is set (indicating new data since the previous 00104 // time this register was read) or until it fails to receive an I2C acknowledge (indicating an error on the I2C bus). 00105 int8_t LTC2991::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) 00106 { 00107 int8_t ack = 0; 00108 uint8_t reg_data; 00109 uint16_t timer_count; // Timer count for data_valid 00110 00111 for (timer_count = 0; timer_count < 2000; timer_count++) 00112 { 00113 //pc2.printf("status_bit: %d\n", status_bit); 00114 00115 if (status_bit<8) 00116 { 00117 ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_LOW_REG, ®_data); //! 1)Read status register until correct data valid bit is set 00118 } 00119 else 00120 { 00121 ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_HIGH_REG, ®_data); //! 1)Read status register until correct data valid bit is set 00122 if (status_bit==8) 00123 { 00124 status_bit =1; 00125 } 00126 else 00127 { 00128 status_bit = 0; 00129 } 00130 } 00131 00132 if ((ack) || (((reg_data>>status_bit)&0x1)==1)) 00133 { 00134 break; 00135 } 00136 00137 wait_us(50); 00138 } 00139 00140 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 00141 if (*data_valid !=1) 00142 { 00143 //pc2.printf("Data not valid: 0x%X\n", *data_valid); 00144 return (1); 00145 } 00146 00147 return(ack); 00148 } 00149 00150 // Reads new data (even after a mode change) by flushing old data and waiting for the data_valid bit to be set. 00151 // This function simplifies adc reads when modes are changing. For example, if V1-V2 changes from temperature mode 00152 // to differential voltage mode, the data in the register may still correspond to the temperature reading immediately 00153 // after the mode change. Flushing one reading and waiting for a new reading guarantees fresh data is received. 00154 // If the timeout is reached without valid data (*data_valid=1) the function exits. 00155 int8_t LTC2991::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) 00156 { 00157 int8_t ack = 0; 00158 00159 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 00160 ack |= LTC2991_adc_read_timeout(i2c_address, msb_register_address, &(*adc_code), &(*data_valid), timeout, ((msb_register_address/2) - 0x05)); //! 2) Read new data 00161 00162 return(ack); 00163 } 00164 00165 // Reads an 8-bit register from the LTC2991 using the standard repeated start format. 00166 int8_t LTC2991::LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data) 00167 { 00168 int8_t ack = 0; 00169 ack = lti2c->i2c_read_byte_data(i2c_address, register_address, register_data); 00170 return(ack); 00171 } 00172 00173 // Write one byte to an LTC2991 register. 00174 // Writes to an 8-bit register inside the LTC2991 using the standard I2C repeated start format. 00175 int8_t LTC2991::LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data) 00176 { 00177 int8_t ack = 0; 00178 ack = lti2c->i2c_write_byte_data(i2c_address, register_address, register_data); 00179 return(ack); 00180 } 00181 00182 // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register. 00183 // 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. 00184 int8_t LTC2991::LTC2991_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear) 00185 { 00186 uint8_t register_data; 00187 int8_t ack = 0; 00188 00189 ack |= LTC2991_register_read(i2c_address, register_address, ®ister_data); //! 1) Read register 00190 register_data = register_data & (~bits_to_clear); //! 2) Clear bits that were set to be cleared 00191 register_data = register_data | bits_to_set; 00192 ack |= LTC2991_register_write(i2c_address, register_address, register_data); //! 3) Write to register with the cleared bits 00193 return(ack); 00194 } 00195 00196 // Calculates the LTC2991 single-ended input voltages 00197 float LTC2991::LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb) 00198 { 00199 float voltage; 00200 int16_t sign = 1; 00201 if (adc_code >> 14) 00202 { 00203 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary 00204 sign = -1; 00205 } 00206 adc_code = (adc_code & 0x3FFF); 00207 voltage = ((float) adc_code) * LTC2991_single_ended_lsb * sign; //! 2) Convert code to voltage from lsb 00208 return (voltage); 00209 } 00210 00211 // Calculates the LTC2991 Vcc voltage 00212 float LTC2991::LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb) 00213 { 00214 float voltage; 00215 int16_t sign = 1; 00216 if (adc_code >> 14) 00217 { 00218 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary 00219 sign = -1; 00220 } 00221 00222 voltage = (((float) adc_code) * LTC2991_single_ended_lsb * sign) + 2.5; //! 2) Convert code to Vcc Voltage from single-ended lsb 00223 return (voltage); 00224 } 00225 00226 // Calculates the LTC2991 differential input voltage. 00227 float LTC2991::LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb) 00228 { 00229 float voltage; 00230 int16_t sign = 1; 00231 if (adc_code >> 14) 00232 { 00233 adc_code = (adc_code ^ 0x7FFF) + 1; //! 1)Converts two's complement to binary 00234 sign = -1; 00235 } 00236 voltage = ((float) adc_code) * LTC2991_differential_lsb * sign; //! 2) Convert code to voltage form differential lsb 00237 return (voltage); 00238 } 00239 00240 // Calculates the LTC2991 temperature 00241 float LTC2991::LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit) 00242 { 00243 float temperature; 00244 adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits 00245 if (!unit) //! 2)Checks to see if it's Kelvin 00246 { 00247 if (adc_code >>12) 00248 { 00249 adc_code = (adc_code | 0xE000); //! Sign extend if it's not Kelvin (Celsius) 00250 } 00251 } 00252 temperature = ((float) adc_code) * LTC2991_temperature_lsb; //! 3) Converts code to temperature from temperature lsb 00253 00254 return (temperature); 00255 } 00256 00257 // Calculates the LTC2991 diode voltage 00258 float LTC2991::LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb) 00259 { 00260 float voltage; 00261 adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits 00262 voltage = ((float) adc_code) * LTC2991_diode_voltage_lsb; //! 2) Convert code to voltage from diode voltage lsb 00263 return (voltage); 00264 }
Generated on Wed Jul 13 2022 18:29:42 by
1.7.2
