From 2f4d17e4eddad3e51c4c342811ea3115079a8a9e Mon Sep 17 00:00:00 2001 From: Stephen Olesen Date: Wed, 1 Jul 2015 14:42:20 -0600 Subject: [PATCH] Fixed up the duration timers, clockTick needs a cleanup. --- DDS.cpp | 15 +++++++++++++-- DDS.h | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/DDS.cpp b/DDS.cpp index 5b2336b..e44101f 100644 --- a/DDS.cpp +++ b/DDS.cpp @@ -57,10 +57,11 @@ void DDS::setFrequency(unsigned short freq) { } } +// TODO: Clean this up a bit.. void DDS::clockTick() { - accumulator += stepRate; if(running) { - if(tickDuration == 0) { + accumulator += stepRate; + if(timeLimited && tickDuration == 0) { #ifdef DDS_IDLE_HIGH // Set the duty cycle to 50% OCR2B = pow(2,COMPARATOR_BITS)/2; @@ -69,11 +70,21 @@ void DDS::clockTick() { OCR2B = 0; #endif running = false; + accumulator = 0; } else { OCR2B = getDutyCycle(); } // Reduce our playback duration by one tick tickDuration--; + } else { + // Hold it low +#ifdef DDS_IDLE_HIGH + // Set the duty cycle to 50% + OCR2B = pow(2,COMPARATOR_BITS)/2; +#else + // Set duty cycle to 0, effectively off + OCR2B = 0; +#endif } } diff --git a/DDS.h b/DDS.h index 8c17432..36df413 100644 --- a/DDS.h +++ b/DDS.h @@ -121,6 +121,7 @@ public: // Start the PWM output, or stop it void on() { + timeLimited = false; running = true; } // Provide a duration in ms for the tone @@ -128,6 +129,7 @@ public: // Duration in ticks from milliseconds is: // t = (1/refclk) tickDuration = (duration * refclk) / 1000; + timeLimited = true; running = true; } void off() { @@ -165,6 +167,7 @@ public: private: volatile bool running; volatile unsigned long tickDuration; + volatile bool timeLimited; volatile unsigned char amplitude; #ifdef SHORT_ACCUMULATOR volatile unsigned short accumulator;