Fixed up the duration timers, clockTick needs a cleanup.
This commit is contained in:
		
							parent
							
								
									31eb465ebf
								
							
						
					
					
						commit
						2f4d17e4ed
					
				
							
								
								
									
										15
									
								
								DDS.cpp
								
								
								
								
							
							
						
						
									
										15
									
								
								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
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										3
									
								
								DDS.h
								
								
								
								
							
							
						
						
									
										3
									
								
								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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue