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

Example for PIC18FXX31 PWM
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mike Walne



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

View user's profile Send private message

Waveforms
PostPosted: Sun Mar 25, 2012 7:11 am     Reply with quote

Do you want the waveforms presented to phases 1, 2 & 3 to resemble the traces a, b, & c below?

Code:

         aaa         ccc         bbb         aaa         ccc         bbb     
        a   a       c   c       b   b       a   a       c   c       b   b   
 b     a     a     c     c     b     b     a     a     c     c     b     b   
                                                                                   
  b   a       a   c       c   b       b   a       a   c       c   b         
   b a         a c         c b         
                                                                                   
    x           x           x           x           x           x         
                                                                                   
                                                                                 
   a b         c a         b c         a b         c a         b c       
                                                                                 
  a   b       c   a       b   c       a   b       c   a       b   c         
                                                                                 
                                                                                   
 a     b     c     a     b     c     a     b     c     a     b     c     
                                                                                   
                                                                                 
        b   c       a   b       c   a       b   c       a   b       c   a   
                                                                                 
         b c         a b         c a         b c         a b         c a     
                                                                                 
          x           x           x           x           x           x     
                                                                                 
                                                                                     
         c b         b a         a c         c b         b a         a c
        c   b       b   a       a   c       c   b       b   a       a   c     
                                                                                 
 c     c     b     b     a     a     c     c     b     b     a     a     c   
  c   c       b   b       a   a       c   c       b   b       a   a       
   ccc         bbb         aaa         ccc         bbb         aaa         


Where a, b & c represent the averaged values of the PWM signals.

I still don't understand EXACTLY which waveform you're after.

What you outlined before seems, to me, to be a hybrid of two possible modes.

Like I said before, the microchip data sheet explains ALL the modes. Then either work directly on the registers or find the CCS syntax.

Mike
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 11:21 pm     Reply with quote

I think I know how to setup the pwm channel but I have a few questions.
Here are my setup settings

Code:
setup_power_pwm_pins(PWM_BOTH_ON,PWM_BOTH_ON,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_FREE_RUN, 1, 0, 310, 0, 1,15);


I want some information for the second part. I want to know what some parameters mean.

First: What is the Time Base??? As the help it could be between 0 and 65535.

Second: In my case I`ve setted period to 310. I`m using 25MHz qurtz, so if period is 310 this must define 20kHz output frequency for the pwm pulses. Is that true???

Third: What`s the connection between quartz and the dead time??? How to define the dead time???

Thanks!
Mike Walne



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

View user's profile Send private message

Queries
PostPosted: Wed Apr 04, 2012 3:32 am     Reply with quote

Quote:

I want some information for the second part. I want to know what some parameters mean.

First: What is the Time Base??? As the help it could be between 0 and 65535.

Second: In my case I`ve setted period to 310. I`m using 25MHz qurtz, so if period is 310 this must define 20kHz output frequency for the pwm pulses. Is that true???

Third: What`s the connection between quartz and the dead time??? How to define the dead time???


Hi Stoyanof,

I don't have the latest compiler, or 'xx31 to test. I hope the following is helpful.

The PWM on the xx32 is very different compared to say '452.

Amongst other functions the Time Base is the clock/counter for the PWM period generator. It has a 16 bit register, only 12 are used, so the range is 0 to 4093. CCS correctly quotes 0 to 65535 for a possible full 16 bit version. The PWM time base also includes pre- and post- scale options.

Your 25MHz quartz gives 6.25MIPS. For 20kHz, with 1:1 pre-scale, your pwm period setting is ((6250/20) -1 ) = 311.5 Use either 311 or 312.

Dead time is derived from your 25MHz fosc (i.e. 40ns period). The dead time counter is prescaled by 1:2, 1:4, 1:8, or 1:16 then divided by a pre-settable 6 bit counter. Your dead time range is thus 80ns to just under 41us.

Mike
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu Apr 05, 2012 5:26 am     Reply with quote

OK. I`ve done all of this.
Here is my program for monophase invertor:

Code:
#include <18F4431.h>
#FUSES HS
#use delay(clock=25000000)


int16 arr[21]={0,480,960,1410,1820,2190,2510,2760,2940,3060,3090,3090,3060,2940,2760,2510,2190,1820,1410,960,480};

int16 nextpulse,frequency;
int1 boolpwm;

#INT_PWMTB
void SetPvm()
{
set_power_pwm0_duty(nextpulse);
set_power_pwm2_duty(nextpulse);
boolpwm=0;
}


void PowerPwmPolaritySwap(int1 swap)
{
switch(swap)
{
case 0:{
set_power_pwm_override(1,true,0);
set_power_pwm_override(0,false,1);
set_power_pwm_override(2,true,0);
set_power_pwm_override(3,false,1);

break;
}
case 1:{
set_power_pwm_override(0,true,0);
set_power_pwm_override(1,false,1);
set_power_pwm_override(3,true,0);
set_power_pwm_override(2,false,1);

break;
}
}
}


void TurningLoop()
{
int16 i;
for(i=1;i<21;i++)
{
nextpulse=arr[i];
boolpwm=1;
while(boolpwm==1);
}
}

//Full Period Wave
void FullPeriodWave()
{
PowerPwmPolaritySwap(0);
TurningLoop();
PowerPwmPolaritySwap(1);
TurningLoop();
}

void main(){
delay_ms(3000);

nextpulse=0;
ENABLE_INTERRUPTS(GLOBAL);
ENABLE_INTERRUPTS(INT_PWMTB);
setup_power_pwm_pins(PWM_BOTH_ON,PWM_BOTH_ON,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_FREE_RUN, 1, 0, 3100, 0, 1,15);
set_power_pwm0_duty(nextpulse);
set_power_pwm2_duty(nextpulse);
while(1){
frequency=50;
FullPeriodWave();
}

}



The problem is in this part:
Code:
boolpwm=1;
while(boolpwm==1);


I use this to check if the calculated value is loaded in the pwm. But it causes malfunction to the controller.
How can I do this another way, without disturbing the controller????
Another question: FaultA and FaultB are not connected anywhere. Should I connect them to GRD through resistor???
Mike Walne



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

View user's profile Send private message

PostPosted: Thu Apr 05, 2012 8:24 am     Reply with quote

Quote:

I use this to check if the calculated value is loaded in the pwm. But it causes malfunction to the controller.
How can I do this another way, without disturbing the controller????
Another question: FaultA and FaultB are not connected anywhere. Should I connect them to GRD through resistor???


How are doing your testing?

What is working and what is not?

What are faults A & B?

Mike
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu Apr 05, 2012 9:03 am     Reply with quote

I connected LEDs to port B. So when I add delay in the program I can see which pins are active. Fault A and B(pins 16 abd 17) are inputs for hardware shut down of the controller. If there is a 1 on A or B in 20 increments the pwm output stops.
So the program is not working well. It doesn't output all of the pulses. I'm sure in the finding next value. I think the problem is in the pwm setup, but I don't know where is it.
Mike Walne



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

View user's profile Send private message

PostPosted: Thu Apr 05, 2012 10:55 am     Reply with quote

I work with MPLAB, which is free.

At your stage, I'd be examining registers at selected points in the process.

You can work either in simulation with MPSIM or the real hardware.

Mike
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Fri Apr 06, 2012 8:30 am     Reply with quote

I ran some tests and I found the problem is in PWM adjustments.
My output PWM duty cycle is 4-5 times shorter than it has to be.
I think the problem is in this row:
Code:
setup_power_pwm(PWM_FREE_RUN, 1, 0, 3100, 0, 1,15);


Are you sure I haven't to set time base???
Mike Walne



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

View user's profile Send private message

PostPosted: Sat Apr 07, 2012 2:42 am     Reply with quote

Code:

int16 arr[21]={0,480,960,1410,1820,2190,2510,2760,2940,3060,3090,3090,3060,2940,2760,2510,2190,1820,1410,960,480};

You're feeding the array values above as DUTY to your PWM set up as:-
Code:

setup_power_pwm(PWM_FREE_RUN, 1, 0, 3100, 0, 1,15);


Your range of array values goes from zero to just shy of the 3100 PWM_period.

As I understand it, the PWM_duty has 14 bit resolution, and the PWM_period 12 bit.

The PWM_duty is in multiples of the Fosc_period.
The PWM_period in multiples of Fosc_period / 4.

Does this account for your PWM duty being 4 - 5 times too short?

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19809

View user's profile Send private message

PostPosted: Sat Apr 07, 2012 3:00 am     Reply with quote

stoyanoff wrote:
I ran some tests and I found the problem is in PWM adjustments.
My output PWM duty cycle is 4-5 times shorter than it has to be.
I think the problem is in this row:
Code:
setup_power_pwm(PWM_FREE_RUN, 1, 0, 3100, 0, 1,15);


Are you sure I haven't to set time base???


Always look at the .h file for your processor, and the manual together.

setup_power_pwm, accepts a whole lot of parameters you are not using. The 'modes' value, accepts:

PWM_CLOCK_DIV_4 | PWM_FREE_RUN

for example, to enable the /4 prescaler.

'period' * the prescaler, _is_ the timebase.

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu Apr 19, 2012 9:50 am     Reply with quote

I need information about the option PWM_UP_TRIGGER and PWM_DOWN_TRIGGER. I want to know how to use this. I don`t find any information. I want to switch the pwm in concrete moment during the pulse so I think i should setup such point.
Can some one post me a simple example?
Thanks!
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Fri Apr 20, 2012 11:33 am     Reply with quote

hello stoyanoff,

if you don't mind, may you explain to me about the turning loop and boolpwm in your coding above?

thank you very much.. Smile
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Tue Apr 24, 2012 3:51 am     Reply with quote

The turning loop is a function which loads all the values from the array in to the nextpulse variable one by one. Next comes the boolpwm. This is a boolean variable(true- false, 1-0)With it I`m indicating if the nextpulse value is loaded in the pwm register. So if it`s 0 the turning loop continious with the next value.

I`ve corrected my program so my invertor works fine. Thanks to everyone!

I wast one more thing. This controller(18F4431) has two FAULT inputs(pins 12 and 13). The datasheet says that they are for overcurrent and overvoltage protection. But before I use them I must configure the FLTCON register. Does anyone have any idea how to do this???
Thanks again!
ecentric70



Joined: 05 May 2012
Posts: 3
Location: West Lafayette, IN

View user's profile Send private message

Re: Example for PIC18FXX31 PWM
PostPosted: Sat May 05, 2012 2:38 am     Reply with quote

stoyanoff wrote:
Can someone post an example for the comands which I can use for generating PWM pulses with PIC18FXX31. How can I do change "on fly"??
How can I change the dead time, aling of the pulses.....
Thanks!


http://www.ccsinfo.com/forum/viewtopic.php?p=161811&highlight=#161811

Also the AN900 Application Note from MicroChip might help.
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
Page 2 of 2

 
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