Go Down

Topic: Feature request: Adjustable PWM frequency (Read 37243 times) previous topic - next topic


Check out this page: http://www.arduino.cc/playground/Main/TimerPWMCheatsheet


Sep 17, 2009, 04:20 pm Last Edit: Sep 17, 2009, 04:45 pm by winmutt Reason: 1
Bump. Anyone know if 140z is possible with the internal timer.

I may be misreading this but in fast pwm mode, OCR0A controls the period.

PWM with 2ms period and 10% duty

Configure timer 0 to generate a 2ms period with a 10% duty cycle.

The fast PWM mode where the period is controlled by OCRA has (WGM2,WGM1,WGM0) = (1,1,1). The compare match B output must be used and it should turn on at TOP and reset at match. This happens when (COM0B1,COM0B0) = (1,0). The prescaler is set to 64.

TCCR0A = _BV(COM0B1) | _BV(WGM01) | _BV(WGB00);
TCCR0B = _BV(WGB02) | _BV(CS01) | _BV(CS00);
OCR0A = 250;

The duty cycle (period the PWM is on) is controlled by OCR0B. The range of OCR0B is from 0 to OCR0A (or 250 in this case). If OCR0B exceeds OCR0A, then the PWM output will be alway on. A duty cycle of 10% is set by:

   OCR0B = OCR0A * 10% = 250 * .1 = 25

The configuration is complete when OCR0B is assigned:

OCR0B = 25;

Changing the output compare control setting (COM0B1,COM0B0) = (0,1) inverts the waveframe (i.e., the output will be off for 10% and on for 90%).

What is the setting for a 75% duty cycle? How would the period be changed to 250 uS, and how would this affect the precison of the duty cycle?



Code: [Select]

void setup() {
    TCCR2B = (TCCR2B & 0xF8) | value;

This helps me a lot in my project. :)
To change PWM-frequency on pin9 and 10 i have noticed that TCCR1B works. (don't know what it is. trial and error.)
If I want to change frequency on pin5 and 6. How do I do that?


A little bit more reading on this forum gave me the answer.....

TCCR0B is the timer for 5 and 6

Thanks any way


Jan 07, 2011, 10:34 pm Last Edit: Jan 07, 2011, 10:42 pm by koyaanisqatsi Reason: 1
Be careful with TCCR0B.  delay() and millis() are based on it and their behavior changes when you change TCCR0B.  Earlier in this thread, I posted how to deal with it.  But there are some other little factors I haven't identified.  My sketch would not run with certain TCCR0B values.  So I generally stay away from that one and just use the other PWM pins if I need a different freq.

EDIT: OK, it wasn't earlier in this thread, but in another thread I link to on page 1 of this thread.
Liked again: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0

Go Up