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

PWM frequency incorrect

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



Joined: 05 Sep 2009
Posts: 4

View user's profile Send private message

PWM frequency incorrect
PostPosted: Sat Sep 05, 2009 11:53 pm     Reply with quote

I read the following thread and used the formula shown;
http://www.ccsinfo.com/forum/viewtopic.php?t=17993

But still my coding doesn't work. I'm using a 20mhz crystal and I want to obtain a 5khz pwm output from a 16f877a. For some reasons, I get a frequency of 250hz (T=4ms) (simulated using software).

I also tried to obtain other frequency (using calculation first) and all were incorrect... any help is very welcome.


Here it goes:

Code:
#include "16F877A.h"
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000) //20MHZ crystal

main()
{
//output_low(PIN_C1);   // Set CCP2 output low
 

setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_4, 249, 1);  // 5Khz frequency according to formula..
set_pwm2_duty(124);                    // 50% duty cycle on pin C1

while(1);  // Prevent PIC from going to sleep  (Important !)
}

//Gives 250hz frequency...
Ttelmah
Guest







PostPosted: Sun Sep 06, 2009 2:14 am     Reply with quote

What software?.....
Your frequency looks right.
I'd suggest your software thinks you are running on a 1MHz clock. Remember the simulator itself also needs to be told the processor frequency.

Best Wishes
cell514



Joined: 05 Sep 2009
Posts: 4

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 8:32 pm     Reply with quote

You were right thanks, it was a software problem; frequency is ok now (I were using ISIS 7 software).

However, now the duty cycle is incorrect. Pwm high value is (and stays at) 50us for T=200us. Any ideas?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 11:12 pm     Reply with quote

How about consulting the CCS C manual regarding set_pwm2_duty()? It knows about 10 bit pwm resolution...
cell514



Joined: 05 Sep 2009
Posts: 4

View user's profile Send private message

PostPosted: Tue Sep 08, 2009 8:17 pm     Reply with quote

FvM wrote:
How about consulting the CCS C manual regarding set_pwm2_duty()? It knows about 10 bit pwm resolution...


hmm.. I tried already and it doesn't work. Also tried the L (typecast) and didn't work either...

Also there's more:

Which is the right code when using CCP1 (pin C2) on a 16f877a:

Code:
set_pwm1_duty()


or

Code:
set_pwm2_duty()


Because I get a signal for the second one only and all examples uses the first one... This is confusing.

Also, I'm wondering if there are (more) limitation regarding PWM when clocked at 20mhz?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 08, 2009 8:21 pm     Reply with quote

1. Post your compiler version. It's a 4-digit number in this format x.xxx
and it's given at the top of the .LST file, which is in your project directory.

2. Are you running real hardware or a simulator such as Proteus or Oshonsoft ?
Ttelmah
Guest







PostPosted: Wed Sep 09, 2009 2:36 am     Reply with quote

Perhaps note that you are setting up ccp1, then setting the pulse width on ccp2....

Best Wishes
cell514



Joined: 05 Sep 2009
Posts: 4

View user's profile Send private message

PostPosted: Wed Sep 09, 2009 8:12 pm     Reply with quote

Compiler version: 4.088

I'm running a simulator; Proteus



Quote:
Perhaps note that you are setting up ccp1, then setting the pulse width on ccp2....

Best Wishes


I know... okay, tomorrow I'm programming a real Pic and I'll check the signal with an oscilloscope and post back the results. Rolling Eyes
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Thu Sep 10, 2009 12:30 am     Reply with quote

Are you using PWM1 or PWM2. Looks like you are combining both in your code.

Try:
Code:
setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_4, 249, 1);
set_pwm1_duty(500L);   // 5kHz / 50% duty cycle

or:
Code:
setup_ccp2(CCP_PWM);   // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_4, 249, 1);
set_pwm2_duty(500L);   // 5kHz / 50% duty cycle
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