Setup Timers


#include <timer.h>

	//----- SETUP TIMER 4/5 -----
	//Used for: Heartbeat
	OpenTimer45(T4_ON & T4_GATE_OFF & T4_PS_1_1 & T4_SOURCE_INT, (INSTRUCTION_CLOCK_FREQUENCY / 1000));		//1mS irq, 1:1 prescaller
	WriteTimer45(0);
	ConfigIntTimer45(T4_INT_PRIOR_1 & T4_INT_ON);			//Interrupt priority level 1

	//----- SETUP TIMER 6 -----
	//Used for: Buzzer PWM
	//4000Hz = 250uS per complete PWM cycle
	T6CON = 0x8000;				//Timer on, 16bit, 1:1 (Fosc /2)
	PR6 = (INSTRUCTION_CLOCK_FREQUENCY / 8000);
	TMR6 = 0;

	_T6IP = 4;		 		//7(highest) to 1(lowest)
	_T6IF = 0;
	_T6IE = 0;

	//----- SETUP TIMER 7 -----
	//Used for: 
	//Trigger every 60uS
	T7CON = 0x8000;				//Timer on, 16bit, 1:1 (Fosc /2)
	PR7 = (60 * 40);			//40 cycles per 1uS
	TMR7 = 0;

	_T7IP = 3;		 		//7(highest) to 1(lowest)
	_T7IF = 0;
	_T7IE = 1;
Alternatively setup 32bit timer without the library

	//----- SETUP TIMER 4/5 -----
	//Used for: Heartbeat
	T5CONbits.TON = 0;			//Stop any 16-bit Timer3 operation
	T4CONbits.TON = 0;			//Stop any 16/32-bit Timer3 operation
	T4CONbits.T32 = 1;			//Enable 32-bit Timer mode
	T4CONbits.TCS = 0;			//Select internal instruction cycle clock
	T4CONbits.TGATE = 0;		//Disable Gated Timer mode
	T4CONbits.TCKPS = 0b00;		//Select 1:1 Prescaler
	TMR5 = 0x00;				//Clear 32-bit Timer (msw)
	TMR4 = 0x00;				//Clear 32-bit Timer (lsw)
	PR5 = (INSTRUCTION_CLOCK_FREQUENCY / 1000) >> 16;		//Load 32-bit period value (msw)
	PR4 = (INSTRUCTION_CLOCK_FREQUENCY / 1000) & 0xffff;	//Load 32-bit period value (lsw)
	_T5IP = 0x01;				//Set Timer Interrupt Priority Level - 7(highest) to 1(lowest)
	_T5IF = 0;					//Clear Timer Interrupt Flag
	_T5IE = 1;					//Enable Timer interrupt
	T4CONbits.TON = 1;			//Start 32-bit Timer

Timer Interrupt Functions


//Timer 45 Interrupt
void __attribute__((interrupt, auto_psv)) _T5Interrupt(void)
{
	_T5IF = 0;									 //Clear interrupt flag

//Timer 6 interrupt
void __attribute__((interrupt, auto_psv)) _T6Interrupt(void)
{
	_T6IF = 0;									 //Clear interrupt flag

//Timer 7 interrupt
void __attribute__((interrupt, auto_psv)) _T7Interrupt(void)
{
	_T7IF = 0;									 //Clear interrupt flag
	

 

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *