#include <Wire.h> // for LCD to comunicate in i2c
// #include <LiquidCrystal_I2C.h> // LCD in i2c need module PCF8574
/---------------( Declare Constants )---------------/
// 1st MODULE
#define W_CLK_ONE 10 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD_ONE 11 // Pin 9 - connect to freq update pin (FQ)
#define DATA_ONE 12 // Pin 10 - connect to serial data load pin (DATA - D7)
#define RESET_ONE 13 // Pin 11 - connect to reset pin (RST).
// 2st MODULE
#define W_CLK_TWO 0 // Pin 4 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD_TWO 1 // Pin 5 - connect to freq update pin (FQ)
#define DATA_TWO 2 // Pin 6 - connect to serial data load pin (DATA - D7)
#define RESET_TWO 3 // Pin 7 - connect to reset pin (RST).
// for DDS
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
// potentiometers
#define POT_ONE A1
#define POT_TWO A2
// for mapping the value of potentiometer in hertz
#define MAP 100000
/---------------( Declare objects )---------------/
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
/---------------( Declare Variables )---------------/
long freq_mod_one = 0;
long freq_mod_two = 0;
long pot_value = 0;
/---------------( Declare Fonctions )---------------/
// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data, int pin_clock, int pin_data)
{
for (int i=0; i<8; i++, data>>=1)
{
digitalWrite(pin_data, data & 0x01);
pulseHigh(pin_clock); //after each bit sent, CLK is pulsed high
}
}
// frequency calc from datasheet page 8 = * /2^32
void sendFrequency(double frequency, int pin_clock, int pin_fq, int pin_data)
{
int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
for (int b=0; b<4; b++, freq>>=8)
{
tfr_byte(freq & 0xFF, pin_clock, pin_data);
}
tfr_byte(0x000, pin_clock, pin_data); // Final control byte, all 0 for 9850 chip
pulseHigh(pin_fq); // Done! Should see output
}
/---------------( SETUP: RUNS ONCE )---------------/
void setup()
{
// lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines and turn on backlight
// Configure arduino data pins for output
pinMode(FQ_UD_ONE, OUTPUT);
pinMode(W_CLK_ONE, OUTPUT);
pinMode(DATA_ONE, OUTPUT);
pinMode(RESET_ONE, OUTPUT);
pulseHigh(RESET_ONE);
pulseHigh(W_CLK_ONE);
pulseHigh(FQ_UD_ONE); // this pulse enables serial mode
pinMode(FQ_UD_TWO, OUTPUT);
pinMode(W_CLK_TWO, OUTPUT);
pinMode(DATA_TWO, OUTPUT);
pinMode(RESET_TWO, OUTPUT);
pulseHigh(RESET_TWO);
pulseHigh(W_CLK_TWO);
pulseHigh(FQ_UD_TWO); // this pulse enables serial mode
// Quick 3 blinks of backlight "hello world"
// for(int i = 0; i< 3; i++)
// {
// lcd.backlight();
// delay(100);
// lcd.noBacklight();
// delay(100);
// }
// lcd.backlight(); // finish with backlight on
// Write characters on the display
// lcd.setCursor(1,0);
// lcd.print("Test: DDS");
// delay(2000);
// lcd.clear();
}/--(end setup )---/
/---------------( LOOP: RUNS CONSTANTLY )---------------/
void loop()
{
// lcd.setCursor(0,0); // formating text
// lcd.print("Freq 1:"); //
// lcd.print(freq_mod_one); //
// lcd.print("Hz "); //
// lcd.setCursor(0,1); //
// lcd.print("Freq 2:"); //
// lcd.print(freq_mod_two); //
// lcd.print("Hz "); //
pot_value = analogRead(POT_ONE); //
freq_mod_one = map(pot_value, 0, 1023, 0, MAP); // map the value of potentiometer
sendFrequency(50, W_CLK_ONE, FQ_UD_ONE, DATA_ONE); //transfer value of pot 1 to first AD9850
pot_value = analogRead(POT_TWO); //
freq_mod_two = map(pot_value, 0, 1023, 0, MAP); //
sendFrequency(50, W_CLK_TWO, FQ_UD_TWO, DATA_TWO); //transfer value of pot 2 to second AD9850
}
j'ai trouvé ceux si aussi