Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Sine wave PWM inverter code not working

Status
Not open for further replies.

hoanglocmdc

Newbie
Newbie level 4
Joined
Sep 30, 2014
Messages
5
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Location
Hanoi, Vietnam, Vietnam
Visit site
Activity points
30
Hello everybody
I'm trying to implement a sine wave inverter at 50hz.
here is my code :
Code:
#include <nghich luu.h> 
#include <stdio.h> 
#include <stdlib.h> 
unsigned char j=0; 
unsigned char flag=0; 
unsigned char  duty_cycle_up[18]= {15,30,45,60,75,90,105,120,135,120,105,90,75,60,45,30,15}; 
#int_TIMER2 
void TIMER2_isr(void) 
{ 

flag = 1; 
clear_interrupt(INT_TIMER2); 
} 
void main() 
{ 
    
  //set_tris_c(0x00); 
   setup_timer_2(T2_DIV_BY_16,155,1);      

   setup_ccp1(CCP_PWM); 
   setup_ccp2(CCP_PWM); 
   set_pwm1_duty(0); 
   set_pwm2_duty(0); 
  // set_pwm1_duty(150); 
   enable_interrupts(GLOBAL); 
j=10; 
enable_interrupts(INT_TIMER2); 

   while(TRUE) 
   { 
      //TODO: User Code 
      if(flag == 1) 
      { 
     //j=j-1;if(j==0)j=10; 
      set_pwm1_duty(duty_cycle_up[j]); 
       j=j+1; 
       if(j==19){j=0;} 
      
      flag=0; 
      } 

  } 

}
here is results from proteus simulate :

Untitled.png

don't know why it is not working as expected of me
the duty cycle no change with i >10
Help me !!!
Thanks all for reading
 

Is it possible for duty cycle to go beyond 100% ?

Code C - [expand]
1
duty_cycle_up[18]= {15,30,45,60,75,90,105,120,135,120,105,90,75,60,45,30,15};

 

i don't think that duty cycle to go beyond 100%
you can see my picture about waveforms, they go to 100% at duty cycle ~ 135
then they not work when duty cycle count down from 120 to 15.
but, i very happy when you reply
thanks you !!!
 

I did not performed any calculations, but looking at the picture seems as Timer2 pre scaler is oversized for duty cycle range you are loading into PWM register. Another option would be you scale duty_cycle_up entire vector to a smallest value.
 

Hi
Actually I did not went through any hardware peripherals but looking at your c code and output, I found three possibilities,
1, you have duty_cycle_up of 18 values that is 0 to 17 but your j goes up to 18 !!!
2, when you have duty cycle loading code on 'while'. it is possible that there is delay in loading register also the repeated interrupts can come when executing if condition also.
3, I need exact number of pulses you are getting in the output. When I am looking in your output you have 8 pulses that means upto j = 7, that is 120. but you are getting nearly 50% output at 3rd pulse for 45 that forces the thought of maximum value for the duty cycle has to reduced.

so try this code if it is not working post your proteus project file with hex code.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <nghich luu.h> 
#include <stdio.h> 
#include <stdlib.h> 
unsigned char j=0; 
unsigned char flag=0; 
unsigned char duty_cycle_up[18]= {12,24,36,48,60,72,84,96,100,96,84,72,60,48,36,24,12}; 
#int_TIMER2 
void TIMER2_isr(void) 
{ 
 
       set_pwm1_duty(duty_cycle_up[j]); 
 
       if(++j >= 18)
       j=0; 
 
       clear_interrupt(INT_TIMER2); 
} 
void main() 
{ 
   setup_timer_2(T2_DIV_BY_16,155,1);      
 
   setup_ccp1(CCP_PWM); 
   set_pwm1_duty(0); 
 
   enable_interrupts(GLOBAL); 
     
   enable_interrupts(INT_TIMER2); 
 
   while(TRUE);
 
}

 

i try with your code. but the waveforms not true
if i change your code :
Code:
void TIMER2_isr(void) 
{ 

flag = 1;
       set_pwm1_duty(duty_cycle_up[j]); 
      delay_us(500);
       if(++j >= 20)
       j=0; 
clear_interrupt(INT_TIMER2);
}

the waveform is :
22.png
it not true, too
can you help me ???
 

check for your array size, it has 17 values i.e. [0 to 16] , and size is 18 and your loop is also going from [0 to 18]
 

i try with your code. but the waveforms not true
if i change your code :
Code:
void TIMER2_isr(void) 
{ 

flag = 1;
       set_pwm1_duty(duty_cycle_up[j]); 
      delay_us(500);
       if(++j >= 20)
       j=0; 
clear_interrupt(INT_TIMER2);
}

the waveform is :
View attachment 109899
it not true, too
can you help me ???

It doesnt needed to be true until we are finding what is the exact problem. whenever you post, post the full code and also i wanted the proteus project with hex file.

Also your code seems not relevant to your image output you have put j up to 20 but there are more than 20 pulses in one stream. And also say what are all the outputs you got after making the changes in #6.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top