Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: 1 [2]   Go Down
Author Topic: Feature request: Adjustable PWM frequency  (Read 20178 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 2
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Quote
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?
« Last Edit: September 17, 2009, 09:45:33 am by winmutt » Logged

Sweden, Gothenburg
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice....

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


This helps me a lot in my project. smiley
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?
Logged

Sweden, Gothenburg
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

TCCR0B is the timer for 5 and 6

Thanks any way
Logged

Atascadero, CA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 468
Arduino addict
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 07, 2011, 04:42:42 pm by koyaanisqatsi » Logged

What about elevensies? Luncheon? Afternoon tea? Dinner? Supper?

Pages: 1 [2]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: