PIC18F27K40 - Oscillator.

0.Contents

PIC18F Trainer : PIC18F27K40.

1.HFINTOSC - 64MHz Internal Oscillator.

// Configuration registers.
#pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ
#pragma config CLKOUTEN = ON, CSWEN = OFF, FCMEN = OFF
#pragma config MCLRE = EXTMCLR, PWRTE = OFF, LPBOREN = OFF, BOREN = OFF
#pragma config BORV = VBOR_2P45, ZCD = OFF, PPS1WAY = ON, STVREN = ON, DEBUG = OFF, XINST = OFF
#pragma config WDTCPS = WDTCPS_31, WDTE = OFF
#pragma config WDTCWS = WDTCWS_7, WDTCCS = SC
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRT4 = OFF, WRT5 = OFF, WRT6 = OFF, WRT7 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF, SCANE = OFF, LVP = ON
#pragma config CP = OFF, CPD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTR4 = OFF, EBTR5 = OFF, EBTR6 = OFF, EBTR7 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
// PIC18F27K40 - Compile with XC8(v1.42).

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

// Definitions.
#define PROBE_ANSEL		ANSELAbits.ANSELA0
#define PROBE_LAT		LATAbits.LATA0
#define PROBE_SLR		SLRCONAbits.SLRA0
#define PROBE_TRIS		TRISAbits.TRISA0

// Main.
int main(void)
{
	// Digital Output.
	PROBE_LAT = 0;
	PROBE_TRIS = 0;
	PROBE_ANSEL = 0;
	PROBE_SLR = 0;
	
	// Internal Oscillator Frequency.
	// FRQ3.0 = 1 0 0 0 - 64MHz.
	// FRQ3.0 = 0 1 1 1 - 48MHz.
	// FRQ3.0 = 0 1 1 0 - 32MHz.
	// FRQ3.0 = 0 1 0 1 - 16MHz.
	// FRQ3.0 = 0 1 0 0 - 12MHz.
	// FRQ3.0 = 0 0 1 1 - 8MHz.
	// FRQ3.0 = 0 0 1 0 - 4MHz.
	// FRQ3.0 = 0 0 0 1 - 2MHz.
	// FRQ3.0 = 0 0 0 0 - 1MHz.
	OSCFRQbits.FRQ3 = 0;
	OSCFRQbits.FRQ2 = 0;
	OSCFRQbits.FRQ1 = 0;
	OSCFRQbits.FRQ0 = 0;
	// Frequency Tunning.
	// HFTUN5.0 = 1 1 1 1 1 1 - Maximum frequency.
	// HFTUN5.0 = 1 0 0 0 0 0 - Minimum frequency.
	OSCTUNE = 0b00000000;
	// Wait Frequency is stable.
	while(!OSCSTATbits.HFOR);
	
	while(1){
		PROBE_LAT ^= 1;
	}
	return(0);
}
Size : 50bytes.
Fosc = 1MHz - HFFRQ3.0 = 0 0 0 0 - HFTUN5.0 = 0 0 0 0 0 0
Fosc = 2MHz - HFFRQ3.0 = 0 0 0 1 - HFTUN5.0 = 0 0 0 0 0 0
Fosc = 4MHz - HFFRQ3.0 = 0 0 1 0 - HFTUN5.0 = 0 0 0 0 0 0
Fosc = 8MHz - HFFRQ3.0 = 0 0 1 1 - HFTUN5.0 = 0 0 0 0 0
Fosc = 12MHz - HFFRQ3.0 = 0 1 0 0 - HFTUN5.0 = 0 0 0 0 0 0
Fosc = 16MHz - HFFRQ3.0 = 0 1 0 1 - HFTUN5.0 = 0 0 0 0 0 0
Fosc = 32MHz - HFFRQ3.0 = 0 1 1 0 - HFTUN5.0 = 0 0 0 0 0
Fosc = 48MHz - HFFRQ3.0 = 0 1 1 1 - HFTUN5.0 = 0 0 0 0 0

06.2017