/Users/farid11/MPLABXProjects/simple C Code/C_Blink_LED.X/Interrupt_Example.c
 1 /*
 2  Purpose: Toggle an LED state connected on RD5 using an interrupt
 3  generate by a button connected top RB0.
 4  Input: Button connected to RB0 (INT0)
 5  Output: LED connected to RD5
 6  Compiler: XC8, version 1.35
 7  Author: Rodrigo Barbosa 
 8  */
 9 
10 #include <stdio.h>
11 #include <math.h>
12 #include <xc.h>
13 #include "p18f45k20.h"
14 #include <stdlib.h>
15 #define _XTAL_FREQ 4000000
16 #pragma config FOSC = INTIO67   // Oscillator Selection bits
17 #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit
18 #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit
19 
20 // CONFIG2L
21 #pragma config PWRT = ON          // Power-up Timer Enable bit
22 #pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits
23 #pragma config BORV = 30        // Brown Out Reset Voltage bits
24 
25 // CONFIG2H
26 #pragma config WDTEN = OFF      // Watchdog Timer Enable bit
27 #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits
28 
29 // CONFIG3H
30 #pragma config CCP2MX = PORTC   // CCP2 MUX bit 
31 #pragma config PBADEN = OFF      // PORTB A/D Enable bit
32 #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit
33 #pragma config HFOFST = OFF     // HFINTOSC Fast Start-up
34 #pragma config MCLRE = OFF       // MCLR Pin Enable bit
35 
36 // CONFIG4L
37 #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit
38 #pragma config LVP = OFF        // Single-Supply ICSP Enable bit
39 #pragma config XINST = OFF      // Extended Instruction Set Enable bit
40 
41 // CONFIG5L
42 #pragma config CP0 = OFF        // Code Protection Block 0
43 #pragma config CP1 = OFF        // Code Protection Block 1
44 #pragma config CP2 = OFF        // Code Protection Block 2
45 #pragma config CP3 = OFF        // Code Protection Block 3
46 
47 // CONFIG5H
48 #pragma config CPB = OFF        // Boot Block Code Protection bit
49 #pragma config CPD = OFF        // Data EEPROM Code Protection bit
50 
51 // CONFIG6L
52 #pragma config WRT0 = OFF       // Write Protection Block 0
53 #pragma config WRT1 = OFF       // Write Protection Block 1
54 #pragma config WRT2 = OFF       // Write Protection Block 2
55 #pragma config WRT3 = OFF       // Write Protection Block 3
56 
57 // CONFIG6H
58 #pragma config WRTC = OFF       // Configuration Register Write Protection bit
59 #pragma config WRTB = OFF       // Boot Block Write Protection bit
60 #pragma config WRTD = OFF       // Data EEPROM Write Protection bit
61 
62 // CONFIG7L
63 #pragma config EBTR0 = OFF      // Table Read Protection Block 0
64 #pragma config EBTR1 = OFF      // Table Read Protection Block 1
65 #pragma config EBTR2 = OFF      // Table Read Protection Block 2
66 #pragma config EBTR3 = OFF      // Table Read Protection Block 3
67 
68 #define LG LATDbits.LD5
69 void interrupt low_priority CheckButtonPressed(){
70     int k=0;
71     if(INTCONbits.INT0IF){
72         LG=!LG;
73         for(k=0;k<5;k++)
74             __delay_ms(100);
75         INTCONbits.INT0IF=0;
76     }
77 }
78 
79 int main(){
80     TRISDbits.RD5=0;    // Where the KED is connected to
81     TRISBbits.RB0=1;    // Where the button is connected to
82     
83     LG=0;
84     RCONbits.IPEN=1;
85     INTCONbits.GIEH=1;
86     INTCON2bits.INTEDG0=1;
87     INTCONbits.INT0IE=1;
88     INTCONbits.INT0IF=0;
89     
90     while(1){
91         
92     }
93     
94 }
95 
96