PIC18F2620 - Timer0 8-bit / 16-bit.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.8-bit Mode 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(v1.45).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER0 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer0 8-bit mode.
	T0CONbits.T08BIT = 1;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is not assigned.
	T0CONbits.PSA = 1;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	// ...
	// T0PS2.0 = 0 0 0 - 1:2 Prescale value.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

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

// Interrupt Service Routine.
void interrupt ISR(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer0 8-bit.
	TMR0L = 0x00;
	// Timer0 Clear Interrupt Flag.
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// 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;
}
PSA = 1 - TMR0L = 0x00
PSA = 1 - TMR0L = 0xFF

2.8-bit Mode 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(v1.45).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER0 8-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer0 8-bit mode.
	T0CONbits.T08BIT = 1;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is assigned.
	T0CONbits.PSA = 0;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	// ...
	// T0PS2.0 = 0 0 0 - 1:2 Prescale value.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

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

// Interrupt Service Routine.
void interrupt ISR(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer0 8-bit.
	TMR0L = 0x00;
	// Timer0 Clear Interrupt Flag.
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// 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;
}
PSA = 0 - T0PS2.0 = 0 0 0 - TMR0L = 0x00
PSA = 0 - T0PS2.0 = 0 0 0 - TMR0L = 0xFF
PSA = 0 - T0PS2.0 = 1 1 1 - TMR0L = 0x00
PSA = 0 - T0PS2.0 = 1 1 1 - TMR0L = 0xFF

3.16-bit Mode 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(v1.45).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER0 16-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer0 16-bit mode.
	T0CONbits.T08BIT = 0;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is not assigned.
	T0CONbits.PSA = 1;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	// ...
	// T0PS2.0 = 0 0 0 - 1:2 Prescale value.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

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

// Interrupt Service Routine.
void interrupt ISR(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer0 16-bit.
	TMR0 = 0x0000;
	// Timer0 Clear Interrupt Flag.
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// 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;
}
PSA = 1 - TMR0 = 0x0000
PSA = 1 - TMR0 = 0xFFFF

4.16-bit Mode 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(v1.45).
// PIC18F2620 - @8MHz Internal oscillator.

// TIMER0 16-bit with Software Interrupt.

// MCU.RA0 -> PROBE_A.

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

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	// Timer0 16-bit mode.
	T0CONbits.T08BIT = 0;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is assigned.
	T0CONbits.PSA = 0;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	// ...
	// T0PS2.0 = 0 0 0 - 1:2 Prescale value.
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

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

// Interrupt Service Routine.
void interrupt ISR(void)
{
	// Toggle Output.
	PROBE_A ^= 1;
	// Timer0 16-bit.
	TMR0 = 0x0000;
	// Timer0 Clear Interrupt Flag.
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// 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;
}
PSA = 0 - T0PS2.0 = 0 0 0 - TMR0 = 0x0000
PSA = 0 - T0PS2.0 = 0 0 0 - TMR0 = 0xFFFF
PSA = 0 - T0PS2.0 = 1 1 1 - TMR0 = 0x0000
PSA = 0 - T0PS2.0 = 1 1 1 - TMR0 = 0xFFFF

06.2018