PIC18F2x/4xJ13 - Oscillator.

0.Contents

PIC18F Trainer : PIC18F2xJ13.

1.Internal Oscillator.

// Configuration registers.
#pragma config WDTEN=OFF, PLLDIV=1, CFGPLLEN=OFF, STVREN=ON, XINST=OFF
#pragma config CP0=OFF, OSC=INTOSCO, SOSCSEL=HIGH, CLKOEC=OFF, FCMEN=OFF
#pragma config IESO=OFF, WDTPS=32768, DSWDTOSC=INTOSCREF, RTCOSC=T1OSCREF
#pragma config DSBOREN=ON, DSWDTEN=ON, DSWDTPS=G2
#pragma config IOL1WAY=ON, ADCSEL=BIT10, PLLSEL=PLL4X, MSSP7B_EN=MSK7
#pragma config WPFP=PAGE_63, WPCFG=OFF, WPDIS=OFF, WPEND=PAGE_WPFP

#include <xc.h>
// PIC18FxxJ13 - Compile with XC8(v2.05).
// PIC18FxxJ13 - @8MHz Internal oscillator.

// Oscillator - Internal Oscillator.

// MCU.RB2 - Probe A.
// MCU.RA6 - Probe B.

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator Frequency Select bits.
	// IRCF2.0 = 1 1 1 - 8MHz.
	// IRCF2.0 = 1 1 0 - 4MHz.
	// IRCF2.0 = 1 0 1 - 2MHz.
	// IRCF2.0 = 1 0 0 - 1MHz.
	// IRCF2.0 = 0 1 1 - 500kHz.
	// IRCF2.0 = 0 1 0 - 250kHz.
	// IRCF2.0 = 0 0 1 - 125kHz.
	// IRCF2.0 = 0 0 0 - 31kHz.
	OSCCONbits.IRCF = 0b000;
	// Reference Oscillator Output.
	// RODIV3.0 = 1 1 1 1 - Base Clock Divided by 32768.
	// RODIV3.0 = 1 1 1 0 - Base Clock Divided by 16384.
	// RODIV3.0 = 1 1 0 1 - Base Clock Divided by 8192.
	// RODIV3.0 = 1 1 0 0 - Base Clock Divided by 4096.
	// RODIV3.0 = 1 0 1 1 - Base Clock Divided by 2048.
	// RODIV3.0 = 1 0 1 0 - Base Clock Divided by 1024.
	// RODIV3.0 = 1 0 0 1 - Base Clock Divided by 512.
	// RODIV3.0 = 1 0 0 0 - Base Clock Divided by 256.
	// RODIV3.0 = 0 1 1 1 - Base Clock Divided by 128.
	// RODIV3.0 = 0 1 1 0 - Base Clock Divided by 64.
	// RODIV3.0 = 0 1 0 1 - Base Clock Divided by 32.
	// RODIV3.0 = 0 1 0 0 - Base Clock Divided by 16.
	// RODIV3.0 = 0 0 1 1 - Base Clock Divided by 8.
	// RODIV3.0 = 0 0 1 0 - Base Clock Divided by 4.
	// RODIV3.0 = 0 0 0 1 - Base Clock Divided by 2.
	// RODIV3.0 = 0 0 0 0 - Base Clock.
	REFOCONbits.RODIV = 0b0000;
	REFOCONbits.ROON = 1;
	// Ports Initialization.
    // Open-drain Output Settings.
    ODCON3 = 0b00000000;
    ODCON2 = 0b00000000;
    ODCON1 = 0b00000000;
	// Analog Input Settings.
	ANCON1 = 0b00011111;
	ANCON0 = 0b00011111;	// For PIC18F2xJ13.
	//ANCON0 = 0b11111111;	// For PIC18F4xJ13.
	// Port A.
	PORTA = 0b00000000;
	TRISA = 0b00000000;
	// Port B.
	PORTB = 0b00000000;
	TRISB = 0b00000000;
	// Port C.
	PORTC = 0b00000000;
	TRISC = 0b00000000;
	// Port D.
	//PORTD = 0b00000000;	// For PIC18F4xJ13.
	//TRISD = 0b00000000;	// For PIC18F4xJ13.
	// Port E.
	//PORTE = 0b00000000;	// For PIC18F4xJ13.
	//TRISE = 0b00000000;	// For PIC18F4xJ13.
	
	while(1){
	}
	return(0);
}
Fosc = 31kHz - IRCF2.0 = 0 0 0
Fosc = 125kHz - IRCF2.0 = 0 0 1
Fosc = 250kHz - IRCF2.0 = 0 1 0
Fosc = 500kHz - IRCF2.0 = 0 1 1
Fosc = 2MHz - IRCF2.0 = 1 0 1
Fosc = 4MHz - IRCF2.0 = 1 1 0
Fosc = 8MHz - IRCF2.0 = 1 1 1

2.Internal Oscillator with PLL.

// Configuration registers.
#pragma config WDTEN=OFF, PLLDIV=1, CFGPLLEN=OFF, STVREN=ON, XINST=OFF
#pragma config CP0=OFF, OSC=INTOSCPLLO, SOSCSEL=HIGH, CLKOEC=OFF, FCMEN=OFF
#pragma config IESO=OFF, WDTPS=32768, DSWDTOSC=INTOSCREF, RTCOSC=T1OSCREF
#pragma config DSBOREN=ON, DSWDTEN=ON, DSWDTPS=G2
#pragma config IOL1WAY=ON, ADCSEL=BIT10, PLLSEL=PLL96, MSSP7B_EN=MSK7
#pragma config WPFP=PAGE_63, WPCFG=OFF, WPDIS=OFF, WPEND=PAGE_WPFP

#include <xc.h>
// PIC18FxxJ13 - Compile with XC8(v2.05).
// PIC18FxxJ13 - @8MHz Internal oscillator.

// Oscillator - Internal Oscillator with PLL.

// MCU.RB2 - Probe A.
// MCU.RA6 - Probe B.

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator Frequency Select bits.
	// IRCF2.0 = 1 1 1 - 8MHz.
	// IRCF2.0 = 1 1 0 - 4MHz.
	// IRCF2.0 = 1 0 1 - 2MHz.
	// IRCF2.0 = 1 0 0 - 1MHz.
	// IRCF2.0 = 0 1 1 - 500kHz.
	// IRCF2.0 = 0 1 0 - 250kHz.
	// IRCF2.0 = 0 0 1 - 125kHz.
	// IRCF2.0 = 0 0 0 - 31kHz.
	OSCCONbits.IRCF = 0b110;
	// PLL Enable.
	OSCTUNEbits.PLLEN = 1;
	// Reference Oscillator Output.
	// RODIV3.0 = 1 1 1 1 - Base Clock Divided by 32768.
	// RODIV3.0 = 1 1 1 0 - Base Clock Divided by 16384.
	// RODIV3.0 = 1 1 0 1 - Base Clock Divided by 8192.
	// RODIV3.0 = 1 1 0 0 - Base Clock Divided by 4096.
	// RODIV3.0 = 1 0 1 1 - Base Clock Divided by 2048.
	// RODIV3.0 = 1 0 1 0 - Base Clock Divided by 1024.
	// RODIV3.0 = 1 0 0 1 - Base Clock Divided by 512.
	// RODIV3.0 = 1 0 0 0 - Base Clock Divided by 256.
	// RODIV3.0 = 0 1 1 1 - Base Clock Divided by 128.
	// RODIV3.0 = 0 1 1 0 - Base Clock Divided by 64.
	// RODIV3.0 = 0 1 0 1 - Base Clock Divided by 32.
	// RODIV3.0 = 0 1 0 0 - Base Clock Divided by 16.
	// RODIV3.0 = 0 0 1 1 - Base Clock Divided by 8.
	// RODIV3.0 = 0 0 1 0 - Base Clock Divided by 4.
	// RODIV3.0 = 0 0 0 1 - Base Clock Divided by 2.
	// RODIV3.0 = 0 0 0 0 - Base Clock.
	REFOCONbits.RODIV = 0b0000;
	REFOCONbits.ROON = 1;
	// Ports Initialization.
    // Open-drain Output Settings.
    ODCON3 = 0b00000000;
    ODCON2 = 0b00000000;
    ODCON1 = 0b00000000;
	// Analog Input Settings.
	ANCON1 = 0b00011111;
	ANCON0 = 0b00011111;	// For PIC18F2xJ13.
	//ANCON0 = 0b11111111;	// For PIC18F4xJ13.
	// Port A.
	PORTA = 0b00000000;
	TRISA = 0b00000000;
	// Port B.
	PORTB = 0b00000000;
	TRISB = 0b00000000;
	// Port C.
	PORTC = 0b00000000;
	TRISC = 0b00000000;
	// Port D.
	//PORTD = 0b00000000;	// For PIC18F4xJ13.
	//TRISD = 0b00000000;	// For PIC18F4xJ13.
	// Port E.
	//PORTE = 0b00000000;	// For PIC18F4xJ13.
	//TRISE = 0b00000000;	// For PIC18F4xJ13.
	
	while(1){
	}
	return(0);
}

05.2019