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