|
|
View previous topic :: View next topic |
Author |
Message |
andys
Joined: 23 Oct 2006 Posts: 175
|
pwm_f |
Posted: Tue Oct 24, 2006 6:29 am |
|
|
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
|
|
Posted: Tue Oct 24, 2006 6:35 am |
|
|
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
|
|
Posted: Thu Oct 26, 2006 5:39 am |
|
|
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
|
|
Posted: Thu Oct 26, 2006 7:14 am |
|
|
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
|
|
Posted: Sat Oct 28, 2006 1:35 pm |
|
|
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. |
|
|
|
|
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
|