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

pwm_f

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



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

pwm_f
PostPosted: Tue Oct 24, 2006 6:29 am     Reply with quote

Hello people,
I have a problem and I was wondering if you could help me.
I wrote this code for a pic 18f 4450 microcontroller. The problem is that with the order I give, the frequency to be 4Mhz/16 it should be 250Khz, instead it is 2,9 Khz.(I have 250Khz on the microchip’s board but only 2,9 Khz on breadboard) Do you have any idea what the problem might be?
What is more, do you know how I can use 5 pwm while the component has 4 channels?




#include <18F4550.h>
#device ADC=16
#USE delay(CLOCK=40000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP

//#use delay(clock=40000000, crystal)
//long duty_cycle,period;







void main()
{
long adc_result;

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_div_16);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
//setup_ccp3(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 255, 1);
WHILE (1)
{
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;


if (adc_result<255>255)&&(adc_result<511>511)&&(adc_result<767>767)output_high(PIN_B3); else output_low(PIN_B3);

set_pwm1_duty(adc_result);
set_pwm2_duty(adc_result);
//set_pwm3_duty(adc_result);
}
}
sjbaxter



Joined: 26 Jan 2006
Posts: 141
Location: Cheshire, UK

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 24, 2006 6:35 am     Reply with quote

Your clock is not set up correctly !!

Code:
#USE delay(CLOCK=40000000)


this is expecting 40MHz, but you only specify a HS fuse !!!
should thos be 4000000 (4MHz) ????

What is your actual clock/crystal frequency on your PIC ?
_________________
Regards,
Simon.
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

PostPosted: Thu Oct 26, 2006 5:39 am     Reply with quote

sjbaxter wrote:
Your clock is not set up correctly !!

Code:
#USE delay(CLOCK=40000000)


this is expecting 40MHz, but you only specify a HS fuse !!!
should thos be 4000000 (4MHz) ????

What is your actual clock/crystal frequency on your PIC ?


My crystal frequency on pic is 4MHZ.I try to put
#USE delay(CLOCK=4000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP

and

#USE delay(CLOCK=4000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP

None give 250Khz frequency
Ttelmah
Guest







PostPosted: Thu Oct 26, 2006 7:14 am     Reply with quote

The correct setting (assuming this is a crystal), is:

#USE delay(CLOCK=4000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP

The frequency you will see, will be the master clock, divided by four, divided by 16, divided by 256.

The master clock is divided internally by 4, to give the frequency fed to the Timer2 prescaler. This prescaler you have then set to /16. You have then set timer2, to count 0 to 255, then reset.

Best Wishes
Guest








PostPosted: Sat Oct 28, 2006 1:35 pm     Reply with quote

Ttelmah wrote:
The correct setting (assuming this is a crystal), is:

#USE delay(CLOCK=4000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP

The frequency you will see, will be the master clock, divided by four, divided by 16, divided by 256.

The master clock is divided internally by 4, to give the frequency fed to the Timer2 prescaler. This prescaler you have then set to /16. You have then set timer2, to count 0 to 255, then reset.

Best Wishes


Thanks a lot for your advise. It’s been very helpful. Could you tell me something about how to create a 5 channel pwn when a component of 4 channel?
Thanks again.
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