Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2418.cpp
Go to the documentation of this file.
1 /*!
2 LTC2418: LTC2418 - 16-Channel 24-Bit No Latency Delta-Sigma ADC
3 
4 @verbatim
5 
6 The LTC2414/LTC2418 are 8-/16-channel (4-/8-differential) micropower 24-bit
7 delta-sigma analog-to-digital converters. They operate from 2.7V to 5.5V and
8 include an integrated oscillator, 2ppm INL and 0.2ppm RMS noise. They use
9 delta-sigma technology and provide single cycle settling time for multiplexed
10 applications. Through a single pin, the LTC2414/LTC2418 can be configured for
11 better than 110dB differential mode rejection at 50Hz or 60Hz +/-2%, or they can
12 be driven by an external oscillator for a user-defined rejection frequency. The
13 internal oscillator requires no external frequency setting components.
14 
15 @endverbatim
16 
17 http://www.linear.com/product/LTC2418
18 
19 http://www.linear.com/product/LTC2418#demoboards
20 
21 
22 Copyright 2018(c) Analog Devices, Inc.
23 
24 All rights reserved.
25 
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28  - Redistributions of source code must retain the above copyright
29  notice, this list of conditions and the following disclaimer.
30  - Redistributions in binary form must reproduce the above copyright
31  notice, this list of conditions and the following disclaimer in
32  the documentation and/or other materials provided with the
33  distribution.
34  - Neither the name of Analog Devices, Inc. nor the names of its
35  contributors may be used to endorse or promote products derived
36  from this software without specific prior written permission.
37  - The use of this software may or may not infringe the patent rights
38  of one or more patent holders. This license does not release you
39  from the requirement that you obtain separate licenses from these
40  patent holders to use this software.
41  - Use of the software either in source or binary form, must be run
42  on or directly connected to an Analog Devices Inc. component.
43 
44 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
45 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
46 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
48 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
50 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55 
56 //! @ingroup Analog_to_Digital_Converters
57 //! @{
58 //! @defgroup LTC2418 LTC2418: 16-Channel 24-Bit No Latency Delta-Sigma ADC
59 //! @}
60 
61 /*! @file
62  @ingroup LTC2418
63  Library for LTC2418: 16-Channel 24-Bit No Latency Delta-Sigma ADC
64 */
65 
66 #include <stdint.h>
67 #include <Arduino.h>
68 #include "Linduino.h"
69 #include "LT_SPI.h"
70 #include "LTC2418.h"
71 #include <SPI.h>
72 
73 int8_t LTC2418_EOC_timeout(uint8_t cs, uint16_t miso_timeout)
74 // Checks for EOC with a specified timeout
75 {
76  uint16_t timer_count = 0; // Timer count for MISO
77  output_low(cs); //! 1) Pull CS low
78  while (1) //! 2) Wait for SDO (MISO) to go low
79  {
80  if (input(MISO) == 0) break; //! 3) If SDO is low, break loop
81  if (timer_count++>miso_timeout) // If timeout, return 1 (failure)
82  {
83  output_high(cs); // Pull CS high
84  return(1);
85  }
86  else
87  delay(1);
88  }
89  return(0);
90 }
91 
92 void LTC2418_read(uint8_t cs, uint8_t adc_command, uint32_t *adc_code)
93 // Reads the LTC2418
94 {
95  LT_union_int32_4bytes data, command;
96  command.LT_byte[3] = adc_command;
97  command.LT_byte[2] = 0;
98  command.LT_byte[1] = 0;
99  command.LT_byte[0] = 0;
100 
101  output_low(cs); //! 1) Pull CS low
102  spi_transfer_block(cs, command.LT_byte, data.LT_byte, (uint8_t)4); //! 2) Transfer 4 bytes
103  output_high(cs); //! 3) Pull CS high
104  *adc_code = data.LT_int32;
105 }
106 
108 // Calculates the LTC2418 input bipolar voltage
109 {
110  float adc_voltage;
111  adc_code = adc_code>>6; //! 1) Bit-shift ADC code to the right 6 bits
112  adc_code -= 8388608; //! 2) Convert ADC code from offset binary to binary
113  adc_voltage=((float)adc_code+LTC2418_offset_code)*(LTC2418_lsb); //! 3) Calculate voltage from ADC code, lsb, offset.
114  return(adc_voltage);
115 }
116 
117 void LTC2418_cal_voltage(int32_t zero_code, int32_t fs_code, float zero_voltage, float fs_voltage, float *LTC2418_lsb , int32_t *LTC2418_offset_code)
118 // Calibrate the lsb
119 {
120  zero_code = zero_code >> 6; //! 1) Bit-shift zero code to the right 6 bits
121  zero_code -= 8388608; //! 2) Convert zero code from offset binary to binary
122  fs_code = fs_code >> 6; //! 3) Bit-shift full scale code to the right 6 bits
123  fs_code -= 8388608; //! 4) Convert full scale code from offset binary to binary
124 
125  float temp_offset;
126  *LTC2418_lsb = (fs_voltage-zero_voltage)/((float)(fs_code - zero_code)); //! 5) Calculate the LSB
127 
128  temp_offset = (zero_voltage/ *LTC2418_lsb) - zero_code; //! 6) Calculate Unipolar offset
129  temp_offset = (temp_offset > (floor(temp_offset) + 0.5)) ? ceil(temp_offset) : floor(temp_offset); //! 7) Round
130  *LTC2418_offset_code = (int32_t)temp_offset; //! 8) Cast as int32_t
131 }
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
#define output_high(pin)
Set "pin" high.
Definition: Linduino.h:75
static int32_t LTC2418_offset_code
Ideal offset for a perfect part.
Definition: DC571A.ino:114
void LTC2418_cal_voltage(int32_t zero_code, int32_t fs_code, float zero_voltage, float fs_voltage, float *LTC2418_lsb, int32_t *LTC2418_offset_code)
Calibrate the lsb.
Definition: LTC2418.cpp:117
Header File for Linduino Libraries and Demo Code.
static float adc_voltage
Definition: DC2071AA.ino:115
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
static float LTC2418_lsb
Ideal LSB voltage for a perfect part.
Definition: DC571A.ino:113
LTC2418: 16-Channel 24-Bit No Latency Delta-Sigma ADC.
#define output_low(pin)
Set "pin" low.
Definition: Linduino.h:72
#define input(pin)
Return the state of pin "pin".
Definition: Linduino.h:79
int32_t LT_int32
32-bit signed integer to be converted to four bytes
Definition: Linduino.h:110
float LTC2418_code_to_voltage(int32_t adc_code, float LTC2418_lsb, int32_t LTC2418_offset_code)
Calculates the LTC2418 input bipolar voltage.
Definition: LTC2418.cpp:107
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
int8_t LTC2418_EOC_timeout(uint8_t cs, uint16_t miso_timeout)
Checks for EOC with a specified timeout.
Definition: LTC2418.cpp:73
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 uint32_t adc_code
Definition: DC2071AA.ino:113
void LTC2418_read(uint8_t cs, uint8_t adc_command, uint32_t *adc_code)
Reads the LTC2418 result and programs the configuration for the next conversion.
Definition: LTC2418.cpp:92