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

1khz PWM

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



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

1khz PWM
PostPosted: Wed Oct 24, 2012 11:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2012 12:10 pm     Reply with quote

See this thread which has the PWM frequency formula:
http://www.ccsinfo.com/forum/viewtopic.php?t=17993
The two values (other than crystal frequency) which control PWM
frequency are the PR2 value and the pre-scaler.
IceMetal



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Oct 24, 2012 12:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2012 12:58 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Oct 24, 2012 1:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 24, 2012 1:51 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Oct 24, 2012 1:55 pm     Reply with quote

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!! Very Happy
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