Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
_24xx_iso_spi_poll.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_poll LTC24xx_iso_spi_poll: Modified LTC24xx sketch for isoSPI LTC6820
46  @}
47 */
48 /*! @file
49  @ingroup LTC24xx_iso_spi_poll
50 */
51 
52 #include <Arduino.h>
53 #include <stdint.h>
54 #include "Linduino.h"
55 #include "LT_SPI.h"
56 #include "UserInterface.h"
57 #include "QuikEval_EEPROM.h"
58 #include "LTC24XX_general.h"
59 #include <SPI.h>
60 #include <Wire.h>
61 #include "LT_I2C.h"
62 
63 // Function Declaration
64 void print_title(); // Print the title block
65 void print_prompt(); // Prompt the user for an input command
66 void print_user_command(uint8_t menu); // Display selected differential channels
67 
68 uint8_t menu_1_read_single_ended();
69 uint8_t menu_2_read_differential();
70 
71 // Global variables
72 static uint8_t demo_board_connected; //!< Set to 1 if the board is connected
73 static float reference_voltage = 5.0; //!< The reference voltage range, set to 5v through JP2 and JP3 by default
74 
75 //Constants
76 const uint16_t MISO_TIMEOUT = 100; //!< The MISO timeout (ms)
77 
78 // Build the command for single-ended mode
95  }; //!< Builds the command for single-ended mode
96 
97 // Build the command for differential mode
114  };
115 
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 
127  demo_board_connected = 1; // Bypass EEPROM chack
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
232  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
234  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
235 
236  for (int8_t x = 0; x <= 15; x++)
237  {
238  adc_command = BUILD_COMMAND_SINGLE_ENDED[(x + 1) % 16]; // Read all channels in single-ended mode
240  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
241  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
242  Serial.print(F(" ****"));
243  Serial.print(F("CH"));
244  Serial.print(x);
245  Serial.print(F(": "));
246  Serial.print(adc_voltage, 4);
247  Serial.print(F("V\n"));
248  }
249  }
250  else if (user_command<16 && user_command >=0) // Read selected channel
251  {
253  Serial.print(F("\nADC Command: h"));
254  Serial.println(adc_command, HEX);
256  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
258  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
259  Serial.print(F("Received Code: 0x"));
260  Serial.println(adc_code, HEX);
261  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
262  Serial.print(F(" ****"));
263  Serial.print(F("CH"));
264  Serial.print(user_command);
265  Serial.print(F(": "));
266  Serial.print(adc_voltage, 4);
267  Serial.print(F("V\n"));
268  }
269  }
270 }
271 
272 //! Read channels in differential mode
273 //! @return 0 when m is entered into menu, 1 if timeout for EOC
275 {
276  int8_t y; // Offset into differential channel array to select polarity
277  uint8_t adc_command; // The LTC24XX command byte
278  int16_t user_command; // The user input command
279  int32_t adc_code=1; // The LTC24XX code
280  float adc_voltage; // The LTC24XX voltage
281 
282  while (1)
283  {
284  // Display differential menu
285  Serial.print(F("\n*************************\n\n"));
286  Serial.print(F("0-0P-1N 8-1P-0N\n"));
287  Serial.print(F("1-2P-3N 9-3P-2N\n"));
288  Serial.print(F("2-4P-5N 10-5P-4N\n"));
289  Serial.print(F("3-6P-7N 11-7P-6N\n"));
290  Serial.print(F("4-8P-9N 12-9P-8N\n"));
291  Serial.print(F("5-10P-11N 13-11P-10N\n"));
292  Serial.print(F("6-12P_13N 14-13P-12N\n"));
293  Serial.print(F("7-14P-15N 15-15P-14N\n"));
294  Serial.print(F("16-ALL Even_P-Odd_N\n"));
295  Serial.print(F("17-ALL Odd_P-Even_N\n"));
296  Serial.print(F("m-Main Menu\n"));
297  Serial.print(F("\nEnter a Command: "));
298  user_command = read_int();
299  if (user_command == 'm')
300  return(0);
301  Serial.println(user_command);
302  Serial.println();
303 
304  if ((user_command == 16) || (user_command == 17))
305  {
306  if (user_command == 16)
307  {
308  Serial.print(F("ALL Even_P-Odd_N\n")); // Cycles through options 0-7
309  y = 0;
310  adc_command = BUILD_COMMAND_DIFF[y];
311  }
312  if (user_command == 17)
313  {
314  Serial.print(F("ALL Odd_P-Even_N\n")); // Cycles through options 8-15
315  y = 8;
316  adc_command = BUILD_COMMAND_DIFF[y];
317  }
319  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
321  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
322  for (int8_t x = 0; x <= 7; x++) // Read all channels. All even channels are positive and odd channels are negative
323  {
324  adc_command = BUILD_COMMAND_DIFF[((x+1) % 8) + y];
326  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
327  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
328  Serial.println();
329  Serial.print(F("**** "));
330  print_user_command(x + y);
331  Serial.print(F(": "));
332  Serial.print(adc_voltage, 4);
333  Serial.print(F("V\n"));
334  }
335  }
336  else // Read selected channels
337  {
338  // Reads and displays a selected channel
339  adc_command = BUILD_COMMAND_DIFF[user_command];
340  Serial.print(F("ADC Command: 0b"));
341  Serial.println(adc_command, BIN);
343  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code); // Throws out last reading
345  LTC24XX_SPI_16bit_command_32bit_data(LTC24XX_CS, adc_command, 0x00, &adc_code);
346  Serial.print(F("Received Code: 0x"));
347  Serial.println(adc_code, HEX);
348  adc_voltage = LTC24XX_diff_code_to_voltage(adc_code, reference_voltage);
349  Serial.println();
350  Serial.print(F("**** "));
351  Serial.print(F("CH"));
352  print_user_command(user_command);
353  Serial.print(F(": "));
354  Serial.print(adc_voltage, 4);
355  Serial.print(F("V"));
356  Serial.println();
357  }
358  }
359 }
360 
361 //! Display selected differential channels. Displaying Single-Ended channels is
362 //! straightforward; not so with differential because the inputs can take either polarity.
363 void print_user_command(uint8_t menu) //!< the selected channels
364 {
365  switch (menu)
366  {
367  case 0:
368  Serial.print("0P-1N");
369  break;
370  case 1:
371  Serial.print("2P-3N");
372  break;
373  case 2:
374  Serial.print("4P-5N");
375  break;
376  case 3:
377  Serial.print("6P-7N");
378  break;
379  case 4:
380  Serial.print("8P-9N");
381  break;
382  case 5:
383  Serial.print("10P-11N");
384  break;
385  case 6:
386  Serial.print("12P-13N");
387  break;
388  case 7:
389  Serial.print("14P-15N");
390  break;
391  case 8:
392  Serial.print("1P-0N");
393  break;
394  case 9:
395  Serial.print("3P-2N");
396  break;
397  case 10:
398  Serial.print("5P-4N");
399  break;
400  case 11:
401  Serial.print("7P-6N");
402  break;
403  case 12:
404  Serial.print("9P-8N");
405  break;
406  case 13:
407  Serial.print("11P-10N");
408  break;
409  case 14:
410  Serial.print("13P-12N");
411  break;
412  case 15:
413  Serial.print("15P-14N");
414  break;
415  }
416  Serial.print(": ");
417 }
#define LTC24XX_MULTI_CH_CH3
static uint8_t menu_2_read_differential()
Read channels in differential mode.
#define LTC24XX_MULTI_CH_CH14
static void loop()
Repeats Linduino loop.
unsigned char user_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
#define LTC24XX_MULTI_CH_CH8
#define LTC24XX_MULTI_CH_CH15
static uint8_t demo_board_connected
Set to 1 if the board is connected.
#define LTC24XX_MULTI_CH_CH13
#define LTC24XX_MULTI_CH_CH1
#define LTC24XX_MULTI_CH_CH6
static void setup()
Initialize Linduino.
#define LTC24XX_MULTI_CH_P13_N12
#define LTC24XX_MULTI_CH_P1_N0
Header File for Linduino Libraries and Demo Code.
const uint16_t MISO_TIMEOUT
The MISO timeout (ms)
int8_t LTC24XX_EOC_timeout(uint8_t cs, uint16_t miso_timeout)
Checks for EOC with a specified timeout.
#define LTC24XX_MULTI_CH_CH5
static void print_prompt()
Prints main menu.
#define LTC24XX_MULTI_CH_P4_N5
#define LTC24XX_MULTI_CH_CH0
static void print_user_command(uint8_t menu)
Display selected differential channels.
#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
const uint8_t BUILD_COMMAND_DIFF[16]
#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
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
#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. ...
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
#define LTC24XX_MULTI_CH_CH9
static void print_title()
Prints the title block when program first starts.
#define LTC24XX_MULTI_CH_P15_N14
#define LTC24XX_CS
Define the SPI CS pin.
const uint8_t BUILD_COMMAND_SINGLE_ENDED[16]
Builds the command for single-ended mode.
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
#define LTC24XX_MULTI_CH_P11_N10
#define LTC24XX_MULTI_CH_P0_N1
#define LTC24XX_MULTI_CH_CH4
static uint32_t adc_code
Definition: DC2071AA.ino:113
static float reference_voltage
The reference voltage range, set to 5v through JP2 and JP3 by default.
#define LTC24XX_MULTI_CH_P6_N7