Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC6904.cpp
Go to the documentation of this file.
1 /*!
2 LTC6904: 1kHz to 68MHz Serial Port Programmable Oscillator
3 
4 @verbatim
5 
6 The LTC6903/LTC6904 are low power self contained digital
7 frequency sources providing a precision frequency from
8 1kHz to 68MHz, set through a serial port. The LTC6903/
9 LTC6904 require no external components other than a
10 power supply bypass capacitor, and they operate over a
11 single wide supply range of 2.7V to 5.5V.
12 
13 @endverbatim
14 
15 http://www.linear.com/product/LTC6904
16 
17 http://www.linear.com/product/LTC6904#demoboards
18 
19 
20 Copyright 2018(c) Analog Devices, Inc.
21 
22 All rights reserved.
23 
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26  - Redistributions of source code must retain the above copyright
27  notice, this list of conditions and the following disclaimer.
28  - Redistributions in binary form must reproduce the above copyright
29  notice, this list of conditions and the following disclaimer in
30  the documentation and/or other materials provided with the
31  distribution.
32  - Neither the name of Analog Devices, Inc. nor the names of its
33  contributors may be used to endorse or promote products derived
34  from this software without specific prior written permission.
35  - The use of this software may or may not infringe the patent rights
36  of one or more patent holders. This license does not release you
37  from the requirement that you obtain separate licenses from these
38  patent holders to use this software.
39  - Use of the software either in source or binary form, must be run
40  on or directly connected to an Analog Devices Inc. component.
41 
42 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
43 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
44 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
46 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
48 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53 
54 //! @ingroup RF_Timing
55 //! @{
56 //! @defgroup LTC6904 LTC6904: 1kHz to 68MHz Serial Port Programmable Oscillator
57 //! @}
58 
59 /*! @file
60  @ingroup LTC6904
61  Library for LTC6904: 1kHz to 68MHz Serial Port Programmable Oscillator
62 */
63 
64 #include <stdint.h>
65 #include <Arduino.h>
66 #include "Linduino.h"
67 #include "LT_I2C.h"
68 #include "LTC6904.h"
69 #include <Wire.h>
70 #include "math.h"
71 
72 // Writes 2 bytes
73 uint8_t LTC6904_write(uint8_t address, uint16_t code)
74 {
75  uint8_t ack;
76  ack = i2c_write_byte_data(address, (0x0FF00 & code)>>8, 0x0FF & code);
77  return(ack);
78 }
79 
80 // Calculates the code necessary to create the clock frequency
81 // Returns the Code for the LTC6904
82 uint16_t LTC6904_frequency_to_code(float frequency, uint8_t clk)
83 {
84  uint8_t oct;
85  double oct_double;
86  float float_dac;
87  uint16_t dac;
88 
89  // Calculate OCT
90  oct_double = log10((double)((frequency*1000000)/1039));
91  oct_double *= 3.322;
92 
93  // Keep OCT within range
94  if (oct_double>15)
95  oct = 15;
96  if (oct_double<0)
97  oct = 0;
98  oct = (uint8_t)oct_double; // Cast as uint8_t , round down
99 
100  // Calculate DAC code
101  float_dac = 2048-(2078*pow(2, (10+ oct)))/(frequency*1000000); // Calculate the dac code
102  float_dac = (float_dac > (floor(float_dac) + 0.5)) ? ceil(float_dac) : floor(float_dac); // Round
103 
104  // Keep DAC within range
105  if (float_dac>1023)
106  float_dac = 1023;
107  if (float_dac<0)
108  float_dac = 0;
109 
110  dac = (uint16_t)float_dac; // Cast as uint16_t
111 
112  return((uint16_t)((oct<<12)| (dac<<2) | clk));
113 }
LTC6904: 1kHz to 68MHz Serial Port Programmable Oscillator.
Header File for Linduino Libraries and Demo Code.
uint16_t LTC6904_frequency_to_code(float frequency, uint8_t clk)
Calculates the code necessary to create the clock frequency.
Definition: LTC6904.cpp:82
static uint8_t address
Definition: DC2091A.ino:83
int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value)
Write a byte of data to register specified by "command".
Definition: LT_I2C.cpp:155
uint8_t LTC6904_write(uint8_t address, uint16_t code)
Writes 2 bytes.
Definition: LTC6904.cpp:73
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.