Modify main.c
Step 11 - There are two files that need to be modified for this project: main.c and tmr0.c. These are the main code file main.c and the Timer0 driver file tmr0.c. They are modified as described below.
main.c
The main.c file requires a few lines to be uncommented for the interrupt to work properly. To enable the interrupt to work, Global Interrupts and the Peripheral Interrupts need to be enabled. MCC already has the control commands in the default main.c file, they are just commented out with two forward slashes (//). The forward slashes need to be removed to enable these lines of code.
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
The main loop does not require any additional code beyond the default while(1) loop. The Interrupt Service Routine (ISR) will handle the changing of the I/O pin.
while (1)
{
// Add your application code
}
tmr0.c
The tmr0.c file contains the ISR that will run when the interrupt occurs. This is where a function to toggle the LED pin is entered. The function used is the D2_LED_Toggle() macro that is created by MCC and placed in the pin_manager.h file. This D2_LED_Toggle() is added to the ISR as shown below.
void TMR0_ISR(void)
{
// clear the TMR0 interrupt flag
PIR0bits.TMR0IF = 0;
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
// add your TMR0 interrupt custom code
D2_LED_Toggle();
}
In addition to the ISR modification, the tmr0.c file also needs to know where the definitions are for the toggle function. By adding the line #include "mcc.h" to the tmr0.c file, the definitions generated by MCC can be compiled with the tmr0.c file without errors. The #include "mcc.h" is added just after the #include "tmr0.h"
#include <xc.h>
#include "tmr0.h"
#include "mcc.h"