• AVR Freaks

PIC32 two-speed startup

Author
luker
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2015/04/17 06:24:04
  • Location: 0
  • Status: offline
2016/09/07 07:57:33 (permalink)
3 (1)

PIC32 two-speed startup

Hi forum,
 
my PIC32MZ2048EFH144 has a display attached that it controls via LCC. Now it must learn to run on 3AA secondary cells, backlight dimmed.
 
I configured the "Clock" service to start from FRC 8MHz instead of POSC EC 8MHz + PLL 200MHz. So I can check the power supply situation in software and take appropriate measures.
 
In case mains is found, I can switch the resonator's supply voltage on through an output pin.
 
How do I then switch to POSC EC + PLL to have the PIC working as usual?
 
#1
Howard Long
Super Member
  • Total Posts : 681
  • Reward points : 0
  • Joined: 2005/04/04 08:50:32
  • Status: offline
Re: PIC32 two-speed startup 2016/09/08 03:42:24 (permalink)
0
Note 1: There appears to be a silicon feature when setting the FNOSC config field to 7 rather than 0. Either should result in FRCDIV being selected according the data sheet. Setting to 7 actually falls back to BFRC, whereas 0 works correctly.
 
Note 2: the following code is with a 24MHz POSC EC on the EF SK.
 

// Include dir: c:/microchip/harmony/v1_08_01/framework/
// Lib: C:\microchip\harmony\v1_08_01\bin\framework\peripheral\PIC32MZ1024EFM144_peripherals.a
// DEVCFG0
#pragma config JTAGEN = OFF             // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
/*** DEVCFG1 ***/
#pragma config FNOSC =      0 //FRCDIV, note that setting this to 7 instead of 0 causes a clock failure, and COSC set to BFRC
#pragma config IESO =       OFF
#pragma config POSCMOD =    EC
#pragma config OSCIOFNC =   OFF
#pragma config FCKSM =      CSECME
#pragma config FWDTEN =     OFF
#pragma config FDMTEN =     OFF
#define PLIB_DISABLE_OPTIMIZATION_WARNING
#include "peripheral/peripheral.h"
#include <xc.h>
#include <stdint.h>
void __attribute__((vector(_TIMER_2_VECTOR),interrupt(IPL5AUTO))) __Timer2Interrupt(void)
{
    static int i=0;
   
    IFS0bits.T2IF=0;
    i++;
   
    if (i>=10000)
    {
        i=0;
        LATHINV=1; // Toggle LED
    }
}
static void CPUInit(void)
{
    // Note that cache is already configured by startup code
    // C:\Program Files\Microchip\xc32\v1.42\pic32-libs\libpic32\stubs\pic32_init_cache.S
   
    // Set up wait states
    PLIB_PCACHE_WaitStateSet(PCACHE_ID_0, 2); // Adjust as appropriate for memory and clock
   
    // Set up prefetch
    if (PLIB_PCACHE_ExistsPrefetchEnable(PCACHE_ID_0))
    {
        PLIB_PCACHE_PrefetchEnableSet(PCACHE_ID_0, PLIB_PCACHE_PREFETCH_ENABLE_ALL);
    }
   
    // Set up interrupt controller
    PLIB_INT_MultiVectorSelect(INT_ID_0);
    PLIB_INT_Enable(INT_ID_0);
}
void main(void)
{
    CPUInit();
   
    // All digital GPIO
    ANSELA=0;
    ANSELB=0;
    ANSELC=0;
    ANSELD=0;
    ANSELE=0;
    ANSELF=0;
    ANSELG=0;
    ANSELH=0;
   
    TRISHbits.TRISH0=0; // LED output
   
    // Set up timer 2
    TMR2=0;
    PR2=10000;
    IPC2bits.T2IP=1;
    IPC2bits.T2IS=0;
    IFS0bits.T2IF=0;
    IEC0bits.T2IE=1;
    T2CONbits.ON=1;
   
    // Switch to 200MHz PLL sourced from 24MHz EC POSC
    PLIB_DEVCON_SystemUnlock ( DEVCON_ID_0 );
    PLIB_OSC_SysPLLInputDivisorSet(OSC_ID_0, OSC_SYSPLL_IN_DIV_3); // Generate 8MHz from 24MHz POSC
    PLIB_OSC_SysPLLFrequencyRangeSet(OSC_ID_0, OSC_SYSPLL_FREQ_RANGE_5M_TO_10M);
    PLIB_OSC_SysPLLInputClockSourceSet(OSC_ID_0, OSC_SYSPLL_IN_CLK_SOURCE_PRIMARY);
    PLIB_OSC_SysPLLMultiplierSelect(OSC_ID_0, 50);
    PLIB_OSC_SysPLLOutputDivisorSet(OSC_ID_0, OSC_SYSPLL_OUT_DIV_2);
    PLIB_OSC_SysClockSelect(OSC_ID_0, OSC_PRIMARY_WITH_PLL);
    while (!PLIB_OSC_ClockSwitchingIsComplete(OSC_ID_0))
    {
        Nop();
    }
    PLIB_DEVCON_SystemLock ( DEVCON_ID_0 );
    while (1)
    {
        Nop();
    }
}
 

#2
Jump to:
© 2019 APG vNext Commercial Version 4.5