I2C not yet integrated
Tested working with single and differential voltages.
Connect SCL (pin 11) to D15 Connect SDA (pin 10) to D14 Connect pin 16 to +5v Connect pin 9 to gnd
LTC2991.cpp@1:4e4194db7cd6, 2016-11-29 (annotated)
- Committer:
- lrdawg99
- Date:
- Tue Nov 29 03:20:35 2016 +0000
- Revision:
- 1:4e4194db7cd6
- Parent:
- 0:1473318f27b6
- Child:
- 2:c9e727dcd00e
Tested working, reads single ended and differential voltages.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lrdawg99 | 0:1473318f27b6 | 1 | /*! |
lrdawg99 | 0:1473318f27b6 | 2 | LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor |
lrdawg99 | 0:1473318f27b6 | 3 | |
lrdawg99 | 0:1473318f27b6 | 4 | @verbatim |
lrdawg99 | 0:1473318f27b6 | 5 | |
lrdawg99 | 0:1473318f27b6 | 6 | The LTC2991 is used to monitor system temperatures, voltages and currents. |
lrdawg99 | 0:1473318f27b6 | 7 | Through the I2C serial interface, the eight monitors can individually measure |
lrdawg99 | 0:1473318f27b6 | 8 | supply voltages and can be paired for differential measurements of current sense |
lrdawg99 | 0:1473318f27b6 | 9 | resistors or temperature sensing transistors. Additional measurements include |
lrdawg99 | 0:1473318f27b6 | 10 | internal temperature and internal VCC. The internal 10ppm reference minimizes |
lrdawg99 | 0:1473318f27b6 | 11 | the number of supporting components and area required. Selectable address and |
lrdawg99 | 0:1473318f27b6 | 12 | configurable functionality give the LTC2991 flexibility to be incorporated in |
lrdawg99 | 0:1473318f27b6 | 13 | various systems needing temperature, voltage or current data. The LTC2991 fits |
lrdawg99 | 0:1473318f27b6 | 14 | well in systems needing submillivolt voltage resolution, 1% current measurement |
lrdawg99 | 0:1473318f27b6 | 15 | and 1 degree Celsius temperature accuracy or any combination of the three. |
lrdawg99 | 0:1473318f27b6 | 16 | |
lrdawg99 | 0:1473318f27b6 | 17 | @endverbatim |
lrdawg99 | 0:1473318f27b6 | 18 | |
lrdawg99 | 0:1473318f27b6 | 19 | http://www.linear.com/product/LTC2991 |
lrdawg99 | 0:1473318f27b6 | 20 | |
lrdawg99 | 0:1473318f27b6 | 21 | http://www.linear.com/product/LTC2991#demoboards |
lrdawg99 | 0:1473318f27b6 | 22 | |
lrdawg99 | 0:1473318f27b6 | 23 | REVISION HISTORY |
lrdawg99 | 0:1473318f27b6 | 24 | $Revision: 3659 $ |
lrdawg99 | 0:1473318f27b6 | 25 | $Date: 2015-07-01 10:19:20 -0700 (Wed, 01 Jul 2015) $ |
lrdawg99 | 0:1473318f27b6 | 26 | |
lrdawg99 | 0:1473318f27b6 | 27 | Copyright (c) 2013, Linear Technology Corp.(LTC) |
lrdawg99 | 0:1473318f27b6 | 28 | All rights reserved. |
lrdawg99 | 0:1473318f27b6 | 29 | |
lrdawg99 | 0:1473318f27b6 | 30 | Redistribution and use in source and binary forms, with or without |
lrdawg99 | 0:1473318f27b6 | 31 | modification, are permitted provided that the following conditions are met: |
lrdawg99 | 0:1473318f27b6 | 32 | |
lrdawg99 | 0:1473318f27b6 | 33 | 1. Redistributions of source code must retain the above copyright notice, this |
lrdawg99 | 0:1473318f27b6 | 34 | list of conditions and the following disclaimer. |
lrdawg99 | 0:1473318f27b6 | 35 | 2. Redistributions in binary form must reproduce the above copyright notice, |
lrdawg99 | 0:1473318f27b6 | 36 | this list of conditions and the following disclaimer in the documentation |
lrdawg99 | 0:1473318f27b6 | 37 | and/or other materials provided with the distribution. |
lrdawg99 | 0:1473318f27b6 | 38 | |
lrdawg99 | 0:1473318f27b6 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
lrdawg99 | 0:1473318f27b6 | 40 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
lrdawg99 | 0:1473318f27b6 | 41 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lrdawg99 | 0:1473318f27b6 | 42 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
lrdawg99 | 0:1473318f27b6 | 43 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
lrdawg99 | 0:1473318f27b6 | 44 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
lrdawg99 | 0:1473318f27b6 | 45 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
lrdawg99 | 0:1473318f27b6 | 46 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
lrdawg99 | 0:1473318f27b6 | 47 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
lrdawg99 | 0:1473318f27b6 | 48 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lrdawg99 | 0:1473318f27b6 | 49 | |
lrdawg99 | 0:1473318f27b6 | 50 | The views and conclusions contained in the software and documentation are those |
lrdawg99 | 0:1473318f27b6 | 51 | of the authors and should not be interpreted as representing official policies, |
lrdawg99 | 0:1473318f27b6 | 52 | either expressed or implied, of Linear Technology Corp. |
lrdawg99 | 0:1473318f27b6 | 53 | |
lrdawg99 | 0:1473318f27b6 | 54 | The Linear Technology Linduino is not affiliated with the official Arduino team. |
lrdawg99 | 0:1473318f27b6 | 55 | However, the Linduino is only possible because of the Arduino team's commitment |
lrdawg99 | 0:1473318f27b6 | 56 | to the open-source community. Please, visit http://www.arduino.cc and |
lrdawg99 | 0:1473318f27b6 | 57 | http://store.arduino.cc , and consider a purchase that will help fund their |
lrdawg99 | 0:1473318f27b6 | 58 | ongoing work. |
lrdawg99 | 0:1473318f27b6 | 59 | */ |
lrdawg99 | 0:1473318f27b6 | 60 | |
lrdawg99 | 0:1473318f27b6 | 61 | //! @defgroup LTC2991 LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor |
lrdawg99 | 0:1473318f27b6 | 62 | |
lrdawg99 | 0:1473318f27b6 | 63 | /*! @file |
lrdawg99 | 0:1473318f27b6 | 64 | @ingroup LTC2991 |
lrdawg99 | 0:1473318f27b6 | 65 | Library for LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor |
lrdawg99 | 0:1473318f27b6 | 66 | */ |
lrdawg99 | 0:1473318f27b6 | 67 | |
lrdawg99 | 0:1473318f27b6 | 68 | //#include <Arduino.h> |
lrdawg99 | 0:1473318f27b6 | 69 | #include <stdint.h> |
lrdawg99 | 0:1473318f27b6 | 70 | #include "Linduino.h" |
lrdawg99 | 0:1473318f27b6 | 71 | #include "LT_I2C.h" |
lrdawg99 | 0:1473318f27b6 | 72 | #include "LTC2991.h" |
lrdawg99 | 0:1473318f27b6 | 73 | #include "mbed.h" |
lrdawg99 | 0:1473318f27b6 | 74 | |
lrdawg99 | 1:4e4194db7cd6 | 75 | Serial pc2(USBTX, USBRX, 9600); |
lrdawg99 | 1:4e4194db7cd6 | 76 | |
lrdawg99 | 0:1473318f27b6 | 77 | //#include <Wire.h> |
lrdawg99 | 0:1473318f27b6 | 78 | |
lrdawg99 | 0:1473318f27b6 | 79 | // Reads a 14-bit adc_code from LTC2991. |
lrdawg99 | 0:1473318f27b6 | 80 | int8_t LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid) |
lrdawg99 | 0:1473318f27b6 | 81 | { |
lrdawg99 | 0:1473318f27b6 | 82 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 83 | uint16_t code; |
lrdawg99 | 0:1473318f27b6 | 84 | ack = i2c_read_word_data(i2c_address, msb_register_address, &code); |
lrdawg99 | 1:4e4194db7cd6 | 85 | |
lrdawg99 | 0:1473318f27b6 | 86 | |
lrdawg99 | 0:1473318f27b6 | 87 | *data_valid = (code >> 15) & 0x01; // Place Data Valid Bit in *data_valid |
lrdawg99 | 0:1473318f27b6 | 88 | |
lrdawg99 | 0:1473318f27b6 | 89 | *adc_code = code & 0x7FFF; // Removes data valid bit to return proper adc_code value |
lrdawg99 | 1:4e4194db7cd6 | 90 | |
lrdawg99 | 1:4e4194db7cd6 | 91 | //pc2.printf("i2c_read_word_data result: raw code: %0x%X, ack %d\n", code, ack); |
lrdawg99 | 0:1473318f27b6 | 92 | |
lrdawg99 | 0:1473318f27b6 | 93 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 94 | } |
lrdawg99 | 0:1473318f27b6 | 95 | |
lrdawg99 | 0:1473318f27b6 | 96 | // Reads a 14-bit adc_code from the LTC2991 but enforces a maximum timeout. |
lrdawg99 | 0:1473318f27b6 | 97 | // 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) |
lrdawg99 | 0:1473318f27b6 | 98 | // expires. It keeps trying to read from the LTC2991 every millisecond until the data_valid bit is set (indicating new data since the previous |
lrdawg99 | 0:1473318f27b6 | 99 | // time this register was read) or until it fails to receive an I2C acknowledge (indicating an error on the I2C bus). |
lrdawg99 | 0:1473318f27b6 | 100 | 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) |
lrdawg99 | 0:1473318f27b6 | 101 | { |
lrdawg99 | 0:1473318f27b6 | 102 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 103 | uint8_t reg_data; |
lrdawg99 | 0:1473318f27b6 | 104 | uint16_t timer_count; // Timer count for data_valid |
lrdawg99 | 0:1473318f27b6 | 105 | |
lrdawg99 | 0:1473318f27b6 | 106 | for (timer_count = 0; timer_count < timeout; timer_count++) |
lrdawg99 | 0:1473318f27b6 | 107 | { |
lrdawg99 | 1:4e4194db7cd6 | 108 | //pc2.printf("status_bit: %d\n", status_bit); |
lrdawg99 | 1:4e4194db7cd6 | 109 | |
lrdawg99 | 0:1473318f27b6 | 110 | if (status_bit<8) |
lrdawg99 | 0:1473318f27b6 | 111 | { |
lrdawg99 | 0:1473318f27b6 | 112 | ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_LOW_REG, ®_data); //! 1)Read status register until correct data valid bit is set |
lrdawg99 | 0:1473318f27b6 | 113 | } |
lrdawg99 | 0:1473318f27b6 | 114 | else |
lrdawg99 | 0:1473318f27b6 | 115 | { |
lrdawg99 | 0:1473318f27b6 | 116 | ack |= LTC2991_register_read(i2c_address, LTC2991_STATUS_HIGH_REG, ®_data); //! 1)Read status register until correct data valid bit is set |
lrdawg99 | 0:1473318f27b6 | 117 | if (status_bit==8) |
lrdawg99 | 0:1473318f27b6 | 118 | { |
lrdawg99 | 0:1473318f27b6 | 119 | status_bit =1; |
lrdawg99 | 0:1473318f27b6 | 120 | } |
lrdawg99 | 0:1473318f27b6 | 121 | else |
lrdawg99 | 0:1473318f27b6 | 122 | { |
lrdawg99 | 0:1473318f27b6 | 123 | status_bit = 0; |
lrdawg99 | 0:1473318f27b6 | 124 | } |
lrdawg99 | 0:1473318f27b6 | 125 | } |
lrdawg99 | 1:4e4194db7cd6 | 126 | |
lrdawg99 | 0:1473318f27b6 | 127 | if ((ack) || (((reg_data>>status_bit)&0x1)==1)) |
lrdawg99 | 0:1473318f27b6 | 128 | { |
lrdawg99 | 0:1473318f27b6 | 129 | break; |
lrdawg99 | 0:1473318f27b6 | 130 | } |
lrdawg99 | 0:1473318f27b6 | 131 | |
lrdawg99 | 0:1473318f27b6 | 132 | wait_ms(1); |
lrdawg99 | 0:1473318f27b6 | 133 | } |
lrdawg99 | 0:1473318f27b6 | 134 | |
lrdawg99 | 0:1473318f27b6 | 135 | 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 |
lrdawg99 | 0:1473318f27b6 | 136 | if (*data_valid !=1) |
lrdawg99 | 0:1473318f27b6 | 137 | { |
lrdawg99 | 1:4e4194db7cd6 | 138 | pc2.printf("Data not valid: 0x%X\n", *data_valid); |
lrdawg99 | 0:1473318f27b6 | 139 | return (1); |
lrdawg99 | 0:1473318f27b6 | 140 | } |
lrdawg99 | 1:4e4194db7cd6 | 141 | |
lrdawg99 | 0:1473318f27b6 | 142 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 143 | } |
lrdawg99 | 0:1473318f27b6 | 144 | |
lrdawg99 | 0:1473318f27b6 | 145 | // Reads new data (even after a mode change) by flushing old data and waiting for the data_valid bit to be set. |
lrdawg99 | 0:1473318f27b6 | 146 | // This function simplifies adc reads when modes are changing. For example, if V1-V2 changes from temperature mode |
lrdawg99 | 0:1473318f27b6 | 147 | // to differential voltage mode, the data in the register may still correspond to the temperature reading immediately |
lrdawg99 | 0:1473318f27b6 | 148 | // after the mode change. Flushing one reading and waiting for a new reading guarantees fresh data is received. |
lrdawg99 | 0:1473318f27b6 | 149 | // If the timeout is reached without valid data (*data_valid=1) the function exits. |
lrdawg99 | 0:1473318f27b6 | 150 | 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) |
lrdawg99 | 0:1473318f27b6 | 151 | { |
lrdawg99 | 0:1473318f27b6 | 152 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 153 | |
lrdawg99 | 0:1473318f27b6 | 154 | 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 |
lrdawg99 | 0:1473318f27b6 | 155 | ack |= LTC2991_adc_read_timeout(i2c_address, msb_register_address, &(*adc_code), &(*data_valid), timeout, ((msb_register_address/2) - 0x05)); //! 2) Read new data |
lrdawg99 | 0:1473318f27b6 | 156 | |
lrdawg99 | 0:1473318f27b6 | 157 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 158 | } |
lrdawg99 | 0:1473318f27b6 | 159 | |
lrdawg99 | 0:1473318f27b6 | 160 | // Reads an 8-bit register from the LTC2991 using the standard repeated start format. |
lrdawg99 | 0:1473318f27b6 | 161 | int8_t LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data) |
lrdawg99 | 0:1473318f27b6 | 162 | { |
lrdawg99 | 0:1473318f27b6 | 163 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 164 | ack = i2c_read_byte_data(i2c_address, register_address, register_data); |
lrdawg99 | 0:1473318f27b6 | 165 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 166 | } |
lrdawg99 | 0:1473318f27b6 | 167 | |
lrdawg99 | 0:1473318f27b6 | 168 | // Write one byte to an LTC2991 register. |
lrdawg99 | 0:1473318f27b6 | 169 | // Writes to an 8-bit register inside the LTC2991 using the standard I2C repeated start format. |
lrdawg99 | 0:1473318f27b6 | 170 | int8_t LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data) |
lrdawg99 | 0:1473318f27b6 | 171 | { |
lrdawg99 | 0:1473318f27b6 | 172 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 173 | ack = i2c_write_byte_data(i2c_address, register_address, register_data); |
lrdawg99 | 0:1473318f27b6 | 174 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 175 | } |
lrdawg99 | 0:1473318f27b6 | 176 | |
lrdawg99 | 0:1473318f27b6 | 177 | // Used to set and clear bits in a control register. bits_to_set will be bitwise OR'd with the register. |
lrdawg99 | 0:1473318f27b6 | 178 | // 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. |
lrdawg99 | 0:1473318f27b6 | 179 | 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) |
lrdawg99 | 0:1473318f27b6 | 180 | { |
lrdawg99 | 0:1473318f27b6 | 181 | uint8_t register_data; |
lrdawg99 | 0:1473318f27b6 | 182 | int8_t ack = 0; |
lrdawg99 | 0:1473318f27b6 | 183 | |
lrdawg99 | 0:1473318f27b6 | 184 | ack |= LTC2991_register_read(i2c_address, register_address, ®ister_data); //! 1) Read register |
lrdawg99 | 0:1473318f27b6 | 185 | register_data = register_data & (~bits_to_clear); //! 2) Clear bits that were set to be cleared |
lrdawg99 | 0:1473318f27b6 | 186 | register_data = register_data | bits_to_set; |
lrdawg99 | 0:1473318f27b6 | 187 | ack |= LTC2991_register_write(i2c_address, register_address, register_data); //! 3) Write to register with the cleared bits |
lrdawg99 | 0:1473318f27b6 | 188 | return(ack); |
lrdawg99 | 0:1473318f27b6 | 189 | } |
lrdawg99 | 0:1473318f27b6 | 190 | |
lrdawg99 | 0:1473318f27b6 | 191 | // Calculates the LTC2991 single-ended input voltages |
lrdawg99 | 0:1473318f27b6 | 192 | float LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb) |
lrdawg99 | 0:1473318f27b6 | 193 | { |
lrdawg99 | 0:1473318f27b6 | 194 | float voltage; |
lrdawg99 | 0:1473318f27b6 | 195 | int16_t sign = 1; |
lrdawg99 | 0:1473318f27b6 | 196 | if (adc_code >> 14) |
lrdawg99 | 0:1473318f27b6 | 197 | { |
lrdawg99 | 0:1473318f27b6 | 198 | adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary |
lrdawg99 | 0:1473318f27b6 | 199 | sign = -1; |
lrdawg99 | 0:1473318f27b6 | 200 | } |
lrdawg99 | 0:1473318f27b6 | 201 | adc_code = (adc_code & 0x3FFF); |
lrdawg99 | 0:1473318f27b6 | 202 | voltage = ((float) adc_code) * LTC2991_single_ended_lsb * sign; //! 2) Convert code to voltage from lsb |
lrdawg99 | 0:1473318f27b6 | 203 | return (voltage); |
lrdawg99 | 0:1473318f27b6 | 204 | } |
lrdawg99 | 0:1473318f27b6 | 205 | |
lrdawg99 | 0:1473318f27b6 | 206 | // Calculates the LTC2991 Vcc voltage |
lrdawg99 | 0:1473318f27b6 | 207 | float LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb) |
lrdawg99 | 0:1473318f27b6 | 208 | { |
lrdawg99 | 0:1473318f27b6 | 209 | float voltage; |
lrdawg99 | 0:1473318f27b6 | 210 | int16_t sign = 1; |
lrdawg99 | 0:1473318f27b6 | 211 | if (adc_code >> 14) |
lrdawg99 | 0:1473318f27b6 | 212 | { |
lrdawg99 | 0:1473318f27b6 | 213 | adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary |
lrdawg99 | 0:1473318f27b6 | 214 | sign = -1; |
lrdawg99 | 0:1473318f27b6 | 215 | } |
lrdawg99 | 0:1473318f27b6 | 216 | |
lrdawg99 | 0:1473318f27b6 | 217 | voltage = (((float) adc_code) * LTC2991_single_ended_lsb * sign) + 2.5; //! 2) Convert code to Vcc Voltage from single-ended lsb |
lrdawg99 | 0:1473318f27b6 | 218 | return (voltage); |
lrdawg99 | 0:1473318f27b6 | 219 | } |
lrdawg99 | 0:1473318f27b6 | 220 | |
lrdawg99 | 0:1473318f27b6 | 221 | // Calculates the LTC2991 differential input voltage. |
lrdawg99 | 0:1473318f27b6 | 222 | float LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb) |
lrdawg99 | 0:1473318f27b6 | 223 | { |
lrdawg99 | 0:1473318f27b6 | 224 | float voltage; |
lrdawg99 | 0:1473318f27b6 | 225 | int16_t sign = 1; |
lrdawg99 | 0:1473318f27b6 | 226 | if (adc_code >> 14) |
lrdawg99 | 0:1473318f27b6 | 227 | { |
lrdawg99 | 0:1473318f27b6 | 228 | adc_code = (adc_code ^ 0x7FFF) + 1; //! 1)Converts two's complement to binary |
lrdawg99 | 0:1473318f27b6 | 229 | sign = -1; |
lrdawg99 | 0:1473318f27b6 | 230 | } |
lrdawg99 | 0:1473318f27b6 | 231 | voltage = ((float) adc_code) * LTC2991_differential_lsb * sign; //! 2) Convert code to voltage form differential lsb |
lrdawg99 | 0:1473318f27b6 | 232 | return (voltage); |
lrdawg99 | 0:1473318f27b6 | 233 | } |
lrdawg99 | 0:1473318f27b6 | 234 | |
lrdawg99 | 0:1473318f27b6 | 235 | // Calculates the LTC2991 temperature |
lrdawg99 | 0:1473318f27b6 | 236 | float LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit) |
lrdawg99 | 0:1473318f27b6 | 237 | { |
lrdawg99 | 0:1473318f27b6 | 238 | float temperature; |
lrdawg99 | 0:1473318f27b6 | 239 | adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits |
lrdawg99 | 0:1473318f27b6 | 240 | if (!unit) //! 2)Checks to see if it's Kelvin |
lrdawg99 | 0:1473318f27b6 | 241 | { |
lrdawg99 | 0:1473318f27b6 | 242 | if (adc_code >>12) |
lrdawg99 | 0:1473318f27b6 | 243 | { |
lrdawg99 | 0:1473318f27b6 | 244 | adc_code = (adc_code | 0xE000); //! Sign extend if it's not Kelvin (Celsius) |
lrdawg99 | 0:1473318f27b6 | 245 | } |
lrdawg99 | 0:1473318f27b6 | 246 | } |
lrdawg99 | 0:1473318f27b6 | 247 | temperature = ((float) adc_code) * LTC2991_temperature_lsb; //! 3) Converts code to temperature from temperature lsb |
lrdawg99 | 0:1473318f27b6 | 248 | |
lrdawg99 | 0:1473318f27b6 | 249 | return (temperature); |
lrdawg99 | 0:1473318f27b6 | 250 | } |
lrdawg99 | 0:1473318f27b6 | 251 | |
lrdawg99 | 0:1473318f27b6 | 252 | // Calculates the LTC2991 diode voltage |
lrdawg99 | 0:1473318f27b6 | 253 | float LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb) |
lrdawg99 | 0:1473318f27b6 | 254 | { |
lrdawg99 | 0:1473318f27b6 | 255 | float voltage; |
lrdawg99 | 0:1473318f27b6 | 256 | adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits |
lrdawg99 | 0:1473318f27b6 | 257 | voltage = ((float) adc_code) * LTC2991_diode_voltage_lsb; //! 2) Convert code to voltage from diode voltage lsb |
lrdawg99 | 0:1473318f27b6 | 258 | return (voltage); |
lrdawg99 | 0:1473318f27b6 | 259 | } |