PIC24FJ256Gx110 - Timer2/4 - 16-Bit.

0.Contents

PIC24FJGA/GB MiniTrainer : PIC24FJ256GB110.

1.Timer2/4 16-Bit with interrupt.

// Configuration register for PIC24FJ256GA106.
//#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
//#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
//#pragma config POSCMOD = NONE, IOL1WAY = ON,  OSCIOFNC = OFF
//#pragma config FCKSM = CSDCMD, FNOSC = LPRC, IESO = ON
//#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM
// Configuration register for PIC24FJ256GB106.
#pragma config WDTPS = PS32768, FWPSA = PR128, WINDIS = OFF, FWDTEN = ON
#pragma config ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#pragma config POSCMOD = EC, DISUVREG = OFF, IOL1WAY = ON, OSCIOFNC = OFF
#pragma config FCKSM = CSDCMD, FNOSC = PRI, PLL_96MHZ = ON
#pragma config PLLDIV = NODIV, IESO = ON
#pragma config WPFP = WPFP511, WPDIS = WPDIS, WPCFG = WPCFGDIS, WPEND = WPENDMEM

#include <xc.h>
#define FOSC	(24000000UL)
// PIC24FJ256Gx106 - Compile with XC16(v1.33).
// External Oscillator - PRI-EC@24MHz.

// Timer2/4 - 16-Bit Timer.

// PR2 = (Fosc / 2) / (Fout * 2) / Prescaler.
// PR2 = (24000000/2)/(1000*2)/1 = 6000.

// Fout = (Fosc / 2) / (PR2 * 2) / Prescaler.
// Fout = (24000000/2)/(6000*2)/1 = 1000Hz.

// RB0 -> Oscilloscope Chanel A.

// Definitions.
#define PROBE		LATBbits.LATB0
#define PROBE_TRIS	TRISBbits.TRISB0

// Main.
int main(void)
{
	PROBE = 0;
	PROBE_TRIS = 0;
	
	// Timer2/4.
	T2CON = 0x0000;
	TMR2  = 0x0000;

	// Period Register.
	PR2 = 6000;

	// TCKPS - Timer2/4 Input Clock Prescale Select bits.
	// TCKPS1.0 = 1 1 - 1:256.
	// TCKPS1.0 = 1 0 - 1:64.
	// TCKPS1.0 = 0 1 - 1:8.
	// TCKPS1.0 = 0 0 - 1:1.
	T2CONbits.TCKPS1 = 0;
	T2CONbits.TCKPS0 = 0;

	// TON - Timer2/4 Start.
	T2CONbits.TON = 1;

	// Enable Interrupt.
	IFS0bits.T2IF = 0;
	IEC0bits.T2IE = 1;
	//IFS1bits.T4IF = 0;
	//IEC1bits.T4IE = 1;

	while(1){
	}
	return(0);
}

// ISR.
void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void)
{
	PROBE ^= 1;
	IFS0bits.T2IF = 0;
	//IFS1bits.T4IF = 0;
}
TCKPS1.0 = 0 0
TCKPS1.0 = 0 1
TCKPS1.0 = 1 0
TCKPS1.0 = 1 1

03.2018