

#include <xc.h>

void InitCAN(void){

    TRISBbits.RB1=0;
    PORTBbits.RB1=0;
    TRISBbits.RB2=0;
    TRISBbits.RB3=1;
    CANCON=0b10000000;
    while(!(CANSTATbits.OPMODE2==1 && CANSTATbits.OPMODE1==0 && CANSTATbits.OPMODE0==0)){};

    ECANCON=0x00;

    /*
    //125kbit/s @8MHz
    BRGCON1=0b01000001;     //SJW=1, BRP=1
    BRGCON2=0b10101110;     //SEG1PH=6, PRESG=7
    BRGCON3=0b00000001;     //SEG2PH=2
    *//*
    //250kbit/s @8MHz
    BRGCON1=0b01000000;     //SJW=1, BRP=0
    BRGCON2=0b10101110;     //SEG1PH=6, PRESG=7
    BRGCON3=0b00000001;     //SEG2PH=2
      */
    //250kbit/s @20MHz
    BRGCON1=0b10000001;     //SJW=3Tq, BRP=1
    BRGCON2=0b10111111;     //SEG1PH=8Tq, PRESG=8Tq
    BRGCON3=0b00000010;     //SEG2PH=3Tq
    // Initialize Receive Masks, see registers RXMxEIDH, RXMxEIDL, etc...
    // Mask 0 (M0) will accept NO extended addresses, but any standard address
    RXM0EIDH = 0x00;    // Extended Address receive acceptance mask, high byte 
    RXM0EIDL = 0x00;    // Extended Address receive acceptance mask, low byte
    RXM0SIDH = 0xFF;    // Standard Address receive acceptance mask, high byte
    RXM0SIDL = 0xE0;    // Standard Address receive acceptance mask, low byte
    
    // Mask 1 (M1) will accept NO extended addresses, but any standard address
    RXM1EIDH = 0x00;    // Extended Address receive acceptance mask, high byte    
    RXM1EIDL = 0x00;    // Extended Address receive acceptance mask, low byte
    RXM1SIDH = 0xFF;    // Standard Address receive acceptance mask, high byte
    RXM1SIDL = 0xE0;    // Standard Address receive acceptance mask, low byte
    
    // Mode 0 allows use of receiver filters RXF0 through RXF5. Enable filters
    // RXF0 and RXF1, all others disabled. See register RXFCONn.
    //  Only using two filters
    RXFCON0 = 0x03;     //Enable Filter-0 and Filter-1; disable others 
    RXFCON1 = 0x00;     //Disable Filters 8 through 15
    
    // Initialize Receive Filters
    //  Filter 0 = 0x32C0
    //  Filter 1 = 0x33C0
   
    RXF0EIDH = 0x00;    //Extended Address Filter-0 unused, set high byte to 0
    RXF0EIDL = 0x00;    //Extended Address Filter-0 unused, set low byte to 0
    RXF0SIDH = 0x32;    //Standard Address Filter-0 high byte set to 0x32
    RXF0SIDL = 0xC0;    //Standard Address Filter-0 low byte set to 0xC0

    RXF2EIDH = 0x00;    //Extended Address Filter-0 unused, set high byte to 0
    RXF2EIDL = 0x00;    //Extended Address Filter-0 unused, set low byte to 0
    RXF2SIDH = 0x33;    //Standard Address Filter-0 high byte set to 0x33
    RXF2SIDL = 0xC0;    //Standard Address Filter-0 low byte set to 0xC0
    
    CANCON=0x00;
    while(!(CANSTATbits.OPMODE2==0 && CANSTATbits.OPMODE1==0 && CANSTATbits.OPMODE0==0)){};



}

void CANTX_sample(void){
    TXB0EIDH = 0x00;
    TXB0EIDL = 0x00;
    
    //0x35E    0110 1011 110
    TXB0SIDH = 0x6B;
    TXB0SIDL = 0xC0;

    TXB0DLC = 0x08;
    TXB0D0 = 0xAA;
    TXB0D1 = 0xBB;
    TXB0D2 = 0xCC;
    TXB0D3 = 0x00;
    TXB0D4 = 0x00;
    TXB0D5 = 0x00;
    TXB0D6 = 0x00;
    TXB0D7 = 0xEE;
    
    TXB0CONbits.TXREQ = 1; //Set the buffer to transmit


    
    while (TXB1CONbits.TXREQ != 0); // Wait for the CAN TX buffer to be ready
    



}


unsigned int CANRx_con(void){
        if(RXB0CONbits.RXFUL){
            RXB0CONbits.RXFUL=0;
            return RXB0D0;
    }

}