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

// TIMER1 16-bit with Software Interrupt.
// 8-bit Read / Write Mode.

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

	// Timer1 16-bit R/W mode.
	T1CONbits.RD16 = 0;
	// 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;
	// Timer1 Oscillator enable.
	T1CONbits.T1OSCEN = 0;
	// Timer1 External Clock Input Synchronization.
	T1CONbits.T1SYNC = 0;
	// Timer1 Internal clock (Fosc / 4).
	T1CONbits.TMR1CS = 0;
	
	// Timer1 Start.
	T1CONbits.TMR1ON = 1;

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

// Interrupt Service Routine.
void __interrupt(low_priority) timer1_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer1 16-bit.
	TMR1H = 0x00;
	TMR1L = 0x00;
	// Timer1 Clear Interrupt Flag.
	PIR1bits.TMR1IF = 0;
}
T1CKPS1.0 = 0 0 - TMR1H = 0x00 - TMR1L = 0x00
T1CKPS1.0 = 0 0 - TMR1H = 0xff - TMR1L = 0xfe
T1CKPS1.0 = 1 1 - TMR1H = 0x00 - TMR1L = 0x00
T1CKPS1.0 = 1 1 - TMR1H = 0xff - TMR1L = 0xfe

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.

// TIMER1 16-bit with Software Interrupt.
// 16-bit Read / Write Mode.

// MCU.RA0 -> PROBE_A.

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

// Interrupt Service Routine.
void __interrupt(low_priority) timer1_isr(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer1 16-bit.
	TMR1 = 0x0000;
	// Timer1 Clear Interrupt Flag.
	PIR1bits.TMR1IF = 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;

	// Timer1 16-bit R/W mode.
	T1CONbits.RD16 = 1;
	// 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;
	// Timer1 Oscillator enable.
	T1CONbits.T1OSCEN = 0;
	// Timer1 External Clock Input Synchronization.
	T1CONbits.T1SYNC = 0;
	// Timer1 Internal clock (Fosc / 4).
	T1CONbits.TMR1CS = 0;
	
	// Timer1 Start.
	T1CONbits.TMR1ON = 1;

	// Timer1 Overflow Interrupt low priority.
	IPR1bits.TMR1IP = 0;
	// Timer1 Overflow Interrupt enable.
	PIE1bits.TMR1IE = 1;
	
	// Global Interrupt Enable.
	INTCONbits.GIE = 1;
	// Peripheral Interrupt Enable.
	INTCONbits.PEIE = 1;
	while(1){
	}
	return(0);
}
T1CKPS1.0 = 0 0 - TMR1 = 0x0000
T1CKPS1.0 = 0 0 - TMR1 = 0xfffe
T1CKPS1.0 = 1 1 - TMR1 = 0x0000
T1CKPS1.0 = 1 1 - TMR1 = 0xfffe

08.2018