CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pure PWM sine wave generator

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
mohammad3d



Joined: 28 Mar 2009
Posts: 17

View user's profile Send private message Send e-mail

Pure PWM sine wave generator
PostPosted: Tue Mar 26, 2013 12:14 am     Reply with quote

Hi all,
I try to write a Sine wave generator and think it must be helpful for
others.
This routine tested and work fine.
I use sine PWM at 10khz to create 50Hz sine wave.
Code:

// Pure PWM sine wave generator
//
// D0 ->--/\/\/\--|-- Sine output(~ 3vpp)
//        33Kohm  |
//               --- 100 nf
//               ---
//                |
//               GND


#include <30F6014A.h>
#fuses XT
#use delay(clock=10M)
int16 i;
int16 PWM=0;//pwm step counter
int16 sin_table1[181];//Store 0~180 degree sine lookup table
int16 sin_table2[181];//Store 181~360 degree sine lookup table
 
#define PI 3.14159265
#define Step_delay 50 //micro second
#INCLUDE <math.h>

void main()
{
//////////////// setup PWM ///////////////////////////
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1, 0x00fa);//PWM set to 250(0xfa)step at 10 Khz
setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);//PIN 58 (PORT D0)
//////// fill sine lookup table //////////////////////
  for (i = 0; i <=180; i++)
    {
    sin_table1[i]=125+floor(125*sin(i * PI /180));
    sin_table2[i]=250-sin_table1[i];
    }
//////// main loop ///////////////////////////////////   
 for(;;)
  {
//////////// 0~180 degree loop /////////////////////// 
   PWM=0;
   while(PWM<181)
    {
       set_pwm_duty(1,sin_table1[PWM]);
       PWM++;
       delay_us(Step_delay);
    }
//////////// 181~359 degree loop /////////////////////   
   PWM=1;
   while(PWM<180)
    {
       set_pwm_duty(1,sin_table2[PWM]);
       PWM++;
       delay_us(Step_delay);
    }
 }
}
mohammad3d



Joined: 28 Mar 2009
Posts: 17

View user's profile Send private message Send e-mail

improve algorithm ...
PostPosted: Thu Mar 28, 2013 1:10 am     Reply with quote

Hi all,
I improve last software algorithm.
Code:

// Pure PWM sine wave generator
//
// D0 ->--/\/\/\--|-- Sine output(~ 3vpp)
//        33Kohm  |
//               --- 100 nf
//               ---
//                |
//               GND


#include <30F6014A.h>
#fuses XT
#use delay(clock=10M)
int8 resync_step=0;
int16 i;
int16 Sine_wave_step=0;//pwm step counter
int16 sin_table[361];//Store 0~360 degree sine lookup table
 
#define PI 3.14159265
#define Step_delay 50 //micro second
#INCLUDE <math.h>

void main()
{
//////////////// setup PWM ///////////////////////////
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1, 0x00fa);//PWM set to 250(0xfa)step at 10 Khz
setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);//PIN 58 (PORT D0)

//////// fill sine lookup table //////////////////////
  for (i = 0; i <=180; i++)
    {
    sin_table[i]=125+floor(125*sin(i * PI /180));
    sin_table[i+180]=250-sin_table[i];
    }
//////// main loop ///////////////////////////////////   
 for(;;)
  {
//////////// 0~360 degree loop ///////////////////////     
   Sine_wave_step=0;
   while(Sine_wave_step<360)
    {
       set_pwm_duty(1,sin_table[ Sine_wave_step]);       
       Sine_wave_step++;
       delay_us(Step_delay);
    }

 }
}
ihsanbu



Joined: 14 Oct 2006
Posts: 27
Location: islamabad,pakistan

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

PostPosted: Thu Apr 25, 2013 11:39 pm     Reply with quote

ThanK for these codes , i need code to interface SIM900D GSM module with pic18f452 micro-controller.
regard
ihsanbu@gmail.com
_________________
ihsan ullah khan
Embedded Designer Since 2003
ihsanbu



Joined: 14 Oct 2006
Posts: 27
Location: islamabad,pakistan

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

PostPosted: Thu Apr 25, 2013 11:41 pm     Reply with quote

hi can i use this for pic18f452
_________________
ihsan ullah khan
Embedded Designer Since 2003
mohammad3d



Joined: 28 Mar 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 25, 2013 11:58 pm     Reply with quote

Hi dear ihsan,

Yes, you can use this code for any kind of PIC18f series, but you must
configure your chip PWM module and step delay to get right frequency.
I don't know SIM900 module and don't know which kind of PWM you need,
but my code just simulate sine wave form just like 220v/50hz power line.
Very Happy
Regards,
Mohammad.
AsadZero2



Joined: 18 Dec 2013
Posts: 1

View user's profile Send private message

18f4550
PostPosted: Wed Dec 18, 2013 10:20 am     Reply with quote

aoa Mohammed3d

I know I'm asking this after ages but i hope you or someone will be able to help me. I'm new to the uController thing and i need to generate a sine wave that'll vary in frequency upon certain conditions.
I used the code however there were some things i couldn't get to fix according to the pic18f4550.
Code:

#fuses XT
#use delay(clock=10M)
int8 resync_step=0;
int16 i;
int16 Sine_wave_step=0;//pwm step counter
int16 sin_table[361];//Store 0~360 degree sine lookup table
 
#define PI 3.14159265
#define Step_delay 50 //micro second
#INCLUDE <math.h>

void main()
{
//////////////// setup PWM ///////////////////////////
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1, 0x00fa);//PWM set to 250(0xfa)step at 10 Khz
setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);//PIN 58 (PORT D0)

//////// fill sine lookup table //////////////////////
  for (i = 0; i <=180; i++)
    {
    sin_table[i]=125+floor(125*sin(i * PI /180));
    sin_table[i+180]=250-sin_table[i];
    }
//////// main loop ///////////////////////////////////   
 for(;;)
  {
//////////// 0~360 degree loop ///////////////////////     
   Sine_wave_step=0;
   while(Sine_wave_step<360)
    {
       set_pwm_duty(1,sin_table[ Sine_wave_step]);   // *** I'm getting paranthesis error in this line
       Sine_wave_step++;
       delay_us(Step_delay);
    }

 }
}

So i changed the CODE to:
Code:

#include <18F4550.h>
#fuses XT
#use delay(clock=10M)

int8 resync_step=0;
int16 i;
int16 Sine_wave_step=0;//pwm step counter
int16 sin_table[361];//Store 0~360 degree sine lookup table
 
#define PI 3.14159265
#define Step_delay 50 //micro second
#INCLUDE <math.h>

void main()
{
//////////////// setup PWM ///////////////////////////
setup_timer_3(t3_INTERNAL | t3_DIV_BY_1);//PWM set to 250(0xfa)step at 10 Khz
//setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);//PIN 58 (PORT D0) // *** I HAD TO DISABLE THIS LINE TO MAKE IT WORK

//////// fill sine lookup table //////////////////////
  for (i = 0; i <=180; i++)
    {
    sin_table[i]=125+floor(125*sin(i * PI /180));
    sin_table[i+180]=250-sin_table[i];
    }
//////// main loop ///////////////////////////////////   
 for(;;)
  {
//////////// 0~360 degree loop ///////////////////////     
   Sine_wave_step=0;
   while(Sine_wave_step<360)
    {
       set_pwm1_duty(sin_table[Sine_wave_step]);       
       Sine_wave_step++;
       delay_us(Step_delay);
    }

 }
}

I need to know which pin is it giving output on and how can i change the frequency of the output wave.
mohammad3d



Joined: 28 Mar 2009
Posts: 17

View user's profile Send private message Send e-mail

change sine wave frequency.
PostPosted: Mon Dec 23, 2013 5:04 am     Reply with quote

Dear asad,
You can change "Step_delay" to take other period time.
If "step_delay = 50 ", your while loop execute about 0.02 second or 50 time
per second and you have an sine output with 50 Hz frequency.
Regards,
Mohammad Kouchekzadeh.
joanpebupe



Joined: 19 Feb 2015
Posts: 1

View user's profile Send private message

Pure PWM sine wave generator
PostPosted: Thu Feb 19, 2015 11:03 am     Reply with quote

Good afternoon!

I was looking for that style of code to do a sine wave with the pwm. i'm using PIC18F4331 and i would really like to know how implement this code but using power pwm instead of CCP/PWM.

above all i would like to know how configure the sentence:

set_power_pwm0_duty(*******)

cause I think it works different on power PWM.

Thanks!

Juanpe
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group