PIC18F6722 - Timer3 - 16-bit.

0.Contents

1.Timer3 - Timer Overflow Interrupt.

// Configuration registers.
#pragma config FCMEN = ON, IESO = OFF, OSC = INTIO7
#pragma config BOREN = SBORDIS, BORV = 3, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config LPT1OSC = OFF, MCLRE = ON, CCP2MX = PORTC
#pragma config STVREN = ON, LVP = ON, BBSIZ = BB2K, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CP4 = OFF, CP5 = OFF, CP6 = OFF, CP7 = OFF
#pragma config CPB = OFF, CPD = OFF
#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
#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>
// PIC18F6722 - Compile with XC8(v1.44).
// Internal Oscillator @8MHz.

// Timer3 16-bit - Timer Overflow Interrupt.
// RA0(24) -> Oscilloscope Probe.

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

// Function prototypes.
void mcu_initialize(void);

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

	// IPEN - Interrupt Priority Bit.
	// IPEN0 = 1 - Enable priority levels.
	// IPEN0 = 0 - Disable priority levels.
	RCONbits.IPEN = 0;

	// TMR3IP - Timer3 Priority Bit.
	// TMR3IP0 = 1 - High priority.
	// TMR3IP0 = 0 - Low priority.
	IPR2bits.TMR3IP = 0;
	// T3CCP - Timer3 and Timer1 CCPx Enable Bit.
	// T3CCP6.3 = 1 1 - Timer3 and timer4 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	// T3CCP6.3 = 1 0 - Timer3 and timer4 are the clock sources for ECCP3, CCP4 & CPP5,
	//                  Timer1 and timer2 are the clock sources for ECCP1, ECCP2,
	// T3CCP6.3 = 0 1 - Timer3 and timer4 are the clock sources for ECCP2, ECCP3, CCP4 & CCP5.
	//                  Timer1 and timer2 are the clock sources for ECCP1.
	// T3CCP6.3 = 0 0 - Timer1 and timer2 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 0;
	// RD16 - 16-bit Read/Write Mode.
	// RD16.0 = 1 - Enable read/write 16-bit in one operation.
	// RD16.0 = 0 - Enable read/write  8-bit in one operation.
	T3CONbits.RD16 = 0;
	// T3CKPS1 - Timer3 Prescaler Select Bits.
	// T3CKPS1.0 = 1 1 - 1:8 Prescale value.
	// T3CKPS1.0 = 1 0 - 1:4 Prescale value.
	// T3CKPS1.0 = 0 1 - 1:2 Prescale value.
	// T3CKPS1.0 = 0 0 - 1:1 Prescale value.
	T3CONbits.T3CKPS1 = 0;
	T3CONbits.T3CKPS0 = 0;
	// T3SYNC - Timer3 External Clock Input Synchronization.
	// T3SYNC0 = 1 - when TMR1CS = 1 do not synchronize external clock.
	// T3SYNC0 = 0 - when TMR1CS = 0 synchronize external clock.
	T3CONbits.T3SYNC = 0;
	// TMR3CS - Timer3 Clock Source.
	// TMR3CS0 = 1 - External clock from pin T1OSO/RC0.
	// TMR3CS0 = 0 - Internal clock FOSC/4.
	T3CONbits.TMR3CS = 0;
	// Timer3 ON.
	T3CONbits.TMR3ON = 1;
	// Timer3 Overflow Interrupt bit.
	PIE2bits.TMR3IE = 1;

	// Enable Global Interrupt.
	INTCONbits.GIE = 1;
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(PIR2bits.TMR3IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer3.
		TMR3 = 0x0000;
		// Clear Timer3 Interrupt Flag.
		PIR2bits.TMR3IF = 0;
	}
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator @8MHz.
	OSCCON = 0b01110000;
	OSCTUNE = 0b00000000;
	while(!OSCCONbits.IOFS);

	ADCON1 = 0x0f;
	PROBE = 0;
	PROBE_TRIS = 0;
}

TMR3 = 0x0000 - T3CKPS1.0 = 0 0.
TMR3 = 0x0000 - T3CKPS1.0 = 1 1.
TMR3 = 0x7FFF - T3CKPS1.0 = 0 0.

12.2017