Subversion Repositories Code-Repo

Rev

Rev 200 | Rev 212 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

static BTN_DATA *btn_data_ptr;

void BTN_Init(BTN_DATA *data, void (*callback_1)(void), void (*callback_2)(void), void (*callback_3)(void)) {
    btn_data_ptr = data;
    
    btn_data_ptr->callback_function_1 = callback_1;
    btn_data_ptr->callback_function_2 = callback_2;
    btn_data_ptr->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) {
    // Upon interrupt, debounce input and call saved function
    if (BTN1_PORT == 1) {
        Delay_MS(BTN_DEBOUNCE_MS);
        if (BTN1_PORT == 1) {
            if (btn_data_ptr->callback_function_1 != NULL)
                (*btn_data_ptr->callback_function_1)();
        }
    }
    if (BTN2_PORT == 1) {
        Delay_MS(BTN_DEBOUNCE_MS);
        if (BTN2_PORT == 1) {
            if (btn_data_ptr->callback_function_2 != NULL)
                (*btn_data_ptr->callback_function_2)();
        }
    }
    if (BTN3_PORT == 1) {
        Delay_MS(BTN_DEBOUNCE_MS);
        if (BTN3_PORT == 1) {
            if (btn_data_ptr->callback_function_3 != NULL)
                (*btn_data_ptr->callback_function_3)();
        }
    }
    IFS1CLR = 0x1; // Clear interrupt flag
}