![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
phamkien
Joined: 18 Jan 2018 Posts: 5 Location: Vietnam
|
SINE INVERTER |
Posted: Sun Jan 21, 2018 11:55 pm |
|
|
Hi Everyone!
I'm writting a code to make a sine wave inverter using pic 16f684. Please kindly give me some advices. I had wrote the code as below:
At first, PIN_C5 ( CCP1) will be used for creating spwm signal.
and the two square wave signal ( 50Hz ). I will use Pin C0 and Pin C1.
When I try to simulate on Proteus. I saw the delay time like the attached picture.
[img]https://photos.app.goo.gl/erggeFTNOxMV7Jru1[/img]
what I need is: Square wave signal turn on at the same time with Spwm signal and turn off when it finish a half Sine wave.
May I know which one will be used for control Spwm. Is it TMR2 ?
When I read the net, they wrote: TMR2 will increase from 0 to PR2. When TMR2=PR2, timer 2 interrupt is enabled.
I had tried the below code but it didn't work as expected.
Code: | If(TMR2==0) { output_high(pin_c0); output_low(pin_c1); }.... | Please kindly give me an advice. Thank you! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19382
|
|
Posted: Mon Jan 22, 2018 3:04 am |
|
|
Problem is that assuming the loop doing the testing is doing anything else, it can very easily miss TMR2 being actual being equal to 0....
Now you say TMR2 interrupt is enabled. Do you have an interrupt handler?.
You must never enable an interrupt without a handler.
However assuming you do just want to test for the moment when TMR2 passes it's top count, and goes back to zero, then you don't actually want to enable the interrupt....
Code: |
//before you start waiting
disable_interrupts(INT_TIMER2);
clear_interrupt(INT_TIMER2);
//Now your loop whatever it involves 'xxx'
while (xxx)
{
if (interrupt_active(INT_TIMER2))
{
//whetever you have to do when the timer has wrapped
output_high(pin_c0); output_low(pin_c1); //etc.
clear_interrupts(INT_TIMER2);
}
//other stuff in the loop
}
|
Key thing is to understand, that the interrupt will set (and you can clear it), even if it is _not_ enabled. What 'enabling' does, is says that this interrupt can call a handler. Once this is set, the interrupt _will_ call a handler (so the handler must be present), if the global interrupt flag is also set.
If you are using an interrupt handler, then if you want this code outside, instead of testing for TMR2==0, set a flag in the interrupt handler and test for this (again like the interrupt clear it once used). |
|
![](templates/subSilver/images/spacer.gif) |
phamkien
Joined: 18 Jan 2018 Posts: 5 Location: Vietnam
|
|
Posted: Mon Jan 22, 2018 9:47 pm |
|
|
Dear Mr Ttelmah
Thank you for your help. Can you please help me one more time? Refer to your instruction, I had wrote a code as below but it still doesn't work properly. Here is my code.
Code: |
void main()
{
char duty_cycle[41]={0, 15, 31, 46, 60, 75, 89, 102, 115,
127, 139, 149, 159, 168, 176, 183, 188, 193, 196, 199, 200, 200, 199,
196, 193, 188, 183, 176, 168, 159, 149, 139, 127, 115, 102, 89, 75,
60, 46, 31, 15};
setup_timer_2(T2_DIV_BY_4,249,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
clear_interrupt(INT_TIMER2);
disable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
output_low(pin_c0); output_low(pin_c1);
while(true)
{
if (interrupt_active(INT_TIMER2))
{
clear_interrupt(INT_TIMER2);
i++;
if(i==40) {i=0; j++; if(j==2) j=0;}
}
set_pwm1_duty(duty_cycle[i]);
if(j==0) {output_high(pin_c0); output_low(pin_c1);}
if(j==1) {output_low(pin_c0); output_high(pin_c1);}
}
}
]
|
https://photos.app.goo.gl/14XtPE2WGWJgxeBq1
Thank you! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19382
|
|
Posted: Tue Jan 23, 2018 12:29 am |
|
|
One fundamental thing, the 'set_pwm_duty' wants to be inside the part only called when the i changes. |
|
![](templates/subSilver/images/spacer.gif) |
phamkien
Joined: 18 Jan 2018 Posts: 5 Location: Vietnam
|
|
Posted: Tue Jan 23, 2018 1:03 am |
|
|
Dear Mr Ttelmah
Thank you very much for your help.
Best regards! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|