• AVR Freaks

Message filtering in CAN

Author
syamuk2005
Starting Member
  • Total Posts : 71
  • Reward points : 0
  • Joined: 2008/01/18 21:31:45
  • Location: 0
  • Status: offline
2008/02/26 23:04:02 (permalink)
0

Message filtering in CAN

   I am trying to establish communication between 3 CAN modules . I am using MCP2515 CAN controller and  MCP 2551 CAN transreceiver.  I communicated  without using filering and masking methods . But
  now I am planning for a 3 board communication where i can individually send   messages for a particular        node.Can u give me some links or clues for the   procedure to filer out the messages?I have gone through the data sheet .  While receiving will I get the identifier bits of transmitting node in RXBnSIDH & RXBnSIDL registers.Is this identifier used for matching with  mask bits and filter bits? While comparing will the data be rejected when    any one of the bits  get rejected.Can anyone give a basic idea about these   things?
#1

14 Replies Related Threads

    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 01:26:25 (permalink)
    0
    Hi,
    CAN Masks & Filters 101:

    1. The Rx Mask is nothing more than a bit level switch to enable or disable the filter bits.
    Eg/
    (assume 11 bit Id)
    If the Mask is set to a value of 0b00000000111 (0x0007) then only bits 0,1 & 2 of the filter bits will be used to filter the received Id against.

    2. The Rx filter is used to check the received CAN Id against. If a match is detected on the active filter bits, then the received CAN message is placed in the Rx buffer, else the CAN message is discarded.

    (assume 11 bit Id)
    Eg 1/
    Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
    Filter Value: 0b11111111111 (0x03FF)
    The Rx buffer will only receive CAN frames with an Id of 0x03FF

    Eg 2/
    Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
    Filter Value: 0b11111111110 (0x03FE)
    The Rx buffer will only receive CAN frames with an Id of 0x03FE

    Eg 3/
    Mask Value: 0b11111111110 (0x03FE)
    Filter Value: 0b11111111111 (0x03FF)
    The Rx buffer will receive CAN frames with an Id of 0x03FE or 0x03FF as bit 0 of the filter bit is disabled.

    Eg 4/
    Mask Value: 0b11111111110 (0x03FE)
    Filter Value: 0b11111111110 (0x03FE)
    The Rx buffer will receive CAN frames with an Id of 0x03FE or 0x03FF as bit 0 of the filter bit is disabled.

    Eg 5/
    Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
    Filter Value: 0b11111111000 (0x03F8)
    The Rx buffer will only receive CAN frames with an Id of 0x03F8

    Eg 6/
    Mask Value: 0b11111111000 (0x03F8)
    Filter Value: 0b11111111000 (0x03F8)
    The Rx buffer will receive CAN frames with an Id of 0x03F8,0x03F9,0x03FA,0x03FB,0x03FC,0x03FD,0x03FE,0x03FF as bit 0,1 & 2 of the filter bits are disabled.

    Eg 7/
    Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
    Filter Value: 0b00000000000 (0x0000)
    The Rx buffer will only receive CAN frames with an Id of 0x000

    Eg 8/
    Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
    Filter Value: 0b00000000001 (0x0001)
    The Rx buffer will only receive CAN frames with an Id of 0x001

    Eg 9/
    Mask Value: 0b11111111000 (0x03F8)
    Filter Value: 0b00000000000 (0x0000)
    The Rx buffer will receive CAN frames with an Id of 0x000,0x001,0x002,0x003,0x004,0x005,0x006,0x007 as bit 0,1 & 2 of the filter bits are disabled.

    Eg 10/
    Mask Value: 0b00000000111 (0x0007)
    Filter Value: 0b00000000000 (0x0000)
    The Rx buffer will receive CAN frames that end either 0,1,2,3,4,5,6,7 (0x121 = accepted, 0x0128 = discarded)

    I hope these examples better help you understand the relationships between Rx masks & filters.

    Cheers
    Artic

    [Added Examples 7,8,9 & 10]
    post edited by Artic - 2008/02/27 01:34:48
    #2
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 02:35:58 (permalink)
    0
    Thank you for such an immediate response.I have some more doubts like
     
     1.  Can I get the identifier bits of transmitting node in
     RXBnSIDH & RXBnSIDL ?If not what is the purpose of these
     registers.
     
     2.  If I am using RXB0 buffer,what is the use of FILHIT bit
     in RXB0CTRL?
     FILHIT -- 1 = Acceptance Filter 1(RXF1)
     FILHIT -- 0 = Acceptance Filter 0(RXF0)
     Is that means I can use only RXF0SIDH & RXF0SIDL
     and RXF1SIDH & RXF1SIDL as filter registers.
     
     3.Eg 10/
     Mask Value:   0b00000000111 (0x0007)
     Filter Value: 0b00000000000 (0x0000)
     The Rx buffer will receive CAN frames that end either  0,1,2,3,4,5,6,7
     (0x121 = accepted, 0x0128 = discarded)
     
     In this eg you have given: Is 0x121 accepted ? I think it is discarded
     Similarly  0x128 will be accepted.If my concept is wrong
     please help me.
    #3
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 03:37:04 (permalink)
    0
    Hi,
    1.  Can I get the identifier bits of transmitting node in
    RXBnSIDH & RXBnSIDL ?If not what is the purpose of these
    registers.
    Yes, just read these registers as you would any other variable in your ASM or C application.
     
    2.  If I am using RXB0 buffer,what is the use of FILHIT bit
    in RXB0CTRL?
    FILHIT -- 1 = Acceptance Filter 1(RXF1)
    FILHIT -- 0 = Acceptance Filter 0(RXF0)
    Is that means I can use only RXF0SIDH & RXF0SIDL
    and RXF1SIDH & RXF1SIDL as filter registers
    The FILHIT bit can be used to determine which filter passed the message into the Rx buffer. Have a read of section 4.5 in the MCP2515 datasheet.
     
    3.Eg 10/
    Mask Value:   0b00000000111 (0x0007)
    Filter Value: 0b00000000000 (0x0000)
    The Rx buffer will receive CAN frames that end either  0,1,2,3,4,5,6,7
    (0x121 = accepted, 0x0128 = discarded)
     
    In this eg you have given: Is 0x121 accepted ? I think it is discarded
    Similarly  0x128 will be accepted.If my concept is wrong
    please help me.
    You correct. With this example I left an "obvious" mistake - in order to get you to think about how the Mask & filters work.
    The actual CAN ids that will be accepted in this example is - Any CAN Id that ends in 0 or 8 (eg 0x120 = accepted, 0x128 = accepted, 0x121 = discarded).
     
    Hope it helps
    Artic
    #4
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 03:57:19 (permalink)
    0
         
     
     
     Thank you for showing my mistake (FILHIT bits  are read only bits).
     I have made the changes but no effect .I am using one board with
     18f4550 and the other 16f877A.I am attaching my code for both nodes.
     Node1(18F4550) should not receive the data from NOde 2,but its receiving although its filtered.
     

     //////////////////////////NODE 1//////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    void main(void)
    {
         initialise();
     spi_initialise();
       
       
        while(1)
        {
       can_reset();
       
       can_config();
       
       can_stat();
       can_con();
       can_tx_rx_setup ();
       txvalue = 0x01;
       can_datatx ();
       //DelayMs(250);
       //DelayMs(250);
       while(1)
       {
     
        can_datarx (); 
        PORTD = dataread;
        if(dataread == 0x02)
        {
         txvalue = 0x03;
         can_datatx ();
        // DelayMs(250);
        // DelayMs(250);
        }
        if(dataread == 0x04)
        {
         txvalue = 0x05;
         can_datatx ();
        // DelayMs(250);
        // DelayMs(250);
        }
        if(dataread == 0x06)
        {
         txvalue = 0x01;
         can_datatx ();
        // DelayMs(250);
        // DelayMs(250);
        }
       }
        }
      
     }
    ///////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////
    void can_reset(void)
    {
     chip_enable = 0;    //CHIP SELECT ENABLE
     for(i=0;i<900;i++);
     chip_enable = 0;
     for(i=0;i<90;i++);
     txdata = 0xC0;   //COMMAND FOR RESET.
      spi_read();
     chip_enable = 1;    //CHIP SELECT DISABLE
    }
    //////////////////////////////////////////////////////////////////////////////////////
    void can_config(void)
    {
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //WRITING DATA
      spi_read();
     txdata = 0x0F;  //CANCTRL
      spi_read();
     txdata = 0x90;  //We need to get  into configuration mode so that we can
    write in corresponding configuration registers.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    /////////////////////////////////////////////////////////////////////////////////////////
     
    /////////////////////////////////////////////////////////////////////////////////////////
    void can_con(void)
    {
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x2A;  //CNF 1 :
      spi_read();
     txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x29;  //CNF 2
      spi_read();
     txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment
    Length Bits - 2Tq
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x28;  //CNF 3
      spi_read();
     txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0D;  //TXRTS CTRL
      spi_read();
     txdata = 0x00;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x6F;  //CANCTRL FOR  RXB0D0
      spi_read();
     txdata = 0x00;  //NORMALMODE
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0F;  //NORMALMODE
      spi_read();
     txdata = 0x00;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    /////////////////////////////////////////////////////////////////////////////////////
    void can_tx_rx_setup(void)
    {
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x60;  //RXB0 CTRL
      spi_read();
     txdata = 0b00100000;  //RECeive only valid messages with standard
    identifiers.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE  
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0C;  //BFP CTRL
      spi_read();
     txdata = 0x00;  //All pin functions disabled .
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++); 
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x03;  //Highest message priority.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x31;  //TXB0 SIDH
      spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x32;  //TXB0 SIDL
      spi_read();
     txdata = 0x20;  //Standard Identifier Bits (SD2  - SD0)
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x20;  //RXM0 SIDH
     spi_read();
     txdata = 0xFF;  //Standard Identifier Bits (SD10  - SD3)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x21;  //RXM0 SIDL
     spi_read();
     txdata = 0xF0;  //Standard Identifier Bits (SD2  - SD0)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);

     
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x00;  //RXF0 SIDH
     spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x01;  //RXF0 SIDL
     spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x35;  //TXB0 DLC
      spi_read();
     txdata = 0x01;  //Data Length Code - 01.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void can_datatx (void)
    {

     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x36;  //TXB0 DM
      spi_read();
         txdata = txvalue;  //Transmit Buffer 0 Data Field Bytes
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x0B;  //TXREQ = 1 ,,To request message be transmitted.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<9000;i++);
     while (!FLAG.trxcomplete)
      {
       chip_enable = 0;   //CHIP SELECT ENABLE
       txdata = 0xA0;  //status read command
        spi_read();
       chip_enable = 1;   //CHIP SELECT DISABLE
       rxdata = (rxdata & 0x04) ;//checking status of TXREQ
      
       if(rxdata==0x04)
       {
       FLAG.trxcomplete = 0;
       }
       if(rxdata==0x00)
       {
       FLAG.trxcomplete = 1 ;
       }
      
      }
     
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void can_datarx(void)
    {
      chip_enable = 0;    //CHIP SELECT ENABLE
      txdata = 0xA3;   //READING STATUS
      spi_read();
      rxdata = (rxdata & 0x01) ; //CHECKING RX0IF BIT (0th BIT)
      chip_enable = 1;
      if(rxdata == 0x01)
      {
      FLAG.rxcomplete = 1 ;  //data received
      }
      else
      {
      FLAG.rxcomplete = 0 ;  //data not received
      }
      for(i=0;i<90;i++);
      if(FLAG.rxcomplete = 1)
      {
       FLAG.rxcomplete = 0 ;
       chip_enable = 0;    //CHIP SELECT
    ENABLE
       txdata = 0x03;   //READ DATA
        spi_read();
       txdata = 0x66;   //RXB0D0 
        spi_read();
       txdata = 0x00;
       spi_read();
       dataread = rxdata;//Data received is moved to "rxdata".
       chip_enable = 1;
       for(i=0;i<90;i++);
      
       chip_enable = 0;   //CHIP SELECT ENABLE
       txdata = 0x05;  //BIT MODIFY COMMAND
        spi_read();
       txdata = 0x2C;  //CANINTF ,,ADDRESS
        spi_read();
       txdata = 0x01;  //MASKING VALUE .
       spi_read();
       txdata = 0x00;  //DATA VALUE .clearing RX0IF flag to
    receive next data.
       spi_read();
       chip_enable = 1;
       for(i=0;i<90;i++);
      }
    }
    //////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////NODE 2//////////////////////////////////////////////
     
    ///////////////////////////NODE 2 /////////////////////////////////
    ;
    //////////////////////////////////////////////////////////////////////////////
    void main(void)
    {
        initialise();
     spi_initialise();
        can_reset();
     can_config();
     can_stat();
        can_con();
     can_tx_rx_setup();
        while(1)
        {
       can_datarx();
       PORTA = dataread;
       
          if(dataread == 0x01)
       {
       txvalue = 0x02;
       can_datatx();
       DelayMs(250);
       DelayMs(250);
       }
       if(dataread == 0x03)
       {
       txvalue = 0x04;
       can_datatx();
       DelayMs(250);
       DelayMs(250);
       }
       if(dataread == 0x05)
       {
       txvalue =0x06;
       can_datatx();
       DelayMs(250);
       DelayMs(250);
       }
      
       }
      
     }

    ///////////////////////////////////////
    void can_reset(void)
    {
     PORTC=0x04;    //CHIP SELECT DISABLE
     for(i=0;i<900;i++);
     PORTC=0x00;    //CHIP SELECT ENABLE
     for(i=0;i<90;i++);
     txdata = 0xC0; 
      spi_read();
     PORTC=0x04;    //CHIP SELECT DISABLE
    }
    ////////////////////////////////////////////////////
    void can_config(void)
    {
     PORTC=0x00;    //CHIP SELECT ENABLE
     txdata = 0x02;
      spi_read();
     txdata = 0x0F;   //CANCTRL FOR TXB0D0
      spi_read();
     txdata = 0x90;   //IN CONFIGURATION MODE
      spi_read();
     PORTC=0x04;    //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ////////////////////////////////////////////////////////

    void can_con(void)
    {
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x2A;  //CNF 1 :
      spi_read();
     txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x29;  //CNF 2
      spi_read();
     txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment
    Length Bits - 2Tq
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x28;  //CNF 3
      spi_read();
     txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0D;  //TXRTS CTRL
      spi_read();
     txdata = 0x00;
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x6F;  //CANCTRL FOR  RXB0D0
      spi_read();
     txdata = 0x00;  //NORMALMODE
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;    //CHIP SELECT ENABLE
     txdata = 0x02;
      spi_read();
     txdata = 0x0F;   //CANCTRL FOR TXB0D0
      spi_read();
     txdata = 0x00;   //IN CONFIGURATION MODE
      spi_read();
     PORTC=0x04;    //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ////////////////////////////////////////////////////////////////
    void can_tx_rx_setup(void)
    {
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x60;  //RXB0 CTRL
      spi_read();
     txdata = 0b00100000;  //Mask / filters on .
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE  
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0C;  //BFP CTRL
      spi_read();
     txdata = 0x00;  //RX0BF pin mode disabled
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++); 
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x03;  //Highest message priority.
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x31;  //TXB0 SIDH
      spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x32;  //TXB0 SIDL
      spi_read();
     txdata = 0x40;  //Standard Identifier Bits (SD2  - SD0)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     

     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x35;  //TXB0 DLC
      spi_read();
     txdata = 0x01;  //Data Length Code - 01.
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ////////////////////////////////////////////////////////////////////
    void can_datatx (void)
    {
     
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x36;  //TXB0 DM
      spi_read();
          txdata = txvalue;  //Transmit Buffer 0 Data Field Bytes
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x0B;  //TXREQ = 1 ,,To request message be transmitted.
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<9000;i++);
      while (!trxcomplete)
      {
       PORTC=0x00;   //CHIP SELECT ENABLE
       txdata = 0xA0;  //status read command
        spi_read();
       PORTC=0x04;   //CHIP SELECT DISABLE
       rxdata = (rxdata & 0x04) ;//checking status of TXREQ
      
       if(rxdata==0x04)
       {
       trxcomplete = 0;
       }
       if(rxdata==0x00)
       {
       trxcomplete = 1 ;
       }
      
      }
     
     
    }
    ////////////////////////////////////////////////////////////////////
    void can_datarx(void)
    {
      PORTC=0x00;    //CHIP SELECT ENABLE
      txdata = 0xA3;   //READING STATUS
      spi_read();
      rxdata = (rxdata & 0x01) ; //CHECKING RX0IF BIT (0th BIT)
      PORTC=0x04;
      if(rxdata == 0x01)
      {
      rxcomplete = 1 ;  //data received
      }
      else
      {
      rxcomplete = 0 ;  //data not received
      }
      for(i=0;i<90;i++);
      if(rxcomplete = 1)
      {
       rxcomplete = 0 ;
       PORTC=0x00;    //CHIP SELECT ENABLE
       txdata = 0x03;   //READ DATA
        spi_read();
       txdata = 0x66;   //RXB0D0 
        spi_read();
       txdata = 0x00;
       spi_read();
       dataread = rxdata;
       PORTC=0x04;
       for(i=0;i<90;i++);
      
       PORTC=0x00;   //CHIP SELECT ENABLE
       txdata = 0x05;  //BIT MODIFY COMMAND
        spi_read();
       txdata = 0x2C;  //CANINTF ,,ADDRESS
        spi_read();
       txdata = 0x01;  //MASKING VALUE .
       spi_read();
       txdata = 0x00;  //DATA VALUE .clearing RX0IF flag to
    receive next data.
       spi_read();
       PORTC=0x04;
       for(i=0;i<90;i++);
      }
    }
    //////////////////////////////////////////////////////////////////////////////

     
     
     
    #5
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 04:38:39 (permalink)
    0
    Hi,
    I had a quick scan through your code and I think I can see your "problem/bug".
     
    The Filters & Mask registers can only be set while the CAN engine (your MCP2515) is in config mode. It appears, in your code, that you are trying to set the filters & masks in normal mode.
     
    Cheers
    Artic
    #6
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/27 05:01:06 (permalink)
    0
    Thank you very much for your  advice . Its working well,the mistake you
      pointed out was correct . 
    #7
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/28 01:48:31 (permalink)
    0
    Can you explain the use of Wake up Filter bit in CNF2 (Configuration2 register).
    #8
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/02/28 02:05:55 (permalink)
    0
    Hi,
    Can you explain the use of Wake up Filter bit in CNF2 (Configuration2 register).
    I think you mean in CNF3?
    The Wake-Up bit allows you to add a low-pass-filter on the CAN Rx line of the MCP2515. This filter can be used to stop accidental "wake-ups" of the MCP2515 as a result of electrical noise,etc getting onto the CAN bus.
     
    Have a read in more detail in Section 10.2.1 in the MCP2515 data sheet.
     
    Hope it helps
    Artic
    #9
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/28 04:11:20 (permalink)
    0
     
     Hi
      My problem is not yet over.When I used two nodes
      the filering process was working .When i used the
      third module it is not working .I am attaching
      the configuration and communication set up of
      3 nodes.
     
      My logic is that the node1 act as Master(in my    logic )receiving data from the other 2 nodes.
      My plan is to set filtering for node2 receive that
      then set filter for node3 and receive data.
      Node2 and Node3 will keep sending data.
      ////////Node1///////////////////////////
    void main(void)
    {
         initialise();
     spi_initialise();
       
       
        while(1)
        {
       can_reset();
       
       can_config();
       
      // can_stat();
       can_con();
       can_tx_rx_setup ();
       
       while(1)
       {
        can_config();
        can_filterNODE2 ();
        can_datarx (); 
        PORTD = dataread;
        delay(50000);
        delay(50000);
        txvalue = 0x01;
        can_datatx ();
        can_config();
        can_filterNODE3 ();
        can_datarx (); 
        PORTD = dataread;
        delay(50000);
        delay(50000);
        
       
       }
        }
      
     }
    /////////////////////////////////////////////////////////////////////////////////////////
    void can_con(void)
    {
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x2A;  //CNF 1 :
      spi_read();
     txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x29;  //CNF 2
      spi_read();
     txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment Length Bits - 2Tq
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x28;  //CNF 3
      spi_read();
     txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0D;  //TXRTS CTRL
      spi_read();
     txdata = 0x00;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    //*****************MASKING AND FILTER SECTION****************************************//
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x20;  //RXM0 SIDH
     spi_read();
     txdata = 0xFF;  //Standard Identifier Bits (SD10  - SD3)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     chip_enable = 0; //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
     spi_read();
     txdata = 0x21;  //RXM0 SIDL
     spi_read();
     txdata = 0xF0;  //Standard Identifier Bits (SD2  - SD0)
     spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);

     

    //*********************************************************************************//
     
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0F;  //NORMALMODE
      spi_read();
     txdata = 0x00;
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
    }
    /////////////////////////////////////////////////////////////////////////////////////
    void can_tx_rx_setup(void)
    {
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x60;  //RXB0 CTRL
      spi_read();
     txdata = 0b00100000;  //RECeive only valid messages with standard identifiers.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE  
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0C;  //BFP CTRL
      spi_read();
     txdata = 0x00;  //All pin functions disabled .
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++); 
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x03;  //Highest message priority.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x31;  //TXB0 SIDH
      spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x32;  //TXB0 SIDL
      spi_read();
     txdata = 0x20;  //Standard Identifier Bits (SD2  - SD0)
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     
     
     chip_enable = 0;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x35;  //TXB0 DLC
      spi_read();
     txdata = 0x01;  //Data Length Code - 01.
      spi_read();
     chip_enable = 1;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ////////////////////////////////////////////////////
    void can_filterNODE2 (void)
    {
    //******Changing Filter values to receive value from NODE2 ****************************************************************************//
        PORTC=0x00;  //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
        txdata = 0x00;  //RXF0 SIDH
        spi_read();
        txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
        spi_read();
        PORTC=0x04;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
        PORTC=0x00;  //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
        txdata = 0x01;  //RXF0 SIDL
        spi_read();
        txdata = 0x40;  //Standard Identifier Bits (SD2  - SD0)
        spi_read();
        PORTC=0x04;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
    );
        chip_enable = 0;   //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
         spi_read();
        txdata = 0x0F;  //NORMALMODE
         spi_read();
        txdata = 0x00;
         spi_read();
        chip_enable = 1;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
    //**********************************************************************************
        
    }
    /////////////////////////////////////////////////////////////////////////////////
    void can_filterNODE3 (void)
    {
    //******Changing Filter values to receive value from NODE2 ****************************************************************************//
        PORTC=0x00;  //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
        txdata = 0x00;  //RXF0 SIDH
        spi_read();
        txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
        spi_read();
        PORTC=0x04;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
        PORTC=0x00;  //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
        txdata = 0x01;  //RXF0 SIDL
        spi_read();
        txdata = 0x60;  //Standard Identifier Bits (SD2  - SD0)
        spi_read();
        PORTC=0x04;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
     
        chip_enable = 0;   //CHIP SELECT ENABLE
        txdata = 0x02;  //COMMAND TO WRITE
         spi_read();
        txdata = 0x0F;  //NORMALMODE
         spi_read();
        txdata = 0x00;
         spi_read();
        chip_enable = 1;   //CHIP SELECT DISABLE
        for(i=0;i<90;i++);
    }
    ///////////////////////////////////////////////////////////
    /////////////////Node2 //////////////////////////////////////
    void main(void)
    {
        initialise();
     spi_initialise();
        can_reset();
     can_config();
     can_stat();
        can_con();
     can_tx_rx_setup();
        while(1)
        {
      
       txvalue = 0x66;
       can_datatx();
      
      
       }
      
     }
    void can_con(void)
    {
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x2A;  //CNF 1 :
      spi_read();
     txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x29;  //CNF 2
      spi_read();
     txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment Length Bits - 2Tq
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x28;  //CNF 3
      spi_read();
     txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0D;  //TXRTS CTRL
      spi_read();
     txdata = 0x00;
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    //****************MASKING AND FILTER SECTION***************************************************************//
    // PORTC=0x00;  //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x20;  //RXM0 SIDH  (MASK HIGH BYTE)
    // spi_read();
    // txdata = 0xFF;  //Standard Identifier Bits (SD10  - SD3)
    // spi_read();
    // PORTC=0x04;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    // 
    // PORTC=0x00;  //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x21;  //RXM0 SIDL  (MASK LOW BYTE)
    // spi_read();
    // txdata = 0xF0;  //Standard Identifier Bits (SD2  - SD0)
    // spi_read();
    // PORTC=0x04;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    //
    // 
    // PORTC=0x00;  //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x00;  //RXF0 SIDH
    // spi_read();
    // txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
    // spi_read();
    // PORTC=0x04;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    // PORTC=0x00;  //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x01;  //RXF0 SIDL
    // spi_read();
    // txdata = 0x20;  //Standard Identifier Bits (SD2  - SD0)
    // spi_read();
    // PORTC=0x04;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++
    //*******************************************************************************//
     
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x6F;  //CANCTRL FOR  RXB0D0
      spi_read();
     txdata = 0x00;  //NORMALMODE
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;    //CHIP SELECT ENABLE
     txdata = 0x02;
      spi_read();
     txdata = 0x0F;   //CANCTRL FOR TXB0D0
      spi_read();
     txdata = 0x00;   //Normal MODE
      spi_read();
     PORTC=0x04;    //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
    ////////////////////////////////////////////////////////////////
    void can_tx_rx_setup(void)
    {
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x60;  //RXB0 CTRL
      spi_read();
     txdata = 0b00100000;  //Mask / filters on .
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE  
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x0C;  //BFP CTRL
      spi_read();
     txdata = 0x00;  //RX0BF pin mode disabled
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++); 
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x30;  //TXB0 CTRL
      spi_read();
     txdata = 0x03;  //Highest message priority.
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    //***************Transmitter Identifier bits****************************************************************// 
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x31;  //TXB0 SIDH
      spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x32;  //TXB0 SIDL
      spi_read();
     txdata = 0x40;  //Standard Identifier Bits (SD2  - SD0)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    //*************************************************************************************** 
     

     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x35;  //TXB0 DLC
      spi_read();
     txdata = 0x01;  //Data Length Code - 01.
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    }
     
     Node3 is similar to node2 only difference is identifier bits and data send.
      //***************Transmitter Identifier bits****************************************************************// 
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x31;  //TXB0 SIDH
      spi_read();
     txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
     PORTC=0x00;   //CHIP SELECT ENABLE
     txdata = 0x02;  //COMMAND TO WRITE
      spi_read();
     txdata = 0x32;  //TXB0 SIDL
      spi_read();
     txdata = 0x60;  //Standard Identifier Bits (SD2  - SD0)
      spi_read();
     PORTC=0x04;   //CHIP SELECT DISABLE
     for(i=0;i<90;i++);
    #10
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/02/28 04:27:51 (permalink)
    0
    Hi,
    If two nodes are working - then you can be sure that your code is OK(ish). If you are having problems with the third node, try the following;
     
    Use the third node hardware and program it with the second node software - this will prove that the third node hardware is working correctly (or not)
     
    It really sounds to me like there is a "bug" somewhere. Sorry, but I'm not going to bother to chase down every problem/bug you have. If you want, you can pay me to write the application codes for you.
     
    Hope the suggestion helps
    Artic
    #11
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/02/28 05:08:33 (permalink)
    0
    Thank you ,I will check it out once more .
    #12
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/03/26 23:38:46 (permalink)
    0
    Hello Artic,
                   Now I have problem in loading filter registers.Can you check on this?Please.
    #13
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: Message filtering in CAN 2008/03/27 00:06:35 (permalink)
    0
    Hi,
    The filters (like other CAN Engine registers) are setup when you place the CAN engine is in CONFIG mode.

    I suggest you check that you are selecting Config mode correctly before you write the filter values.

    Hope it helps
    Artic
    #14
    syamuk2005
    Starting Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2008/01/18 21:31:45
    • Location: 0
    • Status: offline
    RE: Message filtering in CAN 2008/03/27 01:35:28 (permalink)
    0
    Hai Artic
    Thank you for refering this .
    I am loading filter registers in config mode.Still the value is not loaded.
    I am attaching my program ,and I have marked the configuration in bold format
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void main(void)
    {
        initialise();
    spi_initialise();
      
      
       while(1)
       {
      can_reset();
      
      can_config();
      
      can_con();
      can_tx_rx_setup ();
      can_config();
       can_filterNODE2 ();
      while(1)
      {
      
       

       can_datarx (); 
       PORTD = dataread;
       delay(50000);
       delay(50000);

      
          }
       }
     
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////
    void initialise(void)
    {

    TRISA =0x00;
    ADCON1 =0X0F;
    TRISB =0b00000001;
    TRISC = 0x00;
    TRISD =0x00;
    // PORTA=0x00;
    PORTD=0x00;
    PORTB=0B01000000;
    PORTC = 0x00;

    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void spi_initialise(void)
    {

    SSPCON1bits.SSPEN =0;
    SSPSTATbits.SMP =1;
    SSPSTATbits.CKE =1;   //CKE =1
    SSPCON1bits.SSPM3 =0;
    SSPCON1bits.SSPM2 =0;
    SSPCON1bits.SSPM1 =0;
    SSPCON1bits.SSPM0 =0;

    SSPCON1bits.CKP =0;   //CKP =0
    SSPCON1bits.SSPEN =1;
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void spi_read(void)
    {
    SSPBUF = txdata;
    while(!PIR1bits.SSPIF)
    {
    }   
    rxdata = SSPBUF;
    PIR1bits.SSPIF =0; 
    }
    /////////////////////////////////////////////////////////////////////////////////
    void can_reset(void)
    {
    chip_enable = 0;    //CHIP SELECT ENABLE
    for(i=0;i<900;i++);
    chip_enable = 0;
    for(i=0;i<90;i++);
    txdata = 0xC0;   //COMMAND FOR RESET.
    spi_read();
    chip_enable = 1;    //CHIP SELECT DISABLE
    }
    //////////////////////////////////////////////////////////////////////////////////////
    void can_config(void)
    {
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //WRITING DATA
    spi_read();
    txdata = 0x0F;  //CANCTRL
    spi_read();
    txdata = 0x80;  //We need to get  into configuration mode so that we can write in corresponding configuration registers.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    }
    /////////////////////////////////////////////////////////////////////////////////////////
    void can_stat(void)
    {
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x03;  //READING DATA FROM REGISTER
    spi_read();
    txdata = 0x35;  //
    spi_read();
    txdata = 0x00;  //Sending dummy data for reading via SPI .
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    }

    /////////////////////////////////////////////////////////////////////////////////////////
    void can_con(void)
    {
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x2A;  //CNF 1 :
    spi_read();
    txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x29;  //CNF 2
    spi_read();
    txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment Length Bits - 2Tq
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x28;  //CNF 3
    spi_read();
    txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x0D;  //TXRTS CTRL
    spi_read();
    txdata = 0x00;
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    //*****************MASKING AND FILTER SECTION****************************************//
    chip_enable = 0; //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x20;  //RXM0 SIDH
    spi_read();
    txdata = 0xFF; //Standard Identifier Bits (SD10  - SD3)
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);

    chip_enable = 0; //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x21;  //RXM0 SIDL
    spi_read();
    txdata = 0xF0;  //Standard Identifier Bits (SD2  - SD0)
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    //************************************************************************************//
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x0F;  //NORMALMODE
    spi_read();
    txdata = 0x00;
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);

    }
    /////////////////////////////////////////////////////////////////////////////////////
    void can_tx_rx_setup(void)
    {
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x60;  //RXB0 CTRL
    spi_read();
    txdata = 0b00100000;  //RECeive only valid messages with standard identifiers.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE  
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x70;  //RXB0 CTRL
    spi_read();
    txdata = 0b00100000;  //RECeive only valid messages with standard identifiers.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE  
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x0C;  //BFP CTRL
    spi_read();
    txdata = 0x00;  //All pin functions disabled .
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++); 
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x30;  //TXB0 CTRL
    spi_read();
    txdata = 0x03;  //Highest message priority.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);

    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x31;  //TXB0 SIDH
    spi_read();
    txdata = 0x00;  //Standard Identifier Bits (SD10 - SD3)
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x32;  //TXB0 SIDL
    spi_read();
    txdata = 0x20;  //Standard Identifier Bits (SD2  - SD0)
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);


    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x35;  //TXB0 DLC
    spi_read();
    txdata = 0x01;  //Data Length Code - 01.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void can_datatx (void)
    {

    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x36;  //TXB0 DM
    spi_read();
        txdata = txvalue;  //Transmit Buffer 0 Data Field Bytes
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<90;i++);
    chip_enable = 0;   //CHIP SELECT ENABLE
    txdata = 0x02;  //COMMAND TO WRITE
    spi_read();
    txdata = 0x30;  //TXB0 CTRL
    spi_read();
    txdata = 0x0B;  //TXREQ = 1 ,,To request message be transmitted.
    spi_read();
    chip_enable = 1;   //CHIP SELECT DISABLE
    for(i=0;i<9000;i++);
    while (!FLAG.trxcomplete)
     {
      chip_enable = 0;   //CHIP SELECT ENABLE
      txdata = 0xA0;  //status read command
       spi_read();
      chip_enable = 1;   //CHIP SELECT DISABLE
      rxdata = (rxdata & 0x04) ;//checking status of TXREQ
     
      if(rxdata==0x04)
      {
      FLAG.trxcomplete = 0;
      }
      if(rxdata==0x00)
      {
      FLAG.trxcomplete = 1 ;
      }
     
     }

    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void can_datarx(void)
    {
     chip_enable = 0;    //CHIP SELECT ENABLE
     txdata = 0xA3;   //READING STATUS
     spi_read();
     rxdata = (rxdata & 0x01) ; //CHECKING RX0IF BIT (0th BIT)
     chip_enable = 1;
     if(rxdata == 0x01)
     {
     FLAG.rxcomplete = 1 ;  //data received
     }
     else
     {
     FLAG.rxcomplete = 0 ;  //data not received
     }
     for(i=0;i<90;i++);
     if(FLAG.rxcomplete = 1)
     {
      FLAG.rxcomplete = 0 ;
      chip_enable = 0;    //CHIP SELECT ENABLE
      txdata = 0x03;   //READ DATA
       spi_read();
      txdata = 0x66;   //RXB0D0 
       spi_read();
      txdata = 0x00;
      spi_read();
      dataread = rxdata; //Data received is moved to "rxdata".
      chip_enable = 1;
      for(i=0;i<90;i++);
     
      chip_enable = 0;   //CHIP SELECT ENABLE
      txdata = 0x05;  //BIT MODIFY COMMAND
       spi_read();
      txdata = 0x2C;  //CANINTF ,,ADDRESS
       spi_read();
      txdata = 0x01;  //MASKING VALUE .
      spi_read();
      txdata = 0x00;  //DATA VALUE .clearing RX0IF flag to receive next data.
      spi_read();
      chip_enable = 1;
      for(i=0;i<90;i++);
     }
    }
    //////////////////////////////////////////////////////////////////////////////////////////
    void can_filterNODE2 (void)
    {

    //******Changing Filter values to receive value from NODE2 ****************************************************************************//
       PORTC=0x00;  //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
       spi_read();
       txdata = 0x00;  //RXF0 SIDH
       spi_read();
       txdata = 0x00;  //
       spi_read();
       PORTC=0x04;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);

        PORTC=0x00;  //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
       spi_read();
       txdata = 0x01;  //RXF0 SIDL
       spi_read();
       txdata = 0x40;  //
       spi_read();
       PORTC=0x04;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);

     
        chip_enable = 0;   //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
       txdata = 0x0F;  //NORMALMODE
       spi_read();
       txdata = 0x00;
        spi_read();
       chip_enable = 1;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);

    //**********************************************************************************
       
    }
    /////////////////////////////////////////////////////////////////////////////////
    void can_filterNODE3 (void)
    {
    //chip_enable = 0;   //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    //  spi_read();
    // txdata = 0x2A;  //CNF 1 :
    //  spi_read();
    // txdata = 0x04;      // BRP(BAUD RATE PRESCALAR ) = 04;
    //  spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    // chip_enable = 0;   //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    //  spi_read();
    // txdata = 0x29;  //CNF 2
    //  spi_read();
    // txdata = 0xB1;  //CHECK/////PS1 Length bits - 7Tq,,,,Propagation Segment Length Bits - 2Tq
    //  spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    // chip_enable = 0;   //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    //  spi_read();
    // txdata = 0x28;  //CNF 3
    //  spi_read();
    // txdata = 0x05;  // PS2 Length Bits --6Tq//////////CHECK
    //  spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    // chip_enable = 0;   //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    //  spi_read();
    // txdata = 0x0D;  //TXRTS CTRL
    //  spi_read();
    // txdata = 0x00;
    //  spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    ////*****************MASKING AND FILTER SECTION****************************************//
    //
    // chip_enable = 0; //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x20;  //RXM0 SIDH
    // spi_read();
    // txdata = 0xFF;  //Standard Identifier Bits (SD10  - SD3)
    // spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    // 
    // chip_enable = 0; //CHIP SELECT ENABLE
    // txdata = 0x02;  //COMMAND TO WRITE
    // spi_read();
    // txdata = 0x21;  //RXM0 SIDL
    // spi_read();
    // txdata = 0xF0;  //Standard Identifier Bits (SD2  - SD0)
    // spi_read();
    // chip_enable = 1;   //CHIP SELECT DISABLE
    // for(i=0;i<90;i++);
    //
    //******Changing Filter values to receive value from NODE2 ****************************************************************************//
       PORTC=0x00;  //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
       spi_read();
       txdata = 0x00;  //RXF0 SIDH
       spi_read();
       txdata = 0x00;  //Standard Identifier Bits (SD10  - SD3)
       spi_read();
       PORTC=0x04;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);
       PORTC=0x00;  //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
       spi_read();
       txdata = 0x01;  //RXF0 SIDL
       spi_read();
       txdata = 0x80;  //Standard Identifier Bits (SD2  - SD0)
       spi_read();
       PORTC=0x04;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);

       chip_enable = 0;   //CHIP SELECT ENABLE
       txdata = 0x02;  //COMMAND TO WRITE
        spi_read();
       txdata = 0x0F;  //NORMALMODE
       spi_read();
       txdata = 0x00;
        spi_read();
       chip_enable = 1;   //CHIP SELECT DISABLE
       for(i=0;i<90;i++);
    }
    //**********************************************************************************
    void delay(unsigned int delayvar)
    {
    unsigned int n;
    for (n=0;n<=delayvar;n++)
    {
    }
    }
    //////////////////////////////////////////////////////////    


    post edited by syamuk2005 - 2008/03/27 02:25:28
    #15
    Jump to:
    © 2020 APG vNext Commercial Version 4.5