Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
DC1096B.ino
Go to the documentation of this file.
1 /*!
2 Linear Technology DC1096B Demonstration Board.
3 LTC2642: 16-/14-/12-Bit VOUT DAC with SPI Interface
4 
5 LTC2641: 16-/14-/12-Bit VOUT DAC with SPI Interface
6 @verbatim
7 
8 NOTES
9 
10  Set the terminal baud rate to 115200 and select the newline terminator.
11  No external power supply is required. Two on-board reference voltages of
12  2.5V and 5V are available. The board features LTC2642A with direct and
13  buffered output.
14 
15  The program displays calculated voltages which are based on the reference
16  voltage used. A precision voltmeter is needed to verify the actual
17  measured voltages against the calculated voltage displayed.
18 
19  There is also an option of generating a square wave of required voltage
20  levels and frequency. This proves the fast settling time of the corresponding
21  parts. Setting the software to unipolar or bipolar will not set the hardware
22  to bipolar mode. The jumpers need to be placed in their proper positions in
23  order to accomplish this.
24 
25  Jumper settings:
26  JP3: Sets DAC2 to either unipolar or bipolar mode. In bipolar mode, set JP6
27  and JP7 to EXT and apply an appropriate supply to AMP V+, AMP V–, and GND
28  turret posts.
29 
30  JP4: Sets DAC3 to either unipolar or bipolar mode. In bipolar mode, set JP6
31  and JP7 to EXT and apply an appropriate supply to AMP V+, AMP V–, and GND
32  turret posts.
33 
34  Explanation of Commands:
35  Option 1: Enter a digital value or voltage to obtain analog voltage output.
36  Option 2: Enter the voltage levels to obtain a square wave.
37  Option 3: Vary the reference voltage used.
38  Option 4: Select Bipolar/Unipolar.
39 
40 USER INPUT DATA FORMAT:
41  decimal : 1024
42  hex : 0x400
43  octal : 02000 (leading 0 "zero")
44  binary : B10000000000
45  float : 1024.0
46 
47 @endverbatim
48 
49 http://www.linear.com/product/LTC2642
50 http://www.linear.com/product/LTC2641
51 
52 http://www.linear.com/product/LTC2642#demoboards
53 http://www.linear.com/product/LTC2641#demoboards
54 
55 
56 Copyright 2018(c) Analog Devices, Inc.
57 
58 All rights reserved.
59 
60 Redistribution and use in source and binary forms, with or without
61 modification, are permitted provided that the following conditions are met:
62  - Redistributions of source code must retain the above copyright
63  notice, this list of conditions and the following disclaimer.
64  - Redistributions in binary form must reproduce the above copyright
65  notice, this list of conditions and the following disclaimer in
66  the documentation and/or other materials provided with the
67  distribution.
68  - Neither the name of Analog Devices, Inc. nor the names of its
69  contributors may be used to endorse or promote products derived
70  from this software without specific prior written permission.
71  - The use of this software may or may not infringe the patent rights
72  of one or more patent holders. This license does not release you
73  from the requirement that you obtain separate licenses from these
74  patent holders to use this software.
75  - Use of the software either in source or binary form, must be run
76  on or directly connected to an Analog Devices Inc. component.
77 
78 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
79 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
80 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
81 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
82 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
84 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
85 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
86 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 */
89 
90 /*! @file
91  @ingroup LTC2642
92 */
93 
94 // Headerfiles
95 #include <Arduino.h>
96 #include <stdint.h>
97 #include <SPI.h>
98 #include "LT_SPI.h"
99 #include "LT_I2C.h"
100 #include "Linduino.h"
101 #include "QuikEval_EEPROM.h"
102 #include "UserInterface.h"
103 #include "LTC2642.h"
104 
105 // Macros
106 #define VREF1 5.0
107 #define VREF2 2.5
108 
109 // Global Variables
110 float reference_voltage = VREF1; // Set the reference voltage
111 uint8_t range = UNIPOLAR; // set to unipolar
112 
113 // Function Declarations
114 void print_title();
115 void print_prompt();
116 uint8_t menu1_voltage_output();
117 uint8_t menu2_square_wave_output();
119 void menu4_select_range();
120 
121 //! Used to choose between voltage and code
122 enum
123 {
124  PROMPT_VOLTAGE = 0, /**< 0 */
125  PROMPT_CODE = 1 /**< 1 */
126 };
127 
128 //! Initialize Linduino
129 void setup()
130 {
131  uint8_t demo_board_connected;
132 
133  char demo_name[] = "DC1096"; // Demo Board Name stored in QuikEval EEPROM
134  output_high(LTC2642_CS); //! Pulls LTC2642 Chip Select (LD pin) High
135 
136  quikeval_I2C_init(); //! Initializes Linduino I2C port.
137 
138  quikeval_SPI_init(); //! Configures the SPI port for 4MHz SCK
139  quikeval_SPI_connect(); //! Connects SPI to QuikEval port
140 
141  Serial.begin(115200); //! Initializes the serial port to the PC
142  print_title(); //! Displays the title
143 
144  demo_board_connected = discover_demo_board(demo_name); //! Checks if correct demo board is connected.
145  if (demo_board_connected) //! Prints the prompt if the correct demo board is connected
146  {
147  print_prompt(); // Prints prompt and indicates that "A" is selected.
148  }
149 }
150 
151 //! Repeats Linduino loop
152 void loop()
153 {
154  int8_t user_command; // The user input command
155  uint8_t acknowledge = 0;
156  if (Serial.available()) // Check for user input
157  {
158  user_command = read_int(); // Read the user command
159  if (user_command != 'm')
160  Serial.println(user_command); // Prints the user command to com port
161  Serial.flush();
162  switch (user_command)
163  {
164  case 1:
165  acknowledge |= menu1_voltage_output();
166  break;
167  case 2:
168  acknowledge |= menu2_square_wave_output();
169  break;
170  case 3:
172  break;
173  case 4:
175  break;
176  default:
177  Serial.println(F("Incorrect Option"));
178  }
179  if (acknowledge)
180  Serial.println(F("***** I2C ERROR *****"));
181  print_prompt();
182  }
183 }
184 
185 //! Function to enter a digital value and get the analog output
187 {
188  uint16_t dac_code;
189  float voltage;
191  dac_code = get_voltage();
192  else
193  dac_code = get_code();
194 
195  LTC2642_write(QUIKEVAL_CS, dac_code);
196  voltage = LTC2642_code_to_voltage(dac_code, reference_voltage, range);
197  Serial.print("\nDac Code = 0x");
198  Serial.print(dac_code, HEX);
199  Serial.print("\nOutput voltage = ");
200  Serial.print(voltage, 3);
201  Serial.print(" V");
202  return 0;
203 }
204 
205 //! Function to generate a square wave of desired frequency and voltage ranges
207 {
208  float voltage_high, voltage_low;
209  uint16_t code_high, code_low;
210  //uint8_t receive_enter; // To receive enter key pressed
211 
212  Serial.print("\nEnter voltage_high: ");
213  while (!Serial.available());
214  voltage_high = read_float();
215  Serial.print(voltage_high);
216  Serial.println(" V");
217 
218  Serial.print("Enter voltage_low: ");
219  while (!Serial.available());
220  voltage_low = read_float();
221  Serial.print(voltage_low);
222  Serial.println(" V");
223 
224  //! Converting data into voltage
225  code_high = LTC2642_voltage_to_code(voltage_high, reference_voltage, range);
226  code_low = LTC2642_voltage_to_code(voltage_low, reference_voltage, range);
227 
228  Serial.print(F("Enter any character to stop. "));
229  while (!Serial.available()) //! Generate square wave until a key is pressed
230  {
231  LTC2642_write(LTC2642_CS, code_high);
232  delay(30); // 30 ms delay
233  LTC2642_write(LTC2642_CS, code_low);
234  delay(30); // 30 ms delay
235  }
236  read_int();
237  return 0;
238 }
239 
240 //! Function to change the reference voltage to be used
242 {
243  Serial.print(F("\nEnter the reference voltage: "));
245  Serial.print(reference_voltage);
246  Serial.print(F(" V"));
247 }
248 
249 //! Select between unipolar/bipolar
251 {
252  uint8_t choice;
253  Serial.print("\nEnter your choice (1 - Unipolar, 2 - Bipolar): ");
254  choice = read_int();
255  Serial.print(choice);
256 
257  if (choice == 1)
258  {
259  range = UNIPOLAR;
260  Serial.print(F("\nSet jumpers JP3 and JP4 to UNI."));
261  }
262  else
263  {
264  range = BIPOLAR;
265  Serial.print(F("\nSet jumpers JP3 and JP4 to BIP."));
266  Serial.print(F("\nIn bipolar mode, set JP6 and JP7 to EXT and apply an appropriate supply"));
267  Serial.print(F("\nto AMP V+, AMP V–, and GND turret posts."));
268  }
269 }
270 
271 //! Prompt user to enter a voltage or digital code to send to DAC
273 {
274  int16_t user_input;
275  Serial.print(F("\nType 1 to enter voltage, 2 to enter code:"));
276  Serial.flush();
277  user_input = read_int();
278  Serial.println(user_input);
279 
280  if (user_input != 2)
281  return(PROMPT_VOLTAGE);
282  else
283  return(PROMPT_CODE);
284 }
285 
286 //! Get voltage from user input, calculate DAC code based on lsb, offset
287 uint16_t get_voltage()
288 {
289  float dac_voltage;
290 
291  Serial.print(F("Enter Desired DAC output voltage: "));
292  dac_voltage = read_float();
293  Serial.print(dac_voltage, 3);
294  Serial.println(" V");
295  Serial.flush();
296  return(LTC2642_voltage_to_code(dac_voltage, reference_voltage, range));
297 }
298 
299 //! Get code to send to DAC directly, in decimal, hex, or binary
300 uint16_t get_code()
301 {
302  uint16_t returncode;
303  Serial.println("Enter Desired DAC Code");
304  Serial.print("(Format 32768, 0x8000, 0100000, or B1000000000000000): ");
305  returncode = (uint16_t) read_int();
306  Serial.print("0x");
307  Serial.println(returncode, HEX);
308  Serial.flush();
309  return(returncode);
310 }
311 
312 
313 //! Prints the title block when program first starts.
315 {
316  Serial.print(F("\n*****************************************************************\n"));
317  Serial.print(F("* DC1096B Demonstration Program *\n"));
318  Serial.print(F("* *\n"));
319  Serial.print(F("* This program demonstrates how to send data to the 16-bit DAC. *\n"));
320  Serial.print(F("* Set the baud rate to 115200 and select the newline terminator.*\n"));
321  Serial.print(F("* *\n"));
322  Serial.print(F("*****************************************************************\n"));
323 }
324 
325 //! Prints main menu.
327 {
328  Serial.print(F("\n*****************************************************************\n"));
329  Serial.print(F("\nSelected Range: "));
330  if (range == UNIPOLAR)
331  Serial.print(F("Unipolar"));
332  else
333  Serial.print(F("Bipolar"));
334  Serial.print(F("\nSelected Reference Voltage: "));
335  Serial.print(reference_voltage);
336  Serial.print(F(" V"));
337 
338  Serial.print(F("\n\nMenu:"));
339  Serial.print(F("\n 1 - Set output\n"));
340  Serial.print(F(" 2 - Square Wave output\n"));
341  Serial.print(F(" 3 - Set Reference Voltage\n"));
342  Serial.print(F(" 4 - Select Unipolar/Bipolar\n"));
343  Serial.print(F("Enter a command: "));
344 }
static uint8_t menu1_voltage_output()
Function to enter a digital value and get the analog output.
Definition: DC1096B.ino:186
static void menu4_select_range()
Select between unipolar/bipolar.
Definition: DC1096B.ino:250
static uint16_t get_voltage()
Get voltage from user input, calculate DAC code based on lsb, offset.
Definition: DC1096B.ino:287
unsigned char user_command
static void print_prompt()
Prints main menu.
Definition: DC1096B.ino:326
static int16_t prompt_voltage_or_code()
Prompt user to enter a voltage or digital code to send to DAC.
Definition: DC1096B.ino:272
#define output_high(pin)
Set "pin" high.
Definition: Linduino.h:75
Header File for Linduino Libraries and Demo Code.
#define UNIPOLAR
Definition: LTC2642.h:77
#define VREF1
Definition: DC1096B.ino:106
static float reference_voltage
Definition: DC1096B.ino:110
static void print_title()
Prints the title block when program first starts.
Definition: DC1096B.ino:314
#define BIPOLAR
Definition: LTC2642.h:78
static void setup()
Initialize Linduino.
Definition: DC1096B.ino:129
static uint16_t get_code()
Get code to send to DAC directly, in decimal, hex, or binary.
Definition: DC1096B.ino:300
static uint8_t menu2_square_wave_output()
Function to generate a square wave of desired frequency and voltage ranges.
Definition: DC1096B.ino:206
uint16_t LTC2642_voltage_to_code(float voltage, float reference_voltage, uint8_t range)
Calculates the 16 bit data code from voltage.
Definition: LTC2642.cpp:92
QuikEval EEPROM Library.
static uint8_t range
Definition: DC1096B.ino:111
static void menu3_change_reference_voltage()
Function to change the reference voltage to be used.
Definition: DC1096B.ino:241
#define LTC2642_CS
Define the SPI CS pin.
Definition: LTC2642.h:74
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.
static void loop()
Repeats Linduino loop.
Definition: DC1096B.ino:152
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
float LTC2642_code_to_voltage(uint16_t code, float reference_voltage, uint8_t range)
Calculates the output voltage from the given digital code and reference voltage.
Definition: LTC2642.cpp:107
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
Definition: DC1880A.ino:97
LTC2642: 16-/14-/12-Bit VOUT DAC with SPI Interface LTC2641: 16-/14-/12-Bit VOUT DAC with SPI Interfa...
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()
float read_float()
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
static float voltage
Definition: DC2289AA.ino:71
#define QUIKEVAL_CS
QuikEval CS pin (SPI chip select on QuikEval connector pin 6) connects to Arduino SS pin...
Definition: Linduino.h:57
void LTC2642_write(uint8_t cs, uint16_t data)
Writes the 16-bit data into the DAC.
Definition: LTC2642.cpp:85
static uint8_t demo_board_connected
Set to 1 if the board is connected.