Skip to content

Instantly share code, notes, and snippets.

@e-Gizmo
Created December 9, 2013 02:07
Show Gist options
  • Save e-Gizmo/7866449 to your computer and use it in GitHub Desktop.
Save e-Gizmo/7866449 to your computer and use it in GitHub Desktop.
/* Sketch for e-Gizmo DDS Function Generated Breakout Board */
/* Pin assignment
Change accordingly if you have your own pin allotment preference
*/
const int latchPin = 10; // Connected to Function Gen SYNC pin
const int clockPin = 13; // Connected to Function Gen CLK pin
const int dataPin = 11; // Connected to Function Gen DATA pin
/* Waveform Output Control word */
const int SINE=0x2008;
const int TRIANGLE= 0x200A;
const int SQUARE = 0x2028;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
int i;
/* Demonstration Routine */
// display sine, triangle, and square 5x @ 1khz
for(i=1;i<5;i++){
Set_Gen(SINE,1000);
delay(1000);
Set_Gen(TRIANGLE,1000);
delay(1000);
Set_Gen(SQUARE,1000);
delay(1000);
}
// display sine, triangle, and square 5x @ 100khz
for(i=1;i<5;i++){
Set_Gen(SINE,100000);
delay(1000);
Set_Gen(TRIANGLE,100000);
delay(1000);
Set_Gen(SQUARE,100000);
delay(1000);
}
}
/* ----- DDS Core Functions ---- */
/* Emulate 16-bit SPI transfer */
void SPI16(int spidata){
int spidatah;
int spidatal;
spidatah= spidata>>8;
spidatal= spidata & 0xFF;
digitalWrite(clockPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, spidatah);
digitalWrite(clockPin, HIGH);
shiftOut(dataPin, clockPin, MSBFIRST, spidatal);
digitalWrite(clockPin, HIGH);
digitalWrite(latchPin, HIGH);
}
/* Set up Function gen to generate waveform at desired frequency */
void Set_Gen(int waveform,float frequency){
long int freqdiv;
int freqdivh;
int freqdivl;
// compute for 28 bit frequency divider parameters
freqdiv=frequency*53.6870912;
freqdivh=freqdiv>>14; // upper 14 bits extracted
freqdivh &= 0x3FFF;
freqdivl=freqdiv & 0x3FFF; // lower 14 bits extracted
freqdivh |= 0x4000; // control bits added
freqdivl |= 0x4000;
SPI16(waveform); // set waveform
SPI16(freqdivl); // set frequency
SPI16(freqdivh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment