Electronic – Setting the internal oscillator speed on PIC12F1822

oscillatorpic

I'm trying to get a PIC12F1822 to run at 32 MHz. Here are the relevant bits of code:

__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_19 & _LVP_OFF



MOVLW   b'11110000' ; 32MHz HFINTOSC

BANKSEL OSCCON
MOVWF   OSCCON


BANKSEL OSCSTAT
btfss OSCSTAT, HFIOFR      ; Is internal oscillator running?
goto $-1
btfss OSCSTAT, HFIOFL      ; Is internal oscillator locked?
goto $-1


BANKSEL TRISA
CLRF    TRISA


BANKSEL PORTA

ON
    BSF      PORTA,2          ; 1 cycle
    GOTO    OFF               ; 2 cycles

OFF 
    BCF      PORTA,2          ; 1 cycle
    GOTO     ON               ; 2 cycles

I'm getting a 1.33MHz square wave from PORTA,2, meaning that the clock is running at 8MHz… The PLL is definitely working though, as if I switch it off, the freqency drops to 333KHz (2MHz)

According to the debugger (PicKit3), OSCCON is getting the correct value.

The chip is running at 5 V.

Any ideas?

Best Answer

The instruction clock on this part (and most PICs in general) runs at \$ \frac{F_{OSC}}{4}\$.

Each instruction cycle is \$ \frac{4}{32 MHz} = 125ns\$

which makes total sense when you describe a 1.33 MHz output square wave

(6 total clocks = \$ 750ns\$ = 1.33 MHz)