Author Topic: Generator AD9833 - Arduino - changing frequency of the generator base  (Read 5541 times)

0 Members and 1 Guest are viewing this topic.

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
I want to use such a generator (based on arduino)

https://github.com/Billwilliams1952/AD9833-Library-Arduino

And then he wants to change changing frequency of the generator base oryginal 25 MHz change to 10 MHz

The question is, where there in the code to insert an entry to the generator base was  frequency 10 MHz?

File AD9833.h
Code :

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      // ditto

typedef 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 effect
   void 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 function
   void Reset ( void );

   // Update just the frequency in REG0 or REG1
   void SetFrequency ( Registers freqReg, float frequency );

   // Increment the selected frequency register by freqIncHz
   void IncrementFrequency ( Registers freqReg, float freqIncHz );

   // Update just the phase in REG0 or REG1
   void SetPhase ( Registers phaseReg, float phaseInDeg );

   // Increment the selected phase register by phaseIncDeg
   void 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 REG1
   void 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 disabled
   void SleepMode ( bool enable );

   // Enable / Disable DAC
   void DisableDAC ( bool enable );

   // Enable / Disable Internal Clock
   void DisableInternalClock ( bool enable );

   // Return actual frequency programmed in register
   float GetActualProgrammedFrequency ( Registers reg );

   // Return actual phase programmed in register
   float GetActualProgrammedPhase ( Registers reg );

   // Return frequency resolution
   float GetResolution ( void );

private:

   void          WriteRegister ( int16_t dat );
   void          WriteControlRegister ( void );
   uint16_t      waveForm0, waveForm1;
#ifndef FNC_PIN
   uint8_t         FNCpin;
#endif
   uint8_t         outputEnabled, DacDisabled, IntClkDisabled;
   uint32_t      refFrequency;
   float         frequency0, frequency1, phase0, phase1;
   Registers      activeFreq, activePhase;
};

#endif


arduino :

#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 SPI
AD9833 gen(FNC_PIN);       // Defaults to 25MHz internal reference frequency

void setup() {
    // This MUST be the first command after declaring the AD9833 object
    gen.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.
}
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #1 on: January 02, 2019, 05:36:43 pm »
Maybe here?

AD9833 ( uint8_t FNCpin, uint32_t referenceFrequency = 25000000UL );
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #2 on: January 02, 2019, 05:44:33 pm »
no .... :)
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #3 on: January 03, 2019, 09:57:27 pm »
Can anyone help me?  ::)
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #4 on: January 06, 2019, 09:19:47 am »
and up ..
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6202
  • Country: ro
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #5 on: January 06, 2019, 09:45:25 am »
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 folder https://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 file https://github.com/Billwilliams1952/AD9833-Library-Arduino/blob/master/examples/ApplySignal/ApplySignal.ino):

Code: [Select]
    // In ApplySignal, if Phase is not given, it defaults to 0.
   gen.ApplySignal(SINE_WAVE,REG0,1000);

« Last Edit: January 06, 2019, 09:49:33 am by RoGeorge »
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #6 on: January 06, 2019, 09:55:22 am »
base generator
NO SIGNAL OUT !!!
 |O
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6202
  • Country: ro
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #7 on: January 06, 2019, 10:26:22 am »
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 write 10000000UL, not 10000000.  UL at the end of the number means unsigned long.
« Last Edit: January 06, 2019, 10:32:05 am by RoGeorge »
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #8 on: January 06, 2019, 10:33:12 am »
It does not work either. Like UL and without UL :)
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6202
  • Country: ro
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #9 on: January 06, 2019, 10:45:19 am »
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?

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #10 on: January 06, 2019, 07:02:36 pm »
Here you need the appropriate entry in AD9833.h
Probably .... unfortunately I do not know how much ....
 

Offline max-bitTopic starter

  • Frequent Contributor
  • **
  • Posts: 672
  • Country: pl
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #11 on: January 08, 2019, 10:44:54 pm »
and nothing ....
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2544
  • Country: us
Re: Generator AD9833 - Arduino - changing frequency of the generator base
« Reply #12 on: January 08, 2019, 11:46:24 pm »
Here you need the appropriate entry in AD9833.h
Probably .... unfortunately I do not know how much ....

No..  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: [Select]
/*
 * 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: [url]https://github.com/Billwilliams1952/AD9833-Library-Arduino[/url]
 *
 */


#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 frequency

void setup() {
    pinMode(LED_PIN,OUTPUT);

    while (!Serial);          // Delay until terminal opens
    Serial.begin(9600);

    // This MUST be the first command after declaring the AD9833 object
    gen.Begin();              // The loaded defaults are 1000 Hz SINE_WAVE using REG0
                              // The output is OFF, Sleep mode is disabled
    gen.EnableOutput(false);  // Turn ON the output

    PrintMenu(0,true);        // Display menu for the first time
}


You change the object creation in line 61 from

Code: [Select]
//--------------- 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 frequency

to

Code: [Select]
AD9833 gen(FNC_PIN, 10000000UL);       // Set reference frequency to 10MHz
« Last Edit: January 09, 2019, 12:12:36 am by MarkF »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf