Let's Make Robots!

Need to setup TIMER3 on Mega to higher frenquency PWM

I need to set TIMER3, controlling pins 2 and 3 on an Arduino Mega to either fast PWM mode (acceptable) or faster (higher frequency)(preferable).

I use this PWM to control an audio signal through a Silonex light sensitive resistor/LED unint and some audio contamination can be heard because of the speed at which the Silonex reacts. I used TIMER0 in tests with a prescaler of 1 and it works OK. But TIMER1 is "fast" PWM and TIMER3 is "phase correct".

So, either I make TIMER3 "fast" or even better, I set TIMER3 even faster (I would like to try a maximum frequency of 125000 or 250000 Hz if that's possible. A "fast" PWM does 62500 max).

I have read the whole "Arduino 101: Timers and Interrupts"a few times, but I'm just not good enough to "get it".

Thanks, Robert.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
TinHead's picture

... sometimes people tend to try doing things before even blinking a LED so, I guess I got used to raise the question :)

OK so the answer to q1 is: You don't have to put the Timer3.pwm(...) functions in the setup unless it is your intention to start up with the PWM setup that way.

q2: While analogWrite() might still work, but I would stick with the pwm() function provided by the timer3 lib because I'm not sure what analogWrite does to the timer3 on the mega (I assume it resets the timer to other settings).



I'll give it a try and compare the two, out of curiosity. But I will stick with the PWM() function in the real code.

That piece of code is actually receiving midi commands and adjusting volume (analog) accordingly. Having 1024 steps (as I undestand the TIMER3 code) will improve resolution.

The whole contraption is well over a thousand lines of code already... I'm also learning to write more efficient code.



TerryKing's picture

Hi, This may help, or at least help others who find this thread:



TinHead's picture

So looking at the datasheet reveals:

According to the table on page 148 - Modes of operation for the fastest "Fast PWM" mode, ie 8 bit wide you would have to set the flags:

WGM33 = 0, WGM32=1, WGM31=0, WGM30=1

So far so good ... next we need to see in which registers those flags are set.

On page 158 Register description of chapter 17 we have both registers we are looking for:

TCCR3A which has the flags COM3A1 COM3A0 COM3B1 COM3B0 COM3C1 COM3C0 WGM31 WGM30



TCCR3B with  ICNC3 ICES3 – WGM33 WGM32 CS32 CS31 CS30

So to get Fast PWM on timer3 you need to set the mentioned flags in the registers above. 
This is just a quick look, there might other settings to be done to get the right outputs and timing base but this should point you in the right way, and in the end I might be wrong but the answer is still in the datasheet. 
"Use the datasheet, Luke" ;)


Thanks TinHead,

I got the TIMER3 working in "fast pwm" mode by writing the proper values in the WGM3x bits. That effectively doubles the frequency on the PWM pins associated with that register. I now have a max frequency of 62.5K Hz. (that means using a prescaler of 1, by setting the CS3x bits to B001).

If I wanted to increase that frequency again (let's say to 125K Hz), am I right to guess that I would have to use the "fast PWM-ICR3"mode that is set by using B1110 as a WGM value? But then, what value do I have to assignt o ICR3 and/or TCNT3? Is this feasible?


Chris the Carpenter's picture

I don't know if this will translate to the mega, but this is what I have used in the past to change my PWM freq.


It does work for the Mega, but what I'm trying to do is convert TIMER3 from a phase-correct to a FAST PWM. These instructions change the pre-scaler and top off at 32K on the pins I need.

rogue's picture

Here is a great timer tutorial for AVR, and a breif PWM tutorial. Its doesn't reference Arduino but it probably has the information you need at a lower level still using C programming.

These are great tutorials. Unfortunately the second one was never completed. But it gives me an incentive to read the datasheet for the atMega processors and try to figure which but does what in these timer registers.

In the mean time, if anyone can supply me with the instructions to set TIMER3 as in my question, it would save me a lot of reading. That datasheet is 448 pages long...