Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2442_Datalogger.ino
Go to the documentation of this file.
1 /*!
2 LTC2442: 24-Bit High Speed 4-Channel Delta Sigma ADCs with Integrated Amplifier
3 
4 @verbatim
5 
6 The LTC2442 is a 4-channel (2-differential) high speed 24-bit No Latency Delta Sigma ADCs.
7 They use a proprietary delta-sigma architecture enabling variable speed/resolution. Through
8 a simple 4-wire serial interface, ten speed/resolution combinations 6.9Hz/280nVRMS to
9 3.5kHz/25uVRMS (4kHz with external oscillator) can be selected with no latency between
10 conversion results or shift in DC accuracy (offset, full-scale, linearity, drift).
11 Additionally, a 2X speed mode can be selected enabling output rates up to 7kHz (8kHz if an
12 external oscillator is used) with one cycle latency.
13 
14 @endverbatim
15 
16 http://www.linear.com/product/LTC2442
17 
18 http://www.linear.com/product/LTC2442#demoboards
19 
20 
21 Copyright 2018(c) Analog Devices, Inc.
22 
23 All rights reserved.
24 
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions are met:
27  - Redistributions of source code must retain the above copyright
28  notice, this list of conditions and the following disclaimer.
29  - Redistributions in binary form must reproduce the above copyright
30  notice, this list of conditions and the following disclaimer in
31  the documentation and/or other materials provided with the
32  distribution.
33  - Neither the name of Analog Devices, Inc. nor the names of its
34  contributors may be used to endorse or promote products derived
35  from this software without specific prior written permission.
36  - The use of this software may or may not infringe the patent rights
37  of one or more patent holders. This license does not release you
38  from the requirement that you obtain separate licenses from these
39  patent holders to use this software.
40  - Use of the software either in source or binary form, must be run
41  on or directly connected to an Analog Devices Inc. component.
42 
43 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
44 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
45 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
47 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
49 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54 
55 #include <stdint.h>
56 #include <Arduino.h>
57 #include "Linduino.h"
58 #include "QuikEval_EEPROM.h"
59 #include "UserInterface.h"
60 #include <SPI.h>
61 #include "LT_SPI.h"
62 #include <Wire.h>
63 #include "LT_I2C.h"
64 #include "LTC24XX_general.h"
65 
66 #define CS QUIKEVAL_CS //!< The chip select
67 
68 ///////////////////////////////////////////////////////////////
69 // Begin program behavior setup ///////////////////////////////
70 ///////////////////////////////////////////////////////////////
71 
72 // Lookup tables of channels to scan.
73 // UN-comment one section below: First section is odd channels as negative input
74 // Second section is even channels as negative input.
75 // Third section is single-ended channels with respec to COM.
76 
77 const uint8_t CHANNEL_ARRAY[2] = {LTC24XX_MULTI_CH_P0_N1, LTC24XX_MULTI_CH_P2_N3}; //!< Build the command for differential mode
78 
79 //const uint8_t CHANNEL_ARRAY[2] = {LTC24XX_MULTI_CH_P1_N0, LTC24XX_MULTI_CH_P3_N2}; //!< Build the command for differential mode
80 
81 //const uint8_t CHANNEL_ARRAY[4] = {LTC24XX_MULTI_CH_CH0, LTC24XX_MULTI_CH_CH1, LTC24XX_MULTI_CH_CH2, LTC24XX_MULTI_CH_CH3}; //!< Builds the command for single-ended mode
82 
83 
84 //UN-comment one line below to set the OSR mode:
85 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_32768;
87 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_8192;
88 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_4096;
89 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_2048;
90 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_1024;
91 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_512;
92 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_256;
93 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_128;
94 //const uint8_t OSR = LTC24XX_MULTI_CH_OSR_64;
95 
96 float LTC2442_vref = 5.0; // Reference voltage
97 uint16_t EOC_timout = 250; // End of conversion timeout (must be larger than tconv(max))
98 uint16_t loop_delay = 0; // the delay between loops
99 uint16_t channel_read_delay = 0; // The delay between channel readings
100 
101 // Display format. UN-comment one line below.
102 #define FLOATING
103 //#define RAW
104 
105 // UN-comment to prime the ADC before taking data for a given scan, purging stale data from previous scan.
106 // May be useful for single scans with a long delay between scans.
107 //#define PRIME_BEFORE_SCAN
108 
109 ///////////////////////////////////////////////////////////////
110 // End program behavior setup ///////////////////////////////
111 ///////////////////////////////////////////////////////////////
112 
113 
114 uint8_t array_size = sizeof(CHANNEL_ARRAY); // Get size of array (in bytes, each element is a byte)
115 
116 // Prototypes
117 void read_LTC2442(float vref, uint16_t eoc_timeout, uint16_t channel_delay);
118 void print_raw(int32_t *rawresults);
119 void print_all(float *results);
120 void print_prompt();
121 
122 
123 //! Initialize Linduino
124 void setup()
125 {
126  char demo_name[] = "DC979"; // Demo Board Name stored in QuikEval EEPROM
127  int8_t demo_board_connected; // Set to 1 if the board is connected
128 
129  Serial.begin(115200); // Initialize the serial port to the PC
130  Serial.println(F("Welcome to the LTC2442 Test Program\n"));
131  delay(200);
132 
133  quikeval_I2C_init(); // Enable the I2C port
134  quikeval_SPI_init(); // Configure the spi port for 4volts SCK
135  quikeval_SPI_connect(); // Connect SPI to main data port
136 
137  demo_board_connected = discover_demo_board(demo_name);
138 
139  // If demo board is not connected loop forever
140  if (!demo_board_connected)
141  Serial.println(F("Standard demo board not detected, verify hardware connections to your circuit."));
142 
143  print_prompt(); // Prints the prompt
144 } // End of setup()
145 
146 
147 //! Repeats Linduino loop
148 void loop()
149 {
150  static char user_command; // The user input command
151  user_command = read_char(); // Reads the users input
152 // Serial.println(user_command); // Displays the user input
153 
154  switch (user_command)
155  {
156  case 'S':
157  case 's':
158  // Single loop
159  read_LTC2442(LTC2442_vref, EOC_timout, channel_read_delay); // Read all the channels
160  break;
161  case 'C':
162  case 'c':
163  // Continous loop
164  do
165  {
166  read_LTC2442(LTC2442_vref, EOC_timout, channel_read_delay); // Read all the channels
167  delay(loop_delay); // Delay before repeating the loop
168  }
169  while (Serial.available() == false); // Check to see is anything was entered
170  read_int(); // Clears the Serial.available
171  break;
172  default:
173  Serial.println(F("Incorrect Option"));
174  break;
175  }
176 }
177 
178 //! Print the Prompt
180 {
181  Serial.println();
182  Serial.println(F("S-Single Loop"));
183  Serial.println(F("C-Continuous Loop"));
184  Serial.println(F(" Enter any character to exit Continuous Loop"));
185 
186  Serial.print(F("Enter a command: "));
187 }
188 
189 //! Reads the LTC2442 and displays the results
190 void read_LTC2442(float vref, uint16_t eoc_timeout, uint16_t channel_delay)
191 {
192  float floatresults[16];
193  int32_t rawresults[16];
194  int32_t adc_code = 0;
195  uint8_t i;
196 
197 #ifdef PRIME_BEFORE_SCAN // If defined, prime first channel so that data is "less stale"
198  LTC24XX_EOC_timeout(CS, eoc_timeout); // Wait for end of conversion
199  LTC24XX_SPI_16bit_command_32bit_data(CS, CHANNEL_ARRAY[0], OSR, &adc_code); // Prime with first channel
200 #endif
201 
202  for (i=0; i<array_size; i++)
203  {
204  delay(channel_delay);
205  LTC24XX_EOC_timeout(CS, eoc_timeout); // Wait for end of conversion
206  LTC24XX_SPI_16bit_command_32bit_data(CS, CHANNEL_ARRAY[(i+1)%array_size], OSR, &adc_code); // Read the ADC
207  rawresults[i%array_size] = adc_code;
208  floatresults[i%array_size] = LTC24XX_diff_code_to_voltage(adc_code, vref); // Convert ADC code to voltage
209  }
210 #ifdef FLOATING
211  print_all(floatresults); // Display results
212 #else
213  print_raw(rawresults);
214 #endif
215 }
216 
217 //! Displays the the data in the array
218 void print_all(float *floatresults)
219 {
220  uint8_t i;
221  for (i=0; i<array_size; i++)
222  {
223  Serial.print(floatresults[i], 8);
224  if (!((i+1)%array_size)) Serial.println();
225  else Serial.print(F(" , "));
226  }
227 }
228 
229 void print_raw(int32_t *rawresults)
230 {
231  uint8_t i;
232  for (i=0; i<array_size; i++)
233  {
234  Serial.print(rawresults[i]);
235  if (!((i+1)%array_size)) Serial.println();
236  else Serial.print(F(" , "));
237  }
238 }
static void print_prompt()
Print the Prompt.
unsigned char user_command
static uint8_t array_size
const uint8_t CHANNEL_ARRAY[2]
Build the command for differential mode.
static uint16_t loop_delay
Header File for Linduino Libraries and Demo Code.
static void setup()
Initialize Linduino.
int8_t LTC24XX_EOC_timeout(uint8_t cs, uint16_t miso_timeout)
Checks for EOC with a specified timeout.
static void print_all(float *results)
Displays the the data in the array.
LTC24XX General Library: Functions and defines for all SINC4 Delta Sigma ADCs.
void LTC24XX_SPI_16bit_command_32bit_data(uint8_t cs, uint8_t adc_command_high, uint8_t adc_command_low, int32_t *adc_code)
Reads from LTC24XX ADC that accepts a 16 bit configuration and returns a 32 bit result.
static uint16_t eoc_timeout
timeout in ms
QuikEval EEPROM Library.
#define LTC24XX_MULTI_CH_P2_N3
static void print_raw(int32_t *rawresults)
#define LTC24XX_MULTI_CH_OSR_16384
void quikeval_SPI_init(void)
Configure the SPI port for 4Mhz SCK.
Definition: LT_SPI.cpp:151
int8_t discover_demo_board(char *demo_name)
Read the ID string from the EEPROM and determine if the correct board is connected.
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
const uint8_t OSR
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
#define CS
LTC2442: 24-Bit High Speed 4-Channel Delta Sigma ADCs with Integrated Amplifier.
float LTC24XX_diff_code_to_voltage(int32_t adc_code, float vref)
Calculates the voltage corresponding to an ADC code, given the reference voltage. ...
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
Definition: DC1880A.ino:97
static float LTC2442_vref
void quikeval_SPI_connect()
Connect SPI pins to QuikEval connector through the Linduino MUX. This will disconnect I2C...
Definition: LT_SPI.cpp:138
int32_t read_int()
static uint16_t EOC_timout
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
static int i
Definition: DC2430A.ino:184
#define LTC24XX_MULTI_CH_P0_N1
static void read_LTC2442(float vref, uint16_t eoc_timeout, uint16_t channel_delay)
Reads the LTC2442 and displays the results.
static uint16_t channel_read_delay
int8_t read_char()
static uint32_t adc_code
Definition: DC2071AA.ino:113
static void loop()
Repeats Linduino loop.
static uint8_t demo_board_connected
Set to 1 if the board is connected.