Go Down

Topic: Arduino Mega 2560 and M0 (Read 262 times) previous topic - next topic

Kranvonkyln

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.

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.

aapatil

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.

Kranvonkyln

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.




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.

pylon

Quote
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?

Kranvonkyln

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 ?

Juraj

#6
Aug 11, 2018, 05:54 pm Last Edit: Aug 11, 2018, 05:59 pm by Juraj
M0 doesn't have SPI on 11, 12, 13.
only on the fake ICSP header and only master
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Kranvonkyln

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

Juraj

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
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Kranvonkyln

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

Juraj

#10
Aug 12, 2018, 06:37 am Last Edit: Aug 12, 2018, 06:38 am by Juraj
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

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Kranvonkyln

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?

pylon

Quote
Over SPI go 8Bit how can I transfer 12 Bit?
By transferring 16 bit (2 bytes) and forgetting 4 bits of that transfer.

Go Up