Producing signals with high precision frequency is a very common demand, and this is the umptieth trial to show a solution. It follows the quote “Everything should be made as simple as possible, but no simpler”, possibly once said by Einstein.
Actually, the DDS chip AD9850 uses the rare LSBFIRST mode in the SPI protocol, but that is the only difficulty. The hardware could not be done easier: just three wires plus power connections between the NANO and the HC-SR08. Please note that two wires are hidden under the Arduino NANO.
The code below produces a sweep from 1 Hz to 40 MHz which is the limit. It should be easy to change it in order to match your needs. If you want the square waves you need to adjust the poti to obtain the duty cycle you want, otherwise you might get the duty cycle 0 and see nothing.
[code]
#include <SPI.h>
// AD9850 Board - Arduino Uno connections:
// CLK = 13, clock Arduino board SCK
// DATA = 11, data Arduino board MOSI
// RST = GND
const byte FQ = 10; // Arduino board FQ, frequency Update
// Board VCC - Arduino +5V
// Board GND - Arduino GND
void setup() {
Serial.begin(9600);
Serial.println(__FILE__);
SPI.begin();
SPI.setBitOrder(LSBFIRST);
pinMode(FQ, OUTPUT);
upDownFq();
// range: 1 Hz to 40 MHz, kind of sweep
float f = 1;
while (f < 40E6) {
AD9850_set_frequency(f);
Serial.println(f);
delay(1000);
f = f * 1.5;
}
}
void loop() {}
void AD9850_set_frequency(float frequency) {
// transfer frequency and start DDS
frequency = frequency / 1000000 * 4294967295 / 125;
// for a 125 MHz crystal
union {
long y;
byte b[4];
} freq;
freq.y = frequency; // float to long
SPI.transfer(freq.b, 4);
SPI.transfer(0);
upDownFq();
}
void upDownFq() {
asm("sbi 5,2 \n\t"
"cbi 5, 2 \n\t"); // set new frequency value
}
[/code]
That is all you need.
Comments