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 with 38Khz with 50% duty cycle pulse

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



Joined: 09 Sep 2003
Posts: 52

View user's profile Send private message MSN Messenger

PWM with 38Khz with 50% duty cycle pulse
PostPosted: Tue Jan 11, 2005 12:41 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 11, 2005 1:14 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 11, 2005 1:30 pm     Reply with quote

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
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