View previous topic :: View next topic |
Author |
Message |
hoanglocmdc
Joined: 29 Sep 2014 Posts: 4
|
sine PWM inverter code not working |
Posted: Mon Sep 29, 2014 10:43 pm |
|
|
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
[img]http://s1316.photobucket.com/user/Mcu_Pro/media/Untitled_zps4f8fa910.png.html?sort=3&o=0[/img]
i 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 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Sep 30, 2014 9:00 am |
|
|
first you might check this
http://www.ccsinfo.com/forum/viewtopic.php?t=47549
i cant see where you declare or initialize flag and j
your timer#2 int is defective as CCS will clr the int flag for you
and really you could just POLL the timer 2 int_flag in your main and clear it
when true -just substitute the timer_int flag bit for your "flag" var ...
mostly though i can't imagine how this will produce a sine wave for you even if it was working as you hope.
lastly trusting Proteus results, especially generated waveforms is a losing game.
if this is a real project - and not schoolwork-
use real hardware and then see what you get |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
hoanglocmdc
Joined: 29 Sep 2014 Posts: 4
|
|
Posted: Tue Sep 30, 2014 9:39 am |
|
|
asmboy wrote: | first you might check this
http://www.ccsinfo.com/forum/viewtopic.php?t=47549
i cant see where you declare or initialize flag and j
your timer#2 int is defective as CCS will clr the int flag for you
and really you could just POLL the timer 2 int_flag in your main and clear it
when true -just substitute the timer_int flag bit for your "flag" var ...
mostly though i can't imagine how this will produce a sine wave for you even if it was working as you hope.
lastly trusting Proteus results, especially generated waveforms is a losing game.
if this is a real project - and not schoolwork-
use real hardware and then see what you get |
i declared flag and j at 3rd line and 4th line
i hope that, the code is true and proteus can not be trusted,
but, i haven't real oscilloscope so i must use proteus
i don't know why when duty cycle change from 15 to 135, the system work regular, but when duty cycle down from 120 to 15 it not work ???
i very happy when you reply.
thanks you !!! |
|
|
hoanglocmdc
Joined: 29 Sep 2014 Posts: 4
|
|
Posted: Tue Sep 30, 2014 9:40 am |
|
|
thanks you !!! |
|
|
|