Skip to content

Instantly share code, notes, and snippets.

@schreibfaul1
Created October 6, 2019 16:53
Show Gist options
  • Save schreibfaul1/a5caac2ba71f2cc1bb788ac047a01398 to your computer and use it in GitHub Desktop.
Save schreibfaul1/a5caac2ba71f2cc1bb788ac047a01398 to your computer and use it in GitHub Desktop.
ESP32 AD9850 (DDS Signal Generator)
#include "Arduino.h"
#include "SPI.h"
// Digital I/O used
#define AD9850_CS 21
#define AD9850_RST 22
// Global variables
uint32_t AD9850_CLOCK=125000000; // Module crystal frequency. Tweak here for accuracy.
uint32_t sweep=0;
uint32_t sweep_max=2000;
uint32_t sweep_min=1000;
boolean sweep_direction=false;
// Objects
SPISettings SPI_AD9850;
void AD9850_set_frequency(uint32_t freq){
freq=freq * 4294967295/AD9850_CLOCK;
digitalWrite(AD9850_CS, LOW);
SPI.beginTransaction(SPI_AD9850);
SPI.write32(freq);
SPI.write(0);
SPI.endTransaction();
digitalWrite(AD9850_CS, HIGH);
}
void AD9850_reset(){
// RESET AD9850
digitalWrite(AD9850_RST, HIGH);
delay(1);
digitalWrite(AD9850_RST, LOW);
delay(1);
AD9850_set_frequency(0);
delay(1);
}
//The setup function is called once at startup of the sketch
void setup(){
pinMode(AD9850_CS, OUTPUT); digitalWrite(AD9850_CS, HIGH);
pinMode(AD9850_RST, OUTPUT); digitalWrite(AD9850_RST, LOW);
SPI.begin();
SPI_AD9850=SPISettings(1000000, LSBFIRST, SPI_MODE0);
AD9850_reset();
AD9850_set_frequency(10000);
delay(1000);
sweep=sweep_min;
sweep_direction=true;
}
// The loop function is called in an endless loop
void loop(){
if(sweep==sweep_min) sweep_direction=true;
if(sweep==sweep_max) sweep_direction=false;
if(sweep_direction==true) sweep++; else sweep--;
AD9850_set_frequency(sweep);
delay(2);
}
@CarecrYang
Copy link

Hi Thanks for your post. Could you tell us how you connect pins from AD9850 to ESP? Thank you

@schreibfaul1
Copy link
Author

You need:
GPIO21 CS -> FQ_UD
GPIO22 RST -> RESET
GPIO23 MOSI -> Serial Data
GPIO 18 CLK -> W_CLK

@CarecrYang
Copy link

CarecrYang commented Dec 12, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment