Arduino Mega 2560 and M0

Hello,

I’m trying to connect Arduino Mega 2560 and Arduino M0 thru SPI. I wrote some code but it doesn’t work.

Master:

#include <SPI.h>
#include “pins_arduino.h”

void setup()
{
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop(void)
{
char c;
digitalWrite(SS, LOW);
for (const char *p=“Hello\n”; c = *p; p++){
SPI.transfer(c);
digitalWrite(SS, HIGH);
delay (500);
}
}

Slave:

#include <SPI.h>
#include “pins_arduino.h”

char buf[100];
volatile byte pos;
volatile boolean process_it;

void setup()
{
Serial.begin(9600);
Serial.println(“SPI test”);
pinMode(MISO, OUTPUT);
//digitalWrite(MISO, LOW);
SPCR |= _BV(SPE);
pos = 0;
process_it = true;
SPI.attachInterrupt();
}

ISR (SPI_STC_vect){
byte c = SPDR;
if(pos < sizeof buf)
{
buf[pos++] = c;

if(c ==‘y’){
process_it = true;
}
}
}

void loop (void)
{
if(process_it){
Serial.println(buf);
Serial.println(“X”);
pos=0;
process_it = false;
}
}

It looks like ISR (SPI_STC_vect) wouldn’t work. c is allways 0 and buf ist empty.

Mega2560 and M0 have different voltage levels. Do you use level converter for all SPI signals?

If you connected them directly check the pins on the M0 for damage as the 5V of the Mega2560 are over specs.

Kranvonkyln:
digitalWrite(SS, LOW);
for (const char *p=“Hello\n”; c = *p; p++){
SPI.transfer(c);
digitalWrite(SS, HIGH);
delay (500);
}

Looking at this piece of code, you pull SS low, transfer code, pull SS high, transfer code again, pull SS high, etc. You never pull SS low again, so you wouldn’t be able to transfer anything.

pylon:
Mega2560 and M0 have different voltage levels. Do you use level converter for all SPI signals?

If you connected them directly check the pins on the M0 for damage as the 5V of the Mega2560 are over specs.

What do you mean with different voltage levels? No I'm not using level converter.

aapatil:
Looking at this piece of code, you pull SS low, transfer code, pull SS high, transfer code again, pull SS high, etc. You never pull SS low again, so you wouldn't be able to transfer anything.

Yes, my bad. I took SS high out of for loop but it didn't help.

What do you mean with different voltage levels? No I'm not using level converter.

The Mega2560 runs on 5V so do all of it's IO signals. The M0 runs on 3V3 and so do all it's IO signals. If you put 5V to one of the pins of the M0 you might fry it.

BTW: we haven't seen your wiring diagram yet. Did you connect the GNDs?

Yes i connected the GND, I’m posting my wiring. That’s M0 and Mega 2560 in my program there is no M0.
About voltage level, M0 works fine so I think i didn’t fry it. Can the different voltage level be the issue ?

M0 doesn't have SPI on 11, 12, 13.
only on the fake ICSP header and only master

So what does it mean? M0 can’t comunicate through SPI?

Kranvonkyln:
So what does it mean? M0 can't comunicate through SPI?

you can connect SPI slaves to the ICSP header .

for slave it is more work and a level deeper
https://forums.adafruit.com/viewtopic.php?f=25&t=94366

I want to use M0 as a Master and Mega 2560 as a slave.

Kranvonkyln:
I want to use M0 as a Master and Mega 2560 as a slave.

I like this picture by Nick Gamon. The M0 hasn’t the SPI function on 11, 12, 13 but the ISCP header is the same, including 5 V VCC

ArduinoUno_R3_Pinouts.png

I feel so stupid, thank you very much, I've used wrong Pins. Now when I'm using ICSP it works. Can I ask one more question? Over SPI go 8Bit how can I transfer 12 Bit?

Over SPI go 8Bit how can I transfer 12 Bit?

By transferring 16 bit (2 bytes) and forgetting 4 bits of that transfer.