SPI issues... works on the NANO, not on the MEGA. Pin numbers are right. Ideas?

I am debugging a SPI connection with another device. I am using the NANO for the project. I want an extra serial port to assist with debugging so I switched to the MEGA. This code works correctly on the NANO:

#include <digitalWriteFast.h>
#include <SPI.h>

#define DATAOUT 11//MOSI
#define DATAIN  12//MISO 
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss

byte inByte;
byte cypressByte;



void setup(){
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);//SS must be output to function as master
  digitalWrite(SLAVESELECT,HIGH);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  SPI.setDataMode(SPI_MODE2);
  Serial.begin(57600);
  digitalWrite(SLAVESELECT,LOW);
}

void loop(){
  while(Serial.available()>0){
    inByte = Serial.read();
    //Serial.write(inByte);
    cypressByte=SPI.transfer(inByte);
    //digitalWriteFast(SLAVESELECT,HIGH);
    Serial.write(cypressByte);
  }
}

The nearly identical code does not work on the MEGA:

#include <digitalWriteFast.h>
#include <SPI.h>

#define DATAOUT 51//MOSI
#define DATAIN  50//MISO 
#define SPICLOCK  52//sck
#define SLAVESELECT 53//ss

byte inByte;
byte cypressByte;



void setup(){
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);//SS must be output to function as master
  digitalWrite(SLAVESELECT,HIGH);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  SPI.setDataMode(SPI_MODE2);
  Serial.begin(57600);
  digitalWrite(SLAVESELECT,LOW);
}

void loop(){
  while(Serial.available()>0){
    inByte = Serial.read();
    //Serial.write(inByte);
    cypressByte=SPI.transfer(inByte);
    //digitalWriteFast(SLAVESELECT,HIGH);
    Serial.write(cypressByte);
  }
}

I have checked and re-checked the connections. I have scoped both the MOSI line and the SCK line. Both stay at their idle state even when trying to transmit. This issue is happening on a rev 3 MEGA and also on an OSEPP rev 1.1 MEGA. Both boards are brand new. Any help with this situation would be greatly appreciated. The device I was connection to had no trouble running at SPI_CLOCK_DIV_2 on the NANO. I slowed down the connection to accommodate the inexpensive scope I am using.

mikesbaker:

#define DATAOUT 51//MOSI

#define DATAIN  50//MISO
#define SPICLOCK  52//sck
#define SLAVESELECT 53//ss

Shouldn't that be:

#define DATAOUT      50 //MOSI
#define DATAIN         51 //MISO 
#define SPICLOCK      52 //sck
#define SLAVESELECT 53 //ss

I'm pretty sure 11->50, 12->51, 13->52.

nope, he's got the pins right: SPI - Arduino Reference

#include <digitalWriteFast.h>

You assume digitalWriteFast is bug-free. I suggest testing that assumption.

adcurtin:
nope, he's got the pins right: SPI - Arduino Reference

Oops. Now I feel silly. :frowning:

Please don't feel silly. I appreciate any help I can get with this matter. Also, I tried without using digitalwritefast... same results.

make sure your connections to pins 50-53 are correct. I had mistakenly connected to the last position (which is connected to GND) on the header a few times.

another option to try is to connect to ICSP spi lines.

I fixed the problem by changing the code to this:

#include <SPI.h>

byte inByte;
byte cypressByte;

void setup(){
  pinMode(MISO,INPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.setDataMode(SPI_MODE2);  
  digitalWrite(SS,LOW);
  Serial.begin(57600);
}


void loop(){
  while(Serial.available()>0){
    inByte = Serial.read();
    cypressByte=SPI.transfer(inByte);
    Serial.write(cypressByte);
  }
}

Instead of manually assigning the ports numbers and the direction I let the library do the work for me. Everything is functioning properly now. Thanks for the input.

Thank you for the follow-up.