Programming multiple NODE for CAN BUS SHEILD ?

Ardiuno Compatible shields

Moderators: lily.li, violet

nw_giri
Pre-kindergarten
Pre-kindergarten
Posts: 12
Joined: Mon Mar 11, 2013 12:48 pm

Programming multiple NODE for CAN BUS SHEILD ?

Post by nw_giri » Mon Mar 11, 2013 1:03 pm

Hi all, i have been trying to get the CAN bus recieve code to work from the wiki site,

And it's work ,, unfortunately in wiki , just for 2 Node.. :cry:

Now , I try to communicate using 3 Can-bus shield , 1 for master , others for slave.

(Master for reading all input that given by slave like RPM, Voltage) :idea:

I'm using Arduino R3 and CAN BUS SHIELD from Seeedstudio ,,

Has anyone ever tried to do it before :?: :?:

Any comments will be appreciate,

Thanks

Gerry

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Wed Mar 13, 2013 10:48 pm

hi Gerry, do you want one CANBUS send data and the other two receive ?
if so, you can set the receive mask to make it works~

nw_giri
Pre-kindergarten
Pre-kindergarten
Posts: 12
Joined: Mon Mar 11, 2013 12:48 pm

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by nw_giri » Fri Mar 15, 2013 11:27 am

loovee wrote:hi Gerry, do you want one CANBUS send data and the other two receive ?
if so, you can set the receive mask to make it works~
Hi loovee ,,

Great thanks ,you have answered me ,,:)

yes ,but I mean one CAN BUS for receive and the other send data.

btw could you make simple tutorial how to do use masking and filtering ?

I'm newbie learning in CAN ,,

Thanks
Last edited by nw_giri on Fri Mar 15, 2013 11:46 am, edited 1 time in total.

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Fri Mar 15, 2013 11:42 am

hi Gerry, you can use INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData) to config the mask and init_Filt(INT8U num, INT8U ext, INT32U ulData) to config the filt.

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Fri Mar 15, 2013 11:45 am

as i know ~ one can bus for receive and many for send data is ok~ do you meet any problem ? :D

nw_giri
Pre-kindergarten
Pre-kindergarten
Posts: 12
Joined: Mon Mar 11, 2013 12:48 pm

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by nw_giri » Fri Mar 15, 2013 12:34 pm

I try using Masking and reading output data using serial monitor in extended mode..

sorry ,, I edited the code,,but still no data output in serial monitor
Please correct me if there are any mistakes

  • CAN RX modul
    Enter setting mode success
    set rate success!!
    Enter Normal Mode Success!!
    Begin to set Mask!!
    set Mask success!!
    Begin to set Filter!!
    set Filter success!!

Code: Select all


//RECEIVE CODE//
#include "mcp_can.h"
#include <SPI.h>
//#include <stdio.h>
#define INT8U unsigned char

//boolean firstTime = false;
INT8U Flag_Recv = 0;
//INT8U len = 0;
INT8U ID1 = 0x700;
//INT8U ID2 = 0;
INT8U buf[8];
//const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
//int buttonState = 0;   
//char str[20];
void setup()
{
  Serial.begin(115200);
  Serial.println("CAN RX modul ");
  CAN.begin(CAN_500KBPS);
  CAN.init_Mask(0,1, 0);         /* init Masks                   */
  CAN.init_Filt(0, 1, 0);        /* init filters                    */
  attachInterrupt(0, MCP2515_ISR, FALLING); // digital pin 2
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
 
}

void MCP2515_ISR()
{
  Flag_Recv = 1;
}

void loop()
{
  if(Flag_Recv)
  {

    Flag_Recv = 0;
    //CAN.readMsgBuf(&len, buf);
     
    ID1=CAN.getCanId();
    ID1=CAN.checkReceive();
    Serial.print("Ready");
    CAN.readMsgBuf(&ID1, buf);
    
    if (buf[0]==0){
    // turn LED on:     
    digitalWrite(ledPin, HIGH);
    Serial.print("LED ON ");  
    } 
    else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Serial.print("LED OFF "); 
      
    }   
  
  }
}

Code: Select all


// SENDING CODE //

#include "mcp_can.h"
#include <SPI.h>
INT8U ID1=0x700;
INT8U buf[8];
const int buttonPin = 4; 
// variables will change:
int buttonState = 0; 
//INT32U ID2=0x7e8;
void setup()
{
  Serial.begin(115200);
  if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
  else Serial.print("Can init fail!!\r\n");
  pinMode(buttonPin, INPUT); 
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  ID1=CAN.getCanId();
     if (digitalRead (buttonPin) == LOW)
     {
       buf[0] = 0;
       CAN.sendMsgBuf(ID1,1,8,buf);
       Serial.println ("Sent 0");
     }
     else
     {
        buf[0] = 1;
        CAN.sendMsgBuf(ID1,1,8,buf);
        Serial.println ("sent one");
     }
     delay (10);   


}
Last edited by nw_giri on Sun Mar 17, 2013 10:30 am, edited 4 times in total.

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Sat Mar 16, 2013 8:46 am

seems right!! I'll try next Monday~ I don't have a canbus shield by hand! :?

nw_giri
Pre-kindergarten
Pre-kindergarten
Posts: 12
Joined: Mon Mar 11, 2013 12:48 pm

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by nw_giri » Wed Mar 20, 2013 10:07 am

still no luck... :( :(

init successfull but no data receive..

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Thu Mar 21, 2013 11:04 am

nw_giri wrote:still no luck... :( :(

init successfull but no data receive..

hey~ I got a canbus shield finally ~ and I had modified you code, just a little~ and it works~ :)

Code: Select all

// SENDING CODE //
#include "mcp_can.h"
#include <SPI.h>

INT8U ID1=0x700;
INT8U buf[8]= {0, 0, 0, 0, 0, 0, 0, 0};         // you should init it first
const int buttonPin = 4;

int buttonState = 0;

void setup()
{
    Serial.begin(115200);
    if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
    else Serial.print("Can init fail!!\r\n");
    pinMode(buttonPin, INPUT);
}

void loop()
{
    buttonState = digitalRead(buttonPin);
    //ID1=CAN.getCanId();       when you get some data from can bus, you can use this function to get who send this
        
    if (digitalRead (buttonPin) == LOW)
    {
        buf[0] = 0;
        CAN.sendMsgBuf(ID1,1,8,buf);
        Serial.println ("Sent 0");
    }
    else
    {
        buf[0] = 1;
        CAN.sendMsgBuf(ID1,1,8,buf);
        Serial.println ("sent one");
    }
    
    delay (10);


}

Code: Select all

//RECEIVE CODE//
#include "mcp_can.h"
#include <SPI.h>
//#include <stdio.h>
#define INT8U unsigned char

//boolean firstTime = false;
INT8U Flag_Recv = 0;
//INT8U len = 0;
INT8U ID1 = 0x700;
//INT8U ID2 = 0;
INT8U buf[8];
//const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
//int buttonState = 0;
//char str[20];
void setup()
{
    Serial.begin(115200);
    Serial.println("CAN RX modul ");
    CAN.begin(CAN_500KBPS);
    CAN.init_Mask(0,1, 0);         /* init Masks                   */
    CAN.init_Filt(0, 1, 0);        /* init filters                    */
    attachInterrupt(0, MCP2515_ISR, FALLING); // digital pin 2
    // initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);

}

void MCP2515_ISR()
{
    Flag_Recv = 1;
}

void loop()
{
    if(Flag_Recv)
    {

        Flag_Recv = 0;
        ID1=CAN.getCanId();
        Serial.print("Ready");
        CAN.readMsgBuf(&ID1, buf);

        if (buf[0]==0){
            digitalWrite(ledPin, HIGH);
            Serial.println("LED ON ");
        }
        else {
            // turn LED off:
            digitalWrite(ledPin, LOW);
            Serial.println("LED OFF ");

        }

    }
}

you can try it~ any problem please feel free to contract me!
cheers~ :lol:

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: Programming multiple NODE for CAN BUS SHEILD ?

Post by loovee » Thu Mar 21, 2013 11:09 am

nw_giri wrote:still no luck... :( :(

init successfull but no data receive..
Actually , your code works fine also~ do you download the latest library?

Post Reply