Pages: [1]   Go Down
Author Topic: Bug in WaveHC?  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Manchester, New Hampshire
Offline Offline
Edison Member
Karma: 4
Posts: 1363
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was going through WaveHC, updating it to use Timer3 on the 1284p, and I noticed something that looks like a bug:

/** Set the player's sample rate.
 * \param[in] samplerate The new sample rate in samples per second.
 * No checks are done on the input parameter.
void WaveHC::setSampleRate(uint32_t samplerate) {
  if (samplerate < 500) samplerate = 500;
  if (samplerate > 50000) samplerate = 50000;
  // from ladayada's library.
  while (TCNT0 != 0);
  OCR3A = F_CPU / samplerate;

The library normally uses timer1, so why is the while loop here waiting for TCNT0?  I believe TCNT0 is the counter for the timer used for millis(), and I can't see any reason why it should wait for that to trigger.  I suspect that the intent was to wait for TCNT1 to hit 0.

Also, is waiting for the counter to hit 0 the right thing to even do here?  Doesn't the counter continue to count in a loop even when interrupts are disabled?  And couldn't this code potentially miss when it hits 0?

Offline Offline
Edison Member
Karma: 8
Posts: 1250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WAAAAY over my head at this depth/level.. LOL

but maybe post this over at the Adafruit forum as well?

And I have always been told that "WaveHC uses Timer1, and this affects PWM pin(s))...etc..  but I seem to recall something in the adavoice sketch..about millis() & delay()..and timers..etc..   figured Id post see if sheds any light  smiley


  // Optional, but may make sampling and playback a little smoother:
  // Disable Timer0 interrupt.  This means delay(), millis() etc. won't
  // work.  Comment this out if you really, really need those functions.
  TIMSK0 = 0;

Pages: [1]   Go Up
Jump to: