PDA

View Full Version : A question on defining the external oscillator



ozarkshermit
- 21st November 2009, 22:37
Hi all:

I have a question regarding how I define a HS oscillator. The first three lines of code are as follows

@ DEVICE PIC16F870, HS_OSC
INCLUDE "alldigital.INC"

DEFINE OSC 20


Now - - The program works exactly as I wish, unless I add the DEFINE OSC 20 line -then it seems to revert back to 4 MHZ....
If I omit the DEFINE statement, it works perfectly at the higher speed.

I am using a 20MHZ Resonator, since accuracy is not important in this application.

I guess I don't care, since it works as I hope without the DEFINE, I just am confused why the DEFINE OSC 20 statement would cause it to run slower.

Ken

Melanie
- 22nd November 2009, 09:06
All the DEFINE HS_OSC does is instruct the PIC to drive the Xtal or Resonator harder. As the frequency goes above 4MHz the xtals/resonators require increased drive. This doesn't mean that you HAVE to use XT_OSC at 4MHz and below. If you have multiple PICs connected to one Xtal for example, you may find that the additional loading requires the Master PIC to drive the Xtal at HS_OSC even if it is 4MHz.

Now the question is how fast is your PIC actually running?

The DEFINE OSC 20 tells PICBASIC that you've got a 20MHz circuit and to slow down it's internal timing accordingly - so TIME critical commands like PAUSE, SERIN, SEROUT etc work properly.

Do an experiment, Blink an LED at 1Hz with a simple program...

LED var PortB.1
Loop:
High LED
Pause 500
Low LED
Pause 500
Goto Loop

Compare it against your wristwatch... is it 1Hz WITH or WITHOUT the Define OSC 20 statement?

ozarkshermit
- 22nd November 2009, 13:53
Thanks Melanie

Yes, what you suggested is what I did initially to find that adding DEFINE OSC 20 changed the timing on a blinking LED (using some PAUSE statements) back to what it was at 4 MHZ.

I guess I should have looked at page 172 of the manual a little closer. :o

From that I gather then, only the "timing - related" commands will operate at basically a 4MHX clock rate, - and everything will be faster by a factor of 5 ..

I need the extra speed to get through a lot of IF-THEN statements faster.

Thanks

Ken

Melanie
- 22nd November 2009, 14:31
Yes you are correct. Only Timing Critical commands will be brought back into line (eg PAUSE, PAUSEUS etc) in order to operate correctly (not nescessarilly to operate at 4Mhz but to operate CORRECTLY at your processor speed). All others will be quicker x500%.