PWM speed function to wiring.c


I read a post showing the change of timers to create frequency on the PWM pins as follows:

TCCR1A = 0x00; // sets timer control bits to PWM Phase and Frequency Correct mode TCCR1B = 0x12; // sets timer control bits to Prescaler N = 8 ICR1 = 0x0FA0; // Upper Timer Limit = 4000 (in hex) equals 4ms => 1/5 of servo frequency

I would like to modify the wiring.c file to have a way to set this dynamically. i.e.

void PWMSpeed(int pin, int frequency) { set timer related to the pwm pin here to equal the frequency (I know it may not be exact) }

My questions are as follows:

Does each PWM pin have it's own timer or are they shared? Would changing the timer clock speed while the pin is outputting PWM signal cause issues? If so how can I turn it off, then change the timer then turn it back on without causing issues? Could someone explain to me the prescaler, and the upper time limit as it shows above. I need to know all the timer information as it related to PWM on pins to write the function. i.e. what timers are hooked to what pins and what variables like the ones above: ICR1, TCCR1B, and TCCR1A relate to which PWM channels / timers.

Could anyone help me?

I'd like to use PWM to control steps on 2 separate stepper motors or maybe 3. I have an external step controller to make the motors move but I need to send it on and off bits to increment each step and I would love to be able to change the speeds of the motors and still have my application do other things with sensors.

Pins 9 and 10 are controlled by timer 1, pin 11 by timer 2. Any registers (e.g. ICR1, TCCR1B) with a "1" in the name refer to timer 1, you should be able to replace the 1 with a 2 for the corresponding register of timer 2 (if it's available).

You should be able to change the frequency/speed while the timer is running.

The prescaler is a number by which the main clock on the chip is divided before being used to drive the timers. So if the prescaler is 8, the timer clock will only pulse once for every 8 pulses of the main clock. The higher the prescaler the slower the timers.

I think a function to set the PWM frequency will appear in an upcoming version of Arduino.

I think a function to set the PWM frequency will appear in an upcoming version of Arduino.

Any news on this?

thanks, e.

TCCR1B = 0x12; // This sets timer control bits to Prescaler N = 8 or 16?