Arduino Timere Useage

I have little project going that makes me wonder what Arduino does behind the scenes. I'm generating a TONE at 50Hz and multiplexing a 2-digit array by reading the 50Hz back with an Interrupt, writing the digits out etc.

I am also generating 38KHz for IR transmission by direct register access to timer4 (This is a 32U4 Leonardo Micro @16MHz).

I did read the source on Tone() but saw only one reference to 32U4 during #define.

Is there a document somewhere that shows us timer usage for various Arduino boards without sifting through source code? How can I be sure I'm not stepping on Arduino with register access?

and multiplexing a 2-digit array

Pins are multiplexed, not data.

Is there a document somewhere that shows us timer usage for various Arduino boards without sifting through source code?

Sure. The datasheets for the Arduinos documents EVERYTHING. Heavy going sometimes.

Why are you writing to a pin, and then reading that pin? Quit wasting the timer on tone(). Use it directly.

Quit wasting the timer on tone(). Use it directly.

It was a quick way to get a 50Hz multiplex clock. How does tone() waste any more timer than writing to the registers of a timer?

Which brings me back to my question of what Arduino uses behind the scenes so I can know which timer to avoid.

Okay, so let's say I use one of your "Teensy 2.0" boards with your Arduino plug in. If I look on your website, I don't see any discussion of which timer the system uses where.

I also looked at the Leonardo Datasheet on this site and find no map.

All I'm trying to understand is:

Which Timer does the Tone() function use? The 32U4 has 6 PWM pins, which timers does PWM use? Which Timer does millis() use? Does USB use a timer?

I do know it uses the PLL set at 48Mhz because I blindly downloaded someone's Timer4 code. That person's code set the PLL to 64Mhz, which BREAKS the USB port just fine. Had to re-flash the bootloader to be able to have USB again.

That's why I'm reluctant to just go stomping around in the timers fearing what will break if I do!

These are good questions.

Which Timer does millis() use?

Timer 0. (On an Arduino anyway).

Does USB use a timer?

No, not that I am aware of.

let's say I use one of your "Teensy 2.0" boards with your Arduino plug in

Actually, I don't believe they are "our" boards. See:

https://www.pjrc.com/store/teensy.html

That person's code set the PLL to 64Mhz, which BREAKS the USB port just fine.

With all due respects to the Teensy people, these are Teensy boards, not Arduino boards. Your questions might be better directed to them.

rmetzner49: It was a quick way to get a 50Hz multiplex clock. How does tone() waste any more timer than writing to the registers of a timer?

The Tone library, or the copy I have anyway, uses interrupts so that it can toggle any pin, even ones not on the PWM outputs. As PaulS suggests, configuring the hardware directly is likely to be more efficient.

Thank you, Nick for answering two of my questions. I don't actually have one of Paul's boards I have a "Leonardo Compatible" Freaduino. I chose it for small size and I/O count. I merely used the Teensy as an example and he does claim to have an Arduino plug-in.

http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2055119.m570.l1313.TR0.TRC0.H0.Xfreaduino+micro&_nkw=freaduino+micro&_sacat=0

I've used about 5 of this brand in various applications with no complaints. Yes, the 32U4 can be finicky on how it handles a reset/upload. It's also possible to write code that stomps on the bootloader (e.g. writing past the end of an array). And as stated, the USB system does use the PLL and prescaler which I learned by digging myself out of that post that sets the PLL to a new value. I suspect it's possible to break the 328 too though.

I will take Paul's advice on using a timer directly to generate 50Hz, since you just told me it uses interrupts. Since my code uses an interrupt as well, it seems prudent to relieve the Arduino of the tone() interrupt.

Do you know what sits on Timer3? The 32U4 has no Timer2 and substitutes Timer4 for this. If Timer3 is unavailable I might just throw a 555 on the board since stability and symmetry can be pretty poor, all I'm doing is multiplexing 2 digits of a 7-segment display.

I can't really speak for the 32U4, because I mainly work on the 328, however the default configuration is to use Timer 0 for millis(), delay() etc. and the other timers are set up for PWM output (ie. analogWrite).

I wouldn't get too excited about having no timer 2, because that is only 8 bits anyway on the 328. You have 16 bit timers 1 and 3 on the 32U4 plus a 10-bit timer 4, so you have plenty of options. According to the datasheet:

The Timer/Counter4 is a 10-bit Timer/Counter module that can alternatively be used as an 8-bit Timer/Counter.


I suspect it's possible to break the 328 too though.

The Uno and similar boards have a separate chip for USB which probably makes them more robust in that respect.

Thank you, Nick. I concluded that this is still feasible and you have an agreeing opinion. I think I mentioned I'm using Timer4 to generate the modulated IR-38KHz though.

The good news is, Timer3 is available and just so happens to be sitting on the same pin, D5 (PortC-6) that is the output of Timer3. Imagine that, something just went right! I don't even have to move the wire! I'm not doing any PWM, so that's fine.

So thanks for the support and the info that I need to stay off Timer0 or I break millis(). I think tone() uses timer1 in some fashion but that's of no consequence since I won't need it after configuring Timer3.

The Uno and similar boards have a separate chip for USB which probably makes them more robust in that respect.

Maybe .... if I'm silly enough to clobber the bootloader that's probably moot, isn't it? Still "bricked" it.