Go Down

Topic: MEGA 2560 and CAN-Bus Shield? //Solved!! (Read 11140 times) previous topic - next topic

gggplaya

Sep 18, 2012, 07:18 pm Last Edit: Sep 19, 2012, 06:19 pm by gggplaya Reason: 1
I can get the CAN shield to work on my UNO no problem but i can't get it to work on my Mega 2560. Physically i've wired the SPI lines to the CAN-Bus shield at the same pin locations as the UNO, but i can't find where to change the SPI pins in software? I've tried SPI.h and it's labeled fine on pins_arduino.h but i don't think it's looking at the correct pins_arduin.h.

Does anyone know how to get the CAN-BUS shield code to look at the correct SPI pins?  I'm using the seed studio CAN-Bus Shield and code.


Code: [Select]
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>

void setup()
{
 Serial.begin(115200);
 // init can bus, baudrate: 500k
 if(CAN.begin(CAN_250KBPS) ==CAN_OK) Serial.print("CAN init ok!!\r\n");
 else Serial.print("CAN init fail!!\r\n");
}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
 // send data:  id = 0x00, standard frame, data length = 8, stmp: data buf
 CAN.sendMsgBuf(0xFEB5, 1, 8, stmp);  
 delay(1000);
}

PaulS

Quote
i've wired the SPI lines to the CAN-Bus shield at the same pin locations as the UNO, but i can't find where to change the SPI pins in software?

That's because you can't. They are wired in the hardware. You need to modify the shield, if it doesn't use SPI via the ICSP connector, like the Ethernet shield does.
The art of getting good answers lies in asking good questions.

gggplaya

Well here's the thing, it does work when wired like this.  I simply clipped the header pins at location 10,11,12,13 and wired in 50,51,52, and 53 from the MEGA.   Using modified  code from the sparkfun can-shield the board works just fine like this.  I can read and transmit CAN from the MEGA.  The problem is i need 29bit CAN which the sparkfun code does not have provisions for, it only does 11bit. 

That brings me to the original seed studio code which does support 29bit CAN.  It works great on my UNO, but i need the extra serial ports on the MEGA for other stuff.  I know this wiring works because it works with different code.  I just can't see how to get the seed studio code to use the other pins for SPI.  I tried following the code through SPI.H and i see where it references MISO, MOSI, SCK, and SS in the pins_arduino.h file.  I see after i compile that it infact looks into the \variants\mega folder for this file.  So everything should be right, i just don't understand why it's not working.  Yet, the arduino UNO works fine?

gggplaya

OK, finally figured it out after following the code line by line.  It was in the mcp_can_dfs.h file and wasn't readily apparent because of the way it was worded\coded.

Code: [Select]
//#define SPICS 10 ///////////////////////Standard Arduino
#define SPICS 53 ////////////////////////Arduino Mega
#define MCP2515_SELECT()   digitalWrite(SPICS, LOW)
#define MCP2515_UNSELECT() digitalWrite(SPICS, HIGH)



As you can see above, the SPICS is worded funny, i would have made the variable SPI_CS to make it clear.  A little underscore goes a long way.  As you can see in the code, i changed the CS pin to 53 here.

PaulS

Quote
It was in the mcp_can_dfs.h file and wasn't readily apparent because of the way it was worded\coded.

The compiler can figure out whether you are compiling for a Mega, or not, and should use the correct #define. It is simply laziness on the part of the library developer to require you to edit source files to use a different board.

I'd fire a complaint off to the developer.
The art of getting good answers lies in asking good questions.

cristy2007

The error I received when running this code...
Waht to do?

KingAlex1985

Hello gggplaya,

you have written, that u can use your CAN-BUS Shield with your Arduino UNO.
Maybe you can help me / us to solve the this problem:
http://forum.arduino.cc/index.php?topic=290283.msg2218159#msg2218159

I'm haveing an Arduino UNO R3 Board and a CAN-BUS Shield v1.2 from Seeed Studio.
I'm useing the example code from the seeed-studio Wiki:
http://www.seeedstudio.com/wiki/CAN-BUS_Shield

Thank you in advance.

Kind regards
Alex

Go Up