Go Down

Topic: PWM Freq. I can't change it - please help :-( (Read 2 times) previous topic - next topic

franzSBK

Feb 03, 2011, 01:31 am Last Edit: Feb 03, 2011, 01:48 am by franzSBK Reason: 1
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

CrossRoads

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: [Select]

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
}
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.

franzSBK

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.

CrossRoads

How fast do you want the PWM frequency to be Mauro?
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.

franzSBK

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  ;)

Mauro.

CrossRoads

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: [Select]

  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

}
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.

BetterSense

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

CrossRoads

Dunno, I've only been around since August. 15.9 of the datasheet discusses the several modes of PWM the ATMega328 supports.
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.

retrolefty

Maybe this will help:

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

Lefty

franzSBK

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

gardner

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

egdinger

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.

franzSBK

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

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

egdinger

#13
Feb 04, 2011, 08:39 pm Last Edit: Feb 04, 2011, 08:47 pm by egdinger Reason: 1
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: [Select]

        // 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);

franzSBK

Sorry for my late.

I don't really understand how spi(...) does...   :~ 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

Go Up