Go Down

Topic: MEGA 2560 Tone conflict width PWM (Read 2 times) previous topic - next topic

Nick Gammon

I've turned the fiddling around with registers into a library.

http://www.gammon.com.au/Arduino/TonePlayer.zip

Now you can just use that:

Code: [Select]

#include <TonePlayer.h>

int Hz = 0;
int prevHz;

TonePlayer tone5 (TCCR5A, TCCR5B, OCR5AH, OCR5AL, TCNT5H, TCNT5L);

void setup()
  {
  pinMode (46, OUTPUT);  // OC5A
  }

void loop()
  {
  Hz = analogRead (14) - 300;
 
  if( Hz < 10 )
    Hz = 33;
  if( Hz > 5000 )
    Hz = 5000;

  if ( abs (Hz - prevHz) > 5 )
    {
    tone5.tone (Hz);
    prevHz = Hz;
    }  // end of frequency change
   
  }  // end ofloop


It supports Timers 1, 3, 4, 5. (The Uno only has Timer 1 out of that lot). The comments in the library show how to use the other timers.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

sldiesel

#16
Apr 01, 2013, 06:09 pm Last Edit: Apr 01, 2013, 06:51 pm by sldiesel Reason: 1

I've turned the fiddling around with registers into a library.

http://www.gammon.com.au/Arduino/TonePlayer.zip

Now you can just use that:

Code: [Select]

#include <TonePlayer.h>

int Hz = 0;
int prevHz;

TonePlayer tone5 (TCCR5A, TCCR5B, OCR5AH, OCR5AL, TCNT5H, TCNT5L);

void setup()
 {
 pinMode (46, OUTPUT);  // OC5A
 }

void loop()
 {
 Hz = analogRead (14) - 300;
 
 if( Hz < 10 )
   Hz = 33;
 if( Hz > 5000 )
   Hz = 5000;

 if ( abs (Hz - prevHz) > 5 )
   {
   tone5.tone (Hz);
   prevHz = Hz;
   }  // end of frequency change
   
 }  // end ofloop


It supports Timers 1, 3, 4, 5. (The Uno only has Timer 1 out of that lot). The comments in the library show how to use the other timers.


Hi I have just implemented that in my code and now have a rock solid TACHO driver.
I think that this tread can be useful for many people. having problem width timers on the Mega

I must say i have newer expected to get that kind of help in a Forum, Thanks alot.

Here is a video of it working
http://www.youtube.com/watch?v=Gh-9Jlec8E8


Go Up