PIC24FJ256Gx110 - Timer1 - 16-Bit.

0.Contents

PIC24FJGA/GB MiniTrainer : PIC24FJ256GB110.

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

// Timer1 - 16-Bit Timer.

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

// Fout = (Fosc / 2) / (PR1 * 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;
	
	// Timer1.
	T1CON = 0x0000;
	TMR1  = 0x0000;

	// Period Register.
	PR1 = 6000;

	// TCKPS - Timer1 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.
	T1CONbits.TCKPS1 = 0;
	T1CONbits.TCKPS0 = 0;

	// TON - Timer Start.
	T1CONbits.TON = 1;

	// Enable Interrupt.
	IFS0bits.T1IF = 0;
	IEC0bits.T1IE = 1;

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

// ISR.
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
	PROBE ^= 1;
	IFS0bits.T1IF = 0;
}
TCKPS1.0 = 0 0
TCKPS1.0 = 0 1
TCKPS1.0 = 1 0
TCKPS1.0 = 1 1

2.Timer1 16-Bit Asynchronous counter 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.

// Timer1 - 16-Bit Asynchronous Counter.

// PR1 = (T1CK / 2) / (Fout * 2) / Prescaler.
// PR1 = (1000/2)/(250*2)/1 = 1.

// Fout = (T1CK / 2) / (PR1 * 2) / Prescaler.
// Fout = (1000/2)/(1*2)/1 = 250Hz.

// RC14 -> Input External Signal - 1kHz.
// RC14 -> Oscilloscope Chanel A.
// RB0  -> Oscilloscope Chanel B.

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

// Main.
int main(void)
{
	PROBE = 0;
	PROBE_TRIS = 0;
	
	// Timer1.
	T1CON = 0x0000;
	TMR1  = 0x0000;

	// Period Register.
	PR1 = 0x0001;

	// TCKPS - Timer1 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.
	T1CONbits.TCKPS1 = 0;
	T1CONbits.TCKPS0 = 0;

	// TCS - Timer1 Clock Source Select bit.
	// TCS0 = 1 - External Clock from T1CK.
	// TCS0 = 0 - Internal Clock (Fosc/2).
	T1CONbits.TCS = 1;
	
	// TON - Timer Start.
	T1CONbits.TON = 1;

	// Enable Interrupt.
	IFS0bits.T1IF = 0;
	IEC0bits.T1IE = 1;

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

// ISR.
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
	PROBE ^= 1;
	IFS0bits.T1IF = 0;
}
T1CK ~1kHz - TCKPS1.0 = 0 0

3.Timer1 16-Bit Synchronous counter 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.

// Timer1 - 16-Bit Synchronous Counter.

// PR1 = (T1CK / 2) / (Fout * 2) / Prescaler.
// PR1 = (1000/2)/(250*2)/1 = 1.

// Fout = (T1CK / 2) / (PR1 * 2) / Prescaler.
// Fout = (1000/2)/(1*2)/1 = 250Hz.

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

// Main.
int main(void)
{
	PROBE = 0;
	PROBE_TRIS = 0;
	
	// Timer1.
	T1CON = 0x0000;
	TMR1  = 0x0000;

	// Period Register.
	PR1 = 0x0001;

	// TCKPS - Timer1 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.
	T1CONbits.TCKPS1 = 0;
	T1CONbits.TCKPS0 = 0;

	// TSYNC - Timer1 External Clock Synchronization.
	// TSYNC0 = 1 - Synchronize external clock input.
	// TSYNC0 = 0 - Do not synchronise external clock input.
	T1CONbits.TSYNC = 1;
	
	// TCS - Timer1 Clock Source Select bit.
	// TCS0 = 1 - External Clock from T1CK.
	// TCS0 = 0 - Internal Clock (Fosc/2).
	T1CONbits.TCS = 1;
	
	// TON - Timer Start.
	T1CONbits.TON = 1;

	// Enable Interrupt.
	IFS0bits.T1IF = 0;
	IEC0bits.T1IE = 1;

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

// ISR.
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
	PROBE ^= 1;
	IFS0bits.T1IF = 0;
}

03.2018