| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC2305.cpp
Go to the documentation of this file.
1 /*!
2 LTC2305: 2-channel, 12-Bit SAR ADC with I2C interface
3
4 @verbatim
5
6 The LTC2305 is a low noise, low power, 8-channel, 12-bit successive
7 approximation ADC with an I2C compatible serial interface. This ADC includes an
8 internal reference and a fully differential sample-and-hold circuit to reduce
9 common mode noise. The LTC2305 operates from an internal clock to achieve a fast
10 1.3 microsecond conversion time.
11
12 The LTC2305 operates from a single 5V supply and draws just 300 microamps at a
13 throughput rate of 1ksps. The ADC enters nap mode when not converting, reducing
14 the power dissipation.
15
16 @endverbatim
17
18 http://www.linear.com/product/LTC2305
19
20 http://www.linear.com/product/LTC2305#demoboards
21
22
23 Copyright 2018(c) Analog Devices, Inc.
24
25 All rights reserved.
26
27 Redistribution and use in source and binary forms, with or without
28 modification, are permitted provided that the following conditions are met:
29 - Redistributions of source code must retain the above copyright
30 notice, this list of conditions and the following disclaimer.
31 - Redistributions in binary form must reproduce the above copyright
32 notice, this list of conditions and the following disclaimer in
33 the documentation and/or other materials provided with the
34 distribution.
35 - Neither the name of Analog Devices, Inc. nor the names of its
36 contributors may be used to endorse or promote products derived
37 from this software without specific prior written permission.
38 - The use of this software may or may not infringe the patent rights
39 of one or more patent holders. This license does not release you
40 from the requirement that you obtain separate licenses from these
41 patent holders to use this software.
42 - Use of the software either in source or binary form, must be run
43 on or directly connected to an Analog Devices Inc. component.
44
45 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
46 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
47 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
49 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
51 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 //! @ingroup Analog_to_Digital_Converters
58 //! @{
59 //! @defgroup LTC2305 LTC2305: 2-channel, 12-Bit SAR ADC with I2C interface
60 //! @}
61
62 /*! @file
63 @ingroup LTC2305
64 Library for LTC2305: 2-channel, 12-Bit SAR ADC with I2C interface
65 */
66
67 #include <Arduino.h>
68 #include <stdint.h>
69 #include "Linduino.h"
70 #include "LT_I2C.h"
71 #include "LTC2305.h"
72 #include <Wire.h>
73
74 // Commands
75 // Construct a channel / uni/bipolar by bitwise ORing one choice from the channel configuration
76 // and one choice from the command.
77
78 // Example - read channel 3 single-ended
79 // adc_command = LTC2305_CH3 | LTC2305_UNIPOLAR_MODE;
80
81 // Example - read voltage between channels 5 and 4 with 4 as positive polarity and in bipolar mode.
82 // adc_command = LTC2305_P4_N5 | LTC2305_BIPOLAR_MODE;
83
84
85 // Reads 12 bits in binary format
86 int8_t LTC2305_read(uint8_t i2c_address, uint8_t adc_command, uint16_t *ptr_adc_code)
87 {
88 int8_t ack = 0;
89 LT_union_int16_2bytes data;
90
91 data.LT_byte[1] = 0;
92 data.LT_byte[0] = 0;
93
94 ack = i2c_read_word_data(i2c_address, adc_command, &data.LT_uint16);
95
96 *ptr_adc_code = data.LT_uint16; //note the 12 bits of data are left justified
97
98 return(ack);
99 }
100
101
102 // Calculates the LTC2305 input unipolar voltage.
103 float LTC2305_code_to_voltage(uint16_t adc_code, float vref, uint8_t uni_bipolar)
104 {
105 float voltage;
106 float sign = 1;
107
108 if (uni_bipolar == LTC2305_UNIPOLAR_MODE)
109 {
110 voltage = (float)adc_code;
111 voltage = voltage / (pow(2,16)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
112 }
113 else
114 {
115 vref = vref/2;
116 if ((adc_code & 0x8000) == 0x8000) //adc code is < 0
117 {
118 adc_code = (adc_code ^ 0xFFFF)+1; //! Convert ADC code from two's complement to binary
119 sign = -1;
120 }
121 voltage = sign*(float)adc_code;
122 voltage = voltage / (pow(2,15)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
123 }
124 voltage = voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
125
126
127 return(voltage);
128 }
i2c_address
uint8_t i2c_address
adc_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
sign
bool sign
Definition: Precision_Voltage_Source.ino:77
uni_bipolar
static uint8_t uni_bipolar
Default set for unipolar mode.
Definition: DC682A.ino:121
Linduino.h
Header File for Linduino Libraries and Demo Code.
LTC2305_read
int8_t LTC2305_read(uint8_t i2c_address, uint8_t adc_command, uint16_t *ptr_adc_code)
Reads 12-bit code from LTC2305, programs channel and mode for next conversion.
Definition: LTC2305.cpp:86
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LTC2305_code_to_voltage
float LTC2305_code_to_voltage(uint16_t adc_code, float vref, uint8_t uni_bipolar)
Calculates the LTC2305 input voltage.
Definition: LTC2305.cpp:103
LT_union_int16_2bytes::LT_uint16
uint16_t LT_uint16
16-bit unsigned integer to be converted to two bytes
Definition: Linduino.h:102
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.
LTC2305_UNIPOLAR_MODE
#define LTC2305_UNIPOLAR_MODE
Definition: LTC2305.h:130
LT_union_int16_2bytes
This union splits one int16_t (16-bit signed integer) or uint16_t (16-bit unsigned integer) into two ...
Definition: Linduino.h:99
voltage
static float voltage
Definition: DC2289AA.ino:71
vref
float vref
Definition: LTC2492_Thermocouple_Meter.ino:129
LT_union_int16_2bytes::LT_byte
uint8_t LT_byte[2]
2 bytes (unsigned 8-bit integers) to be converted to a 16-bit signed or unsigned integer ...
Definition: Linduino.h:103
adc_code
static uint32_t adc_code
Definition: DC2071AA.ino:113
LTC2305.h
LTC2305: 8-channel, 12-Bit SAR ADC with I2C interface.