PIC24FJ256GA110 - Oscillator.

0.Contents

PIC24FJGA/GB MiniTrainer : PIC24FJ256GA110.

1.Internal Oscillator - LPRC.

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = NONE, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = LPRC, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// Internal Oscillator - LPRC.

// OSC2 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	// DOZE - CPU Peripheral Clock Ratio Select bits.
	// DOZE2.0 = 1 1 1 - 1:128.
	// DOZE2.0 = 1 1 0 - 1:64.
	// DOZE2.0 = 1 0 1 - 1:32.
	// DOZE2.0 = 1 0 0 - 1:16.
	// DOZE2.0 = 0 1 1 - 1:8.
	// DOZE2.0 = 0 1 0 - 1:4.
	// DOZE2.0 = 0 0 1 - 1:2.
	// DOZE2.0 = 0 0 0 - 1:1.
	CLKDIVbits.DOZE2 = 0;
	CLKDIVbits.DOZE1 = 0;
	CLKDIVbits.DOZE0 = 0;
	// DOZE Enable bit.
	CLKDIVbits.DOZEN = 0;

	PROBE = 0;
	PROBE_TRIS = 0;

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc ~32kHz - DOZEN = 0
Fosc ~250Hz - DOZE2.0 = 1 1 1 - DOZEN = 1

2.Internal Oscillator - FRC

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = NONE, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = FRC, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// Internal Oscillator - FRC.

// OSC2 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	// DOZE - CPU Peripheral Clock Ratio Select bits.
	// DOZE2.0 = 1 1 1 - 1:128.
	// DOZE2.0 = 1 1 0 - 1:64.
	// DOZE2.0 = 1 0 1 - 1:32.
	// DOZE2.0 = 1 0 0 - 1:16.
	// DOZE2.0 = 0 1 1 - 1:8.
	// DOZE2.0 = 0 1 0 - 1:4.
	// DOZE2.0 = 0 0 1 - 1:2.
	// DOZE2.0 = 0 0 0 - 1:1.
	CLKDIVbits.DOZE2 = 0;
	CLKDIVbits.DOZE1 = 0;
	CLKDIVbits.DOZE0 = 0;
	// DOZE Enable bit.
	CLKDIVbits.DOZEN = 0;

	// TUN - FRC Oscillator Tuning bits.
	// TUN5.0 = 0 1 1 1 1 1 - Maximum frequency deviation.
	// TUN5.0 = ...
	// TUN5.0 = 0 0 0 0 0 0 - Center frequency.
	// TUN5.0 = ...
	// TUN5.0 = 1 0 0 0 0 0 - Minimum frequency deviation.
	OSCTUNbits.TUN5 = 0;
	OSCTUNbits.TUN4 = 0;
	OSCTUNbits.TUN3 = 0;
	OSCTUNbits.TUN2 = 0;
	OSCTUNbits.TUN1 = 0;
	OSCTUNbits.TUN0 = 0;

	PROBE = 0;
	PROBE_TRIS = 0;

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc ~8MHz - DOZEN = 0
Fosc ~62.5kHz - DOZE2.0 = 1 1 1 - DOZEN = 1

3.Internal Oscillator - FRCDIV

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = NONE, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = FRCDIV, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// Internal Oscillator - FRCDIV.

// OSC2 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	// DOZE - CPU Peripheral Clock Ratio Select bits.
	// DOZE2.0 = 1 1 1 - 1:128.
	// DOZE2.0 = 1 1 0 - 1:64.
	// DOZE2.0 = 1 0 1 - 1:32.
	// DOZE2.0 = 1 0 0 - 1:16.
	// DOZE2.0 = 0 1 1 - 1:8.
	// DOZE2.0 = 0 1 0 - 1:4.
	// DOZE2.0 = 0 0 1 - 1:2.
	// DOZE2.0 = 0 0 0 - 1:1.
	CLKDIVbits.DOZE2 = 0;
	CLKDIVbits.DOZE1 = 0;
	CLKDIVbits.DOZE0 = 0;
	// DOZE Enable bit.
	CLKDIVbits.DOZEN = 0;
	// RCDIV - FRCDIV Postscaler Select bits.
	// RCDIV2.0 = 1 1 1 - 31.25kHz.
	// RCDIV2.0 = 1 1 0 - 125kHz.
	// RCDIV2.0 = 1 0 1 - 250kHz.
	// RCDIV2.0 = 1 0 0 - 500kHz.
	// RCDIV2.0 = 0 1 1 - 1MHz.
	// RCDIV2.0 = 0 1 0 - 2MHz.
	// RCDIV2.0 = 0 0 1 - 4MHz.
	// RCDIV2.0 = 0 0 0 - 8MHz.
	CLKDIVbits.RCDIV2 = 0;
	CLKDIVbits.RCDIV1 = 0;
	CLKDIVbits.RCDIV0 = 0;

	// TUN - FRC Oscillator Tuning bits.
	// TUN5.0 = 0 1 1 1 1 1 - Maximum frequency deviation.
	// TUN5.0 = ...
	// TUN5.0 = 0 0 0 0 0 0 - Center frequency.
	// TUN5.0 = ...
	// TUN5.0 = 1 0 0 0 0 0 - Minimum frequency deviation.
	OSCTUNbits.TUN5 = 0;
	OSCTUNbits.TUN4 = 0;
	OSCTUNbits.TUN3 = 0;
	OSCTUNbits.TUN2 = 0;
	OSCTUNbits.TUN1 = 0;
	OSCTUNbits.TUN0 = 0;

	PROBE = 0;
	PROBE_TRIS = 0;

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc ~8MHz - DOZEN = 0 - RCDIV = 0 0 0
Fosc ~31.25kHz - DOZEN = 0 - RCDIV = 1 1 1
Fosc ~62.5kHz - DOZE2.0 = 1 1 1 - DOZEN = 1 - RCDIV = 0 0 0
Fosc ~244Hz - DOZE2.0 = 1 1 1 - DOZEN = 1 - RCDIV = 1 1 1

4.Internal Oscillator - FRCPLL

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = NONE, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = FRCPLL, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// Internal Oscillator - FRCPLL.

// Fosc = FRC(8MHz) / PLLDIV2 / PLL96MHz / CPDIV1.0 / 3 / DOZE2.0.
// Fcpu = Fosc / 2.

// OSC2 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	// DOZE - CPU Peripheral Clock Ratio Select bits.
	// DOZE2.0 = 1 1 1 - 1:128.
	// DOZE2.0 = 1 1 0 - 1:64.
	// DOZE2.0 = 1 0 1 - 1:32.
	// DOZE2.0 = 1 0 0 - 1:16.
	// DOZE2.0 = 0 1 1 - 1:8.
	// DOZE2.0 = 0 1 0 - 1:4.
	// DOZE2.0 = 0 0 1 - 1:2.
	// DOZE2.0 = 0 0 0 - 1:1.
	CLKDIVbits.DOZE2 = 0;
	CLKDIVbits.DOZE1 = 0;
	CLKDIVbits.DOZE0 = 0;
	// DOZE Enable bit.
	CLKDIVbits.DOZEN = 0;
	// RCDIV - FRCDIV Postscaler Select bits.
	// RCDIV2.0 = 1 1 1 - 31.25kHz.
	// RCDIV2.0 = 1 1 0 - 125kHz.
	// RCDIV2.0 = 1 0 1 - 250kHz.
	// RCDIV2.0 = 1 0 0 - 500kHz.
	// RCDIV2.0 = 0 1 1 - 1MHz.
	// RCDIV2.0 = 0 1 0 - 2MHz.
	// RCDIV2.0 = 0 0 1 - 4MHz.
	// RCDIV2.0 = 0 0 0 - 8MHz.
	CLKDIVbits.RCDIV2 = 0;
	CLKDIVbits.RCDIV1 = 0;
	CLKDIVbits.RCDIV0 = 0;

	// TUN - FRC Oscillator Tuning bits.
	// TUN5.0 = 0 1 1 1 1 1 - Maximum frequency deviation.
	// TUN5.0 = ...
	// TUN5.0 = 0 0 0 0 0 0 - Center frequency.
	// TUN5.0 = ...
	// TUN5.0 = 1 0 0 0 0 0 - Minimum frequency deviation.
	OSCTUNbits.TUN5 = 0;
	OSCTUNbits.TUN4 = 0;
	OSCTUNbits.TUN3 = 0;
	OSCTUNbits.TUN2 = 0;
	OSCTUNbits.TUN1 = 0;
	OSCTUNbits.TUN0 = 0;

	PROBE = 0;
	PROBE_TRIS = 0;

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc ~32MHz - DOZEN = 0
Fosc ~250kHz - DOZE2.0 = 1 1 1 - DOZEN = 1

5.External Oscillator - PRIMARY-EC

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = EC, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = PRI, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// External Oscillator - PRIMARY-EC @24MHz.

// OSC2 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	// DOZE - CPU Peripheral Clock Ratio Select bits.
	// DOZE2.0 = 1 1 1 - 1:128.
	// DOZE2.0 = 1 1 0 - 1:64.
	// DOZE2.0 = 1 0 1 - 1:32.
	// DOZE2.0 = 1 0 0 - 1:16.
	// DOZE2.0 = 0 1 1 - 1:8.
	// DOZE2.0 = 0 1 0 - 1:4.
	// DOZE2.0 = 0 0 1 - 1:2.
	// DOZE2.0 = 0 0 0 - 1:1.
	CLKDIVbits.DOZE2 = 0;
	CLKDIVbits.DOZE1 = 0;
	CLKDIVbits.DOZE0 = 0;
	// DOZE Enable bit.
	CLKDIVbits.DOZEN = 0;

	PROBE = 0;
	PROBE_TRIS = 0;

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc ~24MHz - DOZEN = 0
Fosc ~187.5kHz - DOZE2.0 = 1 1 1 - DOZEN = 1

6.Switching Clock - REFO - LPRC - FRC - FRCPLL - PRIMARY-EC

// Configuration register.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = EC, IOL1WAY = ON,  OSCIOFNC = OFF
#pragma config FCKSM = CSECMD, FNOSC = LPRC, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
// PIC24FJ256GA106 - Compile with XC16(v1.33).
// Internal/External Oscillator - LPRC - FRC - FRCPLL - PRI-EC@24MHz.

// OSC2		 -> Oscilloscope Chanel A.
// REFO/RB15 -> Oscilloscope Chanel B.

// Main.
int main(void)
{
	// RODIV - Reference Oscillator Divisor Select bits.
	// RODIV3.0 = 1 1 1 1 - Base clock value divided by 32768.
	// RODIV3.0 = 1 1 1 0 - Base clock value divided by 16384.
	// RODIV3.0 = 1 1 0 1 - Base clock value divided by 8192.
	// RODIV3.0 = 1 1 0 0 - Base clock value divided by 4096.
	// RODIV3.0 = 1 0 1 1 - Base clock value divided by 2048.
	// RODIV3.0 = 1 0 1 0 - Base clock value divided by 1024.
	// RODIV3.0 = 1 0 0 1 - Base clock value divided by 512.
	// RODIV3.0 = 1 0 0 0 - Base clock value divided by 256.
	// RODIV3.0 = 0 1 1 1 - Base clock value divided by 128.
	// RODIV3.0 = 0 1 1 0 - Base clock value divided by 64.
	// RODIV3.0 = 0 1 0 1 - Base clock value divided by 32.
	// RODIV3.0 = 0 1 0 0 - Base clock value divided by 16.
	// RODIV3.0 = 0 0 1 1 - Base clock value divided by 8.
	// RODIV3.0 = 0 0 1 0 - Base clock value divided by 4.
	// RODIV3.0 = 0 0 0 1 - Base clock value divided by 2.
	// RODIV3.0 = 0 0 0 0 - Base clock value.
	REFOCONbits.RODIV3 = 0;
	REFOCONbits.RODIV2 = 0;
	REFOCONbits.RODIV1 = 0;
	REFOCONbits.RODIV0 = 0;
	// ROSEL - Reference Oscillator Source Select bit.
	// ROSEL.0 = 1 - Primary Oscillator used as the base clock.
	// ROSEL.0 = 0 - System clock used as the base clock.
	REFOCONbits.ROSEL = 0;
	// ROON - Reference Oscillator Output Enable bit.
	REFOCONbits.ROON = 1;

	uint16_t i, j;
	while(1){
		// LPRC.
		__builtin_write_OSCCONH(0b101);
		if(OSCCONbits.OSWEN == 0) {
			__builtin_write_OSCCONL(0x01);
			while(OSCCONbits.COSC != 0b101);
		}
		for(i=0x0000; i<0x3fff; i++);

		// FRC.
		__builtin_write_OSCCONH(0b000);
		if(OSCCONbits.OSWEN == 0) {
			__builtin_write_OSCCONL(0x01);
			while(OSCCONbits.COSC != 0b000);
		}
		for(i=0x0000; i<0xffff; i++)
			for(j=0x0000; j<0x003f; j++);

		// FRCPLL.
		__builtin_write_OSCCONH(0b001);
		if(OSCCONbits.OSWEN == 0) {
			__builtin_write_OSCCONL(0x01);
			while(OSCCONbits.COSC != 0b001);
		}
		for(i=0x0000; i<0xffff; i++)
			for(j=0x0000; j<0x007f; j++);

		// PRIMARY EC.
		__builtin_write_OSCCONH(0b010);
		if(OSCCONbits.OSWEN == 0) {
			__builtin_write_OSCCONL(0x01);
			while(OSCCONbits.COSC != 0b010);
		}
		for(i=0x0000; i<0xffff; i++)
			for(j=0x0000; j<0x00ef; j++);
	}
	return(0);
}

03.2018