text: Objective
  A timer interrupt is often used to launch an Interrupt Service Routine (ISR) that may toggle a port pin or some other function. Prescalers can be used to extend the time delay although the timer prescale limits may not produce a large enough delay. This is where a tick counter or a counter that increments on every timer overflow can be useful for creating long delays. This can be included in the ISR but within the MPLAB® Code Configurator (MCC) generated driver code for timers is a callback function that handles this tick counter automatically. By setting a callback counter value during the MCC driver setup, longer delays are easily established. This project shows how to control the Flash rate of an LED using a callback function. XC8., the low voltage programming must be enabled by checking the Low-voltage programming Enable box.
  4
  Timer4 Setup
  Add the TMR4 peripheral to the project from the Device Resources area of MCC. To do that, scroll down to the timer entry and expand the list by clicking on the arrow. Now, double click on the TMR4 entry to add it to the Project Resources list. Then, click on TMR4 to open the Timer4 configuration setup screen.
  - Check the Enable Timer box.
  - Select Clock Source FOSC/4, Postscaler 1:1, and Prescaler 1:64.
  - Set Timer Period value to 9.088 ms.
  - Set External Reset Source to T4IN and Control Mode to Roll over pulse.
  - Set Start/Reset Option to Software Control (this will inhibit hardware reset of timer).
  - Check the Enable Timer Interrupt box.
  To extend the Timer4 based delay, software overflows will use a callback function. The overflow of TMR4 will trigger an interrupt and the MCC generated TMR4 driver code will count up to the Callback Function Rate (CFR) value of events, before calling a predefined interrupt function that will then implement the ISR in the main.c file (that will be created in step 7).
  To configure the callback value, enter "11" in the CFR field. This will give us about a 100 ms rate (11 × 9.088 ms = 99.968 ms).
  5
  Pin Module Setup
  Click on Pin Module in the Project Resources area. This will open up the Pin Module and the Pin Management Grid. Click on the RC5 Output row blue lock to turn it green.
  Note: The T4IN pin will automatically be selected and show a green lock from the TMR4 setup.
  Rename the I/O pin
  In the Pin Module, rename the pin RC5 to "IO_LED_D7". Only the Output box should be checked.
  6
  Generate Driver Code
  Click on Generate under Project Resources to have the MCC create the drivers and a base main.c file for the project.
  7
  main.c
  The main.c file needs to be updated to handle the callback function using the function pointers generated by the MCC drivers. The function pointers allow main.c to handle all operations without having to modify the driver code.
  The first modification is to add void myTimer4ISR(void); right after the #include mcc_generated_files/mcc.h.
  #include "mcc_generated_files/mcc.h" void myTimer4ISR(void);
  This defines the name of the ISR that will be created to control the LED.
  Now, the Timer4 driver default Interrupt Handler has to be connected to the ISR defined in main.c.. This is handled by adding the line TMR4_SetInterruptHandler (myTimer4ISR);, right after the SYSTEM_Initialize function.
  // initialize the device SYSTEM_Initialize(); TMR4_SetInterruptHandler (myTimer4ISR); //Define interrupt Handler
  Remove the double backslash comment markers from Global and Peripheral Interrupt Enable functions:
  // INTERRUPT_GlobalInterruptEnable();
  // INTERRUPT_PeripheralInterruptEnable();
  This action will include a call to the predefined macros and enable interrupts.
  // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable();
  Finally, define what the ISR does when the number of callbacks is complete and the interrupt is processed. For this, a macro that was created in the MCC generated pin_manager.h file is used. The macro selected will toggle the LED from off to on and on to off on each ISR call. Add the myTimer4ISR ISR after the while(1) loop. This will complete the modifications to main.c required for this project.
  while (1) { // Add your application code } } void myTimer4ISR(void){ IO_LED_D7_Toggle(); //Control LED } /**bf that the callback function is working and controlling the blink rate of the LED. To prove that to yourself, go back to the TMR4 setup window in MCC and change the callback to a larger value of 55, Generate the drivers again, then Make and Program the device. This will flash the LED at the slower rate of a half-second on/off.
  Analysis
  Using the callback function generated in the TMR4 driver code simplifies creating interrupt delays. It reduces the ISR in your main code to just the action you want to implement when the time delay (or the number of interrupts) has occurred.
  Conclusions
  Try different callback functions and also modify the ISR to do other things, such as possibly controlling more than one LED. These modifications will help you better understand how useful callback functions can be when using interrupts.