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

PWM Frequency

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



Joined: 03 Jun 2005
Posts: 8

View user's profile Send private message

PWM Frequency
PostPosted: Thu Aug 04, 2005 7:23 am     Reply with quote

Please can someone help me with this code? From my calculations for period and duty cycle, I expect results to be frequency of 15khz and 50% duty cycle.

Code:

#include <18F6722.h>

#use delay(clock=8000000)
#fuses INTRC_IO,NOLVP,NOWDT,PUT

void main(void)
{

setup_ccp2(CCP_PWM);   
setup_timer_2(T2_DIV_BY_1, 132, 1);
set_pwm2_duty(267);   
while(true);
}
Ttelmah
Guest







PostPosted: Thu Aug 04, 2005 9:42 am     Reply with quote

Yes.
So what are you seeing?...
I'd suggest adding the 'setup_oscillator(OSC_8MHZ)' line to the code. Some versions of the compiler correctly handle doing this automatically, but others don't, and if this was wrong, the output frequency would also be wrong.
Frequency shoul be fractionally over 15Khz, and the duty cycle, just a little over 50%. The 50% value would be 266 ((132+1)*2). I'd also be 'explicit' here, and make sure this value is a 'Long' (add an 'L' to the definition), since while it shoul work fine with 267, if the value was under 255, the compiler will switch to multiplying the value by 4, as it'll then be seen as an 8bit vaue, rather than a 16bit value.

Best Wishes
aderogba



Joined: 03 Jun 2005
Posts: 8

View user's profile Send private message

PostPosted: Mon Aug 08, 2005 4:15 pm     Reply with quote

Thanks! Your suggestions work.
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