// PIC18 in C - Versione 0.4 - Febbraio 2016 // Copyright (c) 2014-2016, Vincenzo Villa // Creative Commons | Attribuzione - Condividi allo stesso modo 4.0 Internazionale (CC BY-SA 4.0) // Creative Commons | Attribution-Share Alike 4.0 Unported // https://www.vincenzov.net/tutorial/PIC18/interrupt.htm // PIC18F2xK20 / MPLAB X IDE v3.25 / XC8 1.35 // External interrupt INT0 (high priority) #define _XTAL_FREQ 1000000 // Main clock frequency (Default: 1 MHz) void configureRegisters(void); // Configure PIC18 registers // ***** main() code ***** void main(void) { configureRegisters(); // Enable interrupt INTCONbits.GIEH = 1; // 1 = Enables all high priority interrupts while (1) { // super-loop: blink RC7, forever LATCbits.LATC7 = ~LATCbits.LATC7; __delay_ms(250); } } // ***** ISR ***** void interrupt __high_priority my_isr_high(void) { if (INTCONbits.INT0F == 1) { // INT0 external interrupt occurred ? LATCbits.LATC6 = 0; // Turn off RC6 pin INTCONbits.INT0F = 0; // Clear INT0 flag } // if (XXX.flag == 1) { // XXX interrupt occurred ? (NOT USED HERE) // ... // XXX.flag = 0; // Clear XXX flag // } return; } // ***** Configure ports and interrupt ***** void configureRegisters(void) { // Configure Ports TRISCbits.RC7 = 0; // Configure RC7 as output TRISCbits.RC6 = 0; // Configure RC6 as output LATCbits.LATC6 = 1; // Turn on LEDs on RC6 and RC7 LATCbits.LATC7 = 1; TRISBbits.RB0 = 1; // Set RB0 as input ANSELHbits.ANS12 = 0; // Digital input buffer of RB0 is enabled (pin 21 = RB0 = AN12 = INT0) INTCON2bits.nRBPU = 0; // PORTB pull-up global enabled WPUBbits.WPUB0 = 1; // RB0 pull-up individually enabled // Configure Interrupts RCONbits.IPEN = 1; // Enable priority levels on interrupts // Configure INT0 interrupt INTCONbits.INT0IE = 1; // 1 = Enables the INT0 external interrupt INTCON2bits.INTEDG0 = 1; // 1 = Interrupt on rising edge INTCONbits.INT0F = 0; // Clear INT0 flag }