PWM frequency library

Sorry, I haven't looked at this thread in a while. I updated the original post to explicitly say this library does not support the Due. I don't think I will add support because I don't own a Due, and it appears to have some built library support already.

Petit Jean:

Thank you runnerup for this nice piece of work!

I have some questions for you, since I really need to expand this library for the Micro/Leonardo.
So that is what I will do, but maybe you can help me in the right direction.

In your estimation, can I better build of of ATimerDefs or BTimerDefs?
Do you have any idea how I could implement the difference in structure of the new 8 - 10 - 16 bit pins and fast PWM?
I want to change some of the timer—pin assignments for my project. Is that something that you think will work correctly with the construction you used for the library?
I will find my way in the end, but can you give me some places to start looking for the registers and stuff?

Any hint is welcome.

ATimerDefs or BTimerDefs are simply files that the preprocessor will include depending on the avr architecture the user compiles his code for.
If you want to implement different PWM modes, look at the specs sheet (http://www.atmel.com/Images/doc2549.pdf). Table 17-2 on page 148 describes which bits to manipulate to set each of the modes for the 16 bit timers.

mxw8950:

Thank you so much for this library as it has been fantastic in helping me get a pump driver up and running.

I have a question regarding timers using the 328. I have noticed that the library functions do not allow you to set the frequency on TIMER2A and was wondering if there was a specific reason why the library would not allow you to set this timer as I can't really find a default use for TIMER2A that would interfere with any other arduino function.

Thanks,
-matt

You don't set the frequency on TIMER2A because that is a channel, not a timer. A single pin is attached to timer2a, multiple pins are wired to a timer. When you set a frequency, it sets the entire timer and all of the pins wired to that timer are affected. If channel A on timer 2 is not working, then channel B probably won't work either. Can you verify that?

MaxHeadroom:

I want to use your library to up the frequency of the PWM on my motor control project (motors are noisy) on a Uno. It is a twin motor control so I use pins 5, 6 for one and pins 9,10 for other.
Pins 5, 6 are timer0 and I don't want to mess with that timer.
So the next step would be to change it to pins 3, 11 to use the other timer. This would mean hacking my shield.
And that is why I don't understand the above. Does this mean I can map the PWM output to any pin?

No, my wording is just a little confusing. By that I meant the caller (of that specific function) does not need to have any knowledge about which timer that pin is connected to since the function will figure that out for them. The pins are hardwired to the respective timer and cannot be changed. If you aren't using your uno for anything that requires timing functions (such as delay() and millis()) then you can use timer 0 without any problems.

Tenta:

where can I look up which timer on the Mega 2560 is connected to which pin?
Also how can I turn PWM Pin 2 & 3 into phase correct mode?

you can look it up here:

timer# and channel pin #
TIMER3B 2
TIMER3C 3
TIMER0B 4
TIMER3A 5
TIMER4A 6
TIMER4B 7
TIMER4C 8
TIMER2B 9
TIMER2A 10
TIMER1A 11
TIMER1B 12
TIMER0A 13
TIMER5C 44
TIMER5B 45
TIMER5A 46

The library initializes timers to phase and frequency correct PWM. I do not know what you mean by phase correct because the timers have many 'phase correct' settings. I advise you to look at table 17-2 on page 148 and table 17-5 on page 160 in the specs sheet (http://www.atmel.com/Images/doc2549.pdf). The code you listed will not set the correct waveform generation mode. Com bits determine whether the pin is connected/how the pin is manipulated by the timer. Waveform generation mode is a timer setting, not a pin setting. You should manipulate the WGM bits instead.