Function generator using arduino uno

hello, everybody, I am trying to build a function generator using AD9833 with Arduino Uno, I have found a good reference for this project and I have used its code but unfortunately, the Arduino programming continuously tells me that there is an error! I can’t specify what the exact problem in my code.
the source of code: https://github.com/ioxhop/AD2833-Function-generator/blob/master/Function_generator/AD9833.ino

#include <SPI.h>

void AD9833setup() {
  #define FSYNC 13
  pinMode(FSYNC, OUTPUT);
  digitalWrite(FSYNC, HIGH);
  
  SPI.begin();
  delay(50); 

  AD9833reset();
}

// AD9833 documentation advises a 'Reset' on first applying power.
void AD9833reset() {
  WriteRegister(0x100);   // Write '1' to AD9833 Control register bit D8.
  delay(10);
}

// Set the frequency and waveform registers in the AD9833.
void AD9833setFrequency(long frequency, int Waveform) {

  long FreqWord = (frequency * pow(2, 28)) / 25.0E6;

  int MSB = (int)((FreqWord & 0xFFFC000) >> 14);    //Only lower 14 bits are used for data
  int LSB = (int)(FreqWord & 0x3FFF);
  
  //Set control bits 15 ande 14 to 0 and 1, respectively, for frequency register 0
  LSB |= 0x4000;
  MSB |= 0x4000; 
  
  WriteRegister(0x2100);   
  WriteRegister(LSB);                  // Write lower 16 bits to AD9833 registers
  WriteRegister(MSB);                  // Write upper 16 bits to AD9833 registers.
  WriteRegister(0xC000);               // Phase register
  WriteRegister(Waveform);             // Exit & Reset to SINE, SQUARE or TRIANGLE

}

void WriteRegister(int dat) { 
  SPI.setDataMode(SPI_MODE2);       
  
  digitalWrite(FSYNC, LOW);           // Set FSYNC low before writing to AD9833 registers
  delayMicroseconds(10);              // Give AD9833 time to get ready to receive data.
  
  SPI.transfer(dat>>8);               // Each AD9833 register is 32 bits wide and each 16
  SPI.transfer(dat&0xFF);             // bits has to be transferred as 2 x 8-bit bytes.

  digitalWrite(FSYNC, HIGH);          //Write done. Set FSYNC high

  SPI.setDataMode(SPI_MODE0);
}

so please if anyone can help let me know the problem.

thanks in advance

I can't specify what the exact problem in my code.

How about posting the full error message ?

thanks for replying :slight_smile:

How about posting the full error message ?

I really can't understand it but it was :

Arduino: 1.8.2 (Windows 10), TD: 1.36, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Faster, US English"

Archiving built core (caching) in: C:\Users\user\AppData\Local\Temp\arduino_cache_144970\core\core_teensy_avr_teensy31_usb_serial,speed_96,opt_o2std,keys_en-us_71a00981eaa0bad0868b6b2ed88d327b.a
C:\Users\user\AppData\Local\Temp\arduino_build_713331/core\core.a(main.cpp.o): In function `main':

C:\Program Files\Arduino\hardware\teensy\avr\cores\teensy3/main.cpp:21: undefined reference to `setup'

C:\Program Files\Arduino\hardware\teensy\avr\cores\teensy3/main.cpp:23: undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

Error compiling for board Teensy 3.2 / 3.1.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

That code has no setup function nor does it have a loop function, both of which are needed to run on an Arduino.

The code you have are just functions to allow access to your chip, you have to specify in the misssing functions what of these functions you want to call and what to do with the result.

The problem is it's a multi-file sketch. You may be used to Arduino sketches only being a single .ino file. But the Arduino IDE also allows you to break a sketch up into multiple files which are displayed as tabs. For larger projects, this is helpful because it allows you to quickly navigate through the code. This is what was done with the function generator sketch you found. It is actually three .ino files and you need all three of them to have a complete program.

So you can't just grab the file AD9833.ino. You need to download the entire sketch:

Grumpy_Mike:
That code has no setup function nor does it have a loop function, both of which are needed to run on an Arduino.

The code you have are just functions to allow access to your chip, you have to specify in the misssing functions what of these functions you want to call and what to do with the result.

oh yea that is right, thanks. I retried it and get a right code (without programming error),

#include <SPI.h>
const byte FSYNC = 13;

void setup() {}
void AD9833setup() {
  pinMode(FSYNC, OUTPUT);
  digitalWrite(FSYNC, HIGH);

  SPI.begin();
  delay(50); 
 
}
void WriteRegister(int dat) { 
  SPI.setDataMode(SPI_MODE2);       
  
  digitalWrite(FSYNC, LOW);           // Set FSYNC low before writing to AD9833 registers
  delayMicroseconds(10);              // Give AD9833 time to get ready to receive data.
  
  SPI.transfer(dat>>8);               // Each AD9833 register is 32 bits wide and each 16
  SPI.transfer(dat&0xFF);             // bits has to be transferred as 2 x 8-bit bytes.

  digitalWrite(FSYNC, HIGH);          //Write done. Set FSYNC high

  SPI.setDataMode(SPI_MODE0);
}
void AD9833reset() {
  WriteRegister(0x100);   // Write '1' to AD9833 Control register bit D8.
  delay(10);
}

void AD9833setFrequency(long frequency, int Waveform) {


  long FreqWord = (frequency * pow(2, 28)) / 25.0E6;
  
  int MSB = (int)((FreqWord & 0xFFFC000) >> 14);    //Only lower 14 bits are used for data
  int LSB = (int)(FreqWord & 0x3FFF);
  
  //Set control bits 15 ande 14 to 0 and 1, respectively, for frequency register 0
  LSB |= 0x4000;
  MSB |= 0x4000; 
  
  WriteRegister(0x2100);          //DB13 is set to 1. This allows a complete word to be loaded
                                  //into a frequency register in two consecutive writes. The
                                  //first write contains 14 LSBs. The second write contains 14 MSBs. 
                                  //RESET bit DB8 is set to 1          
                                  //This resets internal registers to 0, which corresponds to an analog output of midscale

  WriteRegister(LSB);                  // Write lower 16 bits to AD9833 registers
  WriteRegister(MSB);                  // Write upper 16 bits to AD9833 registers.
  WriteRegister(0xC000);               // Phase register
  WriteRegister(Waveform);             // Exit & Reset to SINE, SQUARE or TRIANGLE
}
void loop() {
  int Waveform;
  
 AD9833reset();
// AD9833 documentation advises a 'Reset' on first applying power.

AD9833setup();
// Set the frequency and waveform registers in the AD9833.
AD9833setFrequency( 400, 0x2000); //  WAVE_SINE = 0x2000,
                                  // WAVE_SQUARE = 0x2028,
                                  // WAVE_TRIANGLE = 0x2002


}

I hope it working well on the Arduino, I will try it today at the evening ^^

thanks again :slight_smile:

pert:
The problem is it’s a multi-file sketch. You may be used to Arduino sketches only being a single .ino file. But the Arduino IDE also allows you to break a sketch up into multiple files which are displayed as tabs. For larger projects, this is helpful because it allows you to quickly navigate through the code. This is what was done with the function generator sketch you found. It is actually three .ino files and you need all three of them to have a complete program.

So you can’t just grab the file AD9833.ino. You need to download the entire sketch:

Thanks for replying Pert, I apply your suggested steps and get the 3 tabs that containing all sketches but unfortunately I get the following error message:
Arduino: 1.8.2 (Windows 10), TD: 1.36, Board: “Arduino/Genuino Uno”

Build options changed, rebuilding all
C:\Users\user\Desktop\AD2833-Function-generator-master\Function_generator\Function_generator.ino:4:27: fatal error: TFT_22_ILI9225.h: No such file or directory

#include<TFT_22_ILI9225.h>

^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

When you see a "No such file or directory" error it almost always means you need to install the library that contains the missing file.

Often the code you're compiling will come with documentation (either a comment or separate document) that tells you where to get the library dependencies.

In other cases the author of the code will not have been so kind and you'll need to go on a hunt for the missing library.

A good place to start is the Arduino IDE's Library Manager:

  • Sketch > Include Library > Manage Libraries...
  • In the "Filter your search..." box, type some keywords you have gleaned from the missing file name.
  • Scroll through the results for the right library. Click on it.
  • Click "Install".
  • Wait for installation to finish.
  • Click "Close".
  • Try compiling your code again.

If you have no luck in Library Manager then load up your favorite search engine and do a search for the missing filename. You will often get multiple results. If you have a lot of results you might add "arduino" as an additional search keyword. I will usually prefer results on github.com since that is where most Arduino libraries are hosted and downloading from there is fast and easy. In some cases there will be multiple libraries that contain the given filename and you'll need to do some evaluation to determine which seems the most appropriate, then try it out. After downloading the library you found you'll need to install it. This requires a different process than the Library Manager installation. You will find instructions here:

In this case, the answer is right there in the project's readme:

Needs Library

 AD9833reset();
// AD9833 documentation advises a 'Reset' on first applying power.

If the comment is correct then do the reset in setup() so that it occurs just once rather than in loop() where it will be repeated

and presumably the same for the AD9833setup(); function

In this case, the answer is right there in the project's readme:

Big thanks for the very important explanation and big thanks for your effort, Pert.

UKHeliBob:

 AD9833reset();

// AD9833 documentation advises a 'Reset' on first applying power.



If the comment is correct then do the reset in setup() so that it occurs just once rather than in loop() where it will be repeated

and presumably the same for the AD9833setup(); function

Thanks for replying, UKHeliBob
I'll try it. :slight_smile:

I hope it working well on the Arduino, I will try it today at the evening

That code will not work.
You have nothing in the setup function. You need to put here things that will only run at the beginning. This is pretty much what you have in the loop function now.

The loop function runs over and over, and as you don’t want to change anything this should be blank.