|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Setting up Internal Oscillator |
Posted: Fri May 07, 2004 5:02 am |
|
|
Hi
How do I set up the internal oscillator to the desired frequency?
I am using PIC 16F819. As per the data sheet internal osc freq are
31K,250K,500K,1M & 2M,4M,8M
But when I use the Command #use delay(clock= ) , I found that , clock is not changing for the different values that I had used
Is it that When Internal Clock is selected , PIC will be at only one freq?
Any relevant info would be of great help
Thanks & Best regards,
Raghu PV |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Fri May 07, 2004 6:58 am |
|
|
From the device datasheet:
Quote: |
The clock source frequency (INTOSC direct, INTRC direct or INTOSC postscaler) is selected by configuring the IRCF bits of the OSCCON register (Register 4-2).
|
And remember you also have to use the correct fuse (in #fuses). |
|
|
Guest
|
|
Posted: Fri May 07, 2004 7:33 am |
|
|
Hi Ali
Thanks for the info on the fuses
I will try it out
Btw , say to set up 1MHz, is it correct to use the command
#use delay(clock=1000000) ?
If not , what is the function to be used for setting up the above internal clock
Thanks again & Best regards,
Raghu |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Fri May 07, 2004 7:53 am |
|
|
You have to use the INTIO2 fuse, and for a 1MHz operation you need to write the value of 0b01000000 (0x40) to the OSCCON register. I'm not sure if CCS has any special function for this purpose but the following code will do the job:
#byte OSCCON=0x8F
OSCCON=0x40;
I recommend you to take a look at sections 4.5.3 and 4.5.4 of the datasheet.
By the way, the '#use delay' preprocessor does NOT perform any frequency setup on the PIC, it just tells the compiler how fast your crystal is, for delay (and some peripheral timings) generation purposes. You still have to indicate a #use delay(clock=1000000), but it is not enough. The correct fuse and OSCCON values need to be chosen too. |
|
|
SteveS
Joined: 27 Oct 2003 Posts: 126
|
|
Posted: Fri May 07, 2004 7:54 am |
|
|
--oops haplo posted just before me - sorry for some repeat....
The 'delay' command is used to tell the compiler what clock you intend to use so it can generate correct code to set bauds, run delays, etc. It doesn't directly affect the oscillator settings.
Here's what I used in the 18F1320 to get an internal 8Mhz clock:
#include <18F1320.h>
#use delay(clock=8000000) // tell compiler what we intend to use
#fuses INTRC, FCMEN, BROWNOUT, BORV20, STVREN, NOLVP, NOIESO, MCLR, NOWDT
#BYTE OSCCON = 0xFd3
// NOTE: I'm sure the register adr is different for you
and in main:
OSCCON = 0x72; // boost up to 8Mhz
// The bit pattern may be different too...
Newer versions of the compiler have the SETUP_OSCILLATOR function (see the 'readme' file) that takes care of this more transparently.
- SteveS |
|
|
Guest
|
|
Posted: Fri May 07, 2004 8:05 am |
|
|
Thank you very much for all the reply
I will try it & post the findings tomorrow
Thanks a lot again
Thanks & Best regards,
Raghu |
|
|
Guest
|
|
Posted: Sat May 08, 2004 2:43 am |
|
|
Yes my internal clock is working fine now after setting the fuses & OSCCON
Ali & Steve, Thanks a lot for your help
Thanks & best regards,
Raghu |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Wed Dec 12, 2007 9:30 am |
|
|
is there any command to use so we can display the real clock speed out of oscon to make sure its correct? |
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 12, 2007 9:47 am |
|
|
OSCON, does not hold a 'real clock speed', it holds a 'trim' factor, applied to adjust the RC osillator.
The best ways of finding out if your timing is right, are simple, and depend on the chip involved. Simplest, just setup a program to output a level change, and wait for (say) 100 seconds, then change the level again, and time this with a stopwatch. Alternatively, if the chip has a CCP, and you have a frequency meter, output a frequency with the PWM, and measure this. The value that is written into the OSCON register, is available in the top memory location of the chip, and can be read by your programmer. Some programmers (Mach-X for example), have the ability to test the oscillator frequency, and reprogram this value, but the accuracy is limited since the frequency actually developed changes with the supply (different when the chip is in circuit), and with temperature...
Because of this, no factor will every be 'right', they may just be slightly 'better' for you.
Best Wishes |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Thu Apr 24, 2008 4:19 pm |
|
|
Is there a way to change it on the fly
for example:
Code: |
Switch(Osc_Speed_Selection)
case 1:
setup_oscillator( OSC_1MHZ ); //this command changes the speed of the oscillator
#use delay(clock=1000000,int) //this command informs the compiler of the oscillator speed change so it sets delays, bauds, etc. accordingly
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, enable=PIN_C1, ERRORS, stream = Network)
ShowSFrequency=getenv("CLOCK");
case 2:
setup_oscillator( OSC_2MHZ ); //this command changes the speed of the oscillator
#use delay(clock=2000000,int) //this command informs the compiler of the oscillator speed change so it sets delays, bauds, etc. accordingly
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, enable=PIN_C1, ERRORS, stream = Network)
ShowSFrequency=getenv("CLOCK");
|
I've exhausted all the ways I can think of, calling them from functions, if statements...
I've read the datasheet about osccon,osctune, ircf2.0 (which is controlled by setup_oscillator())
Is it even possible to switch oscillation speeds? The last #use_delay and setup_oscillator overrides any previous ones, even after I've put them in switch statements, if statements, functions, calling the function again to keep 1MHZ won't work. It will always go 2MHZ since it is the last one. |
|
|
Ttelmah Guest
|
|
Posted: Fri Apr 25, 2008 9:49 am |
|
|
Do a search on "Load Frequency Oscillation from EEPROM". PCM Programmer posts an example of how to do this. See in particular the comment about how #use delay works.
Best Wishes |
|
|
|
|
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
|