View previous topic :: View next topic |
Author |
Message |
cell514
Joined: 05 Sep 2009 Posts: 4
|
PWM frequency incorrect |
Posted: Sat Sep 05, 2009 11:53 pm |
|
|
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
|
|
Posted: Sun Sep 06, 2009 2:14 am |
|
|
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
|
|
Posted: Mon Sep 07, 2009 8:32 pm |
|
|
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
|
|
Posted: Mon Sep 07, 2009 11:12 pm |
|
|
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
|
|
Posted: Tue Sep 08, 2009 8:17 pm |
|
|
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:
or
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
|
|
Posted: Tue Sep 08, 2009 8:21 pm |
|
|
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
|
|
Posted: Wed Sep 09, 2009 2:36 am |
|
|
Perhaps note that you are setting up ccp1, then setting the pulse width on ccp2....
Best Wishes |
|
|
cell514
Joined: 05 Sep 2009 Posts: 4
|
|
Posted: Wed Sep 09, 2009 8:12 pm |
|
|
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. |
|
|
languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
|
Posted: Thu Sep 10, 2009 12:30 am |
|
|
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 |
|
|
|
|