PDA

View Full Version : Oscillator Setting (swimming through treacle)



HankMcSpank
- 21st November 2010, 23:32
Having dabbled with PICs now for long enough now that I probably can't get away with calling myself a n00b, I'm having to face my inner demons & declare that I just don't really understand, exactly which config sets the oscillator!

I'd always thought it was a case of



@ __CONFIG _CONFIG1, _FOSC_INTOSC
define osc 4


If I'm truthful, I've never worked out what's doing what - it's just worked (& I'm from the school "If it works, if it works"...my head has limited RAM & as I get older, some of it has CRC errors)

Ok, so now I applied that same OSC setting method to a new PIC variant I'm now dabbling with a PIC16F1823, but something went askew. Basically I needed to set my Hypertem for a completely different speed setting to get it to display properly (a big warning sign that the osicllator is wrong), but get this a pause command of 500ms, works out much longer (didn't actually time it, but probably about 4 seconds)


Ok, to cut a long story short.....this setting seems to have solved it....



Osccon = %01101010


My 500mS pause now is a 500ms pause, my serial term speed setting is as it should be, blah blah.

But what gives here....it seems I need a config fuse settin.g a define osc command (which appears not to work?) and now an osccon register setting too?

The datasheet says that this PIC defaults to 500kHz clock....so I'm figuring the define command simply tells the compiler what speed the oscillator is going to be running at - hence (until I found the right OSCCON settings) commands like pause taking 8 times longer than they should?

cncmachineguy
- 22nd November 2010, 00:10
Hank, you seem to have it all sorted. hope this provides a little more clarity:

The config line tell the pic how to get its clock ie: internal osc, crystal on XX pin etc.

Define osc tells PBP what speed to use for calculating stuff like baud rate and pause

OSSCON tells the pic what speed to run at from all the choices it provides. and yes, it defaults to 500K. I just figured this out yesterday with a 16F1947.

HankMcSpank
- 22nd November 2010, 10:16
Thanks Bert...nice & succint - I like it! (I think I've got away with all this in the past becuase my usual PIC - a 16f690 defaults to 4Mhz)

Got to say, this 16f1823 is a smashing little chip ...it even has a DAC onboard - all for under a quid! (I'm actually waiting on the 16F1824 - which has more flash - but nobody carries them in stock here in the UK....even the 16f1823 I could only get in TSSOP....which is ludicrously small to work with- had to make myself a TSSOP to DIL adapter - not something I want to repeat in a hurry)

MikeFahey
- 26th August 2011, 04:42
#include <P16F1823.inc>

__CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF) ;18uA
__CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_ON)

#define OSCCON__16MHz b'01111000'
#define OSCCON___8MHz b'01110000'
#define OSCCON___4MHz b'01101000'
#define OSCCON___2MHz b'01100000'
#define OSCCON___1MHz b'01011000'
#define OSCCON_500KHz b'00111000' ;OSCCON default
#define OSCCON_250KHz b'00110000'
#define OSCCON_125KHz b'00101000'
#define OSCCON__64KHz b'00100000'
#define OSCCON__32KHz b'00011000'
.
.
.
BANKSEL OSCCON ; Switch internal clock speed ...
movlw OSCCON___4MHz ; From settings above
movwf OSCCON

This is with the PLL disabled

mister_e
- 26th August 2011, 09:07
sure shot, but BANKSEL and PBP are really poor bedfellow. you want to use CHK?RP instead on the asm side. Still, there's no advantages to define it on the asm side.