Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2662.cpp
Go to the documentation of this file.
1 /*!
2 LTC2662: 5-Channel 16-/12-Bit 300mA SoftSpan Current Source DACs
3 
4 @verbatim
5 
6 The LTC2662 is a family of 5-channel, 16-/12-bit current source digital-to-analog converters with precision
7 integrated reference and accurate full-scale current. The LTC2662 is guaranteed monotonic and offers independent
8 SoftSpan programmability of the current output range from 3.125mA to 300mA.
9 
10 
11 @endverbatim
12 
13 http://www.linear.com/product/LTC2662
14 
15 http://www.linear.com/product/LTC2662#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 LTC2662 LTC2662 5-Channel 16-/12-Bit 300mA SoftSpan Current Source DACs
55 //! @}
56 
57 /*! @file
58  @ingroup LTC2662
59  LTC2662: 5-Channel 16-/12-Bit 300mA SoftSpan Current Source DACs
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 "LTC2662.h"
68 #include <SPI.h>
69 
70 int8_t LTC2662_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 LTC2662
72 {
73  uint8_t data_array[4], rx_array[4];
74  int8_t ret;
76 
77  data.LT_int16 = dac_code; // Copy DAC code to union
78  data_array[3] = 0; // Only required for 32 byte readback transaction
79  data_array[2] = dac_command | dac_address; // Build command / address byte
80  data_array[1] = data.LT_byte[1]; // MS Byte
81  data_array[0] = data.LT_byte[0]; // LS Byte
82 
83  spi_transfer_block(cs, data_array, rx_array, (uint8_t) 4);
84 
85  return(rx_array[3]); // Returns the fault registers
86 }
87 
88 uint16_t LTC2662_current_to_code(float dac_current, float max_output)
89 // Calculate a LTC2662 DAC code given the desired output current and the maximum output
90 // output for a given softspan range.
91 {
92  uint16_t dac_code;
93  float float_code;
94  float_code = 65535.0 * dac_current / max_output; // Calculate the DAC code
95  float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); // Round
96  if (float_code < 0.0) float_code = 0.0;
97  if (float_code > 65535.0) float_code = 65535.0;
98  dac_code = (uint16_t) (float_code); // Convert to unsigned integer
99  return (dac_code);
100 }
101 
102 float LTC2662_code_to_current(uint16_t dac_code, float max_output)
103 // Calculate the LTC2662 DAC output current given the DAC code and and the minimum
104 // output for a given softspan range.
105 {
106  float dac_current;
107  dac_current = ((float) dac_code / 65535.0) * max_output; // Calculate the dac_current
108  return (dac_current);
109 }
long ret
int8_t LTC2662_write(uint8_t cs, uint8_t dac_command, uint8_t dac_address, uint16_t dac_code)
Write the 16-bit dac_code to the LTC2662.
Definition: LTC2662.cpp:70
Header File for Linduino Libraries and Demo Code.
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 LTC2662_code_to_current(uint16_t dac_code, float max_output)
Calculate the LTC2662 DAC output current given the DAC code and max output.
Definition: LTC2662.cpp:102
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LTC2662: 5-Channel 16-/12-Bit 300mA SoftSpan Current Source DACs.
uint16_t LTC2662_current_to_code(float dac_current, float max_output)
Calculate a LTC2662 DAC code given the desired output current.
Definition: LTC2662.cpp:88
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