Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
_24xx_iso_spi_delay.ino
Go to the documentation of this file.
1 /*!
2 Modified LTC24xx sketch for isoSPI LTC6820
3 
4 
5 @endverbatim
6 
7 
8 
9 Copyright 2018(c) Analog Devices, Inc.
10 
11 All rights reserved.
12 
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15  - Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  - Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in
19  the documentation and/or other materials provided with the
20  distribution.
21  - Neither the name of Analog Devices, Inc. nor the names of its
22  contributors may be used to endorse or promote products derived
23  from this software without specific prior written permission.
24  - The use of this software may or may not infringe the patent rights
25  of one or more patent holders. This license does not release you
26  from the requirement that you obtain separate licenses from these
27  patent holders to use this software.
28  - Use of the software either in source or binary form, must be run
29  on or directly connected to an Analog Devices Inc. component.
30 
31 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
32 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
33 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
37 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 
43 /*! @ingroup Example_Designs
44  @{
45  @defgroup LTC24xx_iso_spi_delay LTC24xx_iso_spi_delay: Modified LTC24xx sketch for isoSPI LTC6820
46  @}
47 */
48 /*! @file
49  @ingroup LTC24xx_iso_spi_delay
50 */
51 #include <Arduino.h>
52 #include <stdint.h>
53 #include "Linduino.h"
54 #include "LT_SPI.h"
55 #include "UserInterface.h"
56 #include "QuikEval_EEPROM.h"
57 #include "LTC24XX_general.h"
58 #include <SPI.h>
59 #include <Wire.h>
60 #include "LT_I2C.h"
61 
62 // Function Declaration
63 void print_title(); // Print the title block
64 void print_prompt(); // Prompt the user for an input command
65 void print_user_command(uint8_t menu); // Display selected differential channels
66 
67 uint8_t menu_1_read_single_ended();
68 uint8_t menu_2_read_differential();
69 
70 // Global variables
71 static uint8_t demo_board_connected; //!< Set to 1 if the board is connected
72 static float reference_voltage = 5.0; //!< The reference voltage range, set to 5v through JP2 and JP3 by default
73 
74 //Constants
75 const uint16_t MISO_TIMEOUT = 1000; //!< The MISO timeout (ms)
76 // Build the command for single-ended mode
93  }; //!< Builds the command for single-ended mode
94 
95 // Build the command for differential mode
112  };
113 
114 
115 #define delay_EOC 150
116 
117 //! Initialize Linduino
118 void setup()
119 // Setup the program
120 {
121  quikeval_SPI_init(); // Configure the spi port for 4MHz SCK
122  quikeval_SPI_connect(); // Connect SPI to main data port
123  quikeval_I2C_init(); // Configure the EEPROM I2C port for 100kHz
124  Serial.begin(115200); // Initialize the serial port to the PC
125  print_title();
126 
129  {
130  print_prompt();
131  };
132 }
133 
134 //! Repeats Linduino loop
135 void loop()
136 {
137  uint8_t user_command; // The user input command
138  uint8_t ack = 0;
140  {
141  if (Serial.available()) // Check for user input
142  {
143  user_command = read_int(); // Read the user command
144  if (user_command == 'm');
145  else
146  Serial.println(user_command);
147  delay(50); // Allow the print to finish
148  switch (user_command)
149  {
150  case 1:
151  ack |= menu_1_read_single_ended();
152  break;
153  case 2:
154  ack |= menu_2_read_differential();
155  break;
156  default:
157  Serial.println("Incorrect Option");
158  break;
159  }
160  if (ack)
161  Serial.println(F("***** SPI ERROR *****"));
162  Serial.println(F("*****************************************************************"));
163  print_prompt();
164  }
165  }
166 }
167 
168 //Function Definitions
169 
170 //! Prints the title block when program first starts.
172 {
173  Serial.println();
174  Serial.println(F("*****************************************************************"));
175  Serial.println(F("* *"));
176  Serial.println(F("* *"));
177  Serial.println(F("* This program demonstrates how to send data and receive data *"));
178  Serial.println(F("* from the 24-bit delta-sigma ADC and LTC6820. *"));
179  Serial.println(F("* *"));
180  Serial.println(F("* *"));
181  Serial.println(F("* Set the baud rate to 115200 select the newline terminator. *"));
182  Serial.println(F("* *"));
183  Serial.println(F("*****************************************************************"));
184 }
185 
186 //! Prints main menu.
188 {
189 
190  Serial.println(F("1-Read Single-Ended"));
191  Serial.println(F("2-Read Differential"));
192  Serial.println();
193  Serial.print(F("Enter a command:"));
194 }
195 
196 //! read from ADC single-ended
197 //! @return 0 if successful, 1 if failure
198 //! @return 0 when m is entered into menu, 1 if timeout for EOC
200 {
201  uint8_t adc_command; // The LTC24XX command byte
202  int16_t user_command; // The user input command
203  int32_t adc_code=0; // The LTC24XX code
204  float adc_voltage; // The LTC24XX voltage
205 
206  while (1)
207  {
208  Serial.print(F("*************************\n\n")); // Display single-ended menu
209  Serial.print(F("0-CH0 8-CH8\n"));
210  Serial.print(F("1-CH1 9-CH9\n"));
211  Serial.print(F("2-CH2 10-CH10\n"));
212  Serial.print(F("3-CH3 11-CH11\n"));
213  Serial.print(F("4-CH4 12-CH12\n"));
214  Serial.print(F("5-CH5 13-CH13\n"));
215  Serial.print(F("6-CH6 14-CH14\n"));
216  Serial.print(F("7-CH7 15-CH15\n"));
217  Serial.print(F("16-ALL\n"));
218  Serial.print(F("m-Main Menu\n"));
219  Serial.print(F("\nEnter a Command: "));
220  user_command = read_int(); // Read the single command
221  if (user_command == 'm')
222  return(0);
223  else
224  Serial.println(user_command);
225  Serial.println();
226 
227  if (user_command == 16)
228  {
229  Serial.print(F("ALL\n"));
230  adc_command = BUILD_COMMAND_SINGLE_ENDED[0]; // Build ADC command for channel 0
231  delay(delay_EOC); // Wait for EOC
232  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
233 
234  for (int8_t x = 0; x <= 15; x++)
235  {
236  adc_command = BUILD_COMMAND_SINGLE_ENDED[(x + 1) % 16]; // Read all channels in single-ended mode
237  delay(delay_EOC); // Wait for EOC
238  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
239  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
240  Serial.print(F(" ****"));
241  Serial.print(F("CH"));
242  Serial.print(x);
243  Serial.print(F(": "));
244  Serial.print(adc_voltage, 4);
245  Serial.print(F("V\n"));
246  }
247  }
248  else if (user_command<16 && user_command >=0) // Read selected channel
249  {
251  Serial.print(F("\nADC Command: h"));
252  Serial.println(adc_command, HEX);
253  delay(delay_EOC); // Wait for EOC
254  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
255  delay(delay_EOC); // wait for EOC
256  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
257  Serial.print(F("Received Code: 0x"));
258  Serial.println(adc_code, HEX);
259  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
260  Serial.print(F(" ****"));
261  Serial.print(F("CH"));
262  Serial.print(user_command);
263  Serial.print(F(": "));
264  Serial.print(adc_voltage, 4);
265  Serial.print(F("V\n"));
266  }
267  }
268 }
269 
270 //! Read channels in differential mode
271 //! @return 0 when m is entered into menu, 1 if timeout for EOC
273 {
274  int8_t y; // Offset into differential channel array to select polarity
275  uint8_t adc_command; // The LTC24XX command byte
276  int16_t user_command; // The user input command
277  int32_t adc_code=1; // The LTC24XX code
278  float adc_voltage; // The LTC24XX voltage
279 
280  while (1)
281  {
282  // Display differential menu
283  Serial.print(F("\n*************************\n\n"));
284  Serial.print(F("0-0P-1N 8-1P-0N\n"));
285  Serial.print(F("1-2P-3N 9-3P-2N\n"));
286  Serial.print(F("2-4P-5N 10-5P-4N\n"));
287  Serial.print(F("3-6P-7N 11-7P-6N\n"));
288  Serial.print(F("4-8P-9N 12-9P-8N\n"));
289  Serial.print(F("5-10P-11N 13-11P-10N\n"));
290  Serial.print(F("6-12P_13N 14-13P-12N\n"));
291  Serial.print(F("7-14P-15N 15-15P-14N\n"));
292  Serial.print(F("16-ALL Even_P-Odd_N\n"));
293  Serial.print(F("17-ALL Odd_P-Even_N\n"));
294  Serial.print(F("m-Main Menu\n"));
295  Serial.print(F("\nEnter a Command: "));
296  user_command = read_int();
297  if (user_command == 'm')
298  return(0);
299  Serial.println(user_command);
300  Serial.println();
301 
302  if ((user_command == 16) || (user_command == 17))
303  {
304  if (user_command == 16)
305  {
306  Serial.print(F("ALL Even_P-Odd_N\n")); // Cycles through options 0-7
307  y = 0;
308  }
309  if (user_command == 17)
310  {
311  Serial.print(F("ALL Odd_P-Even_N\n")); // Cycles through options 8-15
312  y = 8;
313  }
314  adc_command = BUILD_COMMAND_DIFF[y]; // Set up first channel
315  delay(delay_EOC); // Wait for EOC
316  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
317  for (int8_t x = 0; x <= 7; x++) // Read all channels. All even channels are positive and odd channels are negative
318  {
319  adc_command = BUILD_COMMAND_DIFF[((x+1) % 8) + y];
320  delay(delay_EOC); // Wait for EOC
321  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
322  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
323  Serial.println();
324  Serial.print(F("**** "));
325  print_user_command(x + y);
326  Serial.print(F(": "));
327  Serial.print(adc_voltage, 4);
328  Serial.print(F("V\n"));
329  }
330  }
331  else // Read selected channels
332  {
333  // Reads and displays a selected channel
334  adc_command = BUILD_COMMAND_DIFF[user_command];
335  Serial.print(F("ADC Command: 0b"));
336  Serial.println(adc_command, BIN);
337  delay(delay_EOC); // Wait for EOC
338  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
339  delay(delay_EOC); // Wait for EOC
340  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
341  Serial.print(F("Received Code: 0x"));
342  Serial.println(adc_code, HEX);
343  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
344  Serial.println();
345  Serial.print(F("**** "));
346  Serial.print(F("CH"));
347  print_user_command(user_command);
348  Serial.print(F(": "));
349  Serial.print(adc_voltage, 4);
350  Serial.print(F("V"));
351  Serial.println();
352  }
353  }
354 }
355 
356 //! Display selected differential channels. Displaying Single-Ended channels is
357 //! straightforward; not so with differential because the inputs can take either polarity.
358 void print_user_command(uint8_t menu) //!< the selected channels
359 {
360  switch (menu)
361  {
362  case 0:
363  Serial.print("0P-1N");
364  break;
365  case 1:
366  Serial.print("2P-3N");
367  break;
368  case 2:
369  Serial.print("4P-5N");
370  break;
371  case 3:
372  Serial.print("6P-7N");
373  break;
374  case 4:
375  Serial.print("8P-9N");
376  break;
377  case 5:
378  Serial.print("10P-11N");
379  break;
380  case 6:
381  Serial.print("12P-13N");
382  break;
383  case 7:
384  Serial.print("14P-15N");
385  break;
386  case 8:
387  Serial.print("1P-0N");
388  break;
389  case 9:
390  Serial.print("3P-2N");
391  break;
392  case 10:
393  Serial.print("5P-4N");
394  break;
395  case 11:
396  Serial.print("7P-6N");
397  break;
398  case 12:
399  Serial.print("9P-8N");
400  break;
401  case 13:
402  Serial.print("11P-10N");
403  break;
404  case 14:
405  Serial.print("13P-12N");
406  break;
407  case 15:
408  Serial.print("15P-14N");
409  break;
410  }
411  Serial.print(": ");
412 }
#define LTC24XX_MULTI_CH_CH3
#define LTC24XX_MULTI_CH_CH14
#define delay_EOC
unsigned char user_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
#define LTC24XX_MULTI_CH_CH8
#define LTC24XX_MULTI_CH_CH15
#define LTC24XX_MULTI_CH_CH13
#define LTC24XX_MULTI_CH_CH1
#define LTC24XX_MULTI_CH_CH6
#define LTC24XX_MULTI_CH_P13_N12
const uint8_t BUILD_COMMAND_DIFF[16]
#define LTC24XX_MULTI_CH_P1_N0
Header File for Linduino Libraries and Demo Code.
#define LTC24XX_MULTI_CH_CH5
#define LTC24XX_MULTI_CH_P4_N5
const uint8_t BUILD_COMMAND_SINGLE_ENDED[16]
Builds the command for single-ended mode.
#define LTC24XX_MULTI_CH_CH0
#define LTC24XX_MULTI_CH_CH11
static float adc_voltage
Definition: DC2071AA.ino:115
LTC24XX General Library: Functions and defines for all SINC4 Delta Sigma ADCs.
#define LTC24XX_MULTI_CH_P9_N8
#define LTC24XX_MULTI_CH_P14_N15
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.
#define LTC24XX_MULTI_CH_CH2
#define LTC24XX_MULTI_CH_P3_N2
static uint8_t menu_1_read_single_ended()
read from ADC single-ended
QuikEval EEPROM Library.
#define LTC24XX_MULTI_CH_P2_N3
static uint8_t menu_2_read_differential()
Read channels in differential mode.
void quikeval_SPI_init(void)
Configure the SPI port for 4Mhz SCK.
Definition: LT_SPI.cpp:151
#define LTC24XX_MULTI_CH_P10_N11
#define LTC24XX_MULTI_CH_CH12
static void print_title()
Prints the title block when program first starts.
#define LTC24XX_MULTI_CH_P12_N13
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
#define LTC24XX_MULTI_CH_CH7
#define LTC24XX_MULTI_CH_P5_N4
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
#define LTC24XX_MULTI_CH_P8_N9
#define LTC24XX_MULTI_CH_P7_N6
float LTC24XX_diff_code_to_voltage(int32_t adc_code, float vref)
Calculates the voltage corresponding to an ADC code, given the reference voltage. ...
static float reference_voltage
The reference voltage range, set to 5v through JP2 and JP3 by default.
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()
#define LTC24XX_MULTI_CH_CH10
static void print_user_command(uint8_t menu)
Display selected differential channels.
#define LTC24XX_MULTI_CH_CH9
#define LTC24XX_MULTI_CH_P15_N14
#define LTC24XX_CS
Define the SPI CS pin.
static void loop()
Repeats Linduino loop.
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
static void print_prompt()
Prints main menu.
static void setup()
Initialize Linduino.
#define LTC24XX_MULTI_CH_P11_N10
#define LTC24XX_MULTI_CH_P0_N1
const uint16_t MISO_TIMEOUT
The MISO timeout (ms)
#define LTC24XX_MULTI_CH_CH4
static uint32_t adc_code
Definition: DC2071AA.ino:113
#define LTC24XX_MULTI_CH_P6_N7
static uint8_t demo_board_connected
Set to 1 if the board is connected.