AnsweredHot!Configure CAN bus dspic30f

Author
freedomhouse
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2014/04/14 05:05:22
  • Location: 0
  • Status: offline
2016/07/26 04:00:36 (permalink)
0

Configure CAN bus dspic30f

Hi,

this is my firs approch on CAN bus. I'm using dspic30f for my first project.
I studied CAN bus standard and configurations registers, but my problem is that i don't understand how Configure Receive registers, Filters and Masks.
I'm studying microchip example code but i donb't know how set Configure Receive, Filters and Masks registers values (in red in the code).
Ii think that they are used for set ID of devices in the CAN node?
 
Can you help me to undestand this and if there is a complete guide for this registers.
 
thanks
 
#include <p30F6014A.h>


#define FCY 30000000 // 30 MHz
#define BITRATE 1000000 // 1Mbps
#define NTQ 15 // Number of Tq cycles which will make the
            //CAN Bit Timing .
#define BRP_VAL ((FCY/(2*NTQ*BITRATE))-1) //Formulae used for C1CFG1bits.BRP

//---------------------------------------------------------------------

// Buffer Registers for CAN data to be send out in the transmit mode.

unsigned int OutData0[4] = {0x5251, 0x5453, 0x5655, 0x5857};
unsigned int OutData1[2] = {0x5A59, 0x5C5B};
unsigned int OutData2[4] = {0x6261, 0x6463, 0x6665, 0x6867};
unsigned int OutData3[2] = {0x6A69, 0x6C6B};

// Intilializing the receive registers to be 0

unsigned int InData0[4] = {0, 0, 0, 0};
unsigned int InData1[2] = {0, 0};
unsigned int InData2[4] = {0, 0, 0, 0};
unsigned int InData3[2] = {0, 0};

//---------------------------------------------------------------------

int main(void)
{



 TRISD = 0x0000; // Initialize the PORTD as output
 LATD = 0xFFFF; // Initially LEDs of dsPICDEM1.1 Plus GP Board are off


//--------------------------------------------------------------------------------------------------------------------
     //Initialization of CAN1 Module and Enabling of CAN1 Interrupts
//--------------------------------------------------------------------------------------------------------------------

 
 
 C1CTRLbits.CANCKS = 1; // Select the CAN Master Clock . It is equal to Fcy here.
        // equal to Fcy.(Fcy=30Mhz)


 C1CFG1bits.SJW=00; //Synchronized jump width time is 1 x TQ when SJW is equal to 00

 
 C1CFG1bits.BRP = BRP_VAL; //((FCY/(2*NTQ*BITRATE))-1)

 C1CFG2 = 0x03F5; // SEG1PH=6Tq, SEG2PH=3Tq, PRSEG=5Tq
                                // Sample 3 times
                                // Each bit time is 15Tq

///Interrupt Section of CAN Peripheral

 C1INTF = 0; //Reset all The CAN Interrupts
 IFS1bits.C1IF = 0; //Reset the Interrupt Flag status register
 C1INTE = 0x00FF; //Enable all CAN interrupt sources
 IEC1bits.C1IE = 1; //Enable the CAN1 Interrupt


//-------------------------------------------------------------------------------------------------------------------
     //Initialization of CAN2 Module and Enabling of CAN2 Interrupts
//-------------------------------------------------------------------------------------------------------------------


C2CTRLbits.CANCKS = 1; // Select the CAN Module Frequency Here CAN Master Clk is
        // equal to Fcy.(Fcy=30Mhz)

C2CFG1bits.SJW=00; //Synchronized jump width time is 1 x TQ when SJW is equal to 00
 
C2CFG1bits.BRP = BRP_VAL; //((FCY/(2*NTQ*BITRATE))-1)

 
C2CFG2 = 0x03F5; // SEG1PH=6Tq, SEG2PH=3Tq, PRSEG=5Tq
                                // Sample 3 times
                                // Each bit time is 15Tq

//CAN2 Interrupt Section

 C2INTF = 0; //Reset all The CAN Interrupts
 IFS2bits.C2IF = 0; //Reset the Interrupt Flag status register
 C2INTE = 0x00FF; //Enable all interrupt sources
 IEC2bits.C2IE = 1; //Enable the CAN2 Interrupt


//-----------------------------------------------------------------------------------------------------------------------
      // Configure Receive registers, Filters and Masks
//-----------------------------------------------------------------------------------------------------------------------

 // We are initializing the Receive Buffer 0 and Receive Buffer 1 for CAN1 and CAN2

 
 C1RX0CON = C1RX1CON = C2RX0CON = C2RX1CON = 0x0000; // Receive Buffer1 and 0 Status
               //and Control Register for CAN1 and CAN2

 
 // Acceptance Mask Register0SID and Register1SID associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1 and CAN2
 C1RXM0SID = C1RXM1SID = C2RXM0SID = C2RXM1SID = 0x1FFD;
 
 // Acceptance Mask Register0EIDH and Register1EIDH associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1 and CAN2
 C1RXM0EIDH = C1RXM1EIDH = C2RXM0EIDH = C2RXM1EIDH = 0x0FFF;

 // Acceptance Mask Register0EIDL and Register1EIDL associated with Recieve Buffer0
 //and Receive Buffer1 for CAN1 and CAN2
 C1RXM0EIDL = C1RXM1EIDL = C2RXM0EIDL = C2RXM1EIDL = 0xFC00;

 
//Initializing of Acceptance Filter n Standard Identifier for CAN1

 C1RXF0SID = 0x0AA8; //CAN1 Receive Acceptance Filter2 SID
 C1RXF2SID = 0x1555; //CAN1 Receive Acceptance Filter2 SID
 C1RXF2EIDH = 0x0004; //CAN1 Receive Acceptace Filter2 Extended Identifier high byte
 C1RXF2EIDL = 0x8C00; //CAN1 Receive Acceptance Filter2 Extended identifier low byte

//Initializing of Acceptance Filter n Standard Identifier for CAN2

 C2RXF0SID = 0x0AA8; //CAN2 Receive Acceptance Filter0 SID
 C2RXF2SID = 0x1555; //CAN2 Receive Acceptance Filter2 SID
 C2RXF2EIDH = 0x0004; //CAN2 Receive Acceptace Filter2 Extended Identifier high byte
 C2RXF2EIDL = 0x8C00; //CAN2 Receive Acceptance Filter2 Extended identifier low byte
 
 //-----------------------------------------------------------------------------------------------------------------------
      // Configure Transmit Registers Buffer 0 and Transmit Buffer 1
//-----------------------------------------------------------------------------------------------------------------------
  
 C1TX0CON = 0x0003; // High priority
 C1TX0SID = 0x50A8; // SID
 C1TX0EID = 0x0000; // EID
 C1TX0DLC = 0x01C0; //Select the Data word Length for CAN1 Transmit Buffer0 which is 8 byte
 
 // Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC

 C1TX0B1 = OutData0[0];
 C1TX0B2 = OutData0[1];
 C1TX0B3 = OutData0[2];
 C1TX0B4 = OutData0[3];
 
 C1TX1CON = 0x0002; // High Intermediate priority
 C1TX1SID = 0xA855; // SID
 C1TX1EID = 0x0004; // EID
 C1TX1DLC = 0x8DA0; //Select the Data word Length for CAN1 Transmit Buffer1 which
        // is 4 byte

 //Data Field 1, Data Field 2 // 4 bytes selected by DLC

 C1TX1B1 = OutData1[0];
 C1TX1B2 = OutData1[1];

 
 C2TX0CON = 0x0003; // High priority
 C2TX0SID = 0x50A8; // SID = 01010101010 (0x2AA)
 C2TX0EID = 0x0000; // EID = 0000000000000000000 (0x00000)
 C2TX0DLC = 0x01C0; //Select the Data word Length for CAN2 Transmit Buffer0 which is
        // 8 byte

 //Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC
 C2TX0B1 = OutData2[0];
 C2TX0B2 = OutData2[1];
 C2TX0B3 = OutData2[2];
 C2TX0B4 = OutData2[3];

  //Configure Transmit registers // Transmit Buffer 0 and Transmit Buffer 1 for CAN2
 
 C2TX1CON = 0x0002; // High Intermediate priority
 C2TX1SID = 0xA855; // SID = 10101010101 (0x555)
 C2TX1EID = 0x0004; // EID = 1110000000100100011 (0x00123)
 C2TX1DLC = 0x8DA0; //Select the Data word Length for CAN1 Transmit Buffer0 which is
        //8 byte

 //Data Field 1, Data Field 2 // 4 bytes selected by DLC
 
 C2TX1B1 = OutData3[0];
 C2TX1B2 = OutData3[1];
  
 //Change to Loopback Operation Mode from Configuration Mode

 C1CTRLbits.REQOP = 2;
 C2CTRLbits.REQOP = 2;
 while(C1CTRLbits.OPMODE != 2);//Wait for CAN1 mode change from Configuration Mode to Loopback mode
 while(C2CTRLbits.OPMODE != 2);//Wait for CAN2 mode change from Configuration Mode to Loopback mode
 
 //Enable transmission

 C1TX0CONbits.TXREQ = 1;
 C1TX1CONbits.TXREQ = 1;
 C2TX0CONbits.TXREQ = 1;
 C2TX1CONbits.TXREQ = 1;

 while(1)
{


          //ADD YOUR CODE HERE//

     // THE CODE WILL LOOP HERE AND WHEN AN CAN INTERRUPT COMES IT WILL GO TO INTERRUPTS//

                           
} //end while loop


} //end main loop

//--------------------------------------------------------------------------------------------------------------------------
           //Interrupt Section for CAN1
//--------------------------------------------------------------------------------------------------------------------------

void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{
      
  IFS1bits.C1IF = 0; //Clear interrupt flag

      if(C1INTFbits.TX0IF)
      {
       
   C1INTFbits.TX0IF = 0; //If the Interrupt is due to Transmit0 of CAN1 Clear the Interrupt

      }
      else if(C1INTFbits.TX1IF)
      {
        
  C1INTFbits.TX1IF = 0; //If the Interrupt is due to Transmit1 of CAN1 Clear the Interrupt

      }

      if(C1INTFbits.RX0IF)
      {
        
  C1INTFbits.RX0IF = 0; //If the Interrupt is due to Receive0 of CAN1 Clear the Interrupt

        InData0[0] = C1RX0B1;
        InData0[1] = C1RX0B2; //Move the recieve data from Buffers to InData
        InData0[2] = C1RX0B3;
        InData0[3] = C1RX0B4;

        if ((InData0[0]==OutData0[0]) && (InData0[1]==OutData0[1]) && (InData0[2]==OutData0[2]) && (InData0[3]==OutData0[3]))
       
            LATDbits.LATD0 = 0; // If the data received is same which was transmitted
                                   // turn on the LED
      }

      else if(C1INTFbits.RX1IF)
      {
        
  C1INTFbits.RX1IF = 0; //If the Interrupt is due to Receive1 of CAN1 Clear the Interrupt
        InData1[0] = C1RX1B1; //Move the data received to Indata Registers
        InData1[1] = C1RX1B2;

        if ((InData1[0]==OutData1[0]) && (InData1[1]==OutData1[1]))
       
           LATDbits.LATD1 =0; //If the data received is same which was transmitted
                                 //turn on the LED
      }
}


//--------------------------------------------------------------------------------------------------------------------------
           //Interrupt Section for CAN2
//--------------------------------------------------------------------------------------------------------------------------

void __attribute__((interrupt, no_auto_psv)) _C2Interrupt(void)
{
      IFS2bits.C2IF = 0; // Clear interrupt flag

      if(C2INTFbits.TX0IF)
      
   {
        
  C2INTFbits.TX0IF = 0;

      }

      else if(C2INTFbits.TX1IF)

      {
        
  C2INTFbits.TX1IF = 0;

      }

      if(C2INTFbits.RX0IF)
      {
       
  C2INTFbits.RX0IF = 0;

        InData2[0] = C2RX0B1;
        InData2[1] = C2RX0B2;
        InData2[2] = C2RX0B3;
        InData2[3] = C2RX0B4;

        if ((InData2[0]==OutData2[0]) && (InData2[1]==OutData2[1]) && (InData2[2]==OutData2[2]) && (InData2[3]==OutData2[3]))
          
    LATDbits.LATD2 = 0;

      }

      else if(C2INTFbits.RX1IF)

      {
        
  C2INTFbits.RX1IF = 0;

        InData3[0] = C2RX1B1;
        InData3[1] = C2RX1B2;

        if ((InData3[0]==OutData3[0]) && (InData3[1]==OutData3[1]))
        
           LATDbits.LATD3 = 0;
      }

}

 
#1
btbass
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2004/12/10 13:50:32
  • Status: offline
Re: Configure CAN bus dspic30f 2016/07/26 06:04:36 (permalink) ☼ Best Answerby freedomhouse 2016/07/26 09:11:28
5 (1)
Some time ago I wrote a simple lib for the pic30f can. It does work ok.
Might be of some help.
I have had to rename the Files as text files to up load them?
#2
freedomhouse
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2014/04/14 05:05:22
  • Location: 0
  • Status: offline
Re: Configure CAN bus dspic30f 2016/07/26 09:14:13 (permalink)
0
 
now i'm reading also
 
microchip.com/forums/m170968.aspx
 
 
for understand how masks and filterer works
 
 
 
 
#3
btbass
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2004/12/10 13:50:32
  • Status: offline
Re: Configure CAN bus dspic30f 2016/07/26 10:08:23 (permalink)
5 (1)
You use the filter bits to filter the messages you want to receive
You use the mask bits with the filter bits so you can accept a range of message ID's
 
Mask Bit     Filter Bit     Message ID     Accept or Reject
0               x               x                    Accept
1               0               0                   Accept
1               0               1                   Reject
1               1               0                   Reject
1               1               1                   Accept
#4
btbass
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2004/12/10 13:50:32
  • Status: offline
Re: Configure CAN bus dspic30f 2016/07/26 10:27:09 (permalink)
5 (1)
IE:
Set Filter to 0x123 which is 100100011
Set Mask to 0x1fc which is  111111100
Messages accepted are now 0x120, 0x121, 0x122, 0x123
#5
freedomhouse
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2014/04/14 05:05:22
  • Location: 0
  • Status: offline
Re: Configure CAN bus dspic30f 2016/07/26 13:30:15 (permalink)
0
thanks a lot for the answers
 
now i understand it.
 
in the microchip code example
 
sidfilter         1010101010
mask         11111111111     11bit
recivedID   1010101010     only
 
 
EIDfilter                       100100011  
mask         111111111111111111    18bit
recivedID                     100100011     only

 
 
when mask is  111111111111  (all 1) it receive only filter ID?
 
or it recive all IDs with ID>= SIDfilter (or EIDfilter) like this
sidfilter         1010101010
ID1              11010101010
ID2             101010101010
ID3             1111010101010
etc...

 
regards
                
post edited by freedomhouse - 2016/07/26 13:44:05
#6
dlh98370
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2010/11/03 14:13:52
  • Location: 0
  • Status: offline
Re: Configure CAN bus dspic30f 2018/02/13 18:34:46 (permalink)
0
So,
I have been using your CanLib.h and CanLib.c for a number of my projects with good success. The problem I am having is that after I send a message using CanSendMessage(const CANMSG *message)
so I form the message and send it out using
while((C1TX0CONbits.TXREQ == 1));
   CanSendMessage(&canmsgtx1);
which does but I cannot receive any subsequent messages. What is it I need to  be able to receive messages again.
Dan H
#7
btbass
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2004/12/10 13:50:32
  • Status: offline
Re: Configure CAN bus dspic30f 2018/02/14 00:53:12 (permalink)
0
When sending a message.
You do not need to wait for C1TX0CONbits.TXREQ flag to clear, that is taken care of.
 
So just using 'CanSendMessage(&canmsgtx1);' on its own is ok.
 
while((C1TX0CONbits.TXREQ == 1)); /* Not needed */

Just this is ok.
CanSendMessage(&canmsgtx1);
 
CanReceiveMessage returns true when a message has been received.
 
Bool CanReceiveMessage(CANMSG *message); /* Returns True if message received. */
 
So you could wait for a message:
 
while(!CanReceiveMessage(CANMSG *message)){
;
}
 
or check if a message has been received:
 
if(CanReceiveMessage(CANMSG *message)){
blah blah;
}
 
Using interrupts to receive messages is better.
I have posted code snippets on this thread for receiving messages via interrupts.
http://www.microchip.com/forums/m1033030.aspx
#8
btbass
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2004/12/10 13:50:32
  • Status: offline
Re: Configure CAN bus dspic30f 2018/02/15 02:10:07 (permalink)
0
In the Family Reference Manual 'Section 23. CAN Module' (Document 70070D.pdf or Document 70046E.pdf)

Look at Figure 23-8:

RXB0 has two filters associated with it and one mask. Mask 0, Filter 0 and Filter 1
RXB1 has four filters and one mask. Mask 1, Filter 2 - 5.
So to receive 0x015 in buffer RXB0 and 0x016 in RXB1, you could try:

void initCan(void)
{
CanInit();
CanSetMask(0, 0x7ff);
CanSetFilter(0, 0x015); /* Mask and filter set for 0x0x015 messages */
CanSetMask(1, 0x7ff);
CanSetFilter(2, 0x016); /* Mask and filter set for 0x016 messages */
CanConfig();
}
 
To set up the mask to receive both messages in one Buffer.
000 0001 0101 = 0x015 Filter 0
000 0001 0110 = 0x016 Filter 1
111 1111 1100 = 0x7fc Mask 0
 
This buffer would also now accept
000 0001 0100 = 0x014
000 0001 0111 = 0x017
post edited by btbass - 2018/02/15 02:57:01
#9
HARI 123
Starting Member
  • Total Posts : 32
  • Reward points : 0
  • Joined: 2018/03/06 04:05:44
  • Location: KERALA
  • Status: offline
Re: Configure CAN bus dspic30f 2018/03/17 00:08:13 (permalink)
0
HOW CAN I INTERFACE 16X1 LCD WITH DSPIC30F2010 IN 4BIT MODE.
I WROTE PROGRAM SUCCESSFULLY BUT I CAN'T GET OUTPUT IN BREAD BOARD.
AM USING MPLAD IDE AND C30 COMPILER.
 
HERE IS MY PROGRAM
 
 
#include "p30f2010.h"
#include "libpic30.h"
#include"lcd display.h"

_FOSC(CSW_FSCM_OFF & XT_PLL16);

_FWDT(WDT_OFF);

_FBORPOR(PBOR_ON & MCLR_EN);

_FGS(CODE_PROT_OFF);

#define LCD_RS           LATCbits.LATC14
#define TRIS_RS          TRISCbits.TRISC14

#define LCD_RW           LATCbits.LATC13
#define TRIS_RW          TRISCbits.TRISC13

#define LCD_EN           LATDbits.LATD0
#define TRIS_EN          TRISCbits.TRISD0

#define LCD_D7           LATBbits.LATB3
#define TRIS_D7          TRISBbits.TRISB3

#define LCD_D6           LATBbits.LATB2
#define TRIS_D6          TRISBbits.TRISB2

#define LCD_D5           LATBbits.LATB1
#define TRIS_D5          TRISBbits.TRISB1

#define LCD_D4           LATBbits.LATB0
#define TRIS_D4         TRISBbits.TRISB0                 

#define Data_PORT        PORTB

#define DATA LATB



#define FirstLine   0x80
 
#define LCD_FIRST_ROW         128
#define LCD_CLEAR              1
#define LCD_RETURN_HOME        2
#define LCD_CURSOR_OFF         12
#define LCD_UNDERLINE_ON       14
#define LCD_BLINK_CURSOR_OFF   15
#define LCD_SHIFT_LEFT         28

unsigned int i,temp;

void lcd_init(void);
void lcd_command(unsigned char);
void lcd_data(unsigned char);

void _delay_ms(int cnt)
{
 int n;
 for(n=0; cnt<100;n++);

}
void lcd_init(void)
{

ADPCFG = 0xFFFF;

TRISC = 0;

TRISB = 0;

PORTB = 0;

LATB = 0;

_delay_ms(100);

lcd_command(0x33);

lcd_command(0x32);

lcd_command(0x20);

lcd_command(0x0C);

lcd_command(0x06);

lcd_command(0x01);

}

void lcd_command(unsigned char cmd)

{


_LATC14=0;
_LATC13=0;

DATA=((cmd >> 4) & 0xf0);

_LATD0 = 1;
_delay_ms(110);
_LATD0 = 0;


DATA=((cmd << 4) & 0xf0);

_LATD0 = 1;
_delay_ms(100);
_LATD0 = 0;

_delay_ms(110);

}

void lcd_data(unsigned char data)

{

_LATC14=1;
_LATC13=0;

DATA=(data & 0xf0);

_LATD0 = 1;
_delay_ms(100);
_LATD0 = 0;


DATA=((data << 4) & 0xf0);

_LATD0 = 1;
_delay_ms(110);
_LATD0 = 0;

_delay_ms(110);

}

int main()

{
    TRISD=0;
    TRISD=0b1111111111110000;

unsigned char Lcd_LINE1[]={"AUREEMAS"};

_delay_ms(100);

lcd_init();

_delay_ms(300);

while(1)

{

lcd_command(0x01);

_delay_ms(100);

lcd_command(0x80);

for(i=0;Lcd_LINE1!='\0';i++)

{

lcd_data(Lcd_LINE1);

_delay_ms(100);

}


}

return 0;
}


 
PLEASE HELP ME.
THANKS
post edited by HARI 123 - 2018/03/17 00:10:21
#10
Ouseppachan
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2018/06/06 20:54:19
  • Location: 0
  • Status: offline
Re: Configure CAN bus dspic30f 2018/07/02 21:57:12 (permalink)
0

I am trying to communicate between two dspic33ep256mc502 microcontroller and I have configured one for transmit and one for receive.I made a can bus trermination with 120ohm resisters.I get reception only when I make my transmit pin in recive module high and low.

plz help how to solve it.
#11
Jump to:
© 2018 APG vNext Commercial Version 4.5