PIC18F27K40 - Timer0 8-bit / 16-bit.

0.Contents

PIC18F Trainer : PIC18F27K40 @8MHz Internal oscillator.

1.TIMER0 8-bit Fosc / 4 Synchrone with PPS.

// Configuration register.
#pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ
#pragma config CLKOUTEN = OFF, 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).
// PIC18F27K40 - @8MHz Internal oscillator.

// TIMER0 8-bit Fosc / 4 Synchrone with PPS.

// MCU.RB0 -> PROBE A - Output on RB0.

// Definitions.
#define PROBE_ANSEL		ANSELBbits.ANSELB0
#define PROBE_LAT		LATBbits.LATB0
#define PROBE_TRIS		TRISBbits.TRISB0

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer 0 8-bit mode.
	T0CON0bits.T016BIT = 0;
	// Postscaler Divider.
	// T0OUTPS3.0 = 1 1 1 1 - 1:16.
	// ...
	// T0OUTPS3.0 = 0 0 0 0 - 1:1.
	T0CON0bits.T0OUTPS3 = 0;
	T0CON0bits.T0OUTPS2 = 0;
	T0CON0bits.T0OUTPS1 = 0;
	T0CON0bits.T0OUTPS0 = 0;
	// Clock Source.
	// T0CS7.5 = 1 0 1 - SOSC.
	// T0CS7.5 = 1 0 0 - LFINTOSC.
	// T0CS7.5 = 0 1 1 - HFINTOSC.
	// T0CS7.5 = 0 1 0 - Fosc / 4.
	// T0CS7.5 = 0 0 1 - T0CKIPPS Inverted.
	// T0CS7.5 = 0 0 0 - T0CKIPPS Non-inverted.
	T0CON1bits.T0CS2 = 0;
	T0CON1bits.T0CS1 = 1;
	T0CON1bits.T0CS0 = 0;
	// Input Asynchronization.
	T0CON1bits.T0ASYNC = 0;
	// Prescaler Rate.
	// T0CKPS3.0 = 1 1 1 1 - 1:32768.
	// ...
	// T0CKPS3.0 = 0 0 0 0 - 1:1.
	T0CON1bits.T0CKPS3 = 0;
	T0CON1bits.T0CKPS2 = 0;
	T0CON1bits.T0CKPS1 = 0;
	T0CON1bits.T0CKPS0 = 0;
	// Period Registers.
	TMR0H = 0x01;
	
	// Start timer0.
	T0CON0bits.T0EN = 1;
	
	while(1){
	}
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCFRQbits.HFFRQ = 0x03;
	// Wait Frequency is stable.
	while(!OSCSTATbits.HFOR);

	// Output Probe.
	PROBE_LAT = 0;
	PROBE_TRIS = 0;
	PROBE_ANSEL = 0;

	// PPS Sequence.
	PPSLOCK = 0x55;
	PPSLOCK = 0xAA;
	PPSLOCKbits.PPSLOCKED = 0x00;
	// TMR0 -> RB0 Output.
	RB0PPS = 0b00010011;
	PPSLOCK = 0x55;
	PPSLOCK = 0xAA;
	PPSLOCKbits.PPSLOCKED = 0x01;
}

2.TIMER0 8-bit HFINTOSC Asynchrone with PPS.

// Configuration register.
#pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ
#pragma config CLKOUTEN = OFF, 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).
// PIC18F27K40 - @8MHz Internal oscillator.

// TIMER0 8-bit HFINTOSC Asynchrone with PPS.

// MCU.RB0 -> PROBE A - Output on RB0.

// Definitions.
#define PROBE_ANSEL		ANSELBbits.ANSELB0
#define PROBE_LAT		LATBbits.LATB0
#define PROBE_TRIS		TRISBbits.TRISB0

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer 0 8-bit mode.
	T0CON0bits.T016BIT = 0;
	// Postscaler Divider.
	// T0OUTPS3.0 = 1 1 1 1 - 1:16.
	// ...
	// T0OUTPS3.0 = 0 0 0 0 - 1:1.
	T0CON0bits.T0OUTPS3 = 0;
	T0CON0bits.T0OUTPS2 = 0;
	T0CON0bits.T0OUTPS1 = 0;
	T0CON0bits.T0OUTPS0 = 0;
	// Clock Source.
	// T0CS7.5 = 1 0 1 - SOSC.
	// T0CS7.5 = 1 0 0 - LFINTOSC.
	// T0CS7.5 = 0 1 1 - HFINTOSC.
	// T0CS7.5 = 0 1 0 - Fosc / 4.
	// T0CS7.5 = 0 0 1 - T0CKIPPS Inverted.
	// T0CS7.5 = 0 0 0 - T0CKIPPS Non-inverted.
	T0CON1bits.T0CS2 = 0;
	T0CON1bits.T0CS1 = 1;
	T0CON1bits.T0CS0 = 1;
	// Input Asynchronization.
	T0CON1bits.T0ASYNC = 1;
	// Prescaler Rate.
	// T0CKPS3.0 = 1 1 1 1 - 1:32768.
	// ...
	// T0CKPS3.0 = 0 0 0 0 - 1:1.
	T0CON1bits.T0CKPS3 = 0;
	T0CON1bits.T0CKPS2 = 0;
	T0CON1bits.T0CKPS1 = 0;
	T0CON1bits.T0CKPS0 = 0;
	// Periode Registers.
	TMR0H = 0x01;
	
	// Start timer0.
	T0CON0bits.T0EN = 1;
	
	while(1){
	}
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCFRQbits.HFFRQ = 0x03;
	// Wait Frequency is stable.
	while(!OSCSTATbits.HFOR);

	// Output Probe.
	PROBE_LAT = 0;
	PROBE_TRIS = 0;
	PROBE_ANSEL = 0;

	// PPS Sequence.
	PPSLOCK = 0x55;
	PPSLOCK = 0xAA;
	PPSLOCKbits.PPSLOCKED = 0x00;
	// TMR0 -> RB0 Output.
	RB0PPS = 0b00010011;
	PPSLOCK = 0x55;
	PPSLOCK = 0xAA;
	PPSLOCKbits.PPSLOCKED = 0x01;
}

3.TIMER0 8-bit Fosc / 4 Synchrone with Software Interrupt.

// Configuration register.
#pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ
#pragma config CLKOUTEN = OFF, 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).
// PIC18F27K40 - @8MHz Internal oscillator.

// TIMER0 8-bit Fosc / 4 Synchrone with Software Interrupt.

// MCU.RB0 -> PROBE A - Output on RB0.

// Definitions.
#define PROBE_ANSEL		ANSELBbits.ANSELB0
#define PROBE_LAT		LATBbits.LATB0
#define PROBE_TRIS		TRISBbits.TRISB0

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer 0 8-bit mode.
	T0CON0bits.T016BIT = 0;
	// Postscaler Divider.
	// T0OUTPS3.0 = 1 1 1 1 - 1:16.
	// ...
	// T0OUTPS3.0 = 0 0 0 0 - 1:1.
	T0CON0bits.T0OUTPS3 = 0;
	T0CON0bits.T0OUTPS2 = 0;
	T0CON0bits.T0OUTPS1 = 0;
	T0CON0bits.T0OUTPS0 = 0;
	// Clock Source.
	// T0CS7.5 = 1 0 1 - SOSC.
	// T0CS7.5 = 1 0 0 - LFINTOSC.
	// T0CS7.5 = 0 1 1 - HFINTOSC.
	// T0CS7.5 = 0 1 0 - Fosc / 4.
	// T0CS7.5 = 0 0 1 - T0CKIPPS Inverted.
	// T0CS7.5 = 0 0 0 - T0CKIPPS Non-inverted.
	T0CON1bits.T0CS2 = 0;
	T0CON1bits.T0CS1 = 1;
	T0CON1bits.T0CS0 = 0;
	// Asynchronization.
	T0CON1bits.T0ASYNC = 0;
	// Prescaler Rate.
	// T0CKPS3.0 = 1 1 1 1 - 1:32768.
	// ...
	// T0CKPS3.0 = 0 0 0 0 - 1:1.
	T0CON1bits.T0CKPS3 = 0;
	T0CON1bits.T0CKPS2 = 0;
	T0CON1bits.T0CKPS1 = 0;
	T0CON1bits.T0CKPS0 = 0;
	// Periode Registers.
	TMR0H = 0x01;
	
	// Start timer0.
	T0CON0bits.T0EN = 1;
	
	// Clear & Enable Timer0 Overflow Interrupt.
	PIR0bits.TMR0IF = 0;
	PIE0bits.TMR0IE = 1;
	
	// Enable Global Interrupt.
	INTCONbits.GIE = 1;	
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void interrupt ISR(void)
{
	PROBE_LAT ^= 1;
	// Clear Timer0 Interrupt Flag.
	PIR0bits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCFRQbits.HFFRQ = 0x03;
	// Wait Frequency is stable.
	while(!OSCSTATbits.HFOR);

	// Output Probe.
	PROBE_LAT = 0;
	PROBE_TRIS = 0;
	PROBE_ANSEL = 0;
	
	// Interrupt Priority Enable.
	INTCONbits.IPEN = 1;
}

4.TIMER0 8-bit HFINTOSC Asynchrone with Software Interrupt.

// Configuration register.
#pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ
#pragma config CLKOUTEN = OFF, 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).
// PIC18F27K40 - @8MHz Internal oscillator.

// TIMER0 8-bit HFINTOSC Asynchrone with Software Interrupt.

// MCU.RB0 -> PROBE A - Output on RB0.

// Definitions.
#define PROBE_ANSEL		ANSELBbits.ANSELB0
#define PROBE_LAT		LATBbits.LATB0
#define PROBE_TRIS		TRISBbits.TRISB0

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer 0 8-bit mode.
	T0CON0bits.T016BIT = 0;
	// Postscaler Divider.
	// T0OUTPS3.0 = 1 1 1 1 - 1:16.
	// ...
	// T0OUTPS3.0 = 0 0 0 0 - 1:1.
	T0CON0bits.T0OUTPS3 = 0;
	T0CON0bits.T0OUTPS2 = 0;
	T0CON0bits.T0OUTPS1 = 0;
	T0CON0bits.T0OUTPS0 = 0;
	// Clock Source.
	// T0CS7.5 = 1 0 1 - SOSC.
	// T0CS7.5 = 1 0 0 - LFINTOSC.
	// T0CS7.5 = 0 1 1 - HFINTOSC.
	// T0CS7.5 = 0 1 0 - Fosc / 4.
	// T0CS7.5 = 0 0 1 - T0CKIPPS Inverted.
	// T0CS7.5 = 0 0 0 - T0CKIPPS Non-inverted.
	T0CON1bits.T0CS2 = 0;
	T0CON1bits.T0CS1 = 1;
	T0CON1bits.T0CS0 = 1;
	// Input Asynchronization.
	T0CON1bits.T0ASYNC = 1;
	// Prescaler Rate.
	// T0CKPS3.0 = 1 1 1 1 - 1:32768.
	// ...
	// T0CKPS3.0 = 0 0 0 0 - 1:1.
	T0CON1bits.T0CKPS3 = 0;
	T0CON1bits.T0CKPS2 = 0;
	T0CON1bits.T0CKPS1 = 0;
	T0CON1bits.T0CKPS0 = 0;
	// Period Registers.
	TMR0H = 0x01;
	
	// Start timer0.
	T0CON0bits.T0EN = 1;
	
	// Clear & Enable Timer0 Overflow Interrupt.
	PIR0bits.TMR0IF = 0;
	PIE0bits.TMR0IE = 1;
	
	// Enable Global Interrupt.
	INTCONbits.GIE = 1;	
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void interrupt ISR(void)
{
	PROBE_LAT ^= 1;
	// Clear Timer0 Interrupt Flag.
	PIR0bits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCFRQbits.HFFRQ = 0x03;
	// Wait Frequency is stable.
	while(!OSCSTATbits.HFOR);

	// Output Probe.
	PROBE_LAT = 0;
	PROBE_TRIS = 0;
	PROBE_ANSEL = 0;
	
	// Interrupt Priority Enable.
	INTCONbits.IPEN = 1;
}

06.2017