Generating PWM waves of desired frequency......

Friends first of all thanks for reading this question.....

 

Actually i need to generate a PWM signal of frequency near about 40 Hz with the help of Atmega 8 microcontroller ,it is easily done with the help of following program

 

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

 

 

But i don't want to do it this way ,actually i want to do it by "analogwrite" function but analogwrite function directly doesn't allow us change the frequency of signal.I will be thankful if anyone can help me changing the frequecy of PWM signal....Need ur help frnds as soon as possible..

 

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Hi Mayank,

    I have generated a near pure PWM sine wave using a mega8 as follows:

    Create a 255 byte array. Each byte of that array will represent the amplitude of 1/255 of a cycle of your waveform. So if you wanted to make a sine wave, then the array[1] would be the start of the sine wave, and array[255] would be the last amplitude value. Each entry in the array would range from 0 to 255 (a byte)

    Now set up a timer interrupt that pings at 2050 hz, and each time it pings it takes an entry from the above array, and uses it to ajdust the PWM output of a second timer setup in PWM mode. Essentially you are creating a PWM output that is modulated according to the wavefrom stored in the array. You need a simplt RC filter on the output to smooth the rough edges.

     I have not put a working code example in here as you may have already sorted this out, but if not please reply and I can post the code to you. I wrote it in BASComm.

This reply was deleted.

Activity