PIC18F6722 - Timer1 - 16-bit.

0.Contents

1.Timer1 - 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.

// Timer1 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;

	// TMR1IP - Timer1 Priority Bit.
	// TMR1IP0 = 1 - High priority.
	// TMR1IP0 = 0 - Low priority.
	IPR1bits.TMR1IP = 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.
	T1CONbits.RD16 = 0;
	// T1RUN - Timer1 Clock Source.
	// T1RUN0 = 1 - Device clock from Timer1 oscillator.
	// T1RUN0 = 0 - Device clock from another source.
	T1CONbits.T1RUN = 0;
	// T1CKPS1 - Timer1 Prescaler Select Bits.
	// T1CKPS1.0 = 1 1 - 1:8 Prescale value.
	// T1CKPS1.0 = 1 0 - 1:4 Prescale value.
	// T1CKPS1.0 = 0 1 - 1:2 Prescale value.
	// T1CKPS1.0 = 0 0 - 1:1 Prescale value.
	T1CONbits.T1CKPS1 = 0;
	T1CONbits.T1CKPS0 = 0;
	// T1OSCEN - Timer1 Oscillator Enable Bit.
	// T1OSCEN0 = 1 - Timer1 enabled.
	// T1OSCEN0 = 0 - Timer1 disabled.
	T1CONbits.T1OSCEN = 0;
	// T1SYNC - Timer1 External Clock Input Synchronization.
	// T1SYNC0 = 1 - when TMR1CS = 1 do not synchronize external clock.
	// T1SYNC0 = 0 - when TMR1CS = 0 synchronize external clock.
	T1CONbits.T1SYNC = 0;
	// TMR1CS - Timer1 Clock Source.
	// TMR1CS0 = 1 - External clock from pin T1OSO/RC0.
	// TMR1CS0 = 0 - Internal clock FOSC/4.
	T1CONbits.TMR1CS = 0;
	// Timer1 ON.
	T1CONbits.TMR1ON = 1;
	// Timer1 Overflow Interrupt bit.
	PIE1bits.TMR1IE = 1;

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

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(PIR1bits.TMR1IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer1.
		TMR1 = 0x0000;
		// Clear Timer1 Interrupt Flag.
		PIR1bits.TMR1IF = 0;
	}
}

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

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

TMR1 = 0x0000 - T1CKPS1.0 = 0 0.
TMR1 = 0x0000 - T1CKPS1.0 = 1 1.
TMR1 = 0x7FFF - T1CKPS1.0 = 0 0.

12.2017