Hardware PWM help please?

Hi sorry could someone explain how to use the hardware PWM pins to output a continuous pulse while the rest of the program is looping.

Hi. You can use the analogWrite() function to produce a PWM wave on pins 9 and 10 of the Arduino board. The function takes two parameters: the pin number and the duty cycle (out of 255). So:

analogWrite(9, 128); //outputs a 50% duty cycle wave on pin 9.

To use pin 11, see this forum thread:

Hi Mellis yes I know I can do analogWrite but this takes up time in the loop.

What I am looking for is a way to do multitasking ie. keep the PWM going while my loop does other things.

PIC chips can do this so I am sure the Atmel can as well??!!??

Can anyone help?

If you call analogWrite, the pin should continue to output a steady PWM wave until the next call to analogWrite (or a digital use of the pin). This won't take any time in the loop.

Ok thanks Mellis thats fantastic then! Will try it out! Thanks

i have to generate a 0-200 khz clock for my stepper motor driver. can i use the pwm port for this?

i tried values from 1-256. but i think the frequency is still too high.

thanks

michael

i read some forum threads about atmega's pwm. they mentioned some ways to define a certain frequency via the timer's prescaler. does anyone know how to get a certain frequency spectrum from the pwm?

i tried this code:

timer1SetPrescaler(64);
timer1PWMInit(9);
pinMode(9, OUTPUT);
timer1PWMAOn();
timer1PWMASet(128);

it seems to work, but i do not know how to calculate the right values.

i found the pulse and pwmsw classes in lib/avrlib. but i can't call pulseInit():

o(.text+0x5e): In function setup': undefined reference to pulseInit'

can you tell me how to use this library?