Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2756.cpp
Go to the documentation of this file.
1 /*!
2  LTC2756: Serial 18-Bit SoftSpan IOUT DAC
3 
4 @verbatim
5 
6 The LTC2756 is an 18-bit multiplying serial-input,
7 current-output digital-to-analog converter. LTC2756A
8 provides full 18-bit performance-INL and DNL of +/-1LSB
9 maximum-over temperature without any adjustments. 18-bit
10 monotonicity is guaranteed in all performance grades. This
11 SoftSpan(TM) DAC operates from a single 3V to 5V supply
12 and offers six output ranges (up to +/-10V) that can be
13 programmed through the 3-wire SPI serial interface or
14 pin-strapped for operation in a single range.
15 
16 @endverbatim
17 
18 http://www.linear.com/product/LTC2756
19 
20 http://www.linear.com/product/LTC2756#demoboards
21 
22 Copyright 2018(c) Analog Devices, Inc.
23 
24 All rights reserved.
25 
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28  - Redistributions of source code must retain the above copyright
29  notice, this list of conditions and the following disclaimer.
30  - Redistributions in binary form must reproduce the above copyright
31  notice, this list of conditions and the following disclaimer in
32  the documentation and/or other materials provided with the
33  distribution.
34  - Neither the name of Analog Devices, Inc. nor the names of its
35  contributors may be used to endorse or promote products derived
36  from this software without specific prior written permission.
37  - The use of this software may or may not infringe the patent rights
38  of one or more patent holders. This license does not release you
39  from the requirement that you obtain separate licenses from these
40  patent holders to use this software.
41  - Use of the software either in source or binary form, must be run
42  on or directly connected to an Analog Devices Inc. component.
43 
44 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
45 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
46 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
48 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
50 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55 
56 //! @ingroup User_Contributed
57 //! @{
58 //! @defgroup LTC2756 LTC2756: Serial 18-Bit SoftSpan IOUT DAC
59 //! @}
60 
61 /*! @file
62  @ingroup LTC2756 UserContributed
63  Library for LTC2756 Serial 18-Bit SoftSpan IOUT DAC
64 */
65 
66 #include <stdint.h>
67 #include <Arduino.h>
68 #include "Linduino.h"
69 #include "LT_SPI.h"
70 #include "LTC2756.h"
71 #include <SPI.h>
72 
73 // Transfer 4 bytes
74 void LTC2756_transfer_4bytes(uint8_t cs, uint8_t *tx, uint8_t *rx)
75 {
76  spi_transfer_block(cs, tx, rx, (uint8_t)4);
77 }
78 
79 // Convert voltage to code
80 // Returns the calculated code
81 uint32_t LTC2756_voltage_to_code(float dac_voltage, float LTC2756_lsb, int32_t LTC2756_offset)
82 {
83  float float_code;
84  int32_t dac_code;
85 
86  float_code = dac_voltage / LTC2756_lsb; // Calculate the DAC code
87  float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); // Round
88  dac_code = (int32_t)float_code - LTC2756_offset; // Subtract offset
89  if (dac_code < 0)
90  dac_code = 0;
91  if (dac_code > 262143)
92  dac_code = 262143;
93  return ((uint32_t)dac_code);
94 }
95 
96 // Calculate the LTC2756 offset and LSB voltage given two measured voltages and their corresponding codes
97 void LTC2756_calibrate(uint32_t zero_code, uint32_t fs_code, float zero_voltage, float fs_voltage, float *LTC2756_lsb, int32_t *LTC2756_offset)
98 {
99  float temp_offset;
100  *LTC2756_lsb = (fs_voltage - zero_voltage) / ((float) (fs_code - zero_code)); // 1) Calculate the LSB
101  temp_offset = zero_voltage/(*LTC2756_lsb) - (float)zero_code; // 2) Calculate the offset
102  temp_offset = (temp_offset > (floor(temp_offset) + 0.5)) ? ceil(temp_offset) : floor(temp_offset); // 3) Round offset
103  *LTC2756_offset = (int32_t)temp_offset; // 4) Cast as int16_t
104 }
105 
static int32_t LTC2756_offset
Header File for Linduino Libraries and Demo Code.
static float LTC2756_lsb
LTC2756: Serial 18-Bit SoftSpan IOUT DAC.
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
void LTC2756_calibrate(uint32_t zero_code, uint32_t fs_code, float zero_voltage, float fs_voltage, float *LTC2756_lsb, int32_t *LTC2756_offset)
Calculate the LTC2756 offset and LSB voltage given two measured voltages and their corresponding code...
Definition: LTC2756.cpp:97
uint32_t LTC2756_voltage_to_code(float dac_voltage, float LTC2756_lsb, int32_t LTC2756_offset)
Converts voltage to code.
Definition: LTC2756.cpp:81
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
void LTC2756_transfer_4bytes(uint8_t cs, uint8_t *tx, uint8_t *rx)
Transfers four bytes to the LTC2756.
Definition: LTC2756.cpp:74