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 support@ccsinfo.com

Strange PWM behavior

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



Joined: 11 Feb 2007
Posts: 19
Location: Cedar Rapids

View user's profile Send private message

Strange PWM behavior
PostPosted: Wed Mar 07, 2007 7:45 pm     Reply with quote

I was finally able to get my PWM to operate at 100KHz, but now I'm observing some strange behavior. Currently, I am using a potentiometer to control the input to the PIC 16F877's ADC port. I scale the ADC signal by dividing by 1024 and multiplying by what SHOULD be a value of 833.

I multiply by 833, because of information I got from the CCS compiler manual. Since I am operating on a 10MHz external oscillator, the period of my clock is 100uS. The maximum duty cycle I want for a 5V input from the ADC is 83.3%. Thus, the duty cycle I give to the PWM is equal to (period desired)/(1/clock) = (83.3uS)/(1/10MHz) = 833.

However, when I am observing the PWM output on the oscilloscope, the duty cycle starts over again at 0% when the output goes above about 63%. Does anyone know why it is doing this? I never had this problem before.

Code:
#include <16F877.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)

void main() {
  INT16 value;

  setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

  setup_timer_2(T2_DIV_BY_1, 24, 1);  //output at 100KHz
                              //Period = (1/clock)*4*t2div*(period+1)

  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );


   while( TRUE ) {
    value=(read_adc()*833)/1024;

    set_pwm1_duty(value);          // This sets the time the pulse is
                                   // high each cycle.  We use the A/D
                                   // input to make a easy demo.
                                   // the high time will be:
                                   //  if value is LONG INT:
                                   //    value*(1/clock)*t2div
                                   //  if value is INT:
                                   //    value*4*(1/clock)*t2div
                                   // for example a value of 30 and t2div
                                   // of 1 the high time is 12us
                                   // WARNING:  A value too high or low will
                                   //           prevent the output from
                                   //           changing.

   }

}
 
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 07, 2007 9:29 pm     Reply with quote

Quote:
value=(read_adc()*833)/1024;


The problem is in the math. You need 32-bit math in the numerator
expression. CCS doesn't automatically do this. To force the compiler
to do it, you need to cast at least one of the operands in the numerator
to 32-bit. Remember this always, with CCS.
sonicfire



Joined: 11 Feb 2007
Posts: 19
Location: Cedar Rapids

View user's profile Send private message

PostPosted: Thu Mar 08, 2007 4:52 pm     Reply with quote

The PWM output is perfect now. Thanks for the tip.
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