View previous topic :: View next topic |
Author |
Message |
cxiong
Joined: 09 Sep 2003 Posts: 52
|
PWM with 38Khz with 50% duty cycle pulse |
Posted: Tue Jan 11, 2005 12:41 pm |
|
|
I use a PIC18F452 with a 4Mhz crystal. I want to generate a 38Khz-40Khz PWM with 50% duty cycle.
Anyone has a sample code that I can use?
Thanks.. |
|
|
cxiong
Joined: 09 Sep 2003 Posts: 52
|
|
Posted: Tue Jan 11, 2005 1:14 pm |
|
|
Can someone look at it?
Here is my code
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
long duty;
duty=6;
while(1)
{
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 127, 1);
set_pwm1_duty(duty);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 11, 2005 1:30 pm |
|
|
Go to the link below and solve the equation for PR2. Then plug
in the values that you already know, such as the crystal frequency
and the desired PWM frequency. Start by using a Timer 2
pre-scaler of 1. Then see what value you get for PR2.
If it's within the acceptable range (0-255), then use it
for the middle value in setup_timer2(). Use half the
value of PR2, to set a duty cycle of 50%, in the set_pwm1_duty()
function.
http://www.ccsinfo.com/forum/viewtopic.php?t=17993 |
|
|
|