Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC6903.cpp
Go to the documentation of this file.
1 /*!
2 LTC6903: 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/LTC6903
16 
17 http://www.linear.com/product/LTC6903#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 LTC6903 LTC6903: 1kHz to 68MHz Serial Port Programmable Oscillator
57 //! @}
58 
59 /*! @file
60  @ingroup LTC6903
61  Library for LTC6903: 1kHz to 68MHz Serial Port Programmable Oscillator
62 */
63 
64 #include <stdint.h>
65 #include <Arduino.h>
66 #include "Linduino.h"
67 #include "LT_SPI.h"
68 #include "LTC6903.h"
69 #include <SPI.h>
70 #include "math.h"
71 
72 // Writes 2 bytes
73 void LTC6903_write(uint8_t cs, uint16_t code)
74 {
75  uint16_t rx;
76  spi_transfer_word(cs, (uint16_t)code, &rx);
77 }
78 
79 // Calculates the code necessary to create the clock frequency
80 // Returns the Code for the LTC6903
81 uint16_t LTC6903_frequency_to_code(float frequency, uint8_t clk)
82 {
83  uint8_t oct;
84  double oct_double;
85  float float_dac;
86  uint16_t dac;
87 
88  // Calculate OCT
89  oct_double = log10((double)((frequency*1000000)/1039));
90  oct_double *= 3.322;
91 
92  // Keep OCT within range
93  if (oct_double>15)
94  oct = 15;
95  if (oct_double<0)
96  oct = 0;
97  oct = (uint8_t)oct_double; // Cast as uint8_t , round down
98 
99  // Calculate DAC code
100  float_dac = 2048-(2078*pow(2, (10+ oct)))/(frequency*1000000); // Calculate the dac code
101  float_dac = (float_dac > (floor(float_dac) + 0.5)) ? ceil(float_dac) : floor(float_dac); // Round
102 
103  // Keep DAC within range
104  if (float_dac>1023)
105  float_dac = 1023;
106  if (float_dac<0)
107  float_dac = 0;
108 
109  dac = (uint16_t)float_dac; // Cast as uint16_t
110 
111  return((uint32_t)((oct<<12)| (dac<<2) | clk));
112 }
Header File for Linduino Libraries and Demo Code.
void LTC6903_write(uint8_t cs, uint16_t code)
Writes 2 bytes.
Definition: LTC6903.cpp:73
uint16_t LTC6903_frequency_to_code(float frequency, uint8_t clk)
Calculates the code necessary to create the clock frequency.
Definition: LTC6903.cpp:81
void spi_transfer_word(uint8_t cs_pin, uint16_t tx, uint16_t *rx)
Reads and sends a word.
Definition: LT_SPI.cpp:98
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
LTC6903: 1kHz to 68MHz Serial Port Programmable Oscillator.