Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC1859.cpp
Go to the documentation of this file.
1 /*!
2 LTC1859: 16-Bit 8-Channel 200ksps ADC
3 
4 @verbatim
5 
6 The LTC1863/LTC1859 are pin-compatible, 8-channel 12-/16-bit A/D converters with
7 serial I/O, and an internal reference. The ADCs typically draw only 1.3mA from a
8 single 5V supply. The 8-channel input multiplexer can be configured for either
9 single-ended or differential inputs and unipolar or bipolar conversions (or
10 combinations thereof). The automatic nap and sleep modes benefit power sensitive
11 applications.
12 
13 The LTC1859's DC performance is outstanding with a +/-2LSB INL specification and
14 no missing codes over temperature. The signal-to-noise ratio (SNR) for the
15 LTC1859 is typically 89dB, with the internal reference.
16 
17 @endverbatim
18 
19 http://www.linear.com/product/LTC1859
20 
21 http://www.linear.com/product/LTC1859#demoboards
22 
23 
24 Copyright 2018(c) Analog Devices, Inc.
25 
26 All rights reserved.
27 
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30  - Redistributions of source code must retain the above copyright
31  notice, this list of conditions and the following disclaimer.
32  - Redistributions in binary form must reproduce the above copyright
33  notice, this list of conditions and the following disclaimer in
34  the documentation and/or other materials provided with the
35  distribution.
36  - Neither the name of Analog Devices, Inc. nor the names of its
37  contributors may be used to endorse or promote products derived
38  from this software without specific prior written permission.
39  - The use of this software may or may not infringe the patent rights
40  of one or more patent holders. This license does not release you
41  from the requirement that you obtain separate licenses from these
42  patent holders to use this software.
43  - Use of the software either in source or binary form, must be run
44  on or directly connected to an Analog Devices Inc. component.
45 
46 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
47 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
48 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
50 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
52 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57 
58 //! @ingroup Analog_to_Digital_Converters
59 //! @{
60 //! @defgroup LTC1859 LTC1859: 16-Bit 8-Channel 200ksps ADC
61 //! @}
62 
63 /*! @file
64  @ingroup LTC1859
65  Library for LTC1859: 16-Bit 8-Channel 200ksps ADC
66 */
67 
68 #include <Arduino.h>
69 #include <stdint.h>
70 #include "Linduino.h"
71 #include "LT_SPI.h"
72 #include "LTC1859.h"
73 #include <SPI.h>
74 
75 
76 // Builds the ADC command
77 uint8_t LTC1859_build_command(uint8_t ch_designate, uint8_t uni_bipolar, uint8_t range_low_high)
78 {
79  uint8_t adc_command;
80  adc_command = ch_designate | uni_bipolar | range_low_high;
81  return (adc_command);
82 }
83 
84 
85 // Reads the ADC and returns 16-bit data
86 void LTC1859_read(uint8_t cs, uint8_t adc_command, uint16_t *adc_code)
87 {
88  spi_transfer_word(cs, (uint16_t)(adc_command<<8), adc_code);
89 }
90 
91 
92 // Calculates the LTC1859 input voltage given the data, vref, bits, and unipolar/bipolar status.
93 float LTC1859_code_to_voltage(uint16_t adc_code, float vref, uint8_t range_low_high, uint8_t uni_bipolar)
94 {
95  float voltage;
96  float sign = 1;
97 
98  if (range_low_high == LTC1859_LOW_RANGE_MODE)
99  vref = vref/2;
100 
101  if (uni_bipolar == LTC1859_UNIPOLAR_MODE)
102  {
103  voltage = (float)adc_code;
104  voltage = voltage / (pow(2,16)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
105  }
106  else
107  {
108  if ((adc_code & 0x8000) == 0x8000) //adc code is < 0
109  {
110  adc_code = (adc_code ^ 0xFFFF)+1; //! Convert ADC code from two's complement to binary
111  sign = -1;
112  }
113  voltage = sign*(float)adc_code;
114  voltage = voltage / (pow(2,15)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
115  }
116 
117  voltage = voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
118 
119  return(voltage);
120 }
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.
static uint8_t range_low_high
Default set for high range mode.
Definition: DC682A.ino:123
void spi_transfer_word(uint8_t cs_pin, uint16_t tx, uint16_t *rx)
Reads and sends a word.
Definition: LT_SPI.cpp:98
void LTC1859_read(uint8_t cs, uint8_t adc_command, uint16_t *adc_code)
Reads the ADC and returns 16-bit data.
Definition: LTC1859.cpp:86
LTC1859: 16-bit 8-channel 100ksps ADC.
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
#define LTC1859_UNIPOLAR_MODE
Definition: LTC1859.h:133
uint8_t LTC1859_build_command(uint8_t ch_designate, uint8_t uni_bipolar, uint8_t range_low_high)
Builds the ADC command and returns an 8 bit command.
Definition: LTC1859.cpp:77
float LTC1859_code_to_voltage(uint16_t adc_code, float vref, uint8_t range_low_high, uint8_t uni_bipolar)
Calculates the LTC1859 input&#39;s unipolar voltage given the binary data and lsb weight.
Definition: LTC1859.cpp:93
static float voltage
Definition: DC2289AA.ino:71
#define LTC1859_LOW_RANGE_MODE
Definition: LTC1859.h:139
static uint32_t adc_code
Definition: DC2071AA.ino:113