Startup Without Peripheral Libraries – PIC32MK

Startup without peripheral libraries series

Microchip MPLAB X hides some of the details of starting up the PIC32.  The default startup code and linker scripts are hidden in the compiler directory and included by default.  Generally, they do not need to be touched.  Almost all of the register definitions are included the standard device header file xc.h.  This provides the register values both as a full register variable and as a bit fields.  The bit fields are a really nice touch that Microchip does with all their PIC MCUs.  I also include cp0defs.h to access the defines used to configure CP0.

This example uses the built in 8MHz fast RC internal oscillator and PLL to run at 120MHz.  I then set up Timer1 and enable interrupts.

The startup works as follows:

  1. Set up the proper configuration bits for the clock and features being used.  This populates the clock setting registers with your specified values automatically – MPLAB X contains a gui to help.
  2. Configure the CPU caches – I commented out the pre-fetch enable code since this is covered under some errata for my chip revision
  3. Enable any needed peripheral clocks
  4. Set up peripherals – In this case I set up Timer1 to interrupt every 1ms
  5. Enable interrupts as needed
  6. Call the sysInit function from the main function before the main loop
#include <xc.h>
#include <cp0defs.h>


#pragma config FUSBIDIO2 = OFF
#pragma config FVBUSIO2 = OFF
#pragma config PGL1WAY = OFF
#pragma config PMDL1WAY = OFF
#pragma config IOL1WAY = OFF
#pragma config FUSBIDIO1 = OFF
#pragma config FPLLIDIV = DIV_1
#pragma config FPLLRNG = RANGE_5_10_MHZ
#pragma config FPLLICLK = PLL_FRC
#pragma config FPLLMULT = MUL_60 // 120 MHz
#pragma config FPLLODIV = DIV_4
#pragma config VBATBOREN = OFF
#pragma config DSBOREN = OFF
#pragma config DSWDTEN = OFF
#pragma config BORSEL = HIGH
#pragma config UPLLEN = OFF
#pragma config FNOSC = SPLL
#pragma config FSOSCEN = OFF
#pragma config IESO = OFF
#pragma config POSCMOD = OFF
#pragma config OSCIOFNC = OFF
#pragma config FCKSM = CSECMD
#pragma config DMTCNT = DMT31
#pragma config FWDTEN = OFF
#pragma config FDMTEN = OFF
#pragma config ICESEL = ICS_PGx1

void sysInit (void)
{
   // CPU setup
   __builtin_mtc0(_CP0_CONFIG, 0, 3); // Configure CP0.K0 for cached instruction pre-fetch
   CHECONbits.PERCHEEN = 1; // enable peripheral cache
   CHECONbits.DCHEEN = 1; // enable data cache
   CHECONbits.ICHEEN = 1; // enable instruction cache
   // Comment out due to errata
   //CHECONbits.PREFEN = 1;
   CHECONbits.PFMWS = 3;

   // set up clocks
   PB2DIVbits.ON = 1; // turn on timer1 clock
   PB2DIVbits.PBDIV = 1; // SYSCLK / 2

   // set up timer
   T1CONbits.ON = 0;
   T1CONbits.TECS = 0b10; // use LPRC
   T1CONbits.TCS = 1; // use TECS setting
   T1CONbits.TCKPS = 0; // /1
   TMR1 = 0;
   PR1 = 32;
   IPC1bits.T1IP = 2;
   IFS0bits.T1IF = 0;
   IEC0bits.T1IE = 1;
   T1CONbits.ON = 1;

   // enable ints
   __builtin_enable_interrupts();
}

2 thoughts on “Startup Without Peripheral Libraries – PIC32MK

  1. Hi, Brian C. Havens, your wedside is so great, i ‘m love working with motor control solutions , i work with VFD’s and industrial servomotors at job, it is so facinating to find this kind of informmation about it, i´ve could see that you are really good handling 32 bit MCU , i still work in my free time with 8 bit architecture witch microchip PIC , i am really interesting to work with pic32mk1024mcf064 it will be my first time using it, i wanted to ask you what is the program tool that you have used for programming this device ?

    1. Abiel – I have a few different debugging and programming tools. Since you are just starting out, grab the inexpensive PICkit 4. It can program and debug almost the entire lineup of modern Microchip MCUs.

Leave a Reply to Brian Cancel reply

Your email address will not be published. Required fields are marked *