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 CCS Technical Support

sine PWM inverter code going crazy
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 3:46 pm     Reply with quote

Here's the code Mike
Code:

#include <12F683.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES CPD //Data EEPROM Code Protected
#FUSES PROTECT //Code protected from reads
#FUSES MCLR //Master Clear pin enabled
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled

#use delay(clock=8000000)

int16 i=0;

const int16 duty_cycle[32]={0, 49, 98, 145, 191, 236, 278, 317, 354, 387, 416, 441, 462, 478, 490, 498, 500, 498, 490, 478, 462, 441, 416, 387, 354, 317, 278, 236, 191, 145, 98, 49};

#int_TIMER2
void TIMER2_isr(void)
{
++i;
if(i==32)
  {
   i=0;
   output_toggle(PIN_A1);
   output_toggle(PIN_A5);
  }
set_pwm1_duty(duty_cycle[i]);
}

void main()
{
setup_adc_ports(sAN3|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(3);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,124,5);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_comparator(NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_8MHZ);

output_high(PIN_A1);
output_low(PIN_A5);

while(1);
}

hope this helps

I'm desperate.
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 3:52 pm     Reply with quote

As for Proteus, well i do have hardware and I've been comparing results. in my case Proteus is correctly simulating.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 6:07 pm     Reply with quote

Where is the 'digital oscilloscope' data coming from? Is it a real scope or a Proteus simulation....or a PC 'program' ????
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 12:13 am     Reply with quote

from both real scope and proteus simulation. i get the same results. thats why i'm sure proteus is correctly simulating my case.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 2:03 am     Reply with quote

There is a problem that applies to the older code as well. _When_ you do your switching.
You need:
Code:

#int_TIMER2
void TIMER2_isr(void) {
   static int1 toggle=0;
   if(++i==32) {
       i=0;
       toggle^=1;
   }
   if (i==1) {
       output_low(PIN_A1);
       output_low(PIN_A5);
       if (toggle) {
          output_high(PIN_A5);
       }
       else {
          output_high(PIN_A1);
       }   
   }
   set_pwm1_duty(duty_cycle[i]);
}

Two separate things:
First, turn both drivers _off_, before turning the required one on.
Second, what is coming out of the PWM, always 'lags'. So when 'i==0', the PWM, is currently outputting the value from entry 31. Switching at this point is not what you want.
As Mike has also pointed out, you could also solve this by re-ordering the data in the table, so entry 31, _is_ the zero entry.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 3:45 am     Reply with quote

GOBER wrote:
hey guys

I went with T's suggestion and reduced the points to 32. One array of 32 points only.

Here's how PWM looks!!

http://www.4shared.com/photo/HSMUphwe/Untitled.html?

WHY the fifth pwm is not the same as the first four???!!!! If you look carefully, you will notice that all the fifth pwms are not taking place correctly. As if they are being chopped at some time because lower value points don't have this problem. Lower values have five exact pwms!!! Why???


You don't tell us exactly which part of the waveform we're looking at.

OK. So it's a close up. I'm assuming just before the peak, where you've got 100% duty ratio.

You've changed the timer2 setting in several ways (which is why I asked for current code).
You're now interrupting on every fifth overflow of timer2, not fourth as you were before.
Is the 5 significant?
I don't have any 12F parts to test, so can't duplicate what you're doing.
What I do know is, the total time for your interrupt routine is the same order of magnitude as timer2 cycle time (i.e. 125 machine cycles).
Is it possible the PWM is being updated at the same time as timer2 overflows?
In other words, the PWM is corrupted by happening in two adjacent timer2 cycles.
The fifth PWM pulse is thus partly coded for the current pulse width and the next.

You could test for this by looking closely at the timing relationship between toggling the portA pins and when the PWM pulse width actually changes.
Or by adjusting the timer2 cycle time slightly..........

Mike

EDIT You're still ignoring the advice from myself and others about how you do the inverting.
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 11:09 am     Reply with quote

Hey Ttelmah. Thanks for the code. I appreciate what you are trying to. I did understand your point before but i didn't program it because i'm trying to fix the problem shown in the attached photo above. Take a look at it. The first four PWMs are fine. The last one, fifth, is what is going wrong. See how it has a duty cycle less than the later four. That's my problem. Do you know how to solve this?
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 11:44 am     Reply with quote

Mike, I treated the inverting point with T's code. Inversion is now fine.However, thi is not the main problem. The main problem is what i showed in the photo linked above. "The fifth PWM problem".

Five times is enough: 5*(1/16000)*32=0.01. That's one complete half-cycle.

Try Proteus. It is definitly correctly simulating my case. I check and compare it almost every time with hardware. Same results.

"Is it possible the PWM is being updated at the same time as timer2 overflows?" Absolutely. But this is not the cause f the problem. If you use Proteus, and i URGE you to do so, at leat in my case, You will notice that small duty-cycle PWMs don't bear a problem. Problems start at duty-cycle values of 317 and above! check and see
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 11:46 am     Reply with quote

GOBER wrote:
Hey Ttelmah. Thanks for the code. I appreciate what you are trying to. I did understand your point before but i didn't program it because i'm trying to fix the problem shown in the attached photo above. Take a look at it. The first four PWMs are fine. The last one, fifth, is what is going wrong. See how it has a duty cycle less than the later four. That's my problem. Do you know how to solve this?

I'll rephrase my question:-

Is it significant that the pulse you're having trouble with is the one BEFORE a PWM change rather than the fifth AFTER the previous one?

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 12:17 pm     Reply with quote

Problem is that while the images of the real scope 'show up' for me, the ones of the simulations don't. So I can't tell what your actual problem is.

Best Wishes
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 12:37 pm     Reply with quote

T check this

http://www.4shared.com/photo/HSMUphwe/Untitled.html?
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 12:39 pm     Reply with quote

Mike
Could be
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 3:10 pm     Reply with quote

You keep insisting that the problem pulse is the FIFTH, maybe its the FIRST. It doesn't match either the one before OR after.

Could be, you're looking in the wrong place!


Mike

EDIT I agree you've got a problem with the pulses and it needs addressing, but you must eventually deal with the inversion problem properly. If you don't at this stage, it will come back and bite you.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 3:17 pm     Reply with quote

If you have a spare pin, set it to go high then low at "zero" and put it on the other channel of the scope - that way you know where the start point is - trying to pick out a particular point in a repeating waveform is very tough to identify where the actual "start" is. I often put un-used pins to work as test points like at the start of an ISR or some other point that can be used as a reference.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 3:23 pm     Reply with quote

gpsmikey wrote:
If you have a spare pin, set it to go high then low at "zero" and put it on the other channel of the scope - that way you know where the start point is - trying to pick out a particular point in a repeating waveform is very tough to identify where the actual "start" is. I often put un-used pins to work as test points like at the start of an ISR or some other point that can be used as a reference.

mikey

He could even use the pins A1 or A5.
At this stage the pulse width problem is a show stopper.
Worrying about the inversion issue can be put on hold.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 4 of 6

 
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