My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
CANBusCode  
Updated Dec 1, 2013 by rijadsul...@gmail.com

Code

Transmit

#if defined(__XC)
    #include <xc.h>        /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>       /* HiTech General Include File */
#elif defined(__18CXX)
    #include <p18f26k80.h>   /* C18 General Include File */
#endif

#if defined(__XC) || defined(HI_TECH_C)

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

#endif
#include <usart.h>
#include "stdio.h"
#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"    /* User funct/params, such as InitApp */
#include <delays.h>


/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/
unsigned char temp_EIDH; //extended identifier High byte
unsigned char temp_EIDL; //extended identifier Low byte
unsigned char temp_SIDH; //standard identifier High byte
unsigned char temp_SIDL; //standard identifier Low byte
unsigned char temp_DLC;
unsigned char temp_D0; //data byte 1
unsigned char temp_D1; //data byte 2
unsigned char temp_D2; //data byte 3
unsigned char temp_D3; //data byte 4
unsigned char temp_D4; //data byte 5
unsigned char temp_D5; //data byte 6
unsigned char temp_D6; //data byte 7
unsigned char temp_D7; //data byte 8
unsigned char  RXMsgFlag;
/* i.e. uint8_t <variable_name>; */
 
/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
/* void canTransmit(void);*/
int i;
void main(void)
{
    /* Configure the oscillator for the device */
    OSCCON = 0b01100011;
    TRISAbits.TRISA0 = 0;
     TRISAbits.TRISA1 = 0;
     TRISCbits.TRISC2 = 0;
    /* Initialize I/O and Peripherals for application */
    //InitApp();

    CANCON = 128; // set canbus to config mode
    while(!((CANSTAT&0x80)==0x80)); // wait to enter config mode
    ECANCONbits.MDSEL0=0; // select mode 0
    ECANCONbits.MDSEL1=0;
    /* TODO <INSERT USER APPLICATION CODE HERE> */
    TRISBbits.TRISB2 = 0; // sets Tx to output
    TRISBbits.TRISB3 = 1; // sets Rx to input
    
    BRGCON1 = 0x03; //Configure the baud rate of the CANBUS
    BRGCON2 = 0x83; //62.5 kb/s
    BRGCON3 = 0x01;


    TXB0DLC = 0x01;// how many bytes in data field 0-8 bytes
    CANCON = 0; // set Canbus to normal mode
    while((CANSTAT!=0)); // wait for normal mode

  

    /* Initialize I/O and Peripherals for application */
    //InitApp();

    /* TODO <INSERT USER APPLICATION CODE HERE> */
    i=0;
    while(1)
    {
    TXB0EIDH = 0x00;
    TXB0EIDL = 0x00;

    //set the standard identifier to 0x02
    TXB0SIDH = 0; //should probably be 0x02
    TXB0SIDL = 0;

    //send the byte that was passed to the transmit function
    if(i>5){
    TXB0D0 = 0x01;
    }
    if(i<4){
         TXB0D0 = 0x02;
    }
    /*
    TXB0D1 = 0x00;
    TXB0D2 = 0x00;
    TXB0D3 = 0x00;
    TXB0D4 = 0x00;
    TXB0D5 = 0x00;
    TXB0D6 = 0x00;
    TXB0D7 = 0x00;
*/
    TXB0CONbits.TXREQ = 1; //Set the buffer to transmit
    Delay10KTCYx(100);
    Delay10KTCYx(100);
      if(TXB0CONbits.TXREQ ==1){
        LATAbits.LATA0 =1;
    }
    else{
        LATAbits.LATA0 =0;
    }
     i = i+1;
    if(i==10){
        i=0;
    }
    }
   
}

Receive

/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#if defined(__XC)
    #include <xc.h>        /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>       /* HiTech General Include File */
#elif defined(__18CXX)
    #include <p18f2580.h>   /* C18 General Include File */
#endif

#if defined(__XC) || defined(HI_TECH_C)

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

#endif
#include <usart.h>
#include "stdio.h"
#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"    /* User funct/params, such as InitApp */
#include <delays.h>

/* Configuration registers for the PIC18F2580 or PIC18F4580 **************************/
#include <p18F2580.h>

// CONFIG1H
#pragma config OSC = IRCIO7      // Oscillator Selection bits (External RC oscillator, port function on RA6)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = BOHW     // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (VBOR set to 2.1V)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = 1024     // Boot Block Size Select bit (1K words (2K bytes) boot block)
#pragma config XINST = ON       // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode enabled)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)



/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/
unsigned char temp_EIDH; //extended identifier High byte
unsigned char temp_EIDL; //extended identifier Low byte
unsigned char temp_SIDH; //standard identifier High byte
unsigned char temp_SIDL; //standard identifier Low byte
unsigned char temp_DLC;
unsigned char temp_D0; //data byte 1
unsigned char temp_D1; //data byte 2
unsigned char temp_D2; //data byte 3
unsigned char temp_D3; //data byte 4
unsigned char temp_D4; //data byte 5
unsigned char temp_D5; //data byte 6
unsigned char temp_D6; //data byte 7
unsigned char temp_D7; //data byte 8
unsigned char RXMsgFlag;//flag that gets set if data was received
/* i.e. uint8_t <variable_name>; */
 char data_sending;
 char message [8];
 #define true 1
#define false 0


/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
 unsigned char CanbusReceive(void);
void main(void)
{
    /* Configure the oscillator for the device */
    OSCCON = 0b01110011;
    /* Initialize I/O and Peripherals for application */
    TRISCbits.TRISC2 = 0;
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA0 = 0;
    /*LATCbits.LATC2 = 1;*/
    CANCON = 128; // set canbus to config mode
    while(!((CANSTAT&0x80)==0x80)); // wait to enter config mode
    ECANCONbits.MDSEL0=0; // select mode 0
    ECANCONbits.MDSEL1=0; 
    /* TODO <INSERT USER APPLICATION CODE HERE> */
    TRISBbits.TRISB2 = 0; // sets Tx to output
    TRISBbits.TRISB3 = 1; // sets Rx to input
    BRGCON1 = 0x03; //Configure the baud rate of the CANBUS
    BRGCON2 = 0x83;
    BRGCON3 = 0x01;
    TXB0SIDL = 0;//set identifier
    TXB0SIDH = 0;//set identifier
    TXB0DLC = 0x01;// how many bytes in data field 0-8 bytes

  

    RXB0CON = 0x00;
    RXB1CON = 0x00;

    CANCON = 0; // set Canbus to normal mode

    while((CANSTAT!=0)); // wait for normal mode
    

    while(1)
{

        data_sending = CanbusReceive(); 
        Delay10KTCYx(100);
        if(data_sending == 0x02){
            LATAbits.LATA1 =1;
        }
        else if(data_sending == 0x01){
             LATAbits.LATA0 =1;
         }
        else {
            LATAbits.LATA1 = 0;
            LATAbits.LATA0 =0;

        }

}
}
unsigned char CanbusReceive(void){

    
    if (RXB0CONbits.RXFUL) { //CheckRXB0
        temp_EIDH = RXB0EIDH;
        temp_EIDL = RXB0EIDL;
        temp_SIDH = RXB0SIDH;
        temp_SIDL = RXB0SIDL;
        temp_DLC = RXB0DLC;
        temp_D0 = RXB0D0;
       /* temp_D1 = RXB0D1;
        temp_D2 = RXB0D2;
        temp_D3 = RXB0D3;
        temp_D4 = RXB0D4;
        temp_D5 = RXB0D5;
        temp_D6 = RXB0D6;
        temp_D7 = RXB0D7;*/
        
        RXB0CONbits.RXFUL = 0;
        RXMsgFlag = 0x01;
    } else if (RXB1CONbits.RXFUL) { //CheckRXB1
        temp_EIDH = RXB1EIDH;
        temp_EIDL = RXB1EIDL;
        temp_SIDH = RXB1SIDH;
        temp_SIDL = RXB1SIDL;
        temp_DLC = RXB1DLC;
        temp_D0 = RXB1D0;
        /*temp_D1 = RXB1D1;
        temp_D2 = RXB1D2;
        temp_D3 = RXB1D3;
        temp_D4 = RXB1D4;
        temp_D5 = RXB1D5;
        temp_D6 = RXB1D6;
        temp_D7 = RXB1D7;*/
        RXB1CONbits.RXFUL = 0;
        RXMsgFlag = 0x01;
    } else if (B0CONbits.RXFUL) { //CheckB0
        temp_EIDH = B0EIDH;
        temp_EIDL = B0EIDL;
        temp_SIDH = B0SIDH;
        temp_SIDL = B0SIDL;
        temp_DLC = B0DLC;
        temp_D0 = B0D0;
      /*  temp_D1 = B0D1;
        temp_D2 = B0D2;
        temp_D3 = B0D3;
        temp_D4 = B0D4;
        temp_D5 = B0D5;
        temp_D6 = B0D6;
        temp_D7 = B0D7;*/
        
        B0CONbits.RXFUL = 0;
        RXMsgFlag = 0x01;
    }

    if (RXMsgFlag == 0x01) {
        RXMsgFlag = 0x00;
        PIR3bits.RXB1IF = 0;
        
        return temp_D0;
    } else {
        return false;
    }


}
Powered by Google Project Hosting