I thought I had it working... I was able to cycle through the config function with each press of the button, consistently... but only the while (param == 0) loop would print anything. the rest would not respond to encoder (Also, the cw function stopped running the encoder correctly relative to another similar code? it steps in 2, 5, 14, 35, and gets stuck going one way - it is consistent though).
right now, it steps into param == 1 and hangs.
I'm just looking at the loop, debounce, and config functions, so feel free to ignore the rest.
//AD9850 DDS test
#include <SPI.h>
//#define DDS_CLOCK 125000000
#define DDS_CLOCK 124999000
#define CLOCK 13 //pin 8 connections for DDS
#define LOAD 10 //9 FQ_UD
#define DATA 11 //10
#define RESET 9 //11
#define BUTTON 3
int lastButtonState = HIGH;
//volatile char mode = 'cw';
int mode = 0;
int param = 6;
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
/*
// This optional setting causes Encoder to use more optimized code,
// It must be defined before Encoder.h is included.
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
*/
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(4, 5); // avoid using pins with LEDs attached
long oldPosition = -999;
long debouncing_time = 20; //Debouncing Time in Milliseconds
volatile unsigned long last_micros = 0;
long F1 = 1;
long F2 = 100;
long fStep = 1;
long freq = F1;
long timeDwell = 300;
int32_t tuning_word_F1 = (F1 * 0x100000000LL)/124999100L;
int32_t tuning_word_F2 = (F2 * 0x100000000LL)/124999100L;
int32_t tuning_word_fStep = (fStep * 0x100000000LL)/124999100L;
int32_t tuning_word = (freq * 0x100000000LL)/124999100L;
void setup() {
Serial.begin(115200);
Serial.println("AD9850 DDS");
pinMode(BUTTON, INPUT_PULLUP);
pinMode (CLOCK, OUTPUT);
pinMode (LOAD, OUTPUT);
pinMode (DATA, OUTPUT);
pinMode (RESET, OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE0); // mode 0 seems to be the right one
SPI.setClockDivider(SPI_CLOCK_DIV2); //try to go pretty fast
SPI.setBitOrder(LSBFIRST);
AD9850_init();
AD9850_reset();
SetFrequency(10000000);
Serial.println("10,000,000 Hz");
attachInterrupt(1, debounce, RISING);
delay(100);
}
void loop() {
Serial.println("loop");
if (mode == 0) {
config();
}
if (mode == 1) {
cw();
}
if (mode == 2) {
sweep();
}
}
void debounce() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
last_micros = micros();
mode = 0;
param = param + 1;
if (param > 6) {
param = 0;
}
}
}
void config() {
Serial.println("Config Mode");
Serial.println(param);
Serial.println(mode);
Serial.println("Param 0:");
while (param == 0) {
long newPosition = myEnc.read();
if (newPosition < oldPosition) {
mode = 1;
Serial.println("Changed to CW mode");
//cw();
}
if (newPosition > oldPosition) {
mode = 2;
Serial.println("Changed to Sweep mode");
//sweep();
}
oldPosition = newPosition;
}
Serial.println("Param 1:");
while (param == 1) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - newPosition;
Serial.print("Edit F1: ");
Serial.println(long(freq));
}
}
Serial.println("Param 2:");
while (param == 2) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - (250 * newPosition);
Serial.print("Edit F2: ");
Serial.println(long(freq));
}
}
Serial.println("Param 3:");
while (param == 3) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - (250 * newPosition);
Serial.print("Edit fStep: ");
Serial.println(long(freq));
}
}
Serial.println("Param 4:");
while (param == 4) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
timeDwell = timeDwell - newPosition;
Serial.print("Edit Dwell: ");
Serial.println(long(timeDwell));
}
}
Serial.println("Param 5:");
while (param == 5) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - (250 * newPosition);
Serial.print("Edit 5: ");
Serial.println(long(freq));
}
}
Serial.println("Param 6:");
while (param == 6) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - (250 * newPosition);
Serial.print("Edit 6: ");
Serial.println(long(freq));
}
}
}
void cw() {
boolean updateLCD = true;
//long freq = 1000000;
while (mode == 1) {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
freq = freq - newPosition;
int32_t tuning_word = (freq * 0x100000000LL)/124999100L;
SetFrequency(tuning_word);
updateLCD = true;
}
if (updateLCD) {
Serial.println(" CW Mode");
Serial.print(long (freq));
Serial.println(" Hz");
updateLCD = false;
}
}
}
void sweep() {
/*
long F1 = 1;
long F2 = 100;
long fStep = 1;
long freq = F1;
long timeDwell = 300;
int32_t tuning_word_F1 = (F1 * 0x100000000LL)/124999100L;
int32_t tuning_word_F2 = (F2 * 0x100000000LL)/124999100L;
int32_t tuning_word_fStep = (fStep * 0x100000000LL)/124999100L;
int32_t tuning_word = (freq * 0x100000000LL)/124999100L;
*/
Serial.println(" Sweep Mode");
while (mode == 2) {
long timeSweepStart = micros();
for (int i = 0; i < 10; i++) {
long timeStepStart = micros();
while (tuning_word <= tuning_word_F2) {
long timenow = micros();
if ((timenow - timeStepStart) >= timeDwell) {
timeStepStart = timenow;
SetFrequency(tuning_word);
tuning_word = tuning_word + tuning_word_fStep;
}
}
tuning_word = tuning_word_F1;
}
long timeSweepStop = micros();
long timeSweep = timeSweepStop - timeSweepStart;
Serial.println(timeSweep);
}
}
void SetFrequency(int32_t tuning_word) {
PORTB = PORTB & B11111011; // clear pin 10
SPI.transfer(tuning_word);
SPI.transfer(tuning_word >> 8);
SPI.transfer(tuning_word >> 16);
SPI.transfer(tuning_word >> 24);
SPI.transfer(0x0);
PORTB = PORTB | B00000100; // set pin 10
}
void AD9850_init() {
digitalWrite(RESET, LOW);
digitalWrite(CLOCK, LOW);
digitalWrite(LOAD, LOW);
digitalWrite(DATA, LOW);
}
void AD9850_reset() {
//reset sequence is:
// CLOCK & LOAD = LOW
// Pulse RESET high for a few uS (use 5 uS here)
// Pulse CLOCK high for a few uS (use 5 uS here)
// Set DATA to ZERO and pulse LOAD for a few uS (use 5 uS here)
// data sheet diagrams show only RESET and CLOCK being used to reset the device, but I see no output unless I also
// toggle the LOAD line here.
digitalWrite(CLOCK, LOW);
digitalWrite(LOAD, LOW);
digitalWrite(RESET, LOW);
delay(5);
digitalWrite(RESET, HIGH); //pulse RESET
delay(5);
digitalWrite(RESET, LOW);
delay(5);
digitalWrite(CLOCK, LOW);
delay(5);
digitalWrite(CLOCK, HIGH); //pulse CLOCK
delay(5);
digitalWrite(CLOCK, LOW);
delay(5);
digitalWrite(DATA, LOW); //make sure DATA pin is LOW
digitalWrite(LOAD, LOW);
delay(5);
digitalWrite(LOAD, HIGH); //pulse LOAD
delay(5);
digitalWrite(LOAD, LOW);
// Chip is RESET now
}