Giter Site home page Giter Site logo

ad9833-library-arduino's Introduction

AD9833-Library-Arduino

Synopsis

Library to control the AD9833 DDS waveform generator. The library allows the user to independently program frequency, phase, and waveform type for both registers.

From Analog Devices data sheet: (http://www.analog.com/media/en/technical-documentation/data-sheets/AD9833.pdf)

"The AD9833 is a low power, programmable waveform generator capable of producing sine, triangular, and square wave outputs. Waveform generation is required in various types of sensing, actuation, and time domain reflectometry (TDR) applications. The output frequency and phase are software programmable, allowing easy tuning. No external components are needed. The frequency registers are 28 bits wide: with a 25 MHz clock rate, resolution of 0.1 Hz can be achieved; with a 1 MHz clock rate, the AD9833 can be tuned to 0.004 Hz resolution. The AD9833 is written to via a 3-wire serial interface. This serial interface operates at clock rates up to 40 MHz and is compatible with DSP and microcontroller standards. The device operates with a power supply from 2.3 V to 5.5 V."

Updates

Version Date Description
- - Initial Release
6/2/2018 Added simple ApplySignal.ino file to examples directory

Installation

Download the ZIP file and extract into your sketchbook/libraries directory. Exit the Arduino program (if open) and restart it to see the AD9833 library along with its sketch examples.

The AD9833 uses SPI for communication. The following connections are required:

AD9833 Pin Arduino Pin Description
CLK SCK SPI Clock pin
DAT MOSI SPI Master Out Slave In data pin
FNC User defined SPI transfer enable (active LOW)
VCC 5V Recommend a 10 uF capacitor in parallel with a 0.1 uF capacitor connected between this pin and ground.
GND GND see comment above

API Reference

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

// Must be the first command after creating the AD9833 object.
void Begin ( void );

// The difference between Reset() and EnableOutput(false) is that
// EnableOutput(false) keeps the AD9833 in the RESET state until you
// specifically remove the RESET state using EnableOutput(true).
// With a call to Reset(), ANY subsequent call to ANY function (other
// than Reset itself and Set/IncrementPhase) will also remove the
// RESET state.
void Reset ( 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  );

// 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 EnableDAC ( bool enable );

// Enable / Disable Internal Clock
void EnableInternalClock ( 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 );

This program uses the Arduino API (Arduino.h and spi.h); no other special libraries are required. It has been tested on the Arduino Micro.

Tests

Use the AD9833_test_suite example sketch to verify correct operation. Note that an oscilloscope and / or a spectrum analzer are required to completely verify correct operation.

alt tag

alt tag

License

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

ad9833-library-arduino's People

Contributors

billwilliams1952 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ad9833-library-arduino's Issues

Tried to make a beeping sound

Hi,

I tried to make a beeping sound so I used gen.SleepMode(true); and gen.SleepMode(false); to start and stop the sound. But I get a clicking noise. Is there a better way to realise beeping?

for ESP32?

Hello. How can this library be used for ESP32? Can you add a selection of arbitrary pins for SPI?

Compiling error with the test suite

Hi Bill,

I'm trying to compile the test suite in the Arduino 1.8.1 IDE, but I get an error:

AD9833_test_suite.ino:

In function 'void setup()':
AD9833_test_suite:66: error: 'CLOCK_OUTPUT' was not declared in this scope
pinMode(CLOCK_OUTPUT,OUTPUT);
^
exit status 1
'CLOCK_OUTPUT' was not declared in this scope

Supposedly CLOCK_OUTPUT may be defined as the pin used for SCLK in SPI.

Any clues what could have gone wrong?
Thanks for any suggestions!

(I thought I'd better ask before I start messing up the code. At least, when I insert a declaration of CLOCK_OUTPUT, the error is gone.
Also renaming CLOCK_OUTPUT with SCK removes the compile error).

SPWM

hi is it possible to create a SPWM with an Ad9833? If not, can I generate a sine and a triangle curve at the same time with one board or do I need two boards for this?

Using of alternate SPI

Hi,
Please take a look - this platform has additional SPI buses.
https://github.com/stm32duino/wiki/wiki/API#spi
When we are using an ISP OLED, on such platforms, we can direct the AD9833 to an alternative bus
I am doing tests, maybe you can implement better in code by a #define

At beginning of AD9833.cpp
/* Configure SPI3 of STM32 MOSI: PC12, MISO: PC11, SCLK: PC10 */
SPIClass SPIX(PC12, PC11, PC10);

Replace the SPI. with SPIX. used in 4 commands

Add minimal example to documentation

Hi. I am attempting to use your library to control an AD9833 board module. I can see the default ~1000Hz sine wave on my scope. I am trying to generate a sine wave of a different frequency, say 300 Hz. I can program the frequency and read it back with GetActualProgrammedFrequency.

****** AD9833 Frequency Menu ******

Current programmed frequency is 999.96106
Enter an float frequency (HZ) >
Sine wave frequency 300.00 Hz.

****** AD9833 Frequency Menu ******

Current programmed frequency is 299.97900
Enter an float frequency (HZ) >

The sine wave on the scope is still ~1000Hz, however. The fact that I can read back a programmed frequency close to 300 Hz indicates to me that my comms are fine. The only thing I can think of is that I have to latch the new configuration somehow.

Could you please add a minimal example that changes the default waveform to a different frequency? What I am using (shown below) is close to minimal but still doesn't work. I've tried different combination of EnableOutput and Reset, etc, trying to get the frequency to change but still no dice. A minimal example would be a useful addition I think. Thanks.

void setup() { 

    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(true);
    
    PrintMenu();        // Display menu for the first time
}

void StartWave(float hz)
{
  if (hz < 0)
  {
    Serial.print(F("*** Invalid frequency "));
    Serial.print(hz);
    Serial.println(F(" ignored ***"));
    return;
  }

  gen.ApplySignal(SINE_WAVE,REG0,hz);
  Serial.print(F("Sine wave frequency "));
  Serial.print(hz);
  Serial.println(F(" Hz."));
}

void loop() { 

    if ( Serial.available() ) {
        float hz = Serial.parseFloat();
                
        StartWave(hz);
        PrintMenu();
    }
}

/* 
* Display the command menu
*/
void PrintMenu () {
    float programmedFrequency = gen.GetActualProgrammedFrequency(REG0);
    char  buffer[20];   // 14 characters actually needed for display    
    dtostrf(programmedFrequency,14,5,buffer); 
    
    Serial.println(); Serial.println();
    Serial.println(F("****** AD9833 Frequency Menu ******\n"));
    Serial.print(F("Current programmed frequency is "));
    Serial.println(buffer);
    Serial.println(F("Enter an float frequency (HZ) >"));
}

Problems when used with LiquidCrystal

I've got an issue using this library with the LiquidCrystal library. Below is the .ino file that I have uploaded to my Uno.
I'm getting corrupt/cycling data showing on the LCD after calling gen.Begin(). I've removed the section that sorts out the generator which fixes the problem, and reintroduced just the gen.Begin() line which messes up the LCD again.
I'm pretty sure i've got everything wired up as it should be. Is it a case of the two libraries clashing somehow? I'm a bit confused by it.
Thanks



#include <AD9833.h>
#include <digitalWriteFast.h>

#include <LiquidCrystal.h>

#define FNC_PIN 10

LiquidCrystal lcd(12, 7, 5, 4, 3, 2);

int Htime;              //integer for storing high time
int Ltime;                //integer for storing low time
float Ttime;            // integer for storing total time of a cycle
float frequency;        //storing frequency

// Declare the AD9833 object
AD9833 gen(FNC_PIN);  // Defaults to 25MHz internal reference frequency

void setup()
{
    pinMode(8,INPUT);
    lcd.begin(16, 2);

    gen.Begin();    // The loaded defaults are 1000Hz SINE_WAVE using REG0 with the output off
    gen.SetWaveform(REG0,SQUARE_WAVE);
    gen.SetFrequency(REG0,1000.0);
    gen.EnableOutput(true);  // Turn ON the output
}
void loop()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Frequency:");

    Htime=pulseIn(8,HIGH);      //read high time
    Ltime=pulseIn(8,LOW);        //read low time
    
    Ttime = Htime+Ltime;

    frequency=1000000/Ttime;    //getting frequency with Ttime is in Micro seconds
    lcd.setCursor(0,1);
    lcd.print(frequency);
    lcd.print(" Hz");
    delay(2000);
}


Cant set freqency above 32khz

I am trying to generate a sine wave at 60khz, however this library does not seem to allow a frequency over 32khz. Ideas?

Images for README.MD

Phase register test. 1 KHz Triangle wave at at 0 degree phase in register0, 2 KHz sine wave at 180 degrees in register1.

newfile11

fmod crashes teensy 4.1, fmodf does not

when compiling and flashing this library as-is, my teensy 4.1 would crash, at least I would fail to get any serial logs out of the device... after debugging, I found the fmod function is for doubles, but fmodf is for floats. Thus these changes are suggested:

 void AD9833 :: SetPhase ( Registers phaseReg, float phaseInDeg ) {
        // Sanity checks on input
-       phaseInDeg = fmod(phaseInDeg,360);
-       if ( phaseInDeg < 0 ) phaseInDeg += 360;
+       // fmod crashes the teensy, fmodf does not
+       phaseInDeg = fmodf(phaseInDeg, 360.);
+       if ( phaseInDeg < 0 ) phaseInDeg += 360.;

Use Library with multiple AD9833 circuits

Hi,
I'm trying to get this library working with multiple AD9833 circuits connected on the same SPI-bus.
So I've added the following;
AD9833.cpp

void AD9833 :: SetFNCPin ( uint8_t FNCpin ) {
	this->FNCpin = FNCpin;
}

AD9833.h

// Change FNCPin
void SetFNCPin ( uint8_t FNCpin );

However it doesn't really seem to work properly. One of them sometimes get the command that was supposed for the other one, but could perhaps be related to an electrical issue. I just wonder if there's anything more I should do to the library?

Interface of AD9833 with pic24fj64ga406

My requirement is to generate a sinewave of 1MHz with AD9833 IC using PIC24FJ64GA404 controller. Kindly provide some example codes using PIC24 series controller.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.