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

16F877A 40khz pwm

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



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

16F877A 40khz pwm
PostPosted: Mon May 23, 2011 10:57 am     Reply with quote

Hi
I'm trying to generate 40kHz pwm signal with 50% duty.
Following is the code for 16F877A. It doesn't work, no pwm is generated.
However I can generate 1.227kHz signal. Even 10kHz is not possible. Can someone point me the problem?

My requirement is fixed 40kHz 50% duty pwm signal for ultrasonic transducer. So pwm resolution doesn't matter.

My compiler version is v4.120

Code:

#include <16F877A.h>
#device adc=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)

void main()
{

   delay_ms(1000);
     
   setup_timer_2(T2_DIV_BY_1,124,1);      //25.0 us overflow, 25.0 us interrupt
   setup_ccp1(ccp_pwm);
   set_pwm1_duty(248);
   setup_comparator(NC_NC_NC_NC);
   
   while(1)
   {
   delay_ms(500);
   output_toggle(PIN_D0);
   }


}


Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 23, 2011 11:47 am     Reply with quote

If you use a duty cycle value greater than the PR2 value (which is 124 in
your program), then you will get 100% duty cycle. That's in 8-bit PWM
mode. To force the compiler to use 10-bit PWM mode, you need to tell
the compiler that you're using a 'long' value (16-bits) for the duty cycle.
One way to do this is to add an 'L' at the end of the constant value.
Example:
Quote:

set_pwm1_duty(248L);

Now it should work. You should see PWM output with your scope.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon May 23, 2011 11:56 am     Reply with quote

PCM programmer wrote:
If you use a duty cycle value greater than the PR2 value (which is 124 in
your program), then you will get 100% duty cycle. That's in 8-bit PWM
mode. To force the compiler to use 10-bit PWM mode, you need to tell
the compiler that you're using a 'long' value (16-bits) for the duty cycle.
One way to do this is to add an 'L' at the end of the constant value.
Example:
Quote:

set_pwm1_duty(248L);

Now it should work. You should see PWM output with your scope.

Hi

I made the correction as you said and now it is working.

but shouldnt wizard do this? is this a bug in compiler? i also noticed that when i set pwm in wizard, it adds
setup_ccp1();

instead of

setup_ccp1(ccp_pwm);

I have to fill "ccp_pwm" part manually.


and also can you please explain what you just did? I just made the correction but dont know what that really does

thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 23, 2011 12:00 pm     Reply with quote

Don't trust the Wizard.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon May 23, 2011 12:14 pm     Reply with quote

PCM programmer wrote:
Don't trust the Wizard.

can you tell me what you did? I dont understand what you said in first post
thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 23, 2011 12:38 pm     Reply with quote

Here's another thread on it. It also has 4 more links at the end for you
to read:
http://www.ccsinfo.com/forum/viewtopic.php?t=17729
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon May 23, 2011 6:50 pm     Reply with quote

PCM programmer wrote:
Here's another thread on it. It also has 4 more links at the end for you
to read:
http://www.ccsinfo.com/forum/viewtopic.php?t=17729

thank you Smile
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