| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

DC935A.ino
Go to the documentation of this file.
1 /*!
2 Linear Technology DC935A Demonstration Board
3 LTC2605: Octal 16-/14-/12-Bit Rail-to Rail DACs in 16-Lead SSOP
4
5 @verbatim
6 NOTES
7 Setup:
8 Set the terminal baud rate to 115200 and select the newline terminator.
9 The program displays calculated voltages which are based on the voltage
10 of the reference used, be it internal or external. A precision voltmeter
11 is needed to verify the actual measured voltages against the calculated
12 voltage displayed. If an external reference is used, a precision voltage
13 source is required to apply the external reference voltage. A precision
14 voltmeter is also required to measure the external reference voltage.
15 No external power supply is required. Any assembly option may be used:
16
17 Explanation of Commands:
18 1- Select DAC: Select one of four DACs to test: A, B, C, D.
19
20 2- Write to DAC input register: Value is stored in the DAC for updating
21 later, allowing multiple channels to be updated at once, either
22 through a software "Update All" command or by asserting the LDAC# pin.
23 User will be prompted to enter either a code in hex or decimal, or a
24 voltage. If a voltage is entered, a code will be calculated based on
25 the active scaling and reference parameters - ideal values if no
26 calibration was ever stored.
27
28 3- Write and Update: Similar to item 1, but DAC is updated immediately.
29
30 4- Update DAC: Copies the value from the input register into the DAC
31 Register. Note that a "write and update" command writes the code to
32 BOTH the input register and DAC register, so subsequent "update" commands
33 will simply re-copy the same data (no change in output).
34
35 5- Power Down DAC: Disable DAC output. Power supply current is reduced. DAC
36 code present in DAC registers at time of shutdown are preserved.
37
38 USER INPUT DATA FORMAT:
39 decimal : 1024
40 hex : 0x400
41 octal : 02000 (leading 0 "zero")
42 binary : B10000000000
43 float : 1024.0
44
45 @endverbatim
46 http://ww.linear.com/product/LTC2605
47
48 http://www.linear.com/product/LTC2605#demoboards
49
50
51
52 Copyright 2018(c) Analog Devices, Inc.
53
54 All rights reserved.
55
56 Redistribution and use in source and binary forms, with or without
57 modification, are permitted provided that the following conditions are met:
58 - Redistributions of source code must retain the above copyright
59 notice, this list of conditions and the following disclaimer.
60 - Redistributions in binary form must reproduce the above copyright
61 notice, this list of conditions and the following disclaimer in
62 the documentation and/or other materials provided with the
63 distribution.
64 - Neither the name of Analog Devices, Inc. nor the names of its
65 contributors may be used to endorse or promote products derived
66 from this software without specific prior written permission.
67 - The use of this software may or may not infringe the patent rights
68 of one or more patent holders. This license does not release you
69 from the requirement that you obtain separate licenses from these
70 patent holders to use this software.
71 - Use of the software either in source or binary form, must be run
72 on or directly connected to an Analog Devices Inc. component.
73
74 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
75 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
76 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
77 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
78 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
80 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
81 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
82 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
83 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84
85 */
86 /*! @file
87 @ingroup LTC2605
88 */
89
90 #include <Arduino.h>
91 #include <stdint.h>
92 #include "Linduino.h"
93 #include "LT_SPI.h"
94 #include "UserInterface.h"
95 #include "LT_I2C.h"
96 #include "QuikEval_EEPROM.h"
97 #include "LTC2605.h"
98 #include <Wire.h>
99 #include <SPI.h>
100
101 // DAC Reference State
102 // Could have been 0 or 1. This allows you to use the
103 // variable "reference_mode" as the command argument to a write
104 #define REF_INTERNAL LTC2605_CMD_INTERNAL_REFERENCE //!< Stored reference state is Internal
105 #define REF_EXTERNAL LTC2605_CMD_EXTERNAL_REFERENCE //!< Stored reference state is External
106
107 //Function Declaration
108 void print_title(); // Print the title block
109 void print_prompt(int16_t selected_dac); // Prompt the user for an input command
110 int16_t prompt_voltage_or_code();
111 uint16_t get_voltage(float LTC2605_lsb, int16_t LTC2605_offset);
112 uint16_t get_code();
113 int8_t calibrate_dac(uint8_t index); // Calibrate the selected DAC using a voltmeter. The routine does a linear curve fit given two data points.
114
115 int8_t menu_1_select_dac(int16_t *selected_dac);
116 int8_t menu_2_write_to_input_register(int16_t selected_dac);
117 int8_t menu_3_write_and_update_dac(int16_t selected_dac);
118 int8_t menu_4_update_power_up_dac(int16_t selected_dac);
119 int8_t menu_5_power_down_dac(int16_t selected_dac);
120
121 // Global variables
122 static uint8_t demo_board_connected; //!< Set to 1 if the board is connected
123 static uint8_t shift_count = 0; //!< The data align shift count. For 16-bit=0, for 14 bits=2 for 12-bits=4
124 static uint8_t reference_mode = REF_INTERNAL; //!< Tells whether to set internal or external reference
125
126 // Global calibration variables
127 static float reference_voltage= 5; //!< Reference voltage, either internal or external
128 static int16_t LTC2605_offset = 0; //!< DAC offset
129 static float LTC2605_lsb = (reference_voltage / (pow(2,16) -1)); // least signifigant bit value of the dac
130
131 // Constants
132
133 //! Lookup table for DAC address. Allows the "All DACs" address to be indexed right after DAC D in loops.
134 //! This technique is very useful for devices with non-monotonic channel addresses.
135 const uint8_t address_map[9] = {LTC2605_DAC_A, LTC2605_DAC_B, LTC2605_DAC_C, LTC2605_DAC_D, LTC2605_DAC_E, LTC2605_DAC_F , LTC2605_DAC_G, LTC2605_DAC_H, LTC2605_DAC_ALL}; //!< Map entered option 0..2 to DAC address
136
137 //! Used to keep track to print voltage or print code
138 enum
139 {
140 PROMPT_VOLTAGE = 0, /**< 0 */
141 PROMPT_CODE = 1 /**< 1 */
142 };
143
144 //! Initialize Linduino
145 void setup()
146 // Setup the program
147 {
148 quikeval_I2C_init(); // Configure the EEPROM I2C port for 100khz
149 quikeval_I2C_connect(); // Connect I2C to main data port
150 Serial.begin(115200);
151 print_title();
152 //if(demo_board_connected)
153 demo_board_connected = true;
154 print_prompt(0);
155 LTC2605_write(LTC2605_I2C_ADDRESS, LTC2605_CMD_NO_OPERATION, LTC2605_DAC_ALL, 0x0000);
156 Serial.flush();
157 }
158
159 //! Repeats Linduino loop
160 void loop()
161 {
162 int8_t ack =0;
163 int16_t user_command;
164 static int16_t selected_dac =0; // The selected DAC to be updated (0=A, 1=B ... 9=All). Initialized to "A"
165 // the main control loop
166 if (demo_board_connected) // do nothing if board is not connected
167 {
168 if (Serial.available()) // check for user input
169 {
170 user_command = read_int();
171 Serial.println(user_command);
172 Serial.flush();
173 ack =0;
174 switch (user_command)
175 {
176 case 1:
177 ack |= menu_1_select_dac(&selected_dac);
178 break;
179 case 2:
180 ack |= menu_2_write_to_input_register(selected_dac);
181 break;
182 case 3:
183 ack |= menu_3_write_and_update_dac(selected_dac);
184 break;
185 case 4:
186 ack |= menu_4_update_power_up_dac(selected_dac);
187 break;
188 case 5:
189 ack |= menu_5_power_down_dac(selected_dac);
190 break;
191 default:
192 Serial.println("Incorrect Option");
193 break;
194 }
195 if (ack)Serial.println("I2C NACK received, check address\n");
196 Serial.println("\n*****************************************************************");
197 print_prompt(selected_dac);
198 }
199 }
200 }
201
202 // Function Definitions
203
204 //! Select which DAC to operate on
205 //! @return 0
206 int8_t menu_1_select_dac(int16_t *selected_dac) //!< what DAC to operate on
207 {
208 // Select a DAC to operate on
209 Serial.print("Select DAC to operate on (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, 7=H, 8=All)");
210 *selected_dac = read_int();
211 if (*selected_dac == 8)
212 Serial.println("All");
213 else
214 Serial.println(*selected_dac);
215 return(0);
216 }
217
218 //! Write data to input register, but do not update DAC output
219 //! @return ACK bit (0=acknowledge, 1=no acknowledge)
220 int8_t menu_2_write_to_input_register(int16_t selected_dac) //!< what DAC to operate on
221 {
222 int8_t ack=0;
223 uint16_t dac_code;
224
225 if (prompt_voltage_or_code() == PROMPT_VOLTAGE)
226 dac_code = get_voltage(LTC2605_lsb, LTC2605_offset);
227 else
228 dac_code = get_code();
229
230 Serial.print("DAC code transmitted: 0x");
231 Serial.println(dac_code,HEX);
232
233 ack |= LTC2605_write(LTC2605_I2C_ADDRESS, LTC2605_CMD_WRITE, address_map[selected_dac], dac_code << shift_count);
234 return (ack);
235 }
236 //!Write data to DAC register (which updates output immediately)
237 //! @return ACK bit (0=acknowledge, 1=no acknowledge)
238 int8_t menu_3_write_and_update_dac(int16_t selected_dac) //!< what DAC to operate on
239 {
240 int8_t ack=0;
241 uint16_t dac_code;
242
243 if (prompt_voltage_or_code() == PROMPT_VOLTAGE)
244 dac_code = get_voltage(LTC2605_lsb, LTC2605_offset);
245 else
246 dac_code = get_code();
247 //Serial.print("lsb:");
248 //Serial.println(LTC2605_lsb);
249 Serial.print("DAC code transmitted: 0x");
250 Serial.println(dac_code,HEX);
251
252
253 ack |= LTC2605_write(LTC2605_I2C_ADDRESS, LTC2605_CMD_WRITE_UPDATE, address_map[selected_dac], dac_code << shift_count);
254 return (ack);
255 }
256
257 //! Update DAC with data that is stored in input register, power up if sleeping
258 //! @return ACK bit (0=acknowledge, 1=no acknowledge)
259 int8_t menu_4_update_power_up_dac(int16_t selected_dac) //!< what DAC to operate on
260 {
261 // Update DAC
262 int8_t ack=0;
263 ack |= LTC2605_write(LTC2605_I2C_ADDRESS, LTC2605_CMD_UPDATE, address_map[selected_dac], 0x0000);
264 return (ack);
265 }
266
267 //! Power down DAC
268 //! @return ACK bit (0=acknowledge, 1=no acknowledge)
269 int8_t menu_5_power_down_dac(int16_t selected_dac) //!< what DAC to operate on
270 {
271 // Power down DAC
272 int8_t ack=0;
273 ack |= LTC2605_write(LTC2605_I2C_ADDRESS, LTC2605_CMD_POWER_DOWN, address_map[selected_dac], 0x0000);
274 return (ack);
275 }
276
277
278 //! Prompt user to enter a voltage or digital code to send to DAC
279 //! @return prompt type
280 int16_t prompt_voltage_or_code()
281 {
282 int16_t user_input;
283 Serial.print(F("Type 1 to enter voltage, 2 to enter code:"));
284 Serial.flush();
285 user_input = read_int();
286 Serial.println(user_input);
287
288 if (user_input != 2)
289 return(PROMPT_VOLTAGE);
290 else
291 return(PROMPT_CODE);
292 }
293 //! Get voltage from user input, calculate DAC code based on lsb, offset
294 //! @return voltage
295 uint16_t get_voltage(float LTC2605_lsb, //!< the voltage LSB of the DAC
296 int16_t LTC2605_offset) //!< the Offset of the DAC
297 {
298 float dac_voltage;
299
300 Serial.print(F("Enter Desired DAC output voltage: "));
301 dac_voltage = read_float();
302 Serial.print(dac_voltage);
303 Serial.println(" V");
304 Serial.flush();
305 return(LTC2605_voltage_to_code(dac_voltage, LTC2605_lsb, LTC2605_offset));
306 }
307
308 //! Get code to send to DAC directly, in decimal, hex, or binary
309 //! @return DAC code
310 uint16_t get_code()
311 {
312 uint16_t returncode;
313 Serial.println("Enter Desired DAC Code");
314 Serial.print("(Format 32768, 0x8000, 0100000, or B1000000000000000): ");
315 returncode = (uint16_t) read_int();
316 Serial.print("0x");
317 Serial.println(returncode, HEX);
318 Serial.print("code corresponding to voltage: ");
319 Serial.print(LTC2605_code_to_voltage( returncode, LTC2605_lsb,LTC2605_offset));
320 Serial.println(" V");
321 Serial.flush();
322 return(returncode);
323 }
324
325 //! Prints the title block when program first starts.
326 void print_title()
327 {
328 Serial.println("");
329 Serial.println(F("*****************************************************************"));
330 Serial.println(F("* DC935 Demonstration Program *"));
331 Serial.println(F("* *"));
332 Serial.println(F("* This program demonstrates how to send data to the LTC2605 *"));
333 Serial.println(F("* octo 16/14/12-bit DAC found on the DC935 demo board. *"));
334 Serial.println(F("* *"));
335 Serial.println(F("* Set the baud rate to 115200 and select the newline terminator.*"));
336 Serial.println(F("* *"));
337 Serial.println(F("*****************************************************************"));
338 }
339
340 //! Prints main menu.
341 void print_prompt(int16_t selected_dac) //!< what DAC to operate on
342 {
343 Serial.println(F("\nCommand Summary:"));
344 Serial.println(F(" 1-Select DAC"));
345 Serial.println(F(" 2-Write to input register (no update)"));
346 Serial.println(F(" 3-Write and update DAC"));
347 Serial.println(F(" 4-Update / power up DAC"));
348 Serial.println(F(" 5-Power down DAC"));
349
350
351 Serial.println("\nPresent Values:");
352 Serial.print(" Selected DAC: ");
353 if (selected_dac != 4)
354 Serial.println((char) (selected_dac + 0x41));
355 else
356 Serial.println("All");
357 //Serial.print(" DAC Code: 0x");
358 // Serial.println(code[selected_dac], HEX);
359 // Serial.print(" Calculated DAC Output Voltage = ");
360 // Serial.print(voltage, 4);
361 // Serial.println(" V");
362 Serial.print(" DAC Reference: ");
363 if (reference_mode == REF_INTERNAL)
364 Serial.println("Internal");
365 else
366 {
367 Serial.print(F("External "));
368 Serial.print(reference_voltage, 5);
369 Serial.println(F("V reference, please verify"));
370 Serial.print(F("Enter a command:"));
371
372 }
373 Serial.flush();
374 }
demo_board_connected
static uint8_t demo_board_connected
Set to 1 if the board is connected.
Definition: DC935A.ino:122
LTC2605_DAC_G
#define LTC2605_DAC_G
Definition: LTC2605.h:213
LTC2605_CMD_NO_OPERATION
#define LTC2605_CMD_NO_OPERATION
No operation.
Definition: LTC2605.h:187
LTC2605_CMD_UPDATE
#define LTC2605_CMD_UPDATE
Update (power up) DAC register n.
Definition: LTC2605.h:181
user_command
unsigned char user_command
Definition: Serial_EEPROM_Test.ino:75
LTC2605_offset
static int16_t LTC2605_offset
DAC offset.
Definition: DC935A.ino:128
LTC2605_DAC_H
#define LTC2605_DAC_H
Definition: LTC2605.h:214
LTC2605_CMD_POWER_DOWN
#define LTC2605_CMD_POWER_DOWN
Power down n.
Definition: LTC2605.h:183
menu_2_write_to_input_register
static int8_t menu_2_write_to_input_register(int16_t selected_dac)
Write data to input register, but do not update DAC output.
Definition: DC935A.ino:220
address_map
const uint8_t address_map[9]
Lookup table for DAC address.
Definition: DC935A.ino:135
PROMPT_CODE
1
Definition: DC935A.ino:141
Linduino.h
Header File for Linduino Libraries and Demo Code.
LTC2605_DAC_C
#define LTC2605_DAC_C
Definition: LTC2605.h:209
LTC2605_I2C_ADDRESS
#define LTC2605_I2C_ADDRESS
Definition: LTC2605.h:169
shift_count
static uint8_t shift_count
The data align shift count.
Definition: DC935A.ino:123
LTC2605_DAC_ALL
#define LTC2605_DAC_ALL
Definition: LTC2605.h:215
LTC2605_voltage_to_code
uint16_t LTC2605_voltage_to_code(float dac_voltage, float LTC2605_lsb, int16_t LTC2605_offset)
Calculate a LTC2605 DAC code given the desired output voltage.
Definition: LTC2605.cpp:85
setup
static void setup()
Initialize Linduino.
Definition: DC935A.ino:145
LTC2605_CMD_WRITE_UPDATE
#define LTC2605_CMD_WRITE_UPDATE
Write to input register n, update (power up) all.
Definition: LTC2605.h:182
menu_3_write_and_update_dac
static int8_t menu_3_write_and_update_dac(int16_t selected_dac)
Write data to DAC register (which updates output immediately)
Definition: DC935A.ino:238
print_title
static void print_title()
Prints the title block when program first starts.
Definition: DC935A.ino:326
LTC2605_DAC_F
#define LTC2605_DAC_F
Definition: LTC2605.h:212
QuikEval_EEPROM.h
QuikEval EEPROM Library.
PROMPT_VOLTAGE
0
Definition: DC935A.ino:140
reference_voltage
static float reference_voltage
Reference voltage, either internal or external.
Definition: DC935A.ino:127
LTC2605_DAC_A
#define LTC2605_DAC_A
Definition: LTC2605.h:207
get_voltage
static uint16_t get_voltage(float LTC2605_lsb, int16_t LTC2605_offset)
Get voltage from user input, calculate DAC code based on lsb, offset.
Definition: DC935A.ino:295
LT_SPI.h
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LTC2605_lsb
static float LTC2605_lsb
Definition: DC935A.ino:129
LTC2605_DAC_B
#define LTC2605_DAC_B
Definition: LTC2605.h:208
loop
static void loop()
Repeats Linduino loop.
Definition: DC935A.ino:160
LTC2605_DAC_E
#define LTC2605_DAC_E
Definition: LTC2605.h:211
LT_I2C.h
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
LTC2605_DAC_D
#define LTC2605_DAC_D
Definition: LTC2605.h:210
LTC2605_CMD_WRITE
#define LTC2605_CMD_WRITE
Write to input register n.
Definition: LTC2605.h:180
menu_5_power_down_dac
static int8_t menu_5_power_down_dac(int16_t selected_dac)
Power down DAC.
Definition: DC935A.ino:269
calibrate_dac
static int8_t calibrate_dac(uint8_t index)
UserInterface.h
read_int
int32_t read_int()
Definition: UserInterface.cpp:93
index
static int index
Definition: device_logger.ino:91
LTC2605_write
int8_t LTC2605_write(uint8_t i2c_address, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
Write a 16-bit dac_code to the LTC2605.
Definition: LTC2605.cpp:76
read_float
float read_float()
Definition: UserInterface.cpp:78
LTC2605_code_to_voltage
float LTC2605_code_to_voltage(uint16_t dac_code, float LTC2605_lsb, int16_t LTC2605_offset)
Calculate the LTC2605 DAC output voltage given the DAC code, offset, and LSB value.
Definition: LTC2605.cpp:98
get_code
static uint16_t get_code()
Get code to send to DAC directly, in decimal, hex, or binary.
Definition: DC935A.ino:310
REF_INTERNAL
#define REF_INTERNAL
Stored reference state is Internal.
Definition: DC935A.ino:104
menu_4_update_power_up_dac
static int8_t menu_4_update_power_up_dac(int16_t selected_dac)
Update DAC with data that is stored in input register, power up if sleeping.
Definition: DC935A.ino:259
print_prompt
static void print_prompt(int16_t selected_dac)
Prints main menu.
Definition: DC935A.ino:341
reference_mode
static uint8_t reference_mode
Tells whether to set internal or external reference.
Definition: DC935A.ino:124
quikeval_I2C_init
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
menu_1_select_dac
static int8_t menu_1_select_dac(int16_t *selected_dac)
Select which DAC to operate on.
Definition: DC935A.ino:206
quikeval_I2C_connect
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
Definition: LT_I2C.cpp:401
prompt_voltage_or_code
static int16_t prompt_voltage_or_code()
Prompt user to enter a voltage or digital code to send to DAC.
Definition: DC935A.ino:280
LTC2605.h
LTC2605: Octal 16-/14-/12-Bit Rail-to Rail DACs in 16-Lead SSOP.