Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2664.cpp
Go to the documentation of this file.
1 /*!
2 LTC2664: 4-Channel SPI 16-/12-Bit +/-10V Vout SoftSpan DACs with 10ppm/C Max Reference
3 
4 @verbatim
5 
6 The LTC2664 is a family of 4-channel, 12/16-bit +/-10V digital-to-analog converters with integrated
7 precision references. They are guaranteed monotonic and have built-in rail-to-rail output buffers.
8 These SoftSpan DACs offer five output ranges up to +/-10V. The range of each channel is independently
9 programmable, or the part can be hardware-configured for operation in a fixed range.
10 
11 @endverbatim
12 
13 http://www.linear.com/product/LTC2664
14 
15 http://www.linear.com/product/LTC2664#demoboards
16 
17 
18 Copyright 2018(c) Analog Devices, Inc.
19 
20 All rights reserved.
21 
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24  - Redistributions of source code must retain the above copyright
25  notice, this list of conditions and the following disclaimer.
26  - Redistributions in binary form must reproduce the above copyright
27  notice, this list of conditions and the following disclaimer in
28  the documentation and/or other materials provided with the
29  distribution.
30  - Neither the name of Analog Devices, Inc. nor the names of its
31  contributors may be used to endorse or promote products derived
32  from this software without specific prior written permission.
33  - The use of this software may or may not infringe the patent rights
34  of one or more patent holders. This license does not release you
35  from the requirement that you obtain separate licenses from these
36  patent holders to use this software.
37  - Use of the software either in source or binary form, must be run
38  on or directly connected to an Analog Devices Inc. component.
39 
40 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
41 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
42 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
44 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
46 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 */
51 
52 //! @ingroup Digital_to_Analog_Converters
53 //! @{
54 //! @defgroup LTC2664 LTC2664 4-Channel SPI 16-/12-Bit +/-10V Vout SoftSpan DACs with 10ppm/C Max Reference
55 //! @}
56 
57 /*! @file
58  @ingroup LTC2664
59  Library for LTC2664 4-Channel SPI 16-/12-Bit +/-10V Vout SoftSpan DACs with 10ppm/C Max Reference
60 */
61 
62 #include <stdint.h>
63 #include <math.h>
64 #include <Arduino.h>
65 #include "Linduino.h"
66 #include "LT_SPI.h"
67 #include "LTC2664.h"
68 #include <SPI.h>
69 
70 int8_t LTC2664_write(uint8_t cs, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
71 // Write the 16-bit dac_code to the LTC2664
72 {
73  static uint8_t last_data_array[4];
74  uint8_t data_array[4], rx_array[4];
75  int8_t ret;
77 
78  data.LT_int16 = dac_code; // Copy DAC code to union
79  data_array[3] = 0; // Only required for 32 byte readback transaction
80  data_array[2] = dac_command | dac_address; // Build command / address byte
81  data_array[1] = data.LT_byte[1]; // MS Byte
82  data_array[0] = data.LT_byte[0]; // LS Byte
83 
84  // Serial.println(F("\nData Transmitted: "));
85  // Serial.println(data_array[3], BIN);
86  // Serial.println(data_array[2], BIN);
87  // Serial.println(data_array[1], BIN);
88  // Serial.println(data_array[0], BIN);
89 
90  spi_transfer_block(cs, data_array, rx_array, (uint8_t) 4);
91 
92  // Compare data read back to data that was sent the previous time this function was called
93  if ((rx_array[2] == last_data_array[2]) && (rx_array[1] == last_data_array[1]) && (rx_array[0] == last_data_array[0]))
94  {
95  ret = 0;
96  }
97  else
98  {
99  ret = 1;
100  }
101 
102  last_data_array[0] = data_array[0]; // Copy data array to a static array to compare
103  last_data_array[1] = data_array[1]; // the next time the function is called
104  last_data_array[2] = data_array[2];
105 
106  return(ret);
107 }
108 
109 uint16_t LTC2664_voltage_to_code(float dac_voltage, float min_output, float max_output)
110 // Calculate a LTC2664 DAC code given the desired output voltage and the minimum / maximum
111 // outputs for a given softspan range.
112 {
113  uint16_t dac_code;
114  float float_code;
115  float_code = 65535.0 * (dac_voltage - min_output) / (max_output - min_output); // Calculate the DAC code
116  float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); // Round
117  if (float_code < 0.0) float_code = 0.0;
118  if (float_code > 65535.0) float_code = 65535.0;
119  dac_code = (uint16_t) (float_code); // Convert to unsigned integer
120  return (dac_code);
121 }
122 
123 float LTC2664_code_to_voltage(uint16_t dac_code, float min_output, float max_output)
124 // Calculate the LTC2664 DAC output voltage given the DAC code and and the minimum / maximum
125 // outputs for a given softspan range.
126 {
127  float dac_voltage;
128  dac_voltage = (((float) dac_code / 65535.0) * (max_output - min_output)) + min_output; // Calculate the dac_voltage
129  return (dac_voltage);
130 }
long ret
Header File for Linduino Libraries and Demo Code.
int8_t LTC2664_write(uint8_t cs, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
Write the 16-bit dac_code to the LTC2664.
Definition: LTC2664.cpp:70
LTC2664: 4-Channel SPI 16-/12-Bit +/-10V Vout SoftSpan DACs with 10ppm/C Max Reference.
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
int16_t LT_int16
16-bit signed integer to be converted to two bytes
Definition: Linduino.h:101
float LTC2664_code_to_voltage(uint16_t dac_code, float min_output, float max_output)
Calculate the LTC2664 DAC output voltage given the DAC code, offset, and LSB.
Definition: LTC2664.cpp:123
uint16_t LTC2664_voltage_to_code(float dac_voltage, float min_output, float max_output)
Calculate a LTC2664 DAC code given the desired output voltage and DAC address (0-3) ...
Definition: LTC2664.cpp:109
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
This union splits one int16_t (16-bit signed integer) or uint16_t (16-bit unsigned integer) into two ...
Definition: Linduino.h:99
void spi_transfer_block(uint8_t cs_pin, uint8_t *tx, uint8_t *rx, uint8_t length)
Reads and sends a byte array.
Definition: LT_SPI.cpp:125
uint8_t LT_byte[2]
2 bytes (unsigned 8-bit integers) to be converted to a 16-bit signed or unsigned integer ...
Definition: Linduino.h:103