A sine wave generator using AD9833 and AD9850 using STM32F103RB

Dependencies:   mbed

This is a sine wave generator using DDS IC' AD9833 and AD9850. The STM32F1 microcontroller produces the SPI commands for the two DDS.

Learn more about STM32F1 in my blog: https://www.teachmemicro.com

Committer:
roland_tmm
Date:
Tue Nov 21 19:18:25 2017 +0000
Revision:
2:602f7589c53e
Parent:
0:6069c0f2a245
Edited comments/description;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roland_tmm 2:602f7589c53e 1 /* AD9833 library for STM3F103
roland_tmm 0:6069c0f2a245 2 * AD9833.cpp
roland_tmm 2:602f7589c53e 3 * Roland Pelayo
roland_tmm 0:6069c0f2a245 4 *
roland_tmm 2:602f7589c53e 5 * Based on the library from Bill Williams <wlwilliams1952@gmail.com, github/BillWilliams1952>
roland_tmm 2:602f7589c53e 6 *
roland_tmm 0:6069c0f2a245 7 *
roland_tmm 0:6069c0f2a245 8 * This program is free software; you can redistribute it and/or modify
roland_tmm 0:6069c0f2a245 9 * it under the terms of the GNU General Public License as published by
roland_tmm 0:6069c0f2a245 10 * the Free Software Foundation; either version 2 of the License, or
roland_tmm 0:6069c0f2a245 11 * (at your option) any later version.
roland_tmm 0:6069c0f2a245 12 *
roland_tmm 0:6069c0f2a245 13 * This program is distributed in the hope that it will be useful,
roland_tmm 0:6069c0f2a245 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
roland_tmm 0:6069c0f2a245 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
roland_tmm 0:6069c0f2a245 16 * GNU General Public License for more details.
roland_tmm 0:6069c0f2a245 17 *
roland_tmm 0:6069c0f2a245 18 * You should have received a copy of the GNU General Public License
roland_tmm 0:6069c0f2a245 19 * along with this program; if not, write to the Free Software
roland_tmm 0:6069c0f2a245 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
roland_tmm 0:6069c0f2a245 21 * MA 02110-1301, USA.
roland_tmm 0:6069c0f2a245 22 *
roland_tmm 0:6069c0f2a245 23 */
roland_tmm 0:6069c0f2a245 24 #include "mbed.h"
roland_tmm 0:6069c0f2a245 25 #include "AD9833.h"
roland_tmm 0:6069c0f2a245 26
roland_tmm 0:6069c0f2a245 27 #define SPI_MOSI PA_7
roland_tmm 0:6069c0f2a245 28 #define SPI_MISO PA_6
roland_tmm 0:6069c0f2a245 29 #define SPI_SCK PA_5
roland_tmm 0:6069c0f2a245 30 #define FNCpin PA_4
roland_tmm 0:6069c0f2a245 31
roland_tmm 0:6069c0f2a245 32 DigitalOut fnc_pin(FNCpin);
roland_tmm 0:6069c0f2a245 33 SPI SPI_dev(SPI_MOSI, SPI_MISO, SPI_SCK);
roland_tmm 0:6069c0f2a245 34
roland_tmm 0:6069c0f2a245 35 /*
roland_tmm 0:6069c0f2a245 36 * Create an AD9833 object
roland_tmm 0:6069c0f2a245 37 */
roland_tmm 0:6069c0f2a245 38 AD9833 :: AD9833 (uint32_t referenceFrequency ) {
roland_tmm 0:6069c0f2a245 39 // Pin used to enable SPI communication (active LOW)
roland_tmm 0:6069c0f2a245 40
roland_tmm 0:6069c0f2a245 41 FNCWrite(1);
roland_tmm 0:6069c0f2a245 42
roland_tmm 0:6069c0f2a245 43 /* TODO: The minimum resolution and max frequency are determined by
roland_tmm 0:6069c0f2a245 44 * by referenceFrequency. We should calculate these values and use
roland_tmm 0:6069c0f2a245 45 * them during setFrequency. The problem is if the user programs a
roland_tmm 0:6069c0f2a245 46 * square wave at refFrequency/2, then changes the waveform to sine.
roland_tmm 0:6069c0f2a245 47 * The sine wave will not have enough points?
roland_tmm 0:6069c0f2a245 48 */
roland_tmm 0:6069c0f2a245 49 refFrequency = referenceFrequency;
roland_tmm 0:6069c0f2a245 50
roland_tmm 0:6069c0f2a245 51 // Setup some defaults
roland_tmm 0:6069c0f2a245 52 DacDisabled = false;
roland_tmm 0:6069c0f2a245 53 IntClkDisabled = false;
roland_tmm 0:6069c0f2a245 54 outputEnabled = false;
roland_tmm 0:6069c0f2a245 55 waveForm0 = waveForm1 = SINE_WAVE;
roland_tmm 0:6069c0f2a245 56 frequency0 = frequency1 = 1000; // 1 KHz sine wave to start
roland_tmm 0:6069c0f2a245 57 phase0 = phase1 = 0.0; // 0 phase
roland_tmm 0:6069c0f2a245 58 activeFreq = REG0; activePhase = REG0;
roland_tmm 0:6069c0f2a245 59 }
roland_tmm 0:6069c0f2a245 60
roland_tmm 0:6069c0f2a245 61 /*
roland_tmm 0:6069c0f2a245 62 * This MUST be the first command after declaring the AD9833 object
roland_tmm 0:6069c0f2a245 63 * Start SPI and place the AD9833 in the RESET state
roland_tmm 0:6069c0f2a245 64 */
roland_tmm 0:6069c0f2a245 65 void AD9833 :: Begin ( void ) {
roland_tmm 0:6069c0f2a245 66 wait(0.1);
roland_tmm 0:6069c0f2a245 67 Reset(); // Hold in RESET until first WriteRegister command
roland_tmm 0:6069c0f2a245 68 }
roland_tmm 0:6069c0f2a245 69
roland_tmm 0:6069c0f2a245 70 /*
roland_tmm 0:6069c0f2a245 71 * Setup and apply a signal. phaseInDeg defaults to 0.0 if not supplied.
roland_tmm 0:6069c0f2a245 72 * phaseReg defaults to value of freqReg if not supplied.
roland_tmm 0:6069c0f2a245 73 * Note that any previous calls to EnableOut,
roland_tmm 0:6069c0f2a245 74 * SleepMode, DisableDAC, or DisableInternalClock remain in effect.
roland_tmm 0:6069c0f2a245 75 */
roland_tmm 0:6069c0f2a245 76 void AD9833 :: ApplySignal ( WaveformType waveType,
roland_tmm 0:6069c0f2a245 77 Registers freqReg, float frequencyInHz,
roland_tmm 0:6069c0f2a245 78 Registers phaseReg, float phaseInDeg ) {
roland_tmm 0:6069c0f2a245 79 SetGENFrequency ( freqReg, frequencyInHz );
roland_tmm 0:6069c0f2a245 80 SetPhase ( phaseReg, phaseInDeg );
roland_tmm 0:6069c0f2a245 81 SetWaveform ( freqReg, waveType );
roland_tmm 0:6069c0f2a245 82 SetOutputSource ( freqReg, phaseReg );
roland_tmm 0:6069c0f2a245 83 }
roland_tmm 0:6069c0f2a245 84
roland_tmm 0:6069c0f2a245 85 /***********************************************************************
roland_tmm 0:6069c0f2a245 86 Control Register
roland_tmm 0:6069c0f2a245 87 ------------------------------------------------------------------------
roland_tmm 0:6069c0f2a245 88 D15,D14(MSB) 10 = FREQ1 write, 01 = FREQ0 write,
roland_tmm 0:6069c0f2a245 89 11 = PHASE write, 00 = control write
roland_tmm 0:6069c0f2a245 90 D13 If D15,D14 = 00, 0 = individual LSB and MSB FREQ write,
roland_tmm 0:6069c0f2a245 91 1 = both LSB and MSB FREQ writes consecutively
roland_tmm 0:6069c0f2a245 92 If D15,D14 = 11, 0 = PHASE0 write, 1 = PHASE1 write
roland_tmm 0:6069c0f2a245 93 D12 0 = writing LSB independently
roland_tmm 0:6069c0f2a245 94 1 = writing MSB independently
roland_tmm 0:6069c0f2a245 95 D11 0 = output FREQ0,
roland_tmm 0:6069c0f2a245 96 1 = output FREQ1
roland_tmm 0:6069c0f2a245 97 D10 0 = output PHASE0
roland_tmm 0:6069c0f2a245 98 1 = output PHASE1
roland_tmm 0:6069c0f2a245 99 D9 Reserved. Must be 0.
roland_tmm 0:6069c0f2a245 100 D8 0 = RESET disabled
roland_tmm 0:6069c0f2a245 101 1 = RESET enabled
roland_tmm 0:6069c0f2a245 102 D7 0 = internal clock is enabled
roland_tmm 0:6069c0f2a245 103 1 = internal clock is disabled
roland_tmm 0:6069c0f2a245 104 D6 0 = onboard DAC is active for sine and triangle wave output,
roland_tmm 0:6069c0f2a245 105 1 = put DAC to sleep for square wave output
roland_tmm 0:6069c0f2a245 106 D5 0 = output depends on D1
roland_tmm 0:6069c0f2a245 107 1 = output is a square wave
roland_tmm 0:6069c0f2a245 108 D4 Reserved. Must be 0.
roland_tmm 0:6069c0f2a245 109 D3 0 = square wave of half frequency output
roland_tmm 0:6069c0f2a245 110 1 = square wave output
roland_tmm 0:6069c0f2a245 111 D2 Reserved. Must be 0.
roland_tmm 0:6069c0f2a245 112 D1 If D5 = 1, D1 = 0.
roland_tmm 0:6069c0f2a245 113 Otherwise 0 = sine output, 1 = triangle output
roland_tmm 0:6069c0f2a245 114 D0 Reserved. Must be 0.
roland_tmm 0:6069c0f2a245 115 ***********************************************************************/
roland_tmm 0:6069c0f2a245 116
roland_tmm 0:6069c0f2a245 117 /*
roland_tmm 0:6069c0f2a245 118 * Hold the AD9833 in RESET state.
roland_tmm 0:6069c0f2a245 119 * Resets internal registers to 0, which corresponds to an output of
roland_tmm 0:6069c0f2a245 120 * midscale - digital output at 0.
roland_tmm 0:6069c0f2a245 121 *
roland_tmm 0:6069c0f2a245 122 * The difference between Reset() and EnableOutput(false) is that
roland_tmm 0:6069c0f2a245 123 * EnableOutput(false) keeps the AD9833 in the RESET state until you
roland_tmm 0:6069c0f2a245 124 * specifically remove the RESET state using EnableOutput(true).
roland_tmm 0:6069c0f2a245 125 * With a call to Reset(), ANY subsequent call to ANY function (other
roland_tmm 0:6069c0f2a245 126 * than Reset itself and Set/IncrementPhase) will also remove the RESET
roland_tmm 0:6069c0f2a245 127 * state.
roland_tmm 0:6069c0f2a245 128 */
roland_tmm 0:6069c0f2a245 129 void AD9833 :: Reset ( void ) {
roland_tmm 0:6069c0f2a245 130 WriteRegister(RESET_CMD);
roland_tmm 0:6069c0f2a245 131 wait(0.015);
roland_tmm 0:6069c0f2a245 132 }
roland_tmm 0:6069c0f2a245 133
roland_tmm 0:6069c0f2a245 134 /*
roland_tmm 0:6069c0f2a245 135 * Set the specified frequency register with the frequency (in Hz)
roland_tmm 0:6069c0f2a245 136 */
roland_tmm 0:6069c0f2a245 137 void AD9833 :: SetGENFrequency ( Registers freqReg, float frequency ) {
roland_tmm 0:6069c0f2a245 138 // TODO: calculate max frequency based on refFrequency.
roland_tmm 0:6069c0f2a245 139 // Use the calculations for sanity checks on numbers.
roland_tmm 0:6069c0f2a245 140 // Sanity check on frequency: Square - refFrequency / 2
roland_tmm 0:6069c0f2a245 141 // Sine/Triangle - refFrequency / 4
roland_tmm 0:6069c0f2a245 142 if ( frequency > 12.5e6 ) // TODO: Fix this based on refFreq
roland_tmm 0:6069c0f2a245 143 frequency = 12.5e6;
roland_tmm 0:6069c0f2a245 144 if ( frequency < 0.0 ) frequency = 0.0;
roland_tmm 0:6069c0f2a245 145
roland_tmm 0:6069c0f2a245 146 // Save frequency for use by IncrementFrequency function
roland_tmm 0:6069c0f2a245 147 if ( freqReg == REG0 ) frequency0 = frequency;
roland_tmm 0:6069c0f2a245 148 else frequency1 = frequency;
roland_tmm 0:6069c0f2a245 149
roland_tmm 0:6069c0f2a245 150 int32_t freqWord = (frequency * pow2_28) / (float)refFrequency;
roland_tmm 0:6069c0f2a245 151 int16_t upper14 = (int16_t)((freqWord & 0xFFFC000) >> 14),
roland_tmm 0:6069c0f2a245 152 lower14 = (int16_t)(freqWord & 0x3FFF);
roland_tmm 0:6069c0f2a245 153
roland_tmm 0:6069c0f2a245 154 // Which frequency register are we updating?
roland_tmm 0:6069c0f2a245 155 uint16_t reg = freqReg == REG0 ? FREQ0_WRITE_REG : FREQ1_WRITE_REG;
roland_tmm 0:6069c0f2a245 156 lower14 |= reg;
roland_tmm 0:6069c0f2a245 157 upper14 |= reg;
roland_tmm 0:6069c0f2a245 158
roland_tmm 0:6069c0f2a245 159 // I do not reset the registers during write. It seems to remove
roland_tmm 0:6069c0f2a245 160 // 'glitching' on the outputs.
roland_tmm 0:6069c0f2a245 161 WriteControlRegister();
roland_tmm 0:6069c0f2a245 162 // Control register has already been setup to accept two frequency
roland_tmm 0:6069c0f2a245 163 // writes, one for each 14 bit part of the 28 bit frequency word
roland_tmm 0:6069c0f2a245 164 WriteRegister(lower14); // Write lower 14 bits to AD9833
roland_tmm 0:6069c0f2a245 165 WriteRegister(upper14); // Write upper 14 bits to AD9833
roland_tmm 0:6069c0f2a245 166 }
roland_tmm 0:6069c0f2a245 167
roland_tmm 0:6069c0f2a245 168 /*
roland_tmm 0:6069c0f2a245 169 * Increment the specified frequency register with the frequency (in Hz)
roland_tmm 0:6069c0f2a245 170 */
roland_tmm 0:6069c0f2a245 171 void AD9833 :: IncrementFrequency ( Registers freqReg, float freqIncHz ) {
roland_tmm 0:6069c0f2a245 172 // Add/subtract a value from the current frequency programmed in
roland_tmm 0:6069c0f2a245 173 // freqReg by the amount given
roland_tmm 0:6069c0f2a245 174 float frequency = (freqReg == REG0) ? frequency0 : frequency1;
roland_tmm 0:6069c0f2a245 175 SetGENFrequency(freqReg,frequency+freqIncHz);
roland_tmm 0:6069c0f2a245 176 }
roland_tmm 0:6069c0f2a245 177
roland_tmm 0:6069c0f2a245 178 /*
roland_tmm 0:6069c0f2a245 179 * Set the specified phase register with the phase (in degrees)
roland_tmm 0:6069c0f2a245 180 * The output signal will be phase shifted by 2π/4096 x PHASEREG
roland_tmm 0:6069c0f2a245 181 */
roland_tmm 0:6069c0f2a245 182 void AD9833 :: SetPhase ( Registers phaseReg, float phaseInDeg ) {
roland_tmm 0:6069c0f2a245 183 // Sanity checks on input
roland_tmm 0:6069c0f2a245 184 phaseInDeg = fmod(phaseInDeg,360);
roland_tmm 0:6069c0f2a245 185 if ( phaseInDeg < 0 ) phaseInDeg += 360;
roland_tmm 0:6069c0f2a245 186
roland_tmm 0:6069c0f2a245 187 // Phase is in float degrees ( 0.0 - 360.0 )
roland_tmm 0:6069c0f2a245 188 // Convert to a number 0 to 4096 where 4096 = 0 by masking
roland_tmm 0:6069c0f2a245 189 uint16_t phaseVal = (uint16_t)(BITS_PER_DEG * phaseInDeg) & 0x0FFF;
roland_tmm 0:6069c0f2a245 190 phaseVal |= PHASE_WRITE_CMD;
roland_tmm 0:6069c0f2a245 191
roland_tmm 0:6069c0f2a245 192 // Save phase for use by IncrementPhase function
roland_tmm 0:6069c0f2a245 193 if ( phaseReg == REG0 ) {
roland_tmm 0:6069c0f2a245 194 phase0 = phaseInDeg;
roland_tmm 0:6069c0f2a245 195 }
roland_tmm 0:6069c0f2a245 196 else {
roland_tmm 0:6069c0f2a245 197 phase1 = phaseInDeg;
roland_tmm 0:6069c0f2a245 198 phaseVal |= PHASE1_WRITE_REG;
roland_tmm 0:6069c0f2a245 199 }
roland_tmm 0:6069c0f2a245 200 WriteRegister(phaseVal);
roland_tmm 0:6069c0f2a245 201 }
roland_tmm 0:6069c0f2a245 202
roland_tmm 0:6069c0f2a245 203 /*
roland_tmm 0:6069c0f2a245 204 * Increment the specified phase register by the phase (in degrees)
roland_tmm 0:6069c0f2a245 205 */
roland_tmm 0:6069c0f2a245 206 void AD9833 :: IncrementPhase ( Registers phaseReg, float phaseIncDeg ) {
roland_tmm 0:6069c0f2a245 207 // Add/subtract a value from the current phase programmed in
roland_tmm 0:6069c0f2a245 208 // phaseReg by the amount given
roland_tmm 0:6069c0f2a245 209 float phase = (phaseReg == REG0) ? phase0 : phase1;
roland_tmm 0:6069c0f2a245 210 SetPhase(phaseReg,phase + phaseIncDeg);
roland_tmm 0:6069c0f2a245 211 }
roland_tmm 0:6069c0f2a245 212
roland_tmm 0:6069c0f2a245 213 /*
roland_tmm 0:6069c0f2a245 214 * Set the type of waveform that is output for a frequency register
roland_tmm 0:6069c0f2a245 215 * SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, HALF_SQUARE_WAVE
roland_tmm 0:6069c0f2a245 216 */
roland_tmm 0:6069c0f2a245 217 void AD9833 :: SetWaveform ( Registers waveFormReg, WaveformType waveType ) {
roland_tmm 0:6069c0f2a245 218 // TODO: Add more error checking?
roland_tmm 0:6069c0f2a245 219 if ( waveFormReg == REG0 )
roland_tmm 0:6069c0f2a245 220 waveForm0 = waveType;
roland_tmm 0:6069c0f2a245 221 else
roland_tmm 0:6069c0f2a245 222 waveForm1 = waveType;
roland_tmm 0:6069c0f2a245 223 WriteControlRegister();
roland_tmm 0:6069c0f2a245 224 }
roland_tmm 0:6069c0f2a245 225
roland_tmm 0:6069c0f2a245 226 /*
roland_tmm 0:6069c0f2a245 227 * EnableOutput(false) keeps the AD9833 is RESET state until a call to
roland_tmm 0:6069c0f2a245 228 * EnableOutput(true). See the Reset function description.
roland_tmm 0:6069c0f2a245 229 */
roland_tmm 0:6069c0f2a245 230 void AD9833 :: EnableOutput ( bool enable ) {
roland_tmm 0:6069c0f2a245 231 outputEnabled = enable;
roland_tmm 0:6069c0f2a245 232 WriteControlRegister();
roland_tmm 0:6069c0f2a245 233 }
roland_tmm 0:6069c0f2a245 234
roland_tmm 0:6069c0f2a245 235 /*
roland_tmm 0:6069c0f2a245 236 * Set which frequency and phase register is being used to output the
roland_tmm 0:6069c0f2a245 237 * waveform. If phaseReg is not supplied, it defaults to the same
roland_tmm 0:6069c0f2a245 238 * register as freqReg.
roland_tmm 0:6069c0f2a245 239 */
roland_tmm 0:6069c0f2a245 240 void AD9833 :: SetOutputSource ( Registers freqReg, Registers phaseReg ) {
roland_tmm 0:6069c0f2a245 241 // TODO: Add more error checking?
roland_tmm 0:6069c0f2a245 242 activeFreq = freqReg;
roland_tmm 0:6069c0f2a245 243 if ( phaseReg == SAME_AS_REG0 ) activePhase = activeFreq;
roland_tmm 0:6069c0f2a245 244 else activePhase = phaseReg;
roland_tmm 0:6069c0f2a245 245 WriteControlRegister();
roland_tmm 0:6069c0f2a245 246 }
roland_tmm 0:6069c0f2a245 247
roland_tmm 0:6069c0f2a245 248 //---------- LOWER LEVEL FUNCTIONS NOT NORMALLY NEEDED -------------
roland_tmm 0:6069c0f2a245 249
roland_tmm 0:6069c0f2a245 250 /*
roland_tmm 0:6069c0f2a245 251 * Disable/enable both the internal clock and the DAC. Note that square
roland_tmm 0:6069c0f2a245 252 * wave outputs are avaiable if using an external Reference.
roland_tmm 0:6069c0f2a245 253 * TODO: ?? IS THIS TRUE ??
roland_tmm 0:6069c0f2a245 254 */
roland_tmm 0:6069c0f2a245 255 void AD9833 :: SleepMode ( bool enable ) {
roland_tmm 0:6069c0f2a245 256 DacDisabled = enable;
roland_tmm 0:6069c0f2a245 257 IntClkDisabled = enable;
roland_tmm 0:6069c0f2a245 258 WriteControlRegister();
roland_tmm 0:6069c0f2a245 259 }
roland_tmm 0:6069c0f2a245 260
roland_tmm 0:6069c0f2a245 261 /*
roland_tmm 0:6069c0f2a245 262 * Enables / disables the DAC. It will override any previous DAC
roland_tmm 0:6069c0f2a245 263 * setting by Waveform type, or via the SleepMode function
roland_tmm 0:6069c0f2a245 264 */
roland_tmm 0:6069c0f2a245 265 void AD9833 :: DisableDAC ( bool enable ) {
roland_tmm 0:6069c0f2a245 266 DacDisabled = enable;
roland_tmm 0:6069c0f2a245 267 WriteControlRegister();
roland_tmm 0:6069c0f2a245 268 }
roland_tmm 0:6069c0f2a245 269
roland_tmm 0:6069c0f2a245 270 /*
roland_tmm 0:6069c0f2a245 271 * Enables / disables the internal clock. It will override any
roland_tmm 0:6069c0f2a245 272 * previous clock setting by the SleepMode function
roland_tmm 0:6069c0f2a245 273 */
roland_tmm 0:6069c0f2a245 274 void AD9833 :: DisableInternalClock ( bool enable ) {
roland_tmm 0:6069c0f2a245 275 IntClkDisabled = enable;
roland_tmm 0:6069c0f2a245 276 WriteControlRegister();
roland_tmm 0:6069c0f2a245 277 }
roland_tmm 0:6069c0f2a245 278
roland_tmm 0:6069c0f2a245 279 // ------------ STATUS / INFORMATION FUNCTIONS -------------------
roland_tmm 0:6069c0f2a245 280 /*
roland_tmm 0:6069c0f2a245 281 * Return actual frequency programmed
roland_tmm 0:6069c0f2a245 282 */
roland_tmm 0:6069c0f2a245 283 float AD9833 :: GetActualProgrammedFrequency ( Registers reg ) {
roland_tmm 0:6069c0f2a245 284 float frequency = reg == REG0 ? frequency0 : frequency1;
roland_tmm 0:6069c0f2a245 285 int32_t freqWord = (uint32_t)((frequency * pow2_28) / (float)refFrequency) & 0x0FFFFFFFUL;
roland_tmm 0:6069c0f2a245 286 return (float)freqWord * (float)refFrequency / (float)pow2_28;
roland_tmm 0:6069c0f2a245 287 }
roland_tmm 0:6069c0f2a245 288
roland_tmm 0:6069c0f2a245 289 /*
roland_tmm 0:6069c0f2a245 290 * Return actual phase programmed
roland_tmm 0:6069c0f2a245 291 */
roland_tmm 0:6069c0f2a245 292 float AD9833 :: GetActualProgrammedPhase ( Registers reg ) {
roland_tmm 0:6069c0f2a245 293 float phase = reg == REG0 ? phase0 : phase1;
roland_tmm 0:6069c0f2a245 294 uint16_t phaseVal = (uint16_t)(BITS_PER_DEG * phase) & 0x0FFF;
roland_tmm 0:6069c0f2a245 295 return (float)phaseVal / BITS_PER_DEG;
roland_tmm 0:6069c0f2a245 296 }
roland_tmm 0:6069c0f2a245 297
roland_tmm 0:6069c0f2a245 298 /*
roland_tmm 0:6069c0f2a245 299 * Return frequency resolution
roland_tmm 0:6069c0f2a245 300 */
roland_tmm 0:6069c0f2a245 301 float AD9833 :: GetResolution ( void ) {
roland_tmm 0:6069c0f2a245 302 return (float)refFrequency / (float)pow2_28;
roland_tmm 0:6069c0f2a245 303 }
roland_tmm 0:6069c0f2a245 304
roland_tmm 0:6069c0f2a245 305 // --------------------- PRIVATE FUNCTIONS --------------------------
roland_tmm 0:6069c0f2a245 306
roland_tmm 0:6069c0f2a245 307 /*
roland_tmm 0:6069c0f2a245 308 * Write control register. Setup register based on defined states
roland_tmm 0:6069c0f2a245 309 */
roland_tmm 0:6069c0f2a245 310 void AD9833 :: WriteControlRegister ( void ) {
roland_tmm 0:6069c0f2a245 311 uint16_t waveForm;
roland_tmm 0:6069c0f2a245 312 // TODO: can speed things up by keeping a writeReg0 and writeReg1
roland_tmm 0:6069c0f2a245 313 // that presets all bits during the various setup function calls
roland_tmm 0:6069c0f2a245 314 // rather than setting flags. Then we could just call WriteRegister
roland_tmm 0:6069c0f2a245 315 // directly.
roland_tmm 0:6069c0f2a245 316 if ( activeFreq == REG0 ) {
roland_tmm 0:6069c0f2a245 317 waveForm = waveForm0;
roland_tmm 0:6069c0f2a245 318 waveForm &= ~FREQ1_OUTPUT_REG;
roland_tmm 0:6069c0f2a245 319 }
roland_tmm 0:6069c0f2a245 320 else {
roland_tmm 0:6069c0f2a245 321 waveForm = waveForm1;
roland_tmm 0:6069c0f2a245 322 waveForm |= FREQ1_OUTPUT_REG;
roland_tmm 0:6069c0f2a245 323 }
roland_tmm 0:6069c0f2a245 324 if ( activePhase == REG0 )
roland_tmm 0:6069c0f2a245 325 waveForm &= ~PHASE1_OUTPUT_REG;
roland_tmm 0:6069c0f2a245 326 else
roland_tmm 0:6069c0f2a245 327 waveForm |= PHASE1_OUTPUT_REG;
roland_tmm 0:6069c0f2a245 328 if ( outputEnabled )
roland_tmm 0:6069c0f2a245 329 waveForm &= ~RESET_CMD;
roland_tmm 0:6069c0f2a245 330 else
roland_tmm 0:6069c0f2a245 331 waveForm |= RESET_CMD;
roland_tmm 0:6069c0f2a245 332 if ( DacDisabled )
roland_tmm 0:6069c0f2a245 333 waveForm |= DISABLE_DAC;
roland_tmm 0:6069c0f2a245 334 else
roland_tmm 0:6069c0f2a245 335 waveForm &= ~DISABLE_DAC;
roland_tmm 0:6069c0f2a245 336 if ( IntClkDisabled )
roland_tmm 0:6069c0f2a245 337 waveForm |= DISABLE_INT_CLK;
roland_tmm 0:6069c0f2a245 338 else
roland_tmm 0:6069c0f2a245 339 waveForm &= ~DISABLE_INT_CLK;
roland_tmm 0:6069c0f2a245 340
roland_tmm 0:6069c0f2a245 341 WriteRegister ( waveForm );
roland_tmm 0:6069c0f2a245 342 }
roland_tmm 0:6069c0f2a245 343
roland_tmm 0:6069c0f2a245 344 void AD9833 :: FNCWrite(int val){
roland_tmm 0:6069c0f2a245 345 fnc_pin = val;
roland_tmm 0:6069c0f2a245 346 }
roland_tmm 0:6069c0f2a245 347
roland_tmm 0:6069c0f2a245 348 void AD9833 :: WriteRegister ( int16_t dat ) {
roland_tmm 0:6069c0f2a245 349 /*
roland_tmm 0:6069c0f2a245 350 * We set the mode here, because other hardware may be doing SPI also
roland_tmm 0:6069c0f2a245 351 */
roland_tmm 0:6069c0f2a245 352 SPI_dev.format(8, 2);
roland_tmm 0:6069c0f2a245 353
roland_tmm 0:6069c0f2a245 354 /* Improve overall switching speed
roland_tmm 0:6069c0f2a245 355 * Note, the times are for this function call, not the write.
roland_tmm 0:6069c0f2a245 356 * digitalWrite(FNCpin) ~ 17.6 usec
roland_tmm 0:6069c0f2a245 357 * digitalWriteFast2(FNC_PIN) ~ 8.8 usec
roland_tmm 0:6069c0f2a245 358 */
roland_tmm 0:6069c0f2a245 359 FNCWrite(0); // FNCpin low to write to AD9833
roland_tmm 0:6069c0f2a245 360
roland_tmm 0:6069c0f2a245 361 //delayMicroseconds(2); // Some delay may be needed
roland_tmm 0:6069c0f2a245 362
roland_tmm 0:6069c0f2a245 363 // TODO: Are we running at the highest clock rate?
roland_tmm 0:6069c0f2a245 364 SPI_dev.write(dat & 0xF0); // Transmit 16 bits 8 bits at a time
roland_tmm 0:6069c0f2a245 365 SPI_dev.write(dat & 0x0F);
roland_tmm 0:6069c0f2a245 366
roland_tmm 0:6069c0f2a245 367 FNCWrite(1); // Write done
roland_tmm 0:6069c0f2a245 368 }