Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2380_24.cpp
Go to the documentation of this file.
1 /*!
2 LTC2380-24: Low Noise, High Speed, 24-Bit SAR ADC With Digital Filter
3 
4 @verbatim
5 
6 
7 @endverbatim
8 
9 http://www.linear.com/product/LTC2380-24
10 
11 
12 Copyright 2018(c) Analog Devices, Inc.
13 
14 All rights reserved.
15 
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18  - Redistributions of source code must retain the above copyright
19  notice, this list of conditions and the following disclaimer.
20  - Redistributions in binary form must reproduce the above copyright
21  notice, this list of conditions and the following disclaimer in
22  the documentation and/or other materials provided with the
23  distribution.
24  - Neither the name of Analog Devices, Inc. nor the names of its
25  contributors may be used to endorse or promote products derived
26  from this software without specific prior written permission.
27  - The use of this software may or may not infringe the patent rights
28  of one or more patent holders. This license does not release you
29  from the requirement that you obtain separate licenses from these
30  patent holders to use this software.
31  - Use of the software either in source or binary form, must be run
32  on or directly connected to an Analog Devices Inc. component.
33 
34 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
35 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
36 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
38 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
40 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 
46 //! @ingroup Analog_to_Digital_Converters
47 //! @{
48 //! @defgroup LTC2380-24 LTC2380-24: Low Noise, High Speed, 24-Bit SAR ADC With Digital Filter
49 //! @}
50 
51 /*! @file
52  @ingroup LTC2380-24
53  Library for LTC2380-24: Low Noise, High Speed, 24-Bit SAR ADC With Digital Filter
54 */
55 
56 #include <Arduino.h>
57 #include <stdint.h>
58 #include "Linduino.h"
59 #include "LT_SPI.h"
60 #include "LTC2380_24.h"
61 #include <SPI.h>
62 #include "UserInterface.h"
63 
64 // Reads from a SPI LTC2380-XX device that has no configuration word and a 32 bit output word in 2's complement format.
65 void LTC2380_read(int32_t *ptr_adc_code, int16_t *ptr_cycles)
66 {
67  int N;
68  int negative;
69 
70  Serial.print("\nEnter the number of CNV pulses: ");
71  while (Serial.available());
72  N = read_int();
73  while (N--) // PB2 (CS) pin is connected to CNV pin.
74  {
75  PORTB |= B00000100; // Pull CNV pin high
76  __asm__("nop\n\t");
77  PORTB &= B11111011; // Pull CNV pin low
78  __asm__("nop\n\t");
79  }
80 
81  LT_union_int16_2bytes cycles; // LTC2380 data and command
82  cycles.LT_uint16 = 0; // Set to zero, not necessary but avoids random data in scope shots.
83 
84  // Form a four byte object to hold four bytes of data
85  LT_union_int32_4bytes data; //instantiate the union
86  data.LT_byte[3] = 0;
87  data.LT_byte[2] = 0;
88  data.LT_byte[1] = 0;
89  data.LT_byte[0] = 0;
90 
91  data.LT_byte[2] = spi_read(0);
92  data.LT_byte[1] = spi_read(0);
93  data.LT_byte[0] = spi_read(0);
94  cycles.LT_byte[1] = spi_read(0);
95  cycles.LT_byte[0] = spi_read(0);
96 
97  negative = (data.LT_byte[2] & 0x80) != 0;
98  if (negative)
99  {
100  data.LT_byte[3] = 0xFF;
101  }
102  *ptr_adc_code = data.LT_int32;
103  *ptr_cycles = cycles.LT_int16;
104 
105  return;
106 }
107 
108 // Calculates the voltage corresponding to an adc code in 2's complement, given the reference voltage (in volts)
109 float LTC2380_code_to_voltage(int32_t adc_code, uint8_t gain_compression, float vref)
110 {
111  float voltage;
112 
113  if (gain_compression == 1)
114  vref = 0.8*vref;
115 
116  voltage = (float)adc_code;
117  voltage = voltage / (pow(2,24)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
118  voltage = voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
119 
120  return(voltage);
121 }
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
Header File for Linduino Libraries and Demo Code.
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LTC2380-24: Low Noise, High Speed, 24-Bit SAR ADC With Digital Filter.
int16_t LT_int16
16-bit signed integer to be converted to two bytes
Definition: Linduino.h:101
uint16_t LT_uint16
16-bit unsigned integer to be converted to two bytes
Definition: Linduino.h:102
int32_t LT_int32
32-bit signed integer to be converted to four bytes
Definition: Linduino.h:110
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
int32_t read_int()
int8_t spi_read(int8_t data)
The data byte to be written.
Definition: LT_SPI.cpp:189
This union splits one int32_t (32-bit signed integer) or uint32_t (32-bit unsigned integer) four uint...
Definition: Linduino.h:108
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
float LTC2380_code_to_voltage(int32_t adc_code, uint8_t gain_compression, float vref)
Calculates the LTC2380 input voltage given the binary data and lsb weight.
Definition: LTC2380_24.cpp:109
void LTC2380_read(int32_t *ptr_adc_code, int16_t *ptr_cycles)
Reads the LTC2380 and returns 32-bit data in 2&#39;s complement format.
Definition: LTC2380_24.cpp:65
static uint32_t adc_code
Definition: DC2071AA.ino:113