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

Hardware PWM problem using PCD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Bill Legge



Joined: 07 Sep 2010
Posts: 24
Location: West Australia

View user's profile Send private message

Hardware PWM problem using PCD
PostPosted: Fri Nov 26, 2010 9:20 pm     Reply with quote

I've written a function to set one of the hardware PWM modules
to a desired frequency and pulse duration.

Uses a 8MHz Xtal so one cycle=0.25uS
And timer2 in 32_bit mode, the code is:
Code:

// MCU is PIC24FJ64GA010 using PCD/24_bit
#include <main.h>

/////////////////////////////////////////////////////////////////////////////
// function to set hardware PWM
// module must be set inside this function
// passed:  fx       frequency in [Hz]
//          t_on     time on as a percentage of period
// uses timer23 in 32_bit mode
// the PWM period will be (1/clock)*2*t2div*(period+1)
// clock=8MHz. Clock period=1/8MHz=0.125uS. Tcy=fosc/2=0.25uS
// period[uS]=0.25*overflow=1/frequency
// overflow=4000000/frequency
void hardware_PWM(int32 fx,int32 t_on)
   {
   // select HPWM module 1 to 5
   const int8 module=2;
   // convert fx to overflow in [Tcy]
   int32 overflow=4000000/fx;
   // convert percentage t_on to [Tcy]
   t_on=overflow*t_on/100;
   
   setup_timer2(TMR_INTERNAL | TMR_32_BIT, overflow);         
   setup_compare(module, COMPARE_PWM | COMPARE_TIMER2);
   set_pwm_duty(module,t_on);     
   }
/////////////////////////////////////////////////////////////////////////////

void main()
{   
   setup_spi( FALSE );
   setup_spi2( FALSE );
   setup_wdt(WDT_OFF);

   // globals
   int32 frequency;  // desired frequency in Hz
   int32 time_on;    // % pulse duration
   
   time_on=10;       // 10% time_on
   
   while(TRUE)
      {
      for(frequency=1000;frequency<=10000;frequency=frequency+200)
         {         
         hardware_PWM(frequency,time_on);         
         delay_ms(2000);
         }
      }
}


The function works OK if I call it once.
But calling it in a 'slow loop' (2 seconds long so that the PWM settles and I get time to read the scope) produces the desired pulses for a few iterations - and then goes haywire???

Any ideas please?

Regards Bill Legge
_________________
Denmark in West Australia
'Where the forest meets the sea'
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 2:13 pm     Reply with quote

Debug it. Find out which input numbers cause the function to fail.
Put printf statements in your function. Display the results of your math.
The math may not work with certain numbers or a certain range.

Try testing it over a much smaller range than 1000 to 10000.

Also, you are doing things that are unconventional for CCS, such as this:
Quote:
int32 overflow=4000000/fx;

It may work. It may not. If you have a problem with some code, it's a
good idea to "back off" on unconventional coding. Split that line up into
two lines, one for the declaration and another for the math.

Try stopping the PWM module completely before you re-program it.

In other words, question everything. Get down into the code and
experiment.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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