Go Down

Topic: SPI issues... works on the NANO, not on the MEGA. Pin numbers are right. Ideas? (Read 1 time) previous topic - next topic

mikesbaker

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:

Code: [Select]
#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:

Code: [Select]
#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.

johnwasser


Code: [Select]

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



Shouldn't that be:
Code: [Select]

#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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

adcurtin

nope, he's got the pins right: http://arduino.cc/en/Reference/SPI

Coding Badly

Code: [Select]
#include <digitalWriteFast.h>

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

johnwasser

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mikesbaker

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

doughboy

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.

mikesbaker

I fixed the problem by changing the code to this:

Code: [Select]
#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.

Coding Badly


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy