Interrupt Delay Using Callback Function in MCC

Last modified by Microchip on 2023/11/09 09:03

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.

Resource Materials

Hardware

Software

Exercise Files

Connection Diagram

The Curiosity board has four LEDs prewired to the I/O pins shown below. This project controls the D7 LED.

Hardware FunctionPinSetting
IO_LED_D4RA5 (2)Output
IO_LED_D5RA1 (18)Output
IO_LED_D6RA2 (17)Output
IO_LED_D7RC5 (5)Output

Curiosity LEDs

Procedure

Create a Project

Create a new project and select the PIC16F1619 along with the Curiosity board and MPLAB XC8.


Launch MCC

Open MCC under the Tools > Embedded menu of MPLAB X IDE.

MCC Launch


System Setup

From Project Resources, choose System Module to open the System Setup window within MCC.

  1. In the clock settings, make sure you select INTOSC.
  2. Select the system clock FOSC.
  3. Set Internal Clock to 4MHz_HF setting.
  4. Check the PLL Enabled box.
  5. The Curiosity board uses a Programmer/Debugger On-Board (PKOB) and uses a low voltage program method to program the MCU. Therefore, the low voltage programming must be enabled by checking the Low-voltage programming Enable box.

Project Resources


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.

Timer 4 Setup

  1. Check the Enable Timer box.
  2. Select Clock Source FOSC/4Postscaler 1:1, and Prescaler 1:64.
  3. Set Timer Period value to 9.088 ms.
  4. Set External Reset Source to T4IN and Control Mode to Roll over pulse.
  5. Set Start/Reset Option to Software Control (this will inhibit hardware reset of timer).
  6. 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).

Callback Function Rate

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).


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.
 

I/O 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.

Renaming I/O

Note: The T4IN pin will automatically be selected and show a green lock from the TMR4 setup.


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.


Edit the generated main.c file

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.

First, add the function prototype to define the name of the ISR that will be used to control the LED.

#include "mcc_generated_files/mcc.h"

void myTimer4ISR(void);

Now, the Timer4 driver default Interrupt Handler has to be connected to the ISR by adding the line TMR4_SetInterruptHandler (myTimer4ISR);, right after the SYSTEM_Initialize(); call.

void main(void)
{
// initialize the device
   SYSTEM_Initialize();

    TMR4_SetInterruptHandler (myTimer4ISR);  //Define interrupt Handler

Next, enable both global and peripheral interrupts by removing the double backslash comment markers from Global and Peripheral Interrupt Enable calls.

// Enable the Global Interrupts
   INTERRUPT_GlobalInterruptEnable();

// Enable the Peripheral Interrupts
   INTERRUPT_PeripheralInterruptEnable();

Finally, define what the ISR will do 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 function code after the while(1) loop. 

   
    while (1)
         {
            // Add your application code
        }

}

void myTimer4ISR(void){
    IO_LED_D7_Toggle(); //Control LED
}
/**
 End of File
*/

This completes the main.c edits.


Build Project

Click on Build Project  Build Project icon to compile the code and assuming syntax is correct, you will see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X within several seconds of processing time. 

BUILD SUCCESSFUL (total time: 8s)

Program Device

Make sure your Curiosity board is connected to the USB port. Then, click on the Make and Program Device Program Target icon. This will build the project again and launch the programmer built into the Curiosity board. In the Output window, you should see a series of messages and when successful, it will end with a "Programming and Verify Successful" message.

Output Window:

Connecting to MPLAB Starter Kit on Board...

Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.41.07
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 2004

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0xbf
configuration memory

Programming...
Programming/Verify complete

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of messages if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


Results

The D7 LED will begin to blink on the Curiosity board. This shows 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.