Allow refclkOffset to be set on the fly in DDS and the calibration tool.

This commit is contained in:
Stephen Olesen 2015-07-04 18:31:18 -06:00
parent 1c29498e44
commit 7131e46ff0
3 changed files with 39 additions and 5 deletions

View File

@ -89,7 +89,7 @@ ddsAccumulator_t DDS::calcFrequency(unsigned short freq) {
newStep = (600.0 / (DDS_REFCLK_DEFAULT+DDS_REFCLK_OFFSET)) * pow(2,ACCUMULATOR_BITS); newStep = (600.0 / (DDS_REFCLK_DEFAULT+DDS_REFCLK_OFFSET)) * pow(2,ACCUMULATOR_BITS);
} }
} else { } else {
newStep = pow(2,ACCUMULATOR_BITS)*freq / (refclk+DDS_REFCLK_OFFSET); newStep = pow(2,ACCUMULATOR_BITS)*freq / (refclk+refclkOffset);
} }
return newStep; return newStep;
} }

11
DDS.h
View File

@ -181,7 +181,8 @@ static const int8_t ddsSineTable[256] PROGMEM = {
class DDS { class DDS {
public: public:
DDS(): refclk(DDS_REFCLK_DEFAULT), accumulator(0), running(false), DDS(): refclk(DDS_REFCLK_DEFAULT), refclkOffset(DDS_REFCLK_OFFSET),
accumulator(0), running(false),
timeLimited(false), tickDuration(0), amplitude(255) timeLimited(false), tickDuration(0), amplitude(255)
{}; {};
@ -244,6 +245,13 @@ public:
return refclk; return refclk;
} }
void setReferenceOffset(int16_t offset) {
refclkOffset = offset;
}
int16_t getReferenceOffset() {
return refclkOffset;
}
uint8_t getDutyCycle(); uint8_t getDutyCycle();
// Set a scaling factor. To keep things quick, this is a power of 2 value. // Set a scaling factor. To keep things quick, this is a power of 2 value.
@ -263,6 +271,7 @@ private:
volatile ddsAccumulator_t accumulator; volatile ddsAccumulator_t accumulator;
volatile ddsAccumulator_t stepRate; volatile ddsAccumulator_t stepRate;
ddsAccumulator_t refclk; ddsAccumulator_t refclk;
int16_t refclkOffset;
static DDS *sDDS; static DDS *sDDS;
}; };

View File

@ -31,7 +31,8 @@ enum Sets {
SET_REF, SET_REF,
SET_TONE, SET_TONE,
SET_AMPLITUDE, SET_AMPLITUDE,
SET_ADC_HALF SET_ADC_HALF,
SET_OFFSET
} setting = SET_TONE; } setting = SET_TONE;
char freqBuffer[8]; char freqBuffer[8];
@ -47,6 +48,7 @@ void loop() {
static uint16_t samples = 0; static uint16_t samples = 0;
static uint16_t pulse; static uint16_t pulse;
static uint32_t lastOutput = 0; static uint32_t lastOutput = 0;
static float pulseFloat = 0.0;
if(recordedPulse) { if(recordedPulse) {
uint32_t pulseAveraging; uint32_t pulseAveraging;
uint16_t tmpPulse; uint16_t tmpPulse;
@ -56,9 +58,11 @@ void loop() {
sei(); sei();
if(samples++ == 0) { if(samples++ == 0) {
pulse = tmpPulse; pulse = tmpPulse;
//pulseFloat = tmpPulse;
} else { } else {
pulseAveraging = (pulse + tmpPulse) >> 1; pulseAveraging = (pulse + tmpPulse) >> 1;
pulse = pulseAveraging; pulse = pulseAveraging;
pulseFloat = pulseFloat + 0.01*((float)pulse-pulseFloat);
} }
if((lastOutput + 1000) < millis()) { if((lastOutput + 1000) < millis()) {
Serial.print(F("Pulse: ")); Serial.print(F("Pulse: "));
@ -74,7 +78,17 @@ void loop() {
Serial.print(F("Freq: ")); Serial.print(F("Freq: "));
// F = 1/(pulse*(1/ref)) // F = 1/(pulse*(1/ref))
// F = ref/pulse // F = ref/pulse
Serial.println((float)(dds.getReferenceClock()+(float)DDS_REFCLK_OFFSET)/(float)pulse); Serial.print((float)((float)dds.getReferenceClock()+(float)dds.getReferenceOffset())/(float)pulse);
Serial.print(F(" / "));
Serial.print((float)((float)dds.getReferenceClock()+(float)dds.getReferenceOffset())/pulseFloat);
Serial.print(F(" / "));
Serial.println(pulseFloat);
Serial.print(F("Freq2: "));
// F = 1/(pulse*(1/ref))
// F = ref/pulse
Serial.print((float)dds.getReferenceClock()/(float)pulse);
Serial.print(F(" / "));
Serial.println((float)dds.getReferenceClock()/pulseFloat);
samples = 0; samples = 0;
lastOutput = millis(); lastOutput = millis();
} }
@ -87,6 +101,7 @@ void loop() {
Serial.println(F("Commands:")); Serial.println(F("Commands:"));
Serial.println(F("RefClk: u = +10, U = +100, r XXXX = XXXX")); Serial.println(F("RefClk: u = +10, U = +100, r XXXX = XXXX"));
Serial.println(F(" d = -10, D = -100")); Serial.println(F(" d = -10, D = -100"));
Serial.println(F("Offset: s XXX = Set refclk offset"));
Serial.println(F("Radio: T = transmit, R = receive")); Serial.println(F("Radio: T = transmit, R = receive"));
Serial.println(F("Tone: t XXXX = XXXX Hz")); Serial.println(F("Tone: t XXXX = XXXX Hz"));
Serial.println(F("Amp.: a XXX = XXX out of 255")); Serial.println(F("Amp.: a XXX = XXX out of 255"));
@ -164,6 +179,9 @@ void loop() {
case 'm': case 'm':
setting = SET_ADC_HALF; setting = SET_ADC_HALF;
break; break;
case 's':
setting = SET_OFFSET;
break;
case 'o': case 'o':
dds.on(); dds.on();
Serial.println("> "); Serial.println("> ");
@ -173,7 +191,7 @@ void loop() {
Serial.println("> "); Serial.println("> ");
break; break;
default: default:
if(c >= '0' && c <= '9') { if(c == '-' || (c >= '0' && c <= '9')) {
*freqBufferPtr = c; *freqBufferPtr = c;
freqBufferPtr++; freqBufferPtr++;
} }
@ -200,6 +218,11 @@ void loop() {
adcHalf = freq&0xFF; adcHalf = freq&0xFF;
Serial.print(F("ADC midpoint set to ")); Serial.print(F("ADC midpoint set to "));
Serial.println((uint8_t)(freq&0xFF)); Serial.println((uint8_t)(freq&0xFF));
} else if(setting == SET_OFFSET) {
dds.setReferenceOffset((int16_t)atoi(freqBuffer));
dds.setFrequency(lastFreq);
Serial.print(F("Refclk offset: "));
Serial.println(dds.getReferenceOffset());
} }
Serial.println("> "); Serial.println("> ");
} }
@ -211,9 +234,11 @@ void loop() {
ISR(ADC_vect) { ISR(ADC_vect) {
static uint16_t pulseLength = 0; static uint16_t pulseLength = 0;
static uint8_t lastADC = 127; static uint8_t lastADC = 127;
cli();
TIFR1 = _BV(ICF1); TIFR1 = _BV(ICF1);
PORTD |= _BV(2); PORTD |= _BV(2);
dds.clockTick(); dds.clockTick();
sei();
if(listening) { if(listening) {
pulseLength++; pulseLength++;
if(ADCH >= adcHalf && lastADC < adcHalf) { if(ADCH >= adcHalf && lastADC < adcHalf) {