Fixed up the duration timers, clockTick needs a cleanup.

This commit is contained in:
Stephen Olesen 2015-07-01 14:42:20 -06:00
parent 31eb465ebf
commit 2f4d17e4ed
2 changed files with 16 additions and 2 deletions

15
DDS.cpp
View File

@ -57,10 +57,11 @@ void DDS::setFrequency(unsigned short freq) {
} }
} }
// TODO: Clean this up a bit..
void DDS::clockTick() { void DDS::clockTick() {
accumulator += stepRate;
if(running) { if(running) {
if(tickDuration == 0) { accumulator += stepRate;
if(timeLimited && tickDuration == 0) {
#ifdef DDS_IDLE_HIGH #ifdef DDS_IDLE_HIGH
// Set the duty cycle to 50% // Set the duty cycle to 50%
OCR2B = pow(2,COMPARATOR_BITS)/2; OCR2B = pow(2,COMPARATOR_BITS)/2;
@ -69,11 +70,21 @@ void DDS::clockTick() {
OCR2B = 0; OCR2B = 0;
#endif #endif
running = false; running = false;
accumulator = 0;
} else { } else {
OCR2B = getDutyCycle(); OCR2B = getDutyCycle();
} }
// Reduce our playback duration by one tick // Reduce our playback duration by one tick
tickDuration--; 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
} }
} }

3
DDS.h
View File

@ -121,6 +121,7 @@ public:
// Start the PWM output, or stop it // Start the PWM output, or stop it
void on() { void on() {
timeLimited = false;
running = true; running = true;
} }
// Provide a duration in ms for the tone // Provide a duration in ms for the tone
@ -128,6 +129,7 @@ public:
// Duration in ticks from milliseconds is: // Duration in ticks from milliseconds is:
// t = (1/refclk) // t = (1/refclk)
tickDuration = (duration * refclk) / 1000; tickDuration = (duration * refclk) / 1000;
timeLimited = true;
running = true; running = true;
} }
void off() { void off() {
@ -165,6 +167,7 @@ public:
private: private:
volatile bool running; volatile bool running;
volatile unsigned long tickDuration; volatile unsigned long tickDuration;
volatile bool timeLimited;
volatile unsigned char amplitude; volatile unsigned char amplitude;
#ifdef SHORT_ACCUMULATOR #ifdef SHORT_ACCUMULATOR
volatile unsigned short accumulator; volatile unsigned short accumulator;