Generating Sine Wave from PwmOut - LPC1768

17 Feb 2014

Hi guys, Earlier I made one post regarding creation of square wave from PwmOut. Well I have created that square wave of 1kHz from 200 kHz. Below is the code:

 #include "mbed.h"

PwmOut PwmPin (p22);
  
int main() {
    PwmPin.period(0.000005);  // Set frequency to 200,000 Hz (period = 5 us)
    
    while(1){ //infinite loop
                 
        PwmPin.write(0);       // Set the duty cycle to 0%
        
        wait_us(500); // wait 100 PWM cyles
        
        PwmPin.write(1);     // Set the duty cycle to 100%
        
        wait_us(500);   // wait 100 PWM cycles
        
    }      
} 

Now my question is that how can I get sine wave of same (1kHz frequency) from this.

I have tried to merge some extra code into it but when I checked it on oscilloscope, its still the square wave.

I got very limited time for this. Any help would be appreciable.

17 Feb 2014

I'm still trying to change that signal into sine wave but the waves on oscilloscope are sweeping.

17 Feb 2014

Anyone please .....

17 Feb 2014

Hi Rayneet, Do a search of mbed on sin_test - should answer your question though you won't need the lcd stuff...

Dave

17 Feb 2014

It is not very clear from your description but it seems you are trying to generate a low frequency waveform by using PWM on a higher frequency digital square wave signal. This is the technique used to generate three phase sinewaves for a variable frequency motordrive. You can do this as follows:

  1. declare a float array of something like 32 entries.
  2. fill the array with sine values between 0 and 2pi in 32 equal steps. The sine values will be between -1.0 and 1.0
  3. scale these sine values to match the duty cycle values needed for PWM to a range of 0.0 through 1.0 This may be done by val = (sine + 1.0) / 2.0
  4. in your main set up a PWM pin with period to match the desired 200Khz
  5. in your main set up a Ticker with a rate of 32x the desired low-freq signal of 1Khz
  6. attach the Ticker to a function that sets the duty cycle using the next entry in the sine table, increment the index for the table and reset it to 0 when it becomes 32
  7. in your main you can now run an idle loop or do something useful. The main code will be interrupted at regular intervals to generate the sine.
  8. you need a low-pass filter on the PWM output to get a smooth sine. Note that the sinewave voltage is between 0 and 3.3V
17 Feb 2014

Hi Wim, thats exactly what I need. But I don't know that how to proceed. I'm not that good at programming.

17 Feb 2014

Hi Dave, I have already done a lot of search and afterwards I posted my question in forum. Well I'm not using lcd, i just checking my signal by connecting mbed to oscilloscope.

18 Feb 2014

Ravneet S wrote:

Hi Wim thanks for posting on the forum. Mate i just wondering if you could help me with the code. I tried so many times to change that square wave into sine wave but I can't get it. I would appreciate your help.

OK, so heres example code http://mbed.org/users/wim/code/SineFromPWM/

The output looks like this: /media/uploads/wim/screenshot_pwm_sine.jpg You can see the dutycyle variation 0..1..0 and it is repeated at 1 ms (1 KHz)

The carrierwave is 200KHz /media/uploads/wim/screenshot_pwm_sine2.jpg

I recommend you play with it until you understand how it works. You could generate other waveforms by changing the values in the dutycyle array.

06 Mar 2014

You could also put a capacitor between the output and ground to smooth a square or sawtooth shape.

I tried this recently with an old analogue oscilloscope and it produced a very smooth wave sequence. It might not exactly be a sine wave however, so it depends how accurate you need it to be. I expect there is probably some way to choose the capacitor in a way that the square wave gets converted into an accurate sine wave but I'm just guessing.

Ashley

15 Mar 2019

Ravneet S wrote:

Hi, Any Pro level embedded programmer here? Need Assistance for my ProJect

15 Mar 2019

joiukhghjgu

Please log in to post a reply.