Pages: 1 [2]   Go Down
Author Topic: MEGA 2560 Tone conflict width PWM  (Read 1841 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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


« Last Edit: April 01, 2013, 11:51:24 am by sldiesel » Logged

Pages: 1 [2]   Go Up
Jump to: