PIC18F6722 - Timer4 - 8-bit.

0.Contents

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

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

	// TMR4IP - Timer4 Priority bit.
	// TMR4IP0 = 1 - High priority.
	// TMR4IP0 = 0 - Low priority.
	IPR3bits.TMR4IP = 0;

	// T4OUTPS - Timer4 Postcale Select Bits.
	// T4OUTPS3.0 = 1 1 1 1 - 1:16 Postscale.
	// T4OUTPS3.0 = ...
	// T4OUTPS3.0 = 0 0 0 0 - 1:1 Postscale.
	T4CONbits.T4OUTPS3 = 0;
	T4CONbits.T4OUTPS2 = 0;
	T4CONbits.T4OUTPS1 = 0;
	T4CONbits.T4OUTPS0 = 0;
	//T4CKPS - Timer4 Prescale Select Bits.
	//T4CKPS1.0 = 1 0 - 1:16 Prescale value.
	//T4CKPS1.0 = 0 1 - 1:4 Prescale value.
	//T4CKPS1.0 = 0 0 - 1:1 Prescale value.
	T4CONbits.T4CKPS1 = 0;
	T4CONbits.T4CKPS0 = 0;
	// Timer4 ON.
	T4CONbits.TMR4ON = 1;
	// Timer4 Overflow Interrupt bit.
	PIE3bits.TMR4IE = 1;

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

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(PIR3bits.TMR4IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer4.
		TMR4 = 0x00;
		// Timer4 Period.
		PR4 = 0x00;
		// Clear Timer4 Interrupt Flag.
		PIR3bits.TMR4IF = 0;
	}
}

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

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

TMR4 = 0x00 - PR4 = 0xFF - T4OUTPS = 0 0 0 0 - T4CKPS = 0 0.
TMR4 = 0x00 - PR4 = 0x7F - T4OUTPS = 0 0 0 0 - T4CKPS = 0 0.

12.2017