Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2302.cpp
Go to the documentation of this file.
1 /*!
2 LTC2302: 12-Bit, 1-Channel 500ksps SAR ADC with SPI Interface.
3 
4 @verbatim
5 
6 The LTC2302 is a single-channel 12-bit A/D converter with with serial I/O.
7 The part can be configured to operate on unipolar or bipolar modes. The
8 automatic sleep mode benefits power sensitive applications.
9 
10 SPI DATA FORMAT (MSB First):
11 
12  Byte #1 Byte #2
13 Data In : X OS X X UNI X X X X X X X X X X X
14 Data Out : D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
15 
16 OS : ODD/Sign Bit
17 UNI : Unipolar/Bipolar Bit
18 Dx : Data Bits
19 X : Don't care
20 
21 @endverbatim
22 
23 http://www.linear.com/product/LTC2302
24 
25 http://www.linear.com/product/LTC2302#demoboards
26 
27 
28 Copyright 2018(c) Analog Devices, Inc.
29 
30 All rights reserved.
31 
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions are met:
34  - Redistributions of source code must retain the above copyright
35  notice, this list of conditions and the following disclaimer.
36  - Redistributions in binary form must reproduce the above copyright
37  notice, this list of conditions and the following disclaimer in
38  the documentation and/or other materials provided with the
39  distribution.
40  - Neither the name of Analog Devices, Inc. nor the names of its
41  contributors may be used to endorse or promote products derived
42  from this software without specific prior written permission.
43  - The use of this software may or may not infringe the patent rights
44  of one or more patent holders. This license does not release you
45  from the requirement that you obtain separate licenses from these
46  patent holders to use this software.
47  - Use of the software either in source or binary form, must be run
48  on or directly connected to an Analog Devices Inc. component.
49 
50 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
51 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
52 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
54 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
56 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 */
61 
62 //! @ingroup Analog_to_Digital_Converters
63 //! @{
64 //! @defgroup LTC2302 LTC2302: 12-Bit, 1-Channel 500ksps SAR ADC with SPI Interface.
65 //! @}
66 
67 /*! @file
68  @ingroup LTC2302
69  Library for LTC2302: 12-Bit, 1-Channel 500ksps SAR ADC with SPI Interface.
70 */
71 
72 #include "LT_SPI.h"
73 #include <SPI.h>
74 #include "LTC2302.h"
75 
76 // Reads the ADC and returns 16-bit data
77 void LTC2302_read(uint8_t cs, uint16_t adc_command, uint16_t *adc_code)
78 {
79  spi_transfer_word(cs, (uint16_t)(adc_command), adc_code);
80  spi_transfer_word(cs, (uint16_t)(adc_command), adc_code); // Throws out last reading and starts a new connection
81 }
82 
83 // Calculates the LTC2302 input voltage given the data, range, and unipolar/bipolar status.
84 float LTC2302_code_to_voltage(uint16_t adc_code, float vref, uint8_t uni_bipolar)
85 {
86  float voltage;
87  float sign = 1;
88 
89  if (uni_bipolar == LTC2302_UNIPOLAR)
90  {
91  voltage = (float)adc_code;
92  voltage = vref * voltage / (4095); //! This calculates the actual voltage at the input (in volts)
93  }
94  else
95  {
96  vref = vref/2;
97  if ((adc_code & 0x8000) == 0x8000) //adc code is < 0
98  {
99  adc_code = (adc_code ^ 0xFFFF)+1; //! Convert ADC code from two's complement to binary
100  sign = -1;
101  }
102  voltage = sign*(float)adc_code;
103  voltage = vref * voltage / 2047; //! This calculates the actual voltage at the input (in volts)
104  }
105 
106  return(voltage);
107 }
void LTC2302_read(uint8_t cs, uint16_t adc_command, uint16_t *adc_code)
Reads the ADC and returns 16-bit data.
Definition: LTC2302.cpp:77
static uint8_t adc_command
Definition: DC2071AA.ino:111
static uint8_t uni_bipolar
Default set for unipolar mode.
Definition: DC682A.ino:121
LTC2302: 12-Bit, 1-Channel 500ksps SAR ADC with SPI Interface.
float LTC2302_code_to_voltage(uint16_t adc_code, float vref, uint8_t uni_bipolar)
Calculates the LTC2302 input voltage given the data, range, and unipolar/bipolar status.
Definition: LTC2302.cpp:84
void spi_transfer_word(uint8_t cs_pin, uint16_t tx, uint16_t *rx)
Reads and sends a word.
Definition: LT_SPI.cpp:98
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
static float voltage
Definition: DC2289AA.ino:71
#define LTC2302_UNIPOLAR
Definition: LTC2302.h:78
static uint32_t adc_code
Definition: DC2071AA.ino:113