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