Generator AD9833 - Arduino - changing frequency of the generator base - Page 1
Forum section: electronics / general

author: max-bit
author_rank: Frequent Contributor
timestamp: January 02, 2019, 04:53:21 pm
content: I want to use such a generator (based on arduino)https://github.com/Billwilliams1952/AD9833-Library-ArduinoAnd then he wants to change changing frequency of the generator base oryginal 25 MHz change to 10 MHzThe question is, where there in the code to insert an entry to the generator base was frequency 10 MHz?File AD9833.hCode :ifndef __AD9833__#define __AD9833__#include <Arduino.h>#include <SPI.h>//#define FNC_PIN 4 // Define FNC_PIN for fast digital writes#ifdef FNC_PIN// Use digitalWriteFast for a speedup#include "digitalWriteFast.h"#define WRITE_FNCPIN(Val) digitalWriteFast2(FNC_PIN,(Val))#else // otherwise, just use digitalWrite#define WRITE_FNCPIN(Val) digitalWrite(FNCpin,(Val))#endif#define pow2_28 268435456L // 2^28 used in frequency word calculation#define BITS_PER_DEG 11.3777777777778 // 4096 / 360#define RESET_CMD 0x0100 // Reset enabled (also CMD RESET)/* Sleep mode* D7 1 = internal clock is disabled* D6 1 = put DAC to sleep*/#define SLEEP_MODE 0x00C0 // Both DAC and Internal Clock#define DISABLE_DAC 0x0040#define DISABLE_INT_CLK 0x0080#define PHASE_WRITE_CMD 0xC000 // Setup for Phase write#define PHASE1_WRITE_REG 0x2000 // Which phase register#define FREQ0_WRITE_REG 0x4000 //#define FREQ1_WRITE_REG 0x8000#define PHASE1_OUTPUT_REG 0x0400 // Output is based off REG0/REG1#define FREQ1_OUTPUT_REG 0x0800 // dittotypedef enum { SINE_WAVE = 0x2000, TRIANGLE_WAVE = 0x2002,SQUARE_WAVE = 0x2028, HALF_SQUARE_WAVE = 0x2020 } WaveformType;typedef enum { REG0, REG1, SAME_AS_REG0 } Registers;class AD9833 {public:AD9833 ( uint8_t FNCpin, uint32_t referenceFrequency = 25000000UL );// Must be the first command after creating the AD9833 object.void Begin ( void );// Setup and apply a signal. Note that any calls to EnableOut,// SleepMode, DisableDAC, or DisableInternalClock remain in effectvoid ApplySignal ( WaveformType waveType, Registers freqReg,float frequencyInHz,Registers phaseReg = SAME_AS_REG0, float phaseInDeg = 0.0 );// Resets internal registers to 0, which corresponds to an output of// midscale - digital output at 0. See EnableOutput functionvoid Reset ( void );// Update just the frequency in REG0 or REG1void SetFrequency ( Registers freqReg, float frequency );// Increment the selected frequency register by freqIncHzvoid IncrementFrequency ( Registers freqReg, float freqIncHz );// Update just the phase in REG0 or REG1void SetPhase ( Registers phaseReg, float phaseInDeg );// Increment the selected phase register by phaseIncDegvoid IncrementPhase ( Registers phaseReg, float phaseIncDeg );// Set the output waveform for the selected frequency register// SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, HALF_SQUARE_WAVE,void SetWaveform ( Registers waveFormReg, WaveformType waveType );// Output based on the contents of REG0 or REG1void SetOutputSource ( Registers freqReg, Registers phaseReg = SAME_AS_REG0 );// Turn ON / OFF output using the RESET command.void EnableOutput ( bool enable );// Enable/disable Sleep mode. Internal clock and DAC disabledvoid SleepMode ( bool enable );// Enable / Disable DACvoid DisableDAC ( bool enable );// Enable / Disable Internal Clockvoid DisableInternalClock ( bool enable );// Return actual frequency programmed in registerfloat GetActualProgrammedFrequency ( Registers reg );// Return actual phase programmed in registerfloat GetActualProgrammedPhase ( Registers reg );// Return frequency resolutionfloat GetResolution ( void );private:void WriteRegister ( int16_t dat );void WriteControlRegister ( void );uint16_t waveForm0, waveForm1;#ifndef FNC_PINuint8_t FNCpin;#endifuint8_t outputEnabled, DacDisabled, IntClkDisabled;uint32_t refFrequency;float frequency0, frequency1, phase0, phase1;Registers activeFreq, activePhase;};#endifarduino :#include <AD9833.h> // Include the library#define FNC_PIN 4 // Can be any digital IO pin//--------------- Create an AD9833 object ----------------// Note, SCK and MOSI must be connected to CLK and DAT pins on the AD9833 for SPIAD9833 gen(FNC_PIN); // Defaults to 25MHz internal reference frequencyvoid setup() {// This MUST be the first command after declaring the AD9833 objectgen.Begin();// Apply a 1000 Hz sine wave using REG0 (register set 0). There are two register sets,// REG0 and REG1.// Each one can be programmed for:// Signal type - SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, and HALF_SQUARE_WAVE// Frequency - 0 to 12.5 MHz// Phase - 0 to 360 degress (this is only useful if it is 'relative' to some other signal// such as the phase difference between REG0 and REG1).// In ApplySignal, if Phase is not given, it defaults to 0.gen.ApplySignal(SINE_WAVE,REG0,1000);gen.EnableOutput(true); // Turn ON the output - it defaults to OFF// There should be a 1000 Hz sine wave on the output of the AD9833}void loop() {// To change the signal, you can just call ApplySignal again with a new frequency and/or signal// type.}

author: rstofer
author_rank: Super Contributor
timestamp: January 02, 2019, 05:36:43 pm
content: Maybe here?AD9833 ( uint8_t FNCpin, uint32_t referenceFrequency = 25000000UL );

author: max-bit
author_rank: Frequent Contributor
timestamp: January 02, 2019, 05:44:33 pm
content: no ....

author: max-bit
author_rank: Frequent Contributor
timestamp: January 03, 2019, 09:57:27 pm
content: Can anyone help me?

author: max-bit
author_rank: Frequent Contributor
timestamp: January 06, 2019, 09:19:47 am
content: and up ..

author: RoGeorge
author_rank: Super Contributor
timestamp: January 06, 2019, 09:45:25 am
content: That file is part of a library. In order to use a library, you need to write a program that make calls to that library.Arduino libraries usually have one or many examples of programs that uses the library.You need to install the library, than go to the examples and compile/run them.For the link you gave, the examples are in the folderhttps://github.com/Billwilliams1952/AD9833-Library-Arduino/tree/master/examples, but if you installed the library corectly, you should see the examples in the Arduino 'Examples' menu, in the graphical interface.For the example program "ApplySignal.ino", the change to generate 10MHz will be to change 1000 into 10000000 (in the line 43 of the example program filehttps://github.com/Billwilliams1952/AD9833-Library-Arduino/blob/master/examples/ApplySignal/ApplySignal.ino):Code:// In ApplySignal, if Phase is not given, it defaults to 0.gen.ApplySignal(SINE_WAVE,REG0,1000);

author: max-bit
author_rank: Frequent Contributor
timestamp: January 06, 2019, 09:55:22 am
content: base generatorNO SIGNAL OUT !!!

author: RoGeorge
author_rank: Super Contributor
timestamp: January 06, 2019, 10:26:22 am
content: I would test first if it works with the unmodified example, at 1000 Hz. Does it output a sine of 1000Hz?Try the other test program, too. Does the second example works? Can you see the two examples in the Arduino menu, in a menu like this one,https://www.arduino.cc/en/Tutorial/BuiltInExamples, but for the AD9833?Later edit:Oh, and you need to write10000000UL, not10000000. UL at the end of the number means unsigned long.

author: max-bit
author_rank: Frequent Contributor
timestamp: January 06, 2019, 10:33:12 am
content: It does not work either. Like UL and without UL

author: RoGeorge
author_rank: Super Contributor
timestamp: January 06, 2019, 10:45:19 am
content: You didn't answered the previous questions:1. Test it first with the unmodified example, at 1000 Hz.Does it output a sine of 1000Hz?2.Can you see the two examples in the Arduino menu "EXAMPLES FROM LIBRARIES:"?(you should have a menu like this one,https://www.arduino.cc/en/Tutorial/BuiltInExamples,but for the AD9833)3.Does the second example works?

author: max-bit
author_rank: Frequent Contributor
timestamp: January 06, 2019, 07:02:36 pm
content: Here you need the appropriate entry in AD9833.hProbably .... unfortunately I do not know how much ....

author: max-bit
author_rank: Frequent Contributor
timestamp: January 08, 2019, 10:44:54 pm
content: and nothing ....

author: MarkF
author_rank: Super Contributor
timestamp: January 08, 2019, 11:46:24 pm
content: Quote from: max-bit on January 06, 2019, 07:02:36 pmNo.. No.. No..You DO NOT modify the AD9833.h or the AD9833.c files.You modify the main program using the AD9833 library.Refer to the AD9833 class constructor definition.Review creating objects in C++ programs.Also, learn how to use the forum code tags.Here is an excerpt from the example driver program provided in GitHub.Code:/** AD9833_test_suite.ino* 2016 WLWilliams** This sketch demonstrates the use of the AD9833 DDS module library.** If you don't have an oscilloscope or spectrum analyzer, I don't quite know how you will* verify correct operation for some of the functions.* TODO: Add tests where the Arduino itself vereifies AD9833 basic operation. Frequency of* square wave, sine/triangular wave using the A/D inputs (would need a level shifter).** This program is free software: you can redistribute it and/or modify it under* the terms of the GNU General Public License as published by the Free Software Foundation,* either version 3 of the License, or (at your option) any later version.* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.* See the GNU General Public License for more details. You should have received a copy of* the GNU General Public License along with this program.* If not, see <http://www.gnu.org/licenses/>.** This example code is in the public domain.** Library code found at: https://github.com/Billwilliams1952/AD9833-Library-Arduino**/#include <AD9833.h>#define RUNNING F("\tRUNNING")#define NOT_RUNNING F("")#define ON F("ON")#define OFF F("OFF")#define LED_PIN 13 // I'm alive blinker#define FNC_PIN 4 // Any digital pin. Used to enable SPI transfers (active LO// Some macros to 'improve' readability#define BLINK_LED digitalWrite(LED_PIN,millis()%1000 > 500);/** We need to manually call serialEventRun since we're not returning through the loop()* function while inside the test functions. If a character is in the receive buffer,* exit the test function. We also blink the I'm Alive LED to give a visual indication* that the program is not hung up.*/#define YIELD_ON_CHAR if ( serialEventRun ) serialEventRun(); \if ( Serial.available() ) return; \BLINK_LED#define DELAY_WITH_YIELD for ( uint8_t i = 0; i < 10; i++ ) { \YIELD_ON_CHAR \delay(100); \}#define FLUSH_SERIAL_INPUT if ( serialEventRun ) serialEventRun(); \do { Serial.read(); delay(100); } while ( Serial.available() > 0 );//--------------- Create an AD9833 object ----------------// Note, SCK and MOSI must be connected to CLK and DAT pins on the AD9833 for SPI// ----- AD9833 ( FNCpin, referenceFrequency = 25000000UL )AD9833 gen(FNC_PIN); // Defaults to 25MHz internal reference frequencyvoid setup() {pinMode(LED_PIN,OUTPUT);while (!Serial); // Delay until terminal opensSerial.begin(9600);// This MUST be the first command after declaring the AD9833 objectgen.Begin(); // The loaded defaults are 1000 Hz SINE_WAVE using REG0// The output is OFF, Sleep mode is disabledgen.EnableOutput(false); // Turn ON the outputPrintMenu(0,true); // Display menu for the first time}You change the object creation in line 61 fromCode://--------------- Create an AD9833 object ----------------// Note, SCK and MOSI must be connected to CLK and DAT pins on the AD9833 for SPI// ----- AD9833 ( FNCpin, referenceFrequency = 25000000UL )AD9833 gen(FNC_PIN); // Defaults to 25MHz internal reference frequencytoCode:AD9833 gen(FNC_PIN, 10000000UL); // Set reference frequency to 10MHz