Step 4: Add Application Code to the Project

Last modified by Microchip on 2026/06/26 07:39

Add Application Code to the Project

Once the project is generated, add the following code outside the main function as shown in the accompanying image.

void TC0_Callback_InterruptHandler(TC_TIMER_STATUS status, uintptr_t context)
{
   /* Toggle LED */
    LED1_Toggle();
}

code outside the main function

An interrupt handler (or callback function) is a special function that runs automatically when a specific event occurs in the microcontroller—such as a timer reaching a certain value. In this case, the function (TC0_Callback_InterruptHandler) is called when the Timer/Counter 0 (TC0) module triggers an interrupt. 

Every time the TC0 timer reaches its set period (every 500 ms), the microcontroller automatically calls this function. The function toggles the LED, making it blink on and off at a precise interval. This process happens in the background, without the main program having to check the time or run a delay loop.

Add the following code inside the main function.

TC0_TimerCallbackRegister(TC0_Callback_InterruptHandler, (uintptr_t)NULL);
TC0_TimerStart();

code inside the main function

TC0_TimerCallbackRegister(TC0_Callback_InterruptHandler, (uintptr_t)NULL) tells the microcontroller to use the function (TC0_Callback_InterruptHandler) as the interrupt handler (callback) for TC0. Whenever the TC0 timer event occurs, the microcontroller automatically calls the registered function

TC0_TimerStart() starts the TC0 timer. It begins counting based on its configuration (prescaler time period, etc.)

Back to Top