Subversion Repositories Code-Repo

Rev

Blame | Last modification | View Log | RSS feed

#include <xc.h>
#include <plib.h>
#include "defines.h"
#include "BTN.h"

static void (*callback_function_1)(void);
static void (*callback_function_2)(void);
static void (*callback_function_3)(void);

void BTN_Init(void (*callback_1)(void), void (*callback_2)(void), void (*callback_3)(void)) {
    callback_function_1 = callback_1;
    callback_function_2 = callback_2;
    callback_function_3 = callback_3;

    BTN1_TRIS = 1;
    BTN2_TRIS = 1;
    BTN3_TRIS = 1;

    INTDisableInterrupts();
    
    CNCONSET = 0x8000;  // Turn on change notice interrupt
    CNENSET = 0x80300;  // Set interrupt on CN8/9/19
    int tmp = BTN1_PORT;
    tmp = BTN2_PORT;
    tmp = BTN3_PORT;
    IPC6SET = 0x50000;  // Set priority level = 1, sub-priority level = 1
    IFS1CLR = 0x1;      // Clear interrupt flag
    IEC1SET = 0x1;      // Enable interrupt

    INTEnableInterrupts();
}

void __ISR(_CHANGE_NOTICE_VECTOR, ipl1) __CN_Interrupt_Handler(void) {
        if (BTN1_PORT == 1) {
            Delay_MS(BTN_DEBOUNCE_MS);
            if (BTN1_PORT == 1) {
                if (callback_function_1 != NULL)
                    (*callback_function_1)();
            }
        }
        if (BTN2_PORT == 1) {
            Delay_MS(BTN_DEBOUNCE_MS);
            if (BTN2_PORT == 1) {
                if (callback_function_2 != NULL)
                    (*callback_function_2)();
            }
        }
        if (BTN3_PORT == 1) {
            Delay_MS(BTN_DEBOUNCE_MS);
            if (BTN3_PORT == 1) {
                if (callback_function_3 != NULL)
                    (*callback_function_3)();
            }
        }
    IFS1CLR = 0x1; // Clear interrupt flag
}