PIC18F6722 - Timer0 - 8-bit/16-bit.

0.Contents

1.Timer0 8-bit - 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.

// Timer0 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;
	
	// TMR0IP - Timer0 Priority bit.
	// TMR0IP0 = 1 - High priority.
	// TMR0IP0 = 0 - Low priority.
	INTCON2bits.TMR0IP = 0;
	
	// T08BIT - Timer0 8-bit/16-bit.
	// T08BIT0 = 1 - 8-bit.
	// T08BIT0 = 0 - 16-bit.
	T0CONbits.T08BIT = 1;
	// T0CS - Timer0 Clock Source.
	// T0CS0 = 1 - Transition on T0CKI pin.
	// T0CS0 = 0 - Internal instruction cycle clock.
	T0CONbits.T0CS = 0;
	// T0SE - Timer0 Source Edge.
	// T0SE0 = 1 - Increment high-to-low transition on T0CKI pin.
	// T0SE0 = 0 - Increment low-to-high transition on T0CKI pin.
	T0CONbits.T0SE = 0;
	// PSA - Timer0 Prescaler Assignment bit.
	// PSA0 = 1 - Prescaler not assigned.
	// PSA0 = 0 - Prescaler is assigned.
	T0CONbits.PSA = 1;
	// T0PS2 - Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256.
	// T0PS2.0 = 1 1 0 - 1:128.
	// T0PS2.0 = 1 0 1 - 1:64.
	// T0PS2.0 = 1 0 0 - 1:32.
	// T0PS2.0 = 0 1 1 - 1:16.
	// T0PS2.0 = 0 1 0 - 1:8.
	// T0PS2.0 = 0 0 1 - 1:4.
	// T0PS2.0 = 0 0 0 - 1:2.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;
	// Timer0 ON.
	T0CONbits.TMR0ON = 1;
	// Timer0 Overflow Interrupt bit.
	INTCONbits.TMR0IE = 1;

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

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(INTCONbits.TMR0IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer0.
		TMR0L = 0x00;
		// Clear Timer0 Interrupt Flag.
		INTCONbits.TMR0IF = 0;
	}
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator @8MHz.
	OSCCON = 0b01110000;
	OSCTUNE = 0b00000000;
	while(!OSCCONbits.IOFS);
	
	ADCON1 = 0x0f;
	PROBE = 0;
	PROBE_TRIS = 0;
}
TMR0L = 0x00 - T0PS2.0 = 0 0 0 - PSA = 1.
TMR0L = 0x00 - T0PS2.0 = 1 1 1 - PSA = 0.
TMR0L = 0x7F - T0PS2.0 = 0 0 0 - PSA = 1.

2.Timer0 16-bit - 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.

// Timer0 16-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;
	
	// TMR0IP - Timer0 Priority bit.
	// TMR0IP0 = 1 - High priority.
	// TMR0IP0 = 0 - Low priority.
	INTCON2bits.TMR0IP = 0;
	
	// T08BIT - Timer0 8-bit/16-bit.
	// T08BIT0 = 1 - 8-bit.
	// T08BIT0 = 0 - 16-bit.
	T0CONbits.T08BIT = 0;
	// T0CS - Timer0 Clock Source.
	// T0CS0 = 1 - Transition on T0CKI pin.
	// T0CS0 = 0 - Internal instruction cycle clock.
	T0CONbits.T0CS = 0;
	// T0SE - Timer0 Source Edge.
	// T0SE0 = 1 - Increment high-to-low transition on T0CKI pin.
	// T0SE0 = 0 - Increment low-to-high transition on T0CKI pin.
	T0CONbits.T0SE = 0;
	// PSA - Timer0 Prescaler Assignment bit.
	// PSA0 = 1 - Prescaler not assigned.
	// PSA0 = 0 - Prescaler is assigned.
	T0CONbits.PSA = 1;
	// T0PS2 - Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256.
	// T0PS2.0 = 1 1 0 - 1:128.
	// T0PS2.0 = 1 0 1 - 1:64.
	// T0PS2.0 = 1 0 0 - 1:32.
	// T0PS2.0 = 0 1 1 - 1:16.
	// T0PS2.0 = 0 1 0 - 1:8.
	// T0PS2.0 = 0 0 1 - 1:4.
	// T0PS2.0 = 0 0 0 - 1:2.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;
	// Timer0 ON.
	T0CONbits.TMR0ON = 1;
	// Timer0 Overflow Interrupt bit.
	INTCONbits.TMR0IE = 1;

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

// Interrupt Service Routines.
void interrupt ISR(void)
{
	if(INTCONbits.TMR0IF)
	{
		// Toggle PROBE Output.
		PROBE ^= 1;
		// Timer0.
		TMR0 = 0x0000;
		// Clear Timer0 Interrupt Flag.
		INTCONbits.TMR0IF = 0;
	}
}

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

	ADCON1 = 0x0f;	
	PROBE = 0;
	PROBE_TRIS = 0;
}
TMR0 = 0x0000 - T0PS2.0 = 0 0 0 - PSA = 1.
TMR0 = 0x0000 - T0PS2.0 = 1 1 1 - PSA = 0.
TMR0 = 0x7FFF - T0PS2.0 = 0 0 0 - PSA = 1.

12.2017