Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
200 Kevin 1
#include <xc.h>
2
#include <plib.h>
3
#include "defines.h"
4
#include "BTN.h"
5
 
6
static void (*callback_function_1)(void);
7
static void (*callback_function_2)(void);
8
static void (*callback_function_3)(void);
9
 
10
void BTN_Init(void (*callback_1)(void), void (*callback_2)(void), void (*callback_3)(void)) {
11
    callback_function_1 = callback_1;
12
    callback_function_2 = callback_2;
13
    callback_function_3 = callback_3;
14
 
15
    BTN1_TRIS = 1;
16
    BTN2_TRIS = 1;
17
    BTN3_TRIS = 1;
18
 
19
    INTDisableInterrupts();
20
 
21
    CNCONSET = 0x8000;  // Turn on change notice interrupt
22
    CNENSET = 0x80300;  // Set interrupt on CN8/9/19
23
    int tmp = BTN1_PORT;
24
    tmp = BTN2_PORT;
25
    tmp = BTN3_PORT;
26
    IPC6SET = 0x50000;  // Set priority level = 1, sub-priority level = 1
27
    IFS1CLR = 0x1;      // Clear interrupt flag
28
    IEC1SET = 0x1;      // Enable interrupt
29
 
30
    INTEnableInterrupts();
31
}
32
 
33
void __ISR(_CHANGE_NOTICE_VECTOR, ipl1) __CN_Interrupt_Handler(void) {
34
        if (BTN1_PORT == 1) {
35
            Delay_MS(BTN_DEBOUNCE_MS);
36
            if (BTN1_PORT == 1) {
37
                if (callback_function_1 != NULL)
38
                    (*callback_function_1)();
39
            }
40
        }
41
        if (BTN2_PORT == 1) {
42
            Delay_MS(BTN_DEBOUNCE_MS);
43
            if (BTN2_PORT == 1) {
44
                if (callback_function_2 != NULL)
45
                    (*callback_function_2)();
46
            }
47
        }
48
        if (BTN3_PORT == 1) {
49
            Delay_MS(BTN_DEBOUNCE_MS);
50
            if (BTN3_PORT == 1) {
51
                if (callback_function_3 != NULL)
52
                    (*callback_function_3)();
53
            }
54
        }
55
    IFS1CLR = 0x1; // Clear interrupt flag
56
}