View previous topic :: View next topic |
Author |
Message |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
16F877A 40khz pwm |
Posted: Mon May 23, 2011 10:57 am |
|
|
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
|
|
Posted: Mon May 23, 2011 11:47 am |
|
|
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
|
|
Posted: Mon May 23, 2011 11:56 am |
|
|
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
|
|
Posted: Mon May 23, 2011 12:00 pm |
|
|
Don't trust the Wizard. |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Mon May 23, 2011 12:14 pm |
|
|
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
|
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Mon May 23, 2011 6:50 pm |
|
|
thank you |
|
|
|