How to set up external clock on uC32 with MPLABX?

Post Reply
riceman0
Posts: 9
Joined: Mon Mar 06, 2017 10:42 pm

How to set up external clock on uC32 with MPLABX?

Post by riceman0 » Sat Mar 11, 2017 9:16 pm

Hello, I am writing code for my chipkit uC32 with MPLABX and a programmer.

I am writing my "hello world" program and I think the default settings for this chip are to use the fast internal oscillator, which is 8MHZ.

However the board specs say that the dev board has a 80MHZ speed, so I assume there is an oscillator on the board. I also assume that the CPU is typically configured to use the oscillator by the bootloader (?).

I'm not using the bootloader, just writing my own C code; so I was wondering, do you (or does anyone) have some sample C code that configures this CPU to use the external oscillator. It looks like this involves a few registers, and I'm not 100% confident I'll get this right. Some sample code would save me a ton of time and also educate me.

Appreciate any help.

*** Quick update... I did try this:

#pragma config FNOSC = PRI

and it seemed to speed up my chip *slightly* (judging by my LEDs blinking), but it was not the 10x (8MHZ to 80MHZ) jump I expected. So, yeah like I said I could use some help. :)

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: How to set up external clock on uC32 with MPLABX?

Post by majenko » Sat Mar 11, 2017 9:51 pm

You need to set up both the oscillator and the PLL. The oscillator is an 8MHz crystal, so you need to get the PLL to multiply by 10.

The settings used in the bootloader are:

Code: Select all

    //*    Oscillator Settings
    #pragma config FNOSC    = PRIPLL                        // Oscillator selection
    #pragma config POSCMOD  = XT                            // Primary oscillator mode
    #pragma config FPLLIDIV = DIV_2                         // PLL input divider
    #pragma config FPLLMUL  = MUL_20                        // PLL multiplier
    #pragma config FPLLODIV = DIV_1                         // PLL output divider
    #pragma config FPBDIV   = DIV_1                         // Peripheral bus clock divider
    #pragma config FSOSCEN  = ON                            // Secondary oscillator enable

    //*    Clock control settings
    #pragma config IESO     = OFF                           // Internal/external clock switchover
    #pragma config FCKSM    = CSDCMD                        // Clock switching (CSx)/Clock monitor (CMx)
    #pragma config OSCIOFNC = OFF                           // Clock output on OSCO pin enable

    //*    Other Peripheral Device settings
    #pragma config FWDTEN   = OFF                           // Watchdog timer enable
    #pragma config WDTPS    = PS1024                        // Watchdog timer postscaler

    //*    Code Protection settings
    #pragma config CP       = OFF                           // Code protection
    #pragma config BWP      = OFF                           // Boot flash write protect
    #pragma config PWP      = OFF                           // Program flash write protect

    //*    Debug settings
    #pragma config ICESEL   = ICS_PGx2                      // ICE pin selection
It multiplies the clock by 20, then divides it by 2, yielding an 80MHz clock from an 8MHz crystal.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

riceman0
Posts: 9
Joined: Mon Mar 06, 2017 10:42 pm

Re: How to set up external clock on uC32 with MPLABX?

Post by riceman0 » Sun Mar 12, 2017 12:25 am

Awesome, on first pass that looks like it did the trick! thanks!

Question... I omitted the watchdog settings, the code protection settings, and the ICE pin selection.

Do I need (1) the "secondary oscillator" and (2) the "clock output on OSC0" and clock switching/switchover?

I'm not sure what those do, so not sure if they're important.

Also... is the entire bootloader source publically available? There's not much sample code *specifically* for this CPU so I thought I'd learn a thing or two. I was looking for it but just found the hex.

Thanks again!

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: How to set up external clock on uC32 with MPLABX?

Post by majenko » Sun Mar 12, 2017 1:18 pm

The secondary oscillator is only useful if you attach a 32768Hz crystal to the SOSCI and SOSCO pins for use with the internal RTCC. Pointless otherwise.

Clock output on OSCO can't be used with a crystal, only the internal FRC/LPRC or an external clock module. It outputs the internal clock on the OSCO pin so you can drive other things at the same frequency (or use it for debugging clock problems). Leave it off.

Clock switching is used for selecting different clock sources at runtime. Good for reducing power while you're not needing high speed, but complex to manage.

The bootloader we use is here: https://github.com/chipKIT32/PIC32-avrdude-bootloader - however it's not the bootloader that comes pre-programmed because Digilent want to use their own version. It is available somewhere, but I forget where.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

EmbeddedMan
Posts: 597
Joined: Tue May 24, 2011 9:24 pm
Location: Minneapolis, MN
Contact:

Re: How to set up external clock on uC32 with MPLABX?

Post by EmbeddedMan » Sun Mar 12, 2017 5:13 pm

All Digilent bootloaders are available here (in source and hex form) : https://github.com/chipKIT32/chipKIT-digiboot

*Brian

Post Reply