Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
DC1444A.ino
Go to the documentation of this file.
1 /*!
2 Linear Technology DC1337A Demonstration Board.
3 LTC2301: 1-Channel, 12-Bit SAR ADC with I2C Interface.
4 LTC2305: 2-Channel, 12-Bit SAR ADC with I2C Interface.
5 
6 @verbatim
7 
8 NOTES
9  Setup:
10  Set the terminal baud rate to 115200 and select the newline terminator. Equipment
11  required is a voltage source (preferably low-noise) and a precision voltmeter.
12  Ensure all jumpers on the demo board are installed in their default positions
13  from the factory. Refer to Demo Manual DC1444A.
14 
15  LTC2301
16  How to test Differential Mode:
17  Bipolar Mode:
18  The voltage source should be connected between inputs VIN+ and VIN-. Ensure both
19  inputs are within their specified absolute input voltage range. (It is easiest
20  to tie the voltage source negative terminal to COM.) Ensure the voltage
21  source is set within the range of 0V to +4.096V (differential voltage range).
22  (Swapping input voltages results in a reversed polarity reading.)
23 
24  Unipolar Mode:
25  The voltage source should be connected between inputs VIN+ and VIN-. Ensure both
26  inputs are within their specified absolute input voltage range. (It is easiest
27  to tie the voltage source negative terminal to COM.) Ensure the voltage
28  source is set within the range of 0V to +4.096V (differential voltage range).
29 
30 LTC2305
31  How to test Single-Ended mode:
32  Bipolar Mode:
33  The voltage source should be connected to input VCH0 or VCH1. Ensure the input is
34  within its specified absolute input voltage range. (It is easiest
35  to tie the voltage source negative terminal to COM.) Ensure the voltage
36  source is set within the range of 0V to +4.096V.
37  (Swapping input voltages results in a reversed polarity reading.)
38 
39  Unipolar Mode:
40  The voltage source should be connected to input VCH0 or VCH1. Ensure the input is
41  within its specified absolute input voltage range. (It is easiest
42  to tie the voltage source negative terminal to COM.) Ensure the voltage
43  source is set within the range of 0V to +4.096V.
44 
45 
46 USER INPUT DATA FORMAT:
47  decimal : 1024
48  hex : 0x400
49  octal : 02000 (leading 0 "zero")
50  binary : B10000000000
51  float : 1024.0
52 
53 @endverbatim
54 
55 http://www.linear.com/product/LTC2301
56 http://www.linear.com/product/LTC2305
57 
58 http://www.linear.com/product/LTC2301#demoboards
59 http://www.linear.com/product/LTC2305#demoboards
60 
61 
62 Copyright 2018(c) Analog Devices, Inc.
63 
64 All rights reserved.
65 
66 Redistribution and use in source and binary forms, with or without
67 modification, are permitted provided that the following conditions are met:
68  - Redistributions of source code must retain the above copyright
69  notice, this list of conditions and the following disclaimer.
70  - Redistributions in binary form must reproduce the above copyright
71  notice, this list of conditions and the following disclaimer in
72  the documentation and/or other materials provided with the
73  distribution.
74  - Neither the name of Analog Devices, Inc. nor the names of its
75  contributors may be used to endorse or promote products derived
76  from this software without specific prior written permission.
77  - The use of this software may or may not infringe the patent rights
78  of one or more patent holders. This license does not release you
79  from the requirement that you obtain separate licenses from these
80  patent holders to use this software.
81  - Use of the software either in source or binary form, must be run
82  on or directly connected to an Analog Devices Inc. component.
83 
84 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
85 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
86 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
87 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
88 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
90 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
91 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
92 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
93 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94  */
95 
96 /*! @file
97  @ingroup LTC2305
98 */
99 
100 #include <Arduino.h>
101 #include <stdint.h>
102 #include "Linduino.h"
103 #include "LT_I2C.h"
104 #include "UserInterface.h"
105 #include "QuikEval_EEPROM.h"
106 #include "LTC2305.h"
107 #include <Wire.h>
108 
109 // Function Declaration
110 void print_title(); // Print the title block
111 void print_prompt(); // Prompt the user for an input command
112 uint8_t print_user_command_single_ended(); // Display selected single ended channels
113 int8_t menu_1_read_input(); // Read channels in single-ended mode
114 void menu_2_select_part(); // Selects LTC2301/2305
115 void menu_3_select_uni_bipolar(); // Sets LTC2305 to Bipolar or Unipolar
116 void menu_4_select_single_ended_differential(); // Selects single-ended or differential mode
117 void menu_5_select_polarity(); // Selects polarity in differential mode
118 int8_t menu_6_sleep(); // Sets LTC2305 Sleep Mode
119 void menu_7_set_address();
120 
121 // Global variables
122 static uint8_t demo_board_connected; //!< Set to 1 if the board is connected
123 static uint8_t single_ended_differential = LTC2305_SINGLE_ENDED_MODE; //!< LTC2305 Single-Ended or Differential mode selection
124 static uint8_t uni_bipolar = LTC2305_UNIPOLAR_MODE; //!< LTC2305 Unipolar or Bipolar mode selection
125 static uint8_t part = LTC2301; //!< LTC2301 or 2305 part selection
126 static uint8_t polarity = LTC2305_P0_N1; //!< LTC230X Input polarity selection
127 static uint8_t channel = LTC2305_CH0; //!< LTC2305 Channel selection
128 static uint8_t i2c_address = LTC2305_I2C_ADDRESS; //!< I2C address in 7 bit format for part
129 static uint8_t LTC2305_bits = 12; //!< Resolution (12 bits)
130 static float LTC2305_vref = 4.096;
131 
132 // Constants
133 // Build the command for single-ended mode
134 const uint8_t BUILD_COMMAND_SINGLE_ENDED[2] = {LTC2305_CH0, LTC2305_CH1}; //!< Builds the command for single-ended mode
135 
136 //! Initialize Linduino
137 void setup()
138 {
139  quikeval_I2C_init(); // Enable the I2C port
140  quikeval_I2C_connect(); // Connect I2C to main data port
141  Serial.begin(115200); // Initialize the serial port to the PC
142  print_title();
143  print_prompt();
144 }
145 
146 
147 //! Repeats Linduino loop
148 void loop()
149 {
150  int8_t user_command; // The user input command
151  uint8_t acknowledge = 0; // I2C acknowledge bit
152 
153  if (Serial.available()) // Check for user input
154  {
155  user_command = read_int(); // Read the user command
156  if (user_command != 'm')
157  Serial.println(user_command); // Prints the user command to com port
158  Serial.flush();
159  switch (user_command)
160  {
161  case 1:
163  break;
164  case 2:
166  break;
167  case 3:
169  break;
170  case 4:
172  break;
173  case 5:
175  break;
176  case 6:
177  menu_6_sleep();
178  break;
179  case 7:
181  break;
182  default:
183  Serial.println(F("Invalid Option"));
184  break;
185  }
186  Serial.println();
187  if (acknowledge != 0) Serial.println(F("Error: No Acknowledge. Check I2C Address."));
188  Serial.println(F("*************************"));
189  print_prompt();
190  }
191 }
192 
193 
194 // Function Definitions
195 //! Read channels in single-ended mode
196 //! @return 1 if successful, 0 if not
198 {
199  uint8_t user_command;
200  uint16_t adc_command; // The LTC2305 command byte
201  uint16_t adc_code = 0; // The LTC2305 code
202  uint8_t x, y, startcount, endcount;
203  uint16_t display_code;
204  float adc_voltage;
205  uint8_t acknowledge = 0; // I2C acknowledge bit
206 
207  if (part == LTC2305)
208  {
210  {
212  {
213  Serial.println(F(" Read LTC2305 Input in Single-Ended Unipolar mode:"));
214  Serial.println(F(" Note that in Unipolar mode, input voltages less than zero are reported as 0.0V"));
215  }
216  else
217  Serial.println(F(" Read LTC2305 Input in Single-Ended Bipolar mode:"));
218  user_command = print_user_command_single_ended();
219  }
220  else
221  {
223  {
224  Serial.println(F(" Read LTC2305 Input in Differential Unipolar mode:"));
225  Serial.println(F(" Note that in Unipolar mode, input voltages less than zero are reported as 0.0V"));
226  }
227  else
228  Serial.println(F(" Read LTC2305 Input in Differential Bipolar mode:"));
229  }
230  }
231 
232  else
233  {
235  {
236  Serial.println(F(" Read LTC2301 Input in Differential Unipolar mode:"));
237  Serial.println(F(" Note that in Unipolar mode, input voltages less than zero are reported as 0.0V"));
238  }
239  else
240  Serial.println(F(" Read LTC2301 Input in Differential Bipolar mode:"));
241  }
242 
243  if (user_command == 8) //read both channels
244  {
245  startcount = 0;
246  endcount = 1;
247  }
248  else
249  {
250  startcount = user_command;
251  endcount = user_command;
252  }
253 
254  if (part == LTC2305) //read LTC2305
255  {
257  {
258  for (int8_t x = startcount; x <= endcount; x++)
259  {
260  adc_command = BUILD_COMMAND_SINGLE_ENDED[(x+1) % 2] | polarity | uni_bipolar; // Send channel config for the NEXT conversion to take place
261  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code); // Throws out last reading and starts CH0 conversion
262  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code); // Read previous channel conversion (x-1) and start next one (x)
263 
264  display_code = adc_code >> (16 - LTC2305_bits);
265  display_code = display_code & 0xFFF;
266 
267  Serial.print(F(" Received Code: b"));
268  Serial.println(display_code, BIN);
269 
270  adc_voltage = LTC2305_code_to_voltage(adc_code, LTC2305_vref, uni_bipolar);
271  Serial.print(F(" Voltage read on "));
272  Serial.print(F("Ch"));
273  Serial.print(x);
274  Serial.print(F(": "));
275  Serial.print(adc_voltage, 4);
276  Serial.println(F("V"));
277  Serial.println();
278  }
279  }
280 
281  else //differential reading
282  {
283  adc_command = polarity | uni_bipolar; // Send channel config for the NEXT conversion to take place
284  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code); // Throws out last reading and starts CH0 conversion
285  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code); // Read previous channel conversion (x-1) and start next one (x)
286 
287  display_code = adc_code >> (16 - LTC2305_bits);
288  display_code = display_code & 0xFFF;
289 
290  Serial.print(F(" Received Code: b"));
291  Serial.println(display_code, BIN);
292 
293  adc_voltage = LTC2305_code_to_voltage(adc_code, LTC2305_vref, uni_bipolar);
294  Serial.print(F(" Voltage read on "));
295  Serial.print(F("Ch"));
296  Serial.print(x);
297  Serial.print(F(": "));
298  Serial.print(adc_voltage, 4);
299  Serial.println(F("V"));
300  Serial.println();
301  }
302  }
303 
304  else //read LTC2301
305  {
306  adc_command = polarity | uni_bipolar;
307  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code); // Throws out last reading and starts new conversion
308  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, adc_command, &adc_code);
309 
310  display_code = adc_code >> (16 - LTC2305_bits);
311  display_code = display_code & 0xFFF;
312 
313  Serial.print(F(" Received Code: b"));
314  Serial.println(display_code, BIN);
315 
316  adc_voltage = LTC2305_code_to_voltage(adc_code, LTC2305_vref, uni_bipolar);
317  Serial.print(F(" Voltage read on "));
318  Serial.print(F("Ch"));
319  Serial.print(user_command);
320  Serial.print(F(": "));
321  Serial.print(adc_voltage, 4);
322  Serial.println(F("V"));
323  Serial.println();
324  }
325  return(acknowledge);
326 }
327 
328 
329 //! Sets LTC2305 to Single-Ended or Differential
331 {
332  uint8_t user_command;
333 
334  Serial.println(F("\n 0 = LTC2301"));
335  Serial.println(F(" 1 = LTC2305"));
336  Serial.print(F(" Enter a Command: "));
337 
338  user_command = read_int(); // Read user input for uni_bipolar
339  Serial.println(user_command);
340  switch (user_command)
341  {
342  case 0:
343  Serial.println(F(" LTC2301 selected"));
344  part = LTC2301;
345  break;
346  case 1:
347  Serial.println(F(" LTC2305 selected"));
348  part = LTC2305;
349  break;
350  default:
351  {
352  Serial.println(" Invalid Option");
353  return;
354  }
355  break;
356  }
357 }
358 
359 
360 //! Sets LTC230X to Bipolar or Unipolar
362 {
363  uint8_t user_command;
364 
365  Serial.println(F("\n 0 = Unipolar"));
366  Serial.println(F(" 1 = Bipolar"));
367  Serial.print(F(" Enter a Command: "));
368 
369  user_command = read_int(); // Read user input for uni_bipolar
370  Serial.println(user_command);
371  switch (user_command)
372  {
373  case 0:
374  Serial.println(F(" Uniipolar mode selected"));
376  break;
377  case 1:
378  Serial.println(F(" Bipolar mode selected"));
380  break;
381  default:
382  {
383  Serial.println(" Invalid Option");
384  return;
385  }
386  break;
387  }
388 }
389 
390 
391 //! Sets LTC2305 single-ended or differential mode
393 {
394  uint8_t user_command;
395 
396  Serial.println(F("\n 0 = Single-Ended"));
397  Serial.println(F(" 1 = Differential"));
398  Serial.print(F(" Enter a Command: "));
399 
400  user_command = read_int(); // Read user input for uni_bipolar
401  Serial.println(user_command);
402  switch (user_command)
403  {
404  case 0:
405  Serial.println(F(" Single-ended mode selected"));
407 // menu_99_select_channel();
408  break;
409  case 1:
410  Serial.println(F(" Differential mode selected"));
412  break;
413  default:
414  {
415  Serial.println(" Invalid Option");
416  return;
417  }
418  break;
419  }
420 }
421 
422 
423 //! Sets LTC2305 polarity in differential mode
425 {
426  uint8_t user_command;
427 
428  Serial.println(F("\n 0 = 0P-1N"));
429  Serial.println(F(" 1 = 1P-0N"));
430  Serial.print(F(" Enter a Command: "));
431 
432  user_command = read_int(); // Read user input for uni_bipolar
433  Serial.println(user_command);
434  switch (user_command)
435  {
436  case 0:
437  Serial.println(F(" 0P-1N selected"));
439  break;
440  case 1:
441  Serial.println(F(" 1P-0N selected"));
443  break;
444  default:
445  {
446  Serial.println(" Invalid Option");
447  return;
448  }
449  break;
450  }
451 }
452 
453 
454 //! Sets LTC2305 Sleep Mode
455 //! @return 1 if successful, 0 if not
456 int8_t menu_6_sleep()
457 {
458  int8_t acknowledge = 0; // I2C acknowledge bit
459  uint16_t user_command;
460  uint16_t adc_code; // The LTC2305 code
461 
462  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, LTC2305_SLEEP_MODE, &adc_code);
463 
464  Serial.println();
465  Serial.print(F(" ADC Command: b"));
466  Serial.println(LTC2305_SLEEP_MODE, BIN);
467  Serial.println(F(" LTC2305 is now in sleep mode"));
468  Serial.println(F(" Enter RETURN to exit Sleep Mode"));
469 
470  user_command = read_int();
471 
472  acknowledge |= LTC2305_read(LTC2305_I2C_ADDRESS, LTC2305_EXIT_SLEEP_MODE, &adc_code);
473  return(acknowledge);
474 }
475 
476 
477 //! Set the I2C 7 bit address
479 {
480  int16_t user_command;
481  Serial.print(F(" Enter the I2C address of the part in decimal format, from 0 to 127 (default is 8)\n"));
482  user_command = read_int();
483  Serial.print(F(" Address entered: "));
484  Serial.println(user_command);
485  i2c_address = user_command&0x7F;
486 }
487 
488 
489 //! Prints the title block when the program first starts.
491 {
492  Serial.println();
493  Serial.println(F("*****************************************************************"));
494  Serial.println(F("* DC1444A Demonstration Program *"));
495  Serial.println(F("* This program demonstrates how to receive data *"));
496  Serial.println(F("* from the following ADCs: *"));
497  Serial.println(F("* LTC2301 *"));
498  Serial.println(F("* LTC2305 *"));
499  Serial.println(F("* *"));
500  Serial.println(F("* *"));
501  Serial.println(F("* Set the baud rate to 115200 and select the newline terminator.*"));
502  Serial.println(F("* *"));
503  Serial.println(F("*****************************************************************"));
504 }
505 
506 
507 //! Prints main menu.
509 {
510  Serial.println(F("1-Read ADC Input "));
511  Serial.println(F("2-Select LTC2301 / LTC2305 (default is LTC2301)"));
512  Serial.println(F("3-Select Unipolar / Bipolar measurement (default is Unipolar)"));
513  Serial.println(F("4-Select Single-Ended / Differential measurement, LTC2305 only (default is Single-Ended)"));
514  Serial.println(F("5-Select Polarity (default is 0+/1-)"));
515  Serial.println(F("6-Sleep Mode"));
516  Serial.println(F("7-Set I2C address (default is 8)"));
517  Serial.println();
518  Serial.print(F("Enter a command: "));
519 }
520 
521 
522 //! Display selected single-ended channels.
524 {
525  uint8_t user_command;
526  Serial.println(F("*************************"));
527  Serial.println(F(" 0 = CH0"));
528  Serial.println(F(" 1 = CH1"));
529  Serial.println(F(" 2 = ALL"));
530  Serial.println(F(" m = Main Menu"));
531  Serial.println();
532  Serial.print(F(" Enter a Command: "));
533 
534  user_command = read_int(); // Read the single-ended menu command
535  Serial.println(user_command);
536  if (user_command == 'm')
537  return(0);
538 
539  switch (user_command) //check for invalid selection
540  {
541  case 0:
542  Serial.println(F(" CH0 selected"));
543  break;
544  case 1:
545  Serial.println(F(" CH1 selected"));
546  break;
547  case 2:
548  Serial.println(F(" All selected"));
549  break;
550  default:
551  Serial.println(F(" Invalid Option"));
552  break;
553  }
554 
555  return(user_command);
556 }
557 
558 
559 
#define LTC2305_DIFFERENTIAL_MODE
Definition: LTC2305.h:133
unsigned char user_command
static uint8_t adc_command
Definition: DC2071AA.ino:111
static uint8_t print_user_command_single_ended()
Display selected single-ended channels.
Definition: DC1444A.ino:523
Header File for Linduino Libraries and Demo Code.
static void setup()
Initialize Linduino.
Definition: DC1444A.ino:137
static void menu_3_select_uni_bipolar()
Sets LTC230X to Bipolar or Unipolar.
Definition: DC1444A.ino:361
#define LTC2305_CH0
Definition: LTC2305.h:120
static uint8_t uni_bipolar
LTC2305 Unipolar or Bipolar mode selection.
Definition: DC1444A.ino:124
static void menu_2_select_part()
Sets LTC2305 to Single-Ended or Differential.
Definition: DC1444A.ino:330
static void print_prompt()
Prints main menu.
Definition: DC1444A.ino:508
#define LTC2305_SINGLE_ENDED_MODE
Definition: LTC2305.h:132
static uint8_t channel
LTC2305 Channel selection.
Definition: DC1444A.ino:127
static void print_title()
Prints the title block when the program first starts.
Definition: DC1444A.ino:490
static uint8_t demo_board_connected
Set to 1 if the board is connected.
Definition: DC1444A.ino:122
#define LTC2305
Definition: LTC2305.h:138
int8_t LTC2305_read(uint8_t i2c_address, uint8_t adc_command, uint16_t *ptr_adc_code)
Reads 12-bit code from LTC2305, programs channel and mode for next conversion.
Definition: LTC2305.cpp:86
#define LTC2305_I2C_ADDRESS
Definition: LTC2305.h:106
static uint8_t i2c_address
I2C address in 7 bit format for part.
Definition: DC1444A.ino:128
static float adc_voltage
Definition: DC2071AA.ino:115
static void loop()
Repeats Linduino loop.
Definition: DC1444A.ino:148
static uint8_t part
LTC2301 or 2305 part selection.
Definition: DC1444A.ino:125
#define LTC2305_CH1
Definition: LTC2305.h:121
float LTC2305_code_to_voltage(uint16_t adc_code, float vref, uint8_t uni_bipolar)
Calculates the LTC2305 input voltage.
Definition: LTC2305.cpp:103
#define LTC2305_SLEEP_MODE
Definition: LTC2305.h:128
static uint8_t polarity
LTC230X Input polarity selection.
Definition: DC1444A.ino:126
QuikEval EEPROM Library.
static uint8_t single_ended_differential
LTC2305 Single-Ended or Differential mode selection.
Definition: DC1444A.ino:123
static int8_t menu_6_sleep()
Sets LTC2305 Sleep Mode.
Definition: DC1444A.ino:456
static float LTC2305_vref
Definition: DC1444A.ino:130
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
const uint8_t BUILD_COMMAND_SINGLE_ENDED[2]
Builds the command for single-ended mode.
Definition: DC1444A.ino:134
#define LTC2301
Definition: LTC2305.h:137
static void menu_4_select_single_ended_differential()
Sets LTC2305 single-ended or differential mode.
Definition: DC1444A.ino:392
static void menu_5_select_polarity()
Sets LTC2305 polarity in differential mode.
Definition: DC1444A.ino:424
static int8_t menu_1_read_input()
Read channels in single-ended mode.
Definition: DC1444A.ino:197
#define LTC2305_UNIPOLAR_MODE
Definition: LTC2305.h:130
int32_t read_int()
static int32_t display_code
Definition: DC2071AA.ino:114
#define LTC2305_EXIT_SLEEP_MODE
Definition: LTC2305.h:129
#define LTC2305_P0_N1
Definition: LTC2305.h:134
static void menu_7_set_address()
Set the I2C 7 bit address.
Definition: DC1444A.ino:478
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
#define LTC2305_P1_N0
Definition: LTC2305.h:135
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
Definition: LT_I2C.cpp:401
#define LTC2305_BIPOLAR_MODE
Definition: LTC2305.h:131
static uint32_t adc_code
Definition: DC2071AA.ino:113
LTC2305: 8-channel, 12-Bit SAR ADC with I2C interface.
static uint8_t LTC2305_bits
Resolution (12 bits)
Definition: DC1444A.ino:129