Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2373.cpp
Go to the documentation of this file.
1 /*!
2 LTC2373: 16/18-bit 1Msps 8 channel SAR ADC
3 LTC2372: 16/18-bit 500ksps 8 channel SAR ADC
4 LTC2374: 16 bit 1.6Msps 8 channel SAR ADC
5 
6 @verbatim
7 
8 The LTC2373_16/18 are pin-compatible, 16/18-bit A/D converters with
9 serial I/O, and an internal reference.
10 
11 @endverbatim
12 
13 http://www.linear.com/product/LTC2373-16
14 http://www.linear.com/product/LTC2373_18
15 http://www.linear.com/product/LTC2372-16
16 http://www.linear.com/product/LTC2372-18
17 http://www.linear.com/product/LTC2374-16
18 
19 http://www.linear.com/product/LTC2373-16#demoboards
20 http://www.linear.com/product/LTC2373_18#demoboards
21 http://www.linear.com/product/LTC2372-16#demoboards
22 http://www.linear.com/product/LTC2372-18#demoboards
23 http://www.linear.com/product/LTC2374-16#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 LTC2373 LTC2373: 16/18-bit 1Msps 8 channel SAR ADC
63 //! @}
64 
65 /*! @file
66  @ingroup LTC2373
67  Library for LTC2373: 16/18-bit 1Msps 8 channel SAR ADC
68 */
69 
70 #include <Arduino.h>
71 #include <stdint.h>
72 #include "Linduino.h"
73 #include "LT_I2C.h"
74 #include "LT_SPI.h"
75 #include "LTC2373.h"
76 #include <SPI.h>
77 
78 // Builds the ADC command
79 uint8_t LTC2373_build_command(uint8_t sequencer_bit, uint8_t ch_designate, uint8_t range_select, uint8_t gain_compression)
80 {
81  uint8_t adc_command;
82  adc_command = sequencer_bit | ch_designate | range_select | gain_compression;
83 
84  return (adc_command);
85 }
86 
87 
88 // Reads from a SPI LTC2373-XX device that has a configuration word and a 20 bit output word.
89 void LTC2373_read(uint8_t cs, uint8_t adc_command, uint32_t *ptr_adc_code)
90 {
91 //Form a four byte object to hold four bytes of command
92  LT_union_int32_4bytes command; //instantiate the union
93  command.LT_byte[3] = adc_command;
94  command.LT_byte[2] = 0;
95  command.LT_byte[1] = 0;
96  command.LT_byte[0] = 0;
97 
98 //Form a four byte object to hold four bytes of data
99  LT_union_int32_4bytes data; //instantiate the union
100  data.LT_byte[3] = 0;
101  data.LT_byte[2] = 0;
102  data.LT_byte[1] = 0;
103  data.LT_byte[0] = 0;
104 
105  spi_transfer_block(cs,command.LT_byte,data.LT_byte,4);
106 
107  *ptr_adc_code = data.LT_int32;
108 
109  return;
110 }
111 
112 
113 // Programs multiple 8 bit control words to an SPI LTC2373-XX device.
114 void LTC2373_configure(uint8_t cs, uint32_t adc_configure)
115 {
116 //Form a four byte object to hold four bytes of command
117  LT_union_int32_4bytes command; //instantiate the union
118  command.LT_uint32 = adc_configure;
119 
120 //Form a four byte object to hold four bytes of dummy data
121  LT_union_int32_4bytes data; //instantiate the union
122  data.LT_byte[3] = 0;
123  data.LT_byte[2] = 0;
124  data.LT_byte[1] = 0;
125  data.LT_byte[0] = 0;
126 
127  spi_transfer_block(cs,command.LT_byte,data.LT_byte,4);
128 
129  return;
130 }
131 
132 
133 // Calculates the voltage corresponding to an adc code, given the reference voltage (in volts)
134 float LTC2373_code_to_voltage(uint8_t adc_command, uint32_t adc_code, float vref)
135 {
136  float voltage, range;
137  float sign = 1;
138  uint8_t config;
139 
140  config = (adc_command >> 1) & 0x03;
141 
142  switch (config)
143  {
144  case (0): //unipolar, 0 to vref
145  voltage = (float)adc_code;
146  voltage = voltage / (pow(2,32)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
147  voltage = voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
148  break;
149 
150  case (1): //bipolar, +/- vref/2, twos complement out
151  if ((adc_code>>31) == 1) //number is < 0
152  {
153  adc_code = (adc_code ^ 0xFFFFFFFF)+1; //! Convert ADC code from two's complement to binary
154  sign = -1;
155  }
156 
157  voltage = (float)adc_code;
158  voltage = voltage / (pow(2,31)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
159  voltage = sign * voltage * vref/2; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
160  break;
161 
162  case (2): //differential unipolar, 0 to vref
163  voltage = (float)adc_code;
164  voltage = voltage / (pow(2,32)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
165  voltage = sign * voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
166  break;
167 
168  case (3): //differential bipolar, +/- vref, twos complement out
169  if ((adc_code>>31) == 1) //number is < 0
170  {
171  adc_code = (adc_code ^ 0xFFFFFFFF)+1; //! Convert ADC code from two's complement to binary
172  sign = -1;
173  }
174 
175  voltage = (float)adc_code;
176  voltage = voltage / (pow(2,31)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
177  voltage = sign * voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
178  break;
179  }
180 
181  return(voltage);
182 }
uint8_t LTC2373_build_command(uint8_t sequencer_bit, uint8_t ch_designate, uint8_t range_select, uint8_t gain_compression)
Definition: LTC2373.cpp:79
static uint8_t adc_command
Definition: DC2071AA.ino:111
uint8_t LT_byte[4]
4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer ...
Definition: Linduino.h:112
void LTC2373_configure(uint8_t cs, uint32_t adc_configure)
Configures the LTC2373.
Definition: LTC2373.cpp:114
LTC2373: 16/18-bit 1Msps 8 channel SAR ADC LTC2372: 16/18-bit 500ksps 8 channel SAR ADC LTC2374: 16 b...
Header File for Linduino Libraries and Demo Code.
uint32_t LT_uint32
32-bit unsigned integer to be converted to four bytes
Definition: Linduino.h:111
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
static uint8_t range
Definition: DC1096B.ino:111
int32_t LT_int32
32-bit signed integer to be converted to four bytes
Definition: Linduino.h:110
void LTC2373_read(uint8_t cs, uint8_t adc_command, uint32_t *ptr_adc_code)
Reads the LTC2373 and returns 32-bit data.
Definition: LTC2373.cpp:89
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
This union splits one int32_t (32-bit signed integer) or uint32_t (32-bit unsigned integer) four uint...
Definition: Linduino.h:108
void spi_transfer_block(uint8_t cs_pin, uint8_t *tx, uint8_t *rx, uint8_t length)
Reads and sends a byte array.
Definition: LT_SPI.cpp:125
static float voltage
Definition: DC2289AA.ino:71
float LTC2373_code_to_voltage(uint8_t adc_command, uint32_t adc_code, float vref)
Calculates the LTC2373 input voltage given the binary data and lsb weight.
Definition: LTC2373.cpp:134
static uint32_t adc_code
Definition: DC2071AA.ino:113