Ext Int Example - PalouseRobosub/SUBLIBinal GitHub Wiki
External Interrupt Example
This page contains information describing the example code provided. Examples can be found in the 'example' folder within the repository.
/********************************************************
* File Name: ext_int_ex.c
*
* Description:
* Main file
*
*
*********************************************************/
/*************************************************************************
System Includes
************************************************************************/
#include "sublibinal.h"
#include "sublibinal_config.h"
void interrupt_Callback();
/*************************************************************************
Main Function
************************************************************************/
int main(void) {
Interrupt_Config interrupt_Config = {0}; //Initialize the structure to zero
//Configure an output pin
TRISAbits.TRISA0 = 0; //Set an output pin on RA0
interrupt_Config.callback = &interrupt_Callback; //set the callback function that executes when the interrupt triggers
interrupt_Config.enable = TRUE; //We will enable the external interrupt
interrupt_Config.extInt = Interrupt_1; //We will use External Interrupt 1
interrupt_Config.pin = Pin_RPB14; //Set our external interrupt pin to be RB14
interrupt_Config.resistor = pullup; //set an internal pullup resistor
interrupt_Config.polarity = rising; //set the interrupt to trigger when the input toggles from low to high,
//e.g. rising edge
initialize_Interrupt(interrupt_Config); //initialize the interrupt
enable_Interrupts();
//Begin the embedded system loop
while (1) {
}
return 0;
}
void interrupt_Callback() {
LATAINV = 1; //toggle bit 1 in PORTA
}
In the provided code example, the structure is first initialized to 0 by calling the line 'Interrupt_Config interrupt_Config = {0};'. This is to ensure that all parameters are specified as 0, or NULL. If this is not completed, the callback function will not be null, and when an interrupt occurs, it could dereference a dangling pointer, breaking program execution. Every member of the configuration structure is fully filled out. The resistor is set to pull-up. That means that we can connect a switch that would tie this pin to ground. When the switch is open, internally the microcontroller will pull the pin high. Then, when the pin is connected to ground, the voltage will be pulled low and a small amount of current will flow through the pull-up resistor. This avoids complications due to floating pin values. In our example, we are also saying that the interrupt will only occur on a rising edge, or when we experience a transition from low voltage to high voltage.
As a final initialization parameter, we initialize the interrupt. When we do this, we completely enable the interrupts functionality and the interrupt callback will occur whenever we encounter a rising edge on pin B14.