View previous topic :: View next topic |
Author |
Message |
janiga
Joined: 10 Apr 2006 Posts: 22
|
PWM Problem |
Posted: Fri Apr 21, 2006 9:35 am |
|
|
Hello again,
i am trying to utilize PWM on a pic16f914. I have read a few threads and it seems like the output frequency is no where near the calculated 1.5khz that i need is on pin C2 pwm output. Pin C2 is configured for me by the compiler, am i supposed to use ccp1 which is Pin C5. I have tried diiferent settings for timer2 and does not go below what seems to be a few mhz.
I can change the duty cycle but freq. remains high.
Thanks!
Code: |
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_lcd(LCD_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,16);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_4MHZ);
while (true)
{
output_low(PIN_C2);
set_pwm1_duty(500);
output_high(PIN_C2);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 21, 2006 12:43 pm |
|
|
Quote: |
I am trying to utilize PWM on a pic16f914. I have read a few threads and
it seems like the output frequency is no where near the calculated 1.5khz |
How did you get the 1.5 KHz ?
If you go to this thread, it has a formula to calculate the PWM frequency:
http://www.ccsinfo.com/forum/viewtopic.php?t=17993
If I take the values from your program and plug them into that formula,
I get this:
Code: | 4 MHz
------------------- = 244 Hz
(255 +1) x 16 x 4
|
I converted your program to run on my test board, which has a 16F877.
It's a similar chip, so the PWM module runs the same.
I measured a frequency on Pin C2 of 244.15 Hz with my oscilloscope.
That fits the equation. |
|
|
janiga
Joined: 10 Apr 2006 Posts: 22
|
Thanks Again! |
Posted: Fri Apr 21, 2006 6:00 pm |
|
|
I had to switch to pin C5 which on my processor is the ccp1 pin. It works great and am implementing PWM and getting rid of all the while loops that simulated PWM!
Again Thank You!
Pedro |
|
|
|