/********************************************/
/*Interrupts Code                           */
/*dsPIC33CK512MP608                         */
/********************************************/
#include "xc.h"
#include "MainMotion.h"

bool sram_busy = true;
        
void dsPIC33_Interrupts_Init(void)
{
    TRISEbits.TRISE1 = 1;
    //Input Change Notification 
    
    //Enable
    CNCONEbits.ON = 1;
    
    //Edge style
    CNCONEbits.CNSTYLE = 1;
    
    //Interrupt Change Notification Enable for PORTE bits
    CNEN0Ebits.CNEN0E1 = 1;   
    CNEN1Ebits.CNEN1E1 = 1;
    
    int a = PORTE;
    IPC19bits.CNEIP = 4;
    
    //Enable CN PortE Int
    IEC4bits.CNEIE = 1;
    
    //Clear Flag
    IFS4bits.CNEIF = 0;    
    
    //QEI > Interrupt
    //IEC3 = 1 << 0;
    IEC3bits.QEI1IE = 0;
    
    //Interrupt nesting
    INTCON1bits.NSTDIS = 0;
    
    //Clear INT0 Flag
    IFS0bits.INT0IF = 0;
    
    //Select Edge
    INTCON2bits.INT0EP = 1;
    
    //Enable INT0 interrupt
    IEC0bits.INT0IE = 0;
    
    //Priority
    IPC0bits.INT0IP = 4;
    
    //enable interrupts
    INTCON2bits.GIE = 1;
}

void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{ 
    
    IFS0bits.T1IF = 0;
}

void __attribute__((__interrupt__,no_auto_psv)) _CNEInterrupt(void)
{
    
    if(CNFEbits.CNFE1)
    {        
        Delay(1);
        SRAM_BUSY = true;
        CNFEbits.CNFE1 = 0;
    }
    
    IFS4bits.CNEIF = 0;
}

void __attribute__((__interrupt__,no_auto_psv)) _QEI1Interrupt(void)
{
    //Motor_Stop();
    
    IFS3bits.QEI1IF = 0;
    //IFS3 = 0 << 0;
}

