Configure the timer in MCC and select interrupt driven.

Once you Geneate the MCC code it will have dealt with initialising the timer ready for you to use it.

In your code
#include <definitions.h>			//Include all the MCC generated files so their fucntions can be called

//IN YOUR INITIALSE CODE
	//----- SETUP TIMER 5 -----
	//Used for: Heartbeat
	//Config done in MCC:
	//	Setup Timer:
	//		Enable interrupts: Yes
	//		Prescaller: 1:1
	//		Clock source: Internal peripheral clock
	//		Time: 1mS
	//	Setup interrupts:
	//		Priority (set for all): 3	//<<<Set as required for your application
	//		Subpriority (set for all): 0	//<<<Set as required for your application

	TMR5_CallbackRegister(TMR5_MyEventHandler, (uintptr_t)NULL);		//Register our callback function to be called when the timer interrupts
	TMR5_Start();				//Start the timer

//YOUR FUNCTION TO BE CALLED ON EACH TIMER ROLL OVER
//*********************************************
//*********************************************
//********** HEARTBEAT IRQ (Timer 5) **********
//*********************************************
//*********************************************
void TMR5_MyEventHandler(uint32_t status, uintptr_t context)
{
	
	//Toggle a pin:
	if (LATAbits.LATA6)
		LATAbits.LATA6 = 0;
	else
		LATAbits.LATA6 = 1;
	
}
Other timer functions

MCC will have created a few useful functions you can call – see the file it generated in: /config/default/peripheral/tmr/

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 *