PIC18F2620 - Timer3 16-bit.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.16-bit - 8-bit Read Write Mode.

// Configuration register.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO67
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v2.00).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER3 16-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

// Definitions.
#define PROBE_A			LATAbits.LATA0
#define PROBE_A_TRIS	TRISAbits.TRISA0

// Interrupt Service Routine.
void __interrupt(low_priority) timer3_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer3 16-bit.
	TMR3H = 0x00;
	TMR3L = 0x00;
	// Timer3 Clear Interrupt Flag.
	PIR2bits.TMR3IF = 0;
}

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// Output Probe.
	PROBE_A = 0;
	PROBE_A_TRIS  = 0;
	// Interrupt Priority Disable.
	RCONbits.IPEN = 0;

	// Timer3 8-bit R/W mode.
	T3CONbits.RD16 = 0;
	// Timer3 & Timer1 CCPx enable bits.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 0;
	// 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;
	// Timer3 External Clock Input Synchronization.
	T3CONbits.T3SYNC = 0;
	// Timer3 Internal clock (Fosc / 4).
	T3CONbits.TMR3CS = 0;

	// Timer3 Start.
	T3CONbits.TMR3ON = 1;

	// Timer3 Overflow Interrupt low priority.
	IPR2bits.TMR3IP = 0;
	// Timer3 Overflow Interrupt enable.
	PIE2bits.TMR3IE = 1;

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

2.16-bit - 16-bit Read Write Mode.

// Configuration register.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO67
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v2.00).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER3 16-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

// Definitions.
#define PROBE_A			LATAbits.LATA0
#define PROBE_A_TRIS	TRISAbits.TRISA0

// Interrupt Service Routine.
void __interrupt(low_priority) timer3_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer3 16-bit.
	TMR3 = 0x0000;
	// Timer3 Clear Interrupt Flag.
	PIR2bits.TMR3IF = 0;
}

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// Output Probe.
	PROBE_A = 0;
	PROBE_A_TRIS  = 0;
	// Interrupt Priority Disable.
	RCONbits.IPEN = 0;

	// Timer3 16-bit R/W mode.
	T3CONbits.RD16 = 1;
	// Timer3 & Timer1 CCPx enable bits.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 0;
	// 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;
	// Timer3 External Clock Input Synchronization.
	T3CONbits.T3SYNC = 0;
	// Timer3 Internal clock (Fosc / 4).
	T3CONbits.TMR3CS = 0;

	// Timer3 Start.
	T3CONbits.TMR3ON = 1;

	// Timer3 Overflow Interrupt low priority.
	IPR2bits.TMR3IP = 0;
	// Timer3 Overflow Interrupt enable.
	PIE2bits.TMR3IE = 1;

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

08.2018