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.