4
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ D'oh... Instruction speed is 1/4 clock frequency, so it's all working fine :) \$\endgroup\$
    – tsiflana
    Sep 13, 2012 at 12:37

1 Answer 1

4
\$\begingroup\$

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)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Er... isn't 6 \$\times\$ 125 ns equal to 750 ns? \$\endgroup\$
    – stevenvh
    Sep 13, 2012 at 13:01
  • 4
    \$\begingroup\$ Need coffee. Badly. \$\endgroup\$ Sep 13, 2012 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.