Problem assigning 16-bit timers for Tone

Hi All,
Hoping someone can help with an issue I’ve been struggling with for a while. I’m trying to assign three 16-bit timers for Tone (on Mega 2560) . Per Tone notes, I believe timers are assigned in the order of 2,3,4,5,1,0. On Mega, I believe timers 3,4,5 are 16 bit - so I’d like to assign those. Below is an example of how I’m trying to test this.

#include <Tone.h>
  Tone timerSkip;
  Tone freq1;              
  Tone freq2;              
  Tone freq3;              

void setup() {

  timerSkip.begin(11);  // Skip 8-bit timer 2 assignment (arbitrary set to pin 11)
  freq1.begin(8);          // Assign freq1 to 16-bit timer 3 (pin 8)
  freq2.begin(9);          // Assign freq2 to 16-bit timer 4 (pin 9)
  freq3.begin(10);        // Assign freq3 to 16-bit timer 5 (pin 10)
}

void loop() {

  freq1.play(10);     // Play 10Hz with timer 3 on pin 8  * Works, 10Hz out *
  freq2.play(10);     // Play 10Hz with timer 4 on pin 9  * Appears to be an 8-bit timer (600Hz out) *
  freq3.play(10);     // Play 10Hz with timer 5 on pin 10 * No output at all *
}

freq1 works fine, freq2 seems to be assigned to an 8-bit timer (high frequency result), and freq3 is not outputting anything.

I’m at a loss here and hopefully it’s something silly I’m missing. Any help would be great.

Thanks,
Greg

What leads you to believe an 8 bit timer will not work for your application?

Hi,

Sorry, I should have given a little more background in my original post. Because I need to be able to set frequency ranges on each output from 1Hz to 5KHz. 8-bit timers limit the low end to 31Hz; whereas 16-bit timers allow the 1Hz low end.

"GitHub - daniel-centore/arduino-tone-library: Produces a square wave of specified frequency on any arduino pin" describes the timer stuff in the "Ugly Details" section at the end.

Any help achieving this would be awesome.

Thanks,
Greg

If you place your code between code tags like
** ** [code]your code here[/code]** **
, you won't have smiley faces in your post. You can edit your post and add them.

What Tone library are you using? The built-in one only does one tone:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

#define AVAILABLE_TONE_PINS 1
#define USE_TIMER2

const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };

Hi John,
I'm basing this on Brett Hagman's notes (link from Tone reference... GitHub - bhagman/Tone: A Wiring Library to produce square wave tones on arbitrary pins.). Excerpt below. If not correct, is there another Tone library that you think I should look at?

"(The library uses the hardware timers on the microcontroller to generate square-wave tones in the audible range.

You can output the tones on any pin (arbitrary). The number of tones that can be played simultaneously depends on the number of hardware timers (with CTC capability) available on the microcontroller.

ATmega8: 2 (timers 2, and 1)
ATmega168/328: 3 (timers 2, 1, and 0)
ATmega1280: 6 (timers 2, 3, 4, 5, 1, 0) * same on 2560 (my addition)
The timer order given above is the order in which the timers are allocated. Timer 0 is a sensitive timer on the Arduino since it provides millis() and PWM functionality.

The range of frequencies that can be produced depends on the microcontroller clock frequency and the timer which is being used:

MCU clock 8 bit timer Flow 16 bit timer Flow Fhigh
8 MHz 16 Hz 1 Hz (1/16 Hz) 4 MHz
16 MHz 31 Hz 1 Hz (1/8 Hz) 8 MHz
Although Fhigh can go as high as 8 MHz, the human hearing range is typically as high as 20 kHz.

Tone accuracy is dependent on the timer prescalar. Frequency quantization occurs as the frequencies increase per prescalar.

If you used a 16 bit timer (e.g. timer 1, or timers 3,4,5 on '1280), you could generate "tones" down to 1/8 Hz (one cycle every 8 seconds), although the library only accepts integers for frequency."

DangerToMyself:
If you place your code between code tags like
```
** [/b]

your code here

[b]**

```
, you won't have smiley faces in your post. You can edit your post and add them.

Thanks, I just edited the post with this.