PIC18F2x/4xK22 - Oscillator.

0.Contents

PIC18F Trainer : PIC18F26K22.

1.Internal Oscillator.

// Configuration register.
#pragma config FOSC = INTIO7, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF
#pragma config IESO = OFF, PWRTEN = OFF, BOREN = OFF, BORV = 190
#pragma config WDTEN = OFF, WDTPS = 32768, CCP2MX = PORTC1, PBADEN = OFF
#pragma config CCP3MX = PORTB5, HFOFST = OFF, T3CMX = PORTC0, P2BMX = PORTB5
#pragma config MCLRE = EXTMCLR, STVREN = ON, LVP = ON, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF

#include <xc.h>
// PIC18F26K22 - Compile with XC8(v2.00).

// MCU.RA6 -> PROBE A - Fosc/4 - Output on RA6.
// MCU.RA0 -> PROBE B - Output on RA0.

// Definitions.
#define PROBE		LATAbits.LATA0
#define PROBE_TRIS	TRISAbits.TRISA0

// Main.
int main(void)
{
	PROBE = 0;
	PROBE_TRIS = 0;

	// Internal Oscillator Frequency Select bits.
	// IRCF2.0 = 1 1 1 - 16MHz.
	// IRCF2.0 = 1 1 0 - 8MHz.
	// IRCF2.0 = 1 0 1 - 4MHz.
	// IRCF2.0 = 1 0 0 - 2MHz.
	// IRCF2.0 = 0 1 1 - 1MHz.
	// IRCF2.0 = 0 1 0 - 500kHz.
	// IRCF2.0 = 0 0 1 - 250kHz.
	OSCCONbits.IRCF2 = 0;
	OSCCONbits.IRCF1 = 0;
	OSCCONbits.IRCF0 = 1;
	// Frequency Tunning.
	// TUN4.0 = 0 1 1 1 1 1 - Maximum frequency.
	// ...
	// TUN4.0 = 0 0 0 0 0 0 - Center frequency, Calibrated frequency.
	// ...
	// TUN4.0 = 1 0 0 0 0 0 - Minimum frequency.
	OSCTUNE = 0b00000000;
	// PLL - Frequency Multiplier PLL for INTOSC Enable bit.
	// PLLEN0 = 0 - PLL Disabled.
	// PLLEN0 = 1 - PLL Enabled for INTOSC 4MHz & 16MHz only.
	OSCTUNEbits.PLLEN = 0;
	// Wait frequency is stable.
	while(!OSCCONbits.HFIOFS);

	while(1){
		PROBE ^= 1;
	}
	return(0);
}
Fosc = 250kHz - IRCF2.0 = 0 0 1 - PLLEN = 0
Fosc = 500kHz - IRCF2.0 = 0 1 0 - PLLEN = 0
Fosc = 1MHz - IRCF2.0 = 0 1 1 - PLLEN = 0
Fosc = 2MHz - IRCF2.0 = 1 0 0 - PLLEN = 0
Fosc = 4MHz - IRCF2.0 = 1 0 1 - PLLEN = 0
Fosc = 8MHz - IRCF2.0 = 1 1 0 - PLLEN = 0
Fosc = 16MHz - IRCF2.0 = 1 1 1 - PLLEN = 0
Fosc = 16MHz - IRCF2.0 = 1 0 1 - PLLEN = 1
Fosc = 32MHz - IRCF2.0 = 1 1 0 - PLLEN = 1
Fosc = 64MHz - IRCF2.0 = 1 1 1 - PLLEN = 1

11.2018