| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			mohammad3d
 
 
  Joined: 28 Mar 2009 Posts: 17
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| Pure PWM sine wave generator | 
			 
			
				 Posted: Tue Mar 26, 2013 12:14 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| improve algorithm ... | 
			 
			
				 Posted: Thu Mar 28, 2013 1:10 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			
			 
			 
			 
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Apr 25, 2013 11:39 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			
			 
			 
			 
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Apr 25, 2013 11:41 pm     | 
				     | 
			 
			
				
  | 
			 
			
				hi can i use this for pic18f452 _________________ ihsan ullah khan
 
Embedded Designer Since 2003 | 
			 
		  | 
	
	
		  | 
	
	
		
			mohammad3d
 
 
  Joined: 28 Mar 2009 Posts: 17
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Apr 25, 2013 11:58 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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.
 
   
 
Regards,
 
Mohammad. | 
			 
		  | 
	
	
		  | 
	
	
		
			AsadZero2
 
 
  Joined: 18 Dec 2013 Posts: 1
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| 18f4550 | 
			 
			
				 Posted: Wed Dec 18, 2013 10:20 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| change sine wave frequency. | 
			 
			
				 Posted: Mon Dec 23, 2013 5:04 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Pure PWM sine wave generator | 
			 
			
				 Posted: Thu Feb 19, 2015 11:03 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 | 
			 
		  | 
	
	
		  | 
	
	
		 |