View previous topic :: View next topic |
Author |
Message |
joseph
Joined: 14 Jun 2010 Posts: 16
|
Pwm frequency problem |
Posted: Wed Oct 31, 2012 10:50 am |
|
|
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
|
|
Posted: Wed Oct 31, 2012 11:55 am |
|
|
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
|
|
Posted: Wed Oct 31, 2012 9:37 pm |
|
|
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 |
|
|
|