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 problem

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



Joined: 14 Jun 2010
Posts: 16

View user's profile Send private message

Pwm frequency problem
PostPosted: Wed Oct 31, 2012 10:50 am     Reply with quote

Hi,

I working on pwm system and I have a problem. I try to understand some things about the pwm frequency:
By computing, I should have 3.311 kHz but I see 6.5khz with a scope.
Where is the mistake?
Thanks in advance

Code:

#include <18F2520.h>

#FUSES NOWDT,INTRC_IO
#use delay(internal=4000000)

void init();
//--------------------------------------
void main (void)
{
      init();
      set_pwm1_duty(20);
      
      for(;;){}
}
//--------------------------------------
void init()
{
      setup_timer_2(T2_DIV_BY_1, 150, 2);   
      set_timer2(1);

      setup_ccp1(ccp_pwm);
}
//--------------------------------------
Mike Walne



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

View user's profile Send private message

PostPosted: Wed Oct 31, 2012 11:55 am     Reply with quote

No. I get 6.623kHz.

The timer2 is clocked at oscillator/4 = 1MHz.

You're then telling timer2 to reset after 151 clocks that's 1^6/151 = 6623Hz.

Then you're asking for interrupts at 6623/2 = 3311Hz.

It's all in the CCS manual.

Mike

EDIT Don't forget the tolerance on the internal clock when measuring with 'scope etc!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 31, 2012 9:37 pm     Reply with quote

Quote:
setup_timer_2(T2_DIV_BY_1, 150, 2);

The last parameter of 2 is only used with the #int_timer2 interrupt.
You don't have an interrupt routine, so it has no effect on your program.

Links on PWM:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1
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