PIC18F6722 - Timer2 - 8-bit.

0.Contents

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

// Timer2 - 8-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 level.
	// IPEN0 = 0 - Disable priority levels.
	RCONbits.IPEN = 0;

	// TMR2IP - Timer2 Priority bit.
	// TMR2IP0 = 1 - High priority.
	// TMR2IP0 = 0 - Low priority.
	IPR1bits.TMR2IP = 0;

	// T2OUTPS - Timer2 Postcale Select Bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale.
	// T2OUTPS3.0 = ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale.
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS0 = 0;
	//T2CKPS - Timer2 Prescale Select Bits.
	//T2CKPS1.0 = 1 0 - 1:16 Prescale value.
	//T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	//T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T2CONbits.T2CKPS1 = 0;
	T2CONbits.T2CKPS0 = 0;
	// Timer2 ON.
	T2CONbits.TMR2ON = 1;
	// Timer2 Overflow Interrupt bit.
	PIE1bits.TMR2IE = 1;

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

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(PIR1bits.TMR2IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer2.
		TMR2 = 0x00;
		// Timer2 Period.
		PR2 = 0x7f;
		// Clear Timer2 Interrupt Flag.
		PIR1bits.TMR2IF = 0;
	}
}

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

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

TMR2 = 0x00 - PR2 = 0xFF - T2OUTPS = 0 0 0 0 - T2CKPS = 0 0.
TMR2 = 0x00 - PR2 = 0x7F - T2OUTPS = 0 0 0 0 - T2CKPS = 0 0.

12.2017