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