View previous topic :: View next topic |
Author |
Message |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
1khz PWM |
Posted: Wed Oct 24, 2012 11:52 am |
|
|
How can I get to 1Khz PWM with this chip? the most I can get is 50hz and I had try many combinations, do I need an external crystal?
Code: |
#include "16F88.h"
#fuses NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
main()
{
int8 value_adc;
setup_adc_ports(sAN1);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(1);
while(1){
value_adc = read_adc();
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 150, 1); // 50 Hz
set_pwm1_duty(value_adc); // var duty cycle
}
} |
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
 |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Wed Oct 24, 2012 12:23 pm |
|
|
Code: |
Crystal Frequency
PWM Freq = -----------------------------------------
(PR2 + 1) * (Timer2 Prescaler) * 4
4000000
PWM Freq = -----------------------
(150 + 1) * (1) * 4
PWM Freq = 6622.51Hz |
but according to that I should get 6.6Khz, and I'm getting 50hz |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 24, 2012 12:58 pm |
|
|
Quote: | do I need an external crystal?
|
You don't have an oscillator fuse, so the compiler will default to using
the RC fuse, which requires an external resistor and capacitor.
Do you have those components installed on the OSC1 pin ? If so,
what are their values ?
How are you testing this project ? Is this a simulator project ?
Just the fact that you don't have an oscillator fuse makes me
think there is no hardware board.
Also, you don't need to call setup_ccp1() and setup_timer_2() inside
the while() loop. You only need to call them one time, above the loop.
See this example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=40007&start=1 |
|
 |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Wed Oct 24, 2012 1:38 pm |
|
|
I have the hardware set but I dont have anything on OSC1 or OSC2 pin, would I be able to get 6.6Khz without external crystal?
here is a picture of the output
 |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 24, 2012 1:51 pm |
|
|
Add the INTRC_IO fuse to your #fuses statement. Then the PIC will
run at 4 MHz from the internal oscillator and you will get better results
from your tests. |
|
 |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Wed Oct 24, 2012 1:55 pm |
|
|
PCM programmer wrote: | Add the INTRC_IO fuse to your #fuses statement. Then the PIC will
run at 4 MHz from the internal oscillator and you will get better results
from your tests. |
Thank you!!! it works!!  |
|
 |
|