Pages: [1] 2   Go Down
Author Topic: PWM Freq. I can't change it - please help :-(  (Read 2397 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working with Arduino MEGA 2560.
I need to encrease the 500Hz pwm freq.
I've read around the forum and Arduino website yet without working result.

I want also understand the arduino clock/timers behaviour but the information around the web are not so clear.
Nowhere I found a fully working sketch!

Can anyone help me?

Best regards,
Mauro
« Last Edit: February 02, 2011, 07:48:29 pm by franzSBK » Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26933
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Its not changeable!
All you can do is change the width of the pulses.

You can write your own of course - you can control the port bits directly, supposedly get up to 2.x MHz frequency that way.

Try a simple test, see what you get (may need a little tweaking to compile correctly)
Code:
byte pin3 =3; // pin to toggle
void setup()
{
pinmode (pin3 output);
}
void loop()
{
digitalWrite (pin3, HIGH);
delayMicroseconds(10); // for example
digitalWrite (pin3, LOW);
delayMicroseconds(20); // to make low time longer than high time for example
// change the times to change the overall frequency and the Pulse widths
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

Many thanks for the answer.

Yes, I know that is not changeable by default, but I referred to the AVR internal register as you wrote.

I can't use the software pwm becouse the CPU must do many many things and I need some working PWM output that works by itself (by hardware).

I tried some code found in the Arduino site and forum but I wasn't able to do it works... :-(

the compiler didn't show errors if I change the timer registers but output is always at 500Hz

Mauro.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26933
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How fast do you want the PWM frequency to be Mauro?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

I need about 8 - 10 Khz.

But what I really want is also udestand how can use it, also for the future other applications.
For example for motors is good to use 20 - 30 Khz.

Anyway, any  help is welcome  smiley-wink

Mauro.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26933
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am running a fencing scoring machine. I have a loop running where I check if 100uS have gone by and then I read some inputs and make decisions. So you can get pretty fast doing the same.

Code:
  currentMicros = micros();  // sample the time
  if (currentMicros - previousMicros >= hundred_uS_interval) // more than our interval?
  {
    // save the last time we okayed time updates
    previousMicros = currentMicros; // save the current time for next comparison
    toggle = 1-toggle;  // results in 0 1 0 1 ...

    // do stuff on 0.1mS intervals, 1mS intervals, ets.
//  I use toggle to write an output high/low, then read it back to see if a contact has been closed for 2mS

}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Dallas
Offline Offline
Sr. Member
****
Karma: 3
Posts: 337
nephew as a kittens
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the Arduino default 500Hz anyway? It seems kind of low to me.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26933
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dunno, I've only been around since August. 15.9 of the datasheet discusses the several modes of PWM the ATMega328 supports.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe this will help:

http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

Lefty
Logged

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

I've checked also the last link. I undestood that there is a base freq  then a prescaler that divide by fixed factor.
I also tried to change the register with this code but the output freq is always 500Hz.

to CrossRoads:
I use all analog input with intensive polling with a serial data comunication to send any analog reading.
the pwm must works on multiple pins about 3 at the same time of other tasks.

I'd like to have a simple working code that drive one pin to study how it works...  smiley-slim

Mauro
Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This thread gives the method I have used to juice up the PWM frequency for my needs.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0
Logged

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

You could try not using phase correct pwm out of the timers, fast pwm will get you an almost double speed increase with the same prescaler. Take a look at the timer sections in the data sheet, has all the info that you're looking for.
Logged

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

x gardner: yes  that source is compiled without errors or warnings but the output is always 500Hz. smiley-sad

x egdinger: yes I'm check it now but my doubt is: are the register name on atmel datasheet wrote same as arduino constant defined in the compiler?
I will check if I can use the register name written in the datasheet.

Mauro
Logged

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

The register names are the same. Can you post some of the code you are using, when trying to set the registers?

If you look in wiring.c you can see where the set the default prescaler values and the waveform type (phase correct/fast, etc).

Code:
        // timers 1 and 2 are used for phase-correct hardware pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
       
        TCCR1B = 0;

// set timer 1 prescale factor to 64
sbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);
// put timer 1 in 8-bit phase correct pwm mode
sbi(TCCR1A, WGM10);
« Last Edit: February 04, 2011, 02:47:38 pm by egdinger » Logged

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

Sorry for my late.

I don't really understand how spi(...) does...   smiley-confuse maybe only set the register flag

:-( I've made more test but arduino always push out 500Hz... :-(

There's no one that have a tested working code? just for check...    smiley-roll-sweat

Mauro
Logged

Pages: [1] 2   Go Up
Jump to: