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: Sun Dec 16, 2012 3:49 pm     Reply with quote

Hello Guys

Sorry for late response. I was a little bit busy. I was able to upload some pictures of proteus DSO and hardware DSO showing what is going on.

This link shows the sine signal at the output (220V) when i only use 39-points array:

http://www.4shared.com/photo/l7ApBtMh/Sine_Signal_with_38-points_arr.html?

These links show Proteus PWM output with 195-points array:

http://www.4shared.com/photo/v9UU-gEa/DSO_PROTEUS_195-point_array-1.html?

http://www.4shared.com/photo/Nm4ewPtM/DSO_PROTEUS_195-point_array.html?

This link shows the hardware sine output at 195-points array:

http://www.4shared.com/photo/DJoh_jKW/HARDWARE_SCOPE_WITH_195_POINTS.html?

I hope it is clear what's happening now

waiting for your comments guys
temtronic



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

View user's profile Send private message

PostPosted: Sun Dec 16, 2012 4:16 pm     Reply with quote

What's happening is that you're NOT using real hardware, but doing a 'simulation' with proteus....arrrgh...

There is NO reason to think it's right ....proteus is FULL of bugs, errors, faulty DRCs....

PIC101...need I say more.
gpsmikey



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

View user's profile Send private message

PostPosted: Sun Dec 16, 2012 11:26 pm     Reply with quote

I heard a rumor once that Proteus was able to accurately simulate something, but was never able to confirm it ... Shocked

The first trick to solving a problem is to make sure the tools you are using to check the "problem" work. More people seem to have wasted more time trying to do it "quick" with Proteus than if they had simply put together a simple test circuit.

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
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Mon Dec 17, 2012 12:42 am     Reply with quote

Hey guys.

I guess in my case Proteus is correctly simulating. However, to be on the safe side i did attach two pictures showing sine output at 39-point array(the first link) and at 195-points array (the last link). These are hardware result. I implemented my design and the pictures shown are from my hardware DSO not Proteus.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Mon Dec 17, 2012 2:18 am     Reply with quote

Hm.
Think about it for a moment. Look at what it is giving. There is nothing whatsoever wrong with the sine wave, except it is jumping at the full scale points. The signal between is fine.
Now, if you are using a feedback value to control a waveform, shifting to waveforms with bigger or smaller outputs, a critical thing is 'where do I change'. Answer has to be _at the zero crossing point_. Change anywhere else, and you will get jumps like you are showing.
Your problem is not with the sine look-up, or with what is being generated, but with how you implement your control. The test for go up/down, _must_ only be done at the zero point.

Best Wishes
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Mon Dec 17, 2012 4:06 pm     Reply with quote

Ttelmah I have not implemented feedback yet. I will take in account what you explained when i do because you are absolutely right. However i have to solve the main problem first which i will explain in another way hoping you will understand me this time correctly, and if you see i'm the one whose not getting your points will you please try to explain it in a different way. Please.

If I use array
Code:
duty_cycle[40]={0, 20, 40, 60, 81, 99, 118, 136, 153, 169, 184, 198, 211, 222, 231, 240, 247, 253, 257, 259, 260, 259, 257, 253, 247, 240, 231, 222, 211, 198, 184, 169, 153, 136, 118, 99, 81, 60, 40, 20}

where it goes from 0 to 40, I obtain a beautiful sine.

If I use array
Code:

duty_cycle[80]={0, 20, 40, 60, 81, 99, 118, 136, 153, 169, 184, 198, 211, 222, 231, 240, 247, 253, 257, 259, 260, 259, 257, 253, 247, 240, 231, 222, 211, 198, 184, 169, 153, 136, 118, 99, 81, 60, 40, 20, 0, 21, 42, 63, 84, 103, 123, 141, 159, 175, 191, 205, 219, 234, 240, 250, 256, 262, 267, 269, 270, 269, 267, 262, 256, 250, 240, 234, 219, 205, 191, 175, 159, 141, 123, 103, 84, 63, 42, 21}

but it goes from 0 to 40 only, I obtain a beautiful sine.

If i use array
Code:

duty_cycle[120]={0, 20, 40, 60, 81, 99, 118, 136, 153, 169, 184, 198, 211, 222, 231, 240, 247, 253, 257, 259, 260, 259, 257, 253, 247, 240, 231, 222, 211, 198, 184, 169, 153, 136, 118, 99, 81, 60, 40, 20, 0, 21, 42, 63, 84, 103, 123, 141, 159, 175, 191, 205, 219, 234, 240, 250, 256, 262, 267, 269, 270, 269, 267, 262, 256, 250, 240, 234, 219, 205, 191, 175, 159, 141, 123, 103, 84, 63, 42, 21, 0, 22, 44, 65, 87, 109, 127, 146, 164, 182, 198, 213, 227, 239, 249, 259, 266, 272, 276, 279, 280, 279, 276, 272, 266, 259, 249, 239, 227, 213, 198, 182, 164, 146, 127, 109, 87, 65, 44, 22}

but it goes from 0 to 40 only, I obtain a beautiful sine.

If I use array
Code:

duty_cycle[160]={0, 20, 40, 60, 81, 99, 118, 136, 153, 169, 184, 198, 211, 222, 231, 240, 247, 253, 257, 259, 260, 259, 257, 253, 247, 240, 231, 222, 211, 198, 184, 169, 153, 136, 118, 99, 81, 60, 40, 20, 0, 21, 42, 63, 84, 103, 123, 141, 159, 175, 191, 205, 219, 234, 240, 250, 256, 262, 267, 269, 270, 269, 267, 262, 256, 250, 240, 234, 219, 205, 191, 175, 159, 141, 123, 103, 84, 63, 42, 21, 0, 22, 44, 65, 87, 109, 127, 146, 164, 182, 198, 213, 227, 239, 249, 259, 266, 272, 276, 279, 280, 279, 276, 272, 266, 259, 249, 239, 227, 213, 198, 182, 164, 146, 127, 109, 87, 65, 44, 22, 0, 22, 46, 67, 90, 111, 132, 151, 170, 188, 205, 220, 235, 247, 258, 268, 275, 282, 286, 289, 290, 289, 286, 282, 275, 268, 258, 247, 235, 220, 205, 188, 170, 151, 132, 111, 90, 67, 46, 22}

but it goes from 0 to 40 only, problems start, and the jumping starts to appear.

WHY?

I need to know why as the array gets bigger the jumping takes place and solve it, before i start working on feedback.
temtronic



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

View user's profile Send private message

PostPosted: Mon Dec 17, 2012 6:34 pm     Reply with quote

T's right again...zero crossing point...which happens to be at location 40.

So I have to ask, if you're getting a great sine wave with 40 points, what is the purpose of having a 4 cycle array, instead of looping 4 times in the one cycle array that works fine. I do see slight change in the numbers though...
If you're trying to send 4 different sine waves (a,b,c,d), just have 4 arrays(a,b,c,d) and 'loop' through them. Easier to edit, easier to see the values,etc.
BUT... you'll still have to deal with the zero cross issue no matter how you cut the code.

Perhaps re-explaining the purpose of the 4 cycle array values...


hth
jay
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 1:20 am     Reply with quote

Hey temtronic

yeah T's is totally right in his point.

As for the other three '40-points', well they are to be used later in feedback. My method of feedback is to read the output (220v), and then choose one full 40-points to be the duty cycle values. That is to say, suppose i have a 210v at the first 40-point. Then, at the zero-cross (T's point), i will change the duty cycle values to take the next 40-points in the array. If still less than 220v out, then at the next zero-cross, i change the duty cycle to the next 40-point, and so on. Now i have not implemented feedback yet. I'm just trying to check how my sine will look like by examining every forty point of the array ( which is going to be a 600-point eventually). The problem is that as the size of the array gets bigger, the sine output starts to deform. A 40 point array is fine. An 80-point array is fine. A 120 -point array is fine. At 160-point, deformation starts and pwm at peak values act weird (see the image attached n the links above).
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 1:53 am     Reply with quote

As I have already said, the problem is that when the array gets larger, the processor has to start handling page switching. This is dependant on the total size of the array, _not_ how many elements are actually used at any one time. So 80byte array, happily sits in one bank. If the compiler is careful, it can page align an array so it doesn't have to page switch up to 128 elements (256 bytes). Beyond this page switching has to occur.
It takes an extra 16 machine cycles to access an array over 128 elements in size....
The same applies if you switched to using a two dimensional array.

Best Wishes
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 5:22 am     Reply with quote

As you've got it you're inverting your waveform when j=39. At that point the duty cycle is finite. There's a possibility the PWM waveform will be high and you'll put a dead short across the power rails as you toggle the port_A pins. You really need to be inverting at the zero-crossing. One solution; re-order your array slightly, so the duty cycle zeroes are later array elements of each group of 40 rather than the first. You have to be really careful here as the PWM system is double buffered. The effect is that the PWM is executing the duty set on a previous timer2 overflow, not the one which has just happened.

Why not prepare the next duty value you need in main? Then there's no need for the ISR to access the huge array.

Mike
temtronic



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

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 6:14 am     Reply with quote

Since you only need a 40 point array to implement the sinewave, I'd try my 'have several 40 point array' idea.
Start with array 'A', test, if ok...stay with it..
if not ok, select array 'b',test, if ok...stay with it..
...'c'....
...'d'...
...'e'....
etc.

As pointed out there's a huge problem with larger arrays in PICs that must perform 'bank switching' as T's said.
By keeping the arrays small, the compiler should be able to stuff complete arrays into the memory more efficiently.

There's another problem with one large array.Your code your algorithm will take a 'lot' of time,just after the zero cross..is it 220V yet(select adc,read,decide)..code, thus 'shifting' when things get done.

Another benefit of the smaller arrays is 'readability'.Having 40 elements or points,allows each array's data to be visually 'aligned' on the screen. This will quickly show up any 'funny' numbers(typos like 68 when 86 was required).

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 9:10 am     Reply with quote

Mike Walne wrote:
As you've got it you're inverting your waveform when j=39. At that point the duty cycle is finite. There's a possibility the PWM waveform will be high and you'll put a dead short across the power rails as you toggle the port_A pins. You really need to be inverting at the zero-crossing. One solution; re-order your array slightly, so the duty cycle zeroes are later array elements of each group of 40 rather than the first. You have to be really careful here as the PWM system is double buffered. The effect is that the PWM is executing the duty set on a previous timer2 overflow, not the one which has just happened.

Why not prepare the next duty value you need in main? Then there's no need for the ISR to access the huge array.

Mike


Unfortunately, on the PIC he has, there is not enough RAM for two copies of the data array. If there was, it'd be a 'doddle' to have two RAM arrays, one in use, and just write the new array, into the out of use copy, then switch which array was in use. Also worth saying that accessing a 40 element RAM array in the interrupt is 16 machine cycles quicker than accessing even the 40element ROM array.
It is also important, that he must switch arrays at the zero crossing point, not at the full cycle point, otherwise when he changes arrays there will be a spike as the amplitude changes, just as with the current 'problem' code....
This applies with Temtronics multi-array solution, as well, but (unfortunately), with more than perhaps three or four arrays, if statements will get to slow, and even the time of a jump table based solution (switch - case, with no default), will probably bring back the timing problem....

Realistically, a slightly faster PIC, or one supporting more efficient 16bit operations, or with enough RAM to have the tables in RAM, would all fix the problem. Or just running the interrupt a little slower, with slightly less points in the lookup.

Given how much quicker binary numbers are to work with, I'd say look to using 32 elements in the array.

Best Wishes
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 10:38 am     Reply with quote

Ttelmah wrote:
Mike Walne wrote:
As you've got it you're inverting your waveform when j=39. At that point the duty cycle is finite. There's a possibility the PWM waveform will be high and you'll put a dead short across the power rails as you toggle the port_A pins. You really need to be inverting at the zero-crossing. One solution; re-order your array slightly, so the duty cycle zeroes are later array elements of each group of 40 rather than the first. You have to be really careful here as the PWM system is double buffered. The effect is that the PWM is executing the duty set on a previous timer2 overflow, not the one which has just happened.

Why not prepare the next duty value you need in main? Then there's no need for the ISR to access the huge array.

Mike


Unfortunately, on the PIC he has, there is not enough RAM for two copies of the data array.


Sorry, I did not make myself clear.

I was thinking to prepare only the next duty value, so it was ready to load into the PWM module, either in the ISR, or in main when the ISR sets a flag that it's needed.

Mike
GOBER



Joined: 22 Jul 2010
Posts: 41

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 2:19 pm     Reply with quote

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???
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Dec 18, 2012 2:45 pm     Reply with quote

Can't comment.

1) You've changed the code significantly, but don't show us complete compilable code we can test.
2) How are we supposed to know what we are looking at?
3) Looks like you still believe in Proteus.

If you think that the problem is with the PWM values, you could load the PWM values into RAM array (on REAL HARDWARE) then dump to a PC via RS232.

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 3 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