Switching the clock source

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
idakota
Posts: 334
Joined: 27 Sep 2006 08:07
Location: Pretoria/South Africa
Contact:

Switching the clock source

#1 Post by idakota » 01 Oct 2008 14:58

I'm struggling to get the micro to switch its clock source.

I've got a 10MHz external crystal so to use the PLL I need to modify the PLLPRE and PLLPOST bits. Following the setup guidelines from microchip tells me that I need to modify the values and then switch from the internal RC (for example) to the external crystal with PLL.

Using the microchip supplied code to switch clock sources produces the following error: "Invalid ASM instruction(135): #OSCCONH"

The ASM code is:

Code: Select all

   asm {
        ;Place the New Oscillator Selection (NOSC=0b011) in W0
        MOV #0x13,W0
        ;OSCCONH (high byte) Unlock Sequence
        MOV #OSCCONH, W1
        MOV #0x78, W2
        MOV #0x9A, W3
        MOV.B W2, [W1] ; Write 0x78
        MOV.B W3, [W1] ; Write 0x9A
        ;Set New Oscillator Selection
        MOV.B W0, [W1]
        ; Place 0x01 in W0 for setting clock switch enabled bit
        MOV #0x01, W0
        ;OSCCONL (low byte) Unlock Sequence
        MOV #OSCCONL, W1
        MOV #0x46, W2
        MOV #0x57, W3
        MOV.B W2, [W1] ; Write 0x46
        MOV.B W3, [W1] ; Write 0x57
        ; Enable Clock Switch
        MOV.B W0, [W1]
wait:
             BTSC OSCCON, #0
             BRA wait
   };
Any help would be appreciated!

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#2 Post by rmteo » 01 Oct 2008 15:26

When using embedded ASM, this applies to mikroBASIC:
If you plan to use a certain mikroBasic variable in embedded assembly only, be sure to at least initialize it (assign it initial value) in mikroBasic code; otherwise, the linker will issue an error. This is not applied to predefined globals such as PORTB.
Should be the same in C. I usually do this before the ASM code:

Code: Select all

OSCCONH = OSCCONH
OSCCONL = OSCCONL
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

idakota
Posts: 334
Joined: 27 Sep 2006 08:07
Location: Pretoria/South Africa
Contact:

#3 Post by idakota » 01 Oct 2008 15:55

Thanks, but it doesn't work.

OSCCON is a SFR for the PIC24s. I think that OSCCONH stores the address for the high byte of OSCCON and OSCCONL stores the address for the low byte in the Microchip compiler. Obviously mC does not share these values.

Edit: I got it working by replacing OSCCONL and OSCCONH by the absolute addresses for the low byte and high byte of OSCCON

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”