PIC18F2620 - Timer2 8-bit.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.8-bit Mode no postscaler & no prescaler.

// 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.

// TIMER2 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

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

	// Timer2 Postscaler select bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale value.
	// ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale value.
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS0 = 0;
	// Timer2 Prescaler select bits.
	// T2CKPS1.0 = 1 x - 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 Start.
	T2CONbits.TMR2ON = 1;

	// Timer2 Overflow Interrupt low priority.
	IPR1bits.TMR2IP = 0;
	// Timer2 Overflow Interrupt enable.
	PIE1bits.TMR2IE = 1;
	
	// Global Interrupt Enable.
	INTCONbits.GIE = 1;
	// Peripheral Interrupt Enable.
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void __interrupt(low_priority) timer2_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer2 8-bit.
	TMR2 = 0x00;
	// Timer2 Clear Interrupt Flag.
	PIR1bits.TMR2IF = 0;
}
T2OUTPS3.0 = 0 0 0 0 - T2CKPS1.0 = 0 0 - TMR2 = 0x00
T2OUTPS3.0 = 0 0 0 0 - T2CKPS1.0 = 0 0 - TMR2 = 0xff

2.8-bit Mode no postscaler & with prescaler.

// 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.

// TIMER2 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

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

	// Timer2 Postscaler select bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale value.
	// ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale value.
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS0 = 0;
	// Timer2 Prescaler select bits.
	// T2CKPS1.0 = 1 x - 1:16 Prescale value.
	// T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	// T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T2CONbits.T2CKPS1 = 1;
	T2CONbits.T2CKPS0 = 1;
	// Timer2 Start.
	T2CONbits.TMR2ON = 1;

	// Timer2 Overflow Interrupt low priority.
	IPR1bits.TMR2IP = 0;
	// Timer2 Overflow Interrupt enable.
	PIE1bits.TMR2IE = 1;
	
	// Global Interrupt Enable.
	INTCONbits.GIE = 1;
	// Peripheral Interrupt Enable.
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void __interrupt(low_priority) timer2_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer2 8-bit.
	TMR2 = 0x00;
	// Timer2 Clear Interrupt Flag.
	PIR1bits.TMR2IF = 0;
}
T2OUTPS3.0 = 0 0 0 0 - T2CKPS1.0 = 1 x - TMR2 = 0x00
T2OUTPS3.0 = 0 0 0 0 - T2CKPS1.0 = 1 x - TMR2 = 0xff

3.8-bit Mode with postscaler & no prescaler.

// 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.

// TIMER2 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

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

	// Timer2 Postscaler select bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale value.
	// ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale value.
	T2CONbits.T2OUTPS3 = 1;
	T2CONbits.T2OUTPS2 = 1;
	T2CONbits.T2OUTPS1 = 1;
	T2CONbits.T2OUTPS0 = 1;
	// Timer2 Prescaler select bits.
	// T2CKPS1.0 = 1 x - 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 Start.
	T2CONbits.TMR2ON = 1;

	// Timer2 Overflow Interrupt low priority.
	IPR1bits.TMR2IP = 0;
	// Timer2 Overflow Interrupt enable.
	PIE1bits.TMR2IE = 1;
	
	// Global Interrupt Enable.
	INTCONbits.GIE = 1;
	// Peripheral Interrupt Enable.
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void __interrupt(low_priority) timer2_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer2 8-bit.
	TMR2 = 0x00;
	// Timer2 Clear Interrupt Flag.
	PIR1bits.TMR2IF = 0;
}
T2OUTPS3.0 = 1 1 1 1 - T2CKPS1.0 = 0 0 - TMR2 = 0x00
T2OUTPS3.0 = 1 1 1 1 - T2CKPS1.0 = 0 0 - TMR2 = 0xff

4.8-bit Mode with postscaler & with prescaler.

// 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.

// TIMER2 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

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

	// Timer2 Postscaler select bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale value.
	// ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale value.
	T2CONbits.T2OUTPS3 = 1;
	T2CONbits.T2OUTPS2 = 1;
	T2CONbits.T2OUTPS1 = 1;
	T2CONbits.T2OUTPS0 = 1;
	// Timer2 Prescaler select bits.
	// T2CKPS1.0 = 1 x - 1:16 Prescale value.
	// T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	// T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T2CONbits.T2CKPS1 = 1;
	T2CONbits.T2CKPS0 = 1;
	// Timer2 Start.
	T2CONbits.TMR2ON = 1;

	// Timer2 Overflow Interrupt low priority.
	IPR1bits.TMR2IP = 0;
	// Timer2 Overflow Interrupt enable.
	PIE1bits.TMR2IE = 1;
	
	// Global Interrupt Enable.
	INTCONbits.GIE = 1;
	// Peripheral Interrupt Enable.
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void __interrupt(low_priority) timer2_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer2 8-bit.
	TMR2 = 0x00;
	// Timer2 Clear Interrupt Flag.
	PIR1bits.TMR2IF = 0;
}
T2OUTPS3.0 = 1 1 1 1 - T2CKPS1.0 = 1 x - TMR2 = 0x00
T2OUTPS3.0 = 1 1 1 1 - T2CKPS1.0 = 1 x - TMR2 = 0xff

08.2018