View previous topic :: View next topic |
Author |
Message |
olga Guest
|
minimum frequency for pic... |
Posted: Mon Apr 27, 2009 8:21 am |
|
|
Hi
I am trying to generate a pwm in 50hz and my pic16f690 generate only 250hz even that I reduced the clock.
When I reduce the clock from 8mhz the pic doesn't change to frequency of the pwm. Why is that ??? I reduced it to 100khz and according to the formula I should get 50hz but I get just 250hz.
I do:
Code: | #use delay(clock=100000) |
and the frequency stay high.
Thanks. |
|
|
Ttelmah Guest
|
|
Posted: Mon Apr 27, 2009 9:03 am |
|
|
Simple answer.
If you really change the system frequency, the PWM _will_ change.
The #use_delay statement, tells the processor what frequency the chip is being fed with, but will only actually _change_ the frequency, if you are using the internal oscillator, and select a supported frequency. 100kHz, is _not_ a frequency available from the internal oscillator.....
Best Wishes |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Mon Apr 27, 2009 4:32 pm |
|
|
Did you change the crystal or the frequency of the internal oscillator?
Or did you just use #use delay for changing the clock? This is not how the frequency is changed.
Here is setup for the 16F690 to use internal clock of 31kHz and PWM is set to 50Hz.
Code: | #include <16F690.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOCPD //No EE protection
#FUSES PUT //Power Up Timer
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#use delay(clock=31000) // <--- This sets the parameters so the delays are correct
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,154,1); // <--- Set for 50Hz
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_comparator(NC_NC_NC_NC);
setup_oscillator(OSC_31KHZ); // <--- Set internal oscillator to 31kHz
// DO SOME CODING
} |
|
|
|
olga Guest
|
thanks a lot but... |
Posted: Wed Apr 29, 2009 6:32 am |
|
|
thank you!
but i couldnt understand something: to change frequency i use the
#use delay(clock=31000) ? ??
or: setup_oscillator(OSC_31KHZ) ???
why do i need the second line if i set the clock to 31 by "use delay" ?
what mean the second one and can i set the clock by the first line ?
thanks again. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
Re: thanks a lot but... |
Posted: Wed Apr 29, 2009 6:56 am |
|
|
#use delay command is used to determine how will the compiler calculate delay. And if the delay function is never used in the program, this line will not add any unnecessary lines of code to uC.
setup_oscillator() set's the actual uC internal frequency.
So both commands are used in the same program. There is a nice programming guide and a lot of Microchips datasheets about the PIC-s, so use them and learn ;) |
|
|
Guest
|
thanks a lot.. why |
Posted: Thu Apr 30, 2009 7:14 am |
|
|
Why when i change the "use delay" it change the pwm osc frequency ?
the formula of the pwm is not taking into acount the "#use delay" ....
if the OSC is different from the #use delay-time, how the pic calculate the delay time ? and if i dont use this line(#use delay) what will happend ?
thanks.
btw
i have data sheets but dont have any good book on the ccs c.. |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 30, 2009 9:38 am |
|
|
There are no internal calculations involved in any PWM setup code.
The timing is set by the timer, which itself accepts just count numbers, not 'times'.
If you wanted to generate your own macros to perform these, then you could (of course) use the clock value defined by the #use delay statement, but CCS makes no claim to doing this maths for you, so why should you expect it to?....
Best Wishes |
|
|
|