| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC2654.cpp
Go to the documentation of this file.
1 /*!
2 LTC2654: Quad SPI 16-/12-Bit Rail-to-Rail DACs with 10ppm/C Max Reference
3
4 @verbatim
5
6 The LTC2654 is a family of quad 16-/12-bit rail-to-rail DACs with integrated
7 10ppm/C maximum reference. The DACs have built-in high performance, rail-to-
8 rail, output buffers and are guaranteed monotonic. The LTC2654-L has a full-
9 scale output of 2.5V with the integrated reference and operates from a single
10 2.7V to 5.5V supply. The LTC2654-H has a full-scale output of 4.096V with the
11 integrated reference and operates from a 4.5V to 5.5V supply. Each DAC can also
12 operate with an external reference, which sets the full-scale output to 2 times
13 the external reference voltage. These DACs communicate via a SPI/MICROWIRE
14 compatible 4-wire serial interface which operates at clock rates up to 50MHz.
15 The LTC2654 incorporates a power-on reset circuit that is controlled by the
16 PORSEL pin. If PORSEL is tied to GND, the DACs reset to zero-scale. If PORSEL is
17 tied to VCC, the DACs reset to mid-scale.
18
19 @endverbatim
20
21 http://www.linear.com/product/LTC2654
22
23 http://www.linear.com/product/LTC2654#demoboards
24
25
26 Copyright 2018(c) Analog Devices, Inc.
27
28 All rights reserved.
29
30 Redistribution and use in source and binary forms, with or without
31 modification, are permitted provided that the following conditions are met:
32 - Redistributions of source code must retain the above copyright
33 notice, this list of conditions and the following disclaimer.
34 - Redistributions in binary form must reproduce the above copyright
35 notice, this list of conditions and the following disclaimer in
36 the documentation and/or other materials provided with the
37 distribution.
38 - Neither the name of Analog Devices, Inc. nor the names of its
39 contributors may be used to endorse or promote products derived
40 from this software without specific prior written permission.
41 - The use of this software may or may not infringe the patent rights
42 of one or more patent holders. This license does not release you
43 from the requirement that you obtain separate licenses from these
44 patent holders to use this software.
45 - Use of the software either in source or binary form, must be run
46 on or directly connected to an Analog Devices Inc. component.
47
48 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
49 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
50 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
52 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
54 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60 //! @ingroup Digital_to_Analog_Converters
61 //! @{
62 //! @defgroup LTC2654 LTC2654: Quad SPI 16-/12-Bit Rail-to-Rail DACs with 10ppm/C Max Reference
63 //! @}
64
65 /*! @file
66 @ingroup LTC2654
67 Library for LTC2654 Quad SPI 16-/12-Bit Rail-to-Rail DACs with 10ppm/C Max Reference
68 */
69
70 #include <stdint.h>
71 #include <math.h>
72 #include <Arduino.h>
73 #include "Linduino.h"
74 #include "LT_SPI.h"
75 #include "LTC2654.h"
76 #include <SPI.h>
77
78 // Write the 16-bit dac_code to the LTC2654
79 void LTC2654_write(uint8_t cs, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
80 {
81 uint8_t data_array[3], rx_array[3];
82 LT_union_int16_2bytes data;
83
84 data.LT_uint16 = dac_code;
85
86 data_array[2] = dac_command | dac_address;
87 data_array[1] = data.LT_byte[1];
88 data_array[0] =data.LT_byte[0];
89
90 spi_transfer_block(cs, data_array, rx_array, (uint8_t) 3);
91 }
92
93 // Calculate a LTC2654 DAC code given the desired output voltage and DAC address (0-3)
94 uint16_t LTC2654_voltage_to_code(float dac_voltage, float LTC2654_lsb, int16_t LTC2654_offset)
95 {
96 int32_t dac_code;
97 float float_code;
98 float_code = dac_voltage / LTC2654_lsb; //! 1) Calculate the DAC code
99 float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); //! 2) Round
100 dac_code = (int32_t)float_code - LTC2654_offset; //! 3) Subtract offset
101 if (dac_code < 0) //! 4) If DAC code < 0, Then DAC code = 0
102 dac_code = 0;
103 return ((uint16_t)dac_code); //! 5) Cast DAC code as uint16_t
104 }
105
106 // Calculate the LTC2654 DAC output voltage given the DAC code and DAC address (0-3)
107 float LTC2654_code_to_voltage(uint16_t dac_code, float LTC2654_lsb, int16_t LTC2654_offset)
108 {
109 float dac_voltage;
110 dac_voltage = ((float)(dac_code + LTC2654_offset)* LTC2654_lsb); //! 1) Calculates the dac_voltage
111 return (dac_voltage);
112 }
113
114 // Calculate the LTC2654 offset and LSB voltage given two measured voltages and their corresponding codes
115 void LTC2654_calibrate(uint16_t dac_code1, uint16_t dac_code2, float voltage1, float voltage2, float *LTC2654_lsb, int16_t *LTC2654_offset)
116 {
117 float temp_offset;
118 *LTC2654_lsb = (voltage2 - voltage1) / ((float) (dac_code2 - dac_code1)); //! 1) Calculate the LSB
119 temp_offset = voltage1/(*LTC2654_lsb) - dac_code1; //! 2) Calculate the offset
120 temp_offset = (temp_offset > (floor(temp_offset) + 0.5)) ? ceil(temp_offset) : floor(temp_offset); //! 3) Round offset
121 *LTC2654_offset = (int16_t)temp_offset; //! 4) Cast as int16_t
122 }
Linduino.h
Header File for Linduino Libraries and Demo Code.
LTC2654_offset
static int16_t LTC2654_offset[5]
DAC offset - index 4 for "all DACs".
Definition: DC1678A.ino:162
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LT_union_int16_2bytes::LT_uint16
uint16_t LT_uint16
16-bit unsigned integer to be converted to two bytes
Definition: Linduino.h:102
LTC2654_calibrate
void LTC2654_calibrate(uint16_t dac_code1, uint16_t dac_code2, float voltage1, float voltage2, float *LTC2654_lsb, int16_t *LTC2654_offset)
Calculate the LTC2654 offset and LSB voltage given two measured voltages and their corresponding code...
Definition: LTC2654.cpp:115
LT_SPI.h
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LT_union_int16_2bytes
This union splits one int16_t (16-bit signed integer) or uint16_t (16-bit unsigned integer) into two ...
Definition: Linduino.h:99
LTC2654_code_to_voltage
float LTC2654_code_to_voltage(uint16_t dac_code, float LTC2654_lsb, int16_t LTC2654_offset)
Calculate the LTC2654 DAC output voltage given the DAC code, offset, and LSB value.
Definition: LTC2654.cpp:107
spi_transfer_block
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
LT_union_int16_2bytes::LT_byte
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
LTC2654_lsb
static float LTC2654_lsb[5]
The LTC2654 lsb - index 4 for "all DACs".
Definition: DC1678A.ino:163
LTC2654_voltage_to_code
uint16_t LTC2654_voltage_to_code(float dac_voltage, float LTC2654_lsb, int16_t LTC2654_offset)
Calculate a LTC2654 DAC code given the desired output voltage and DAC address (0-3) ...
Definition: LTC2654.cpp:94
LTC2654_write
void LTC2654_write(uint8_t cs, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
Write the 16-bit dac_code to the LTC2654.
Definition: LTC2654.cpp:79
LTC2654.h
LTC2654: Quad SPI 16-/12-Bit Rail-to-Rail DACs with 10ppm/C Max Reference.