Pages: [1]   Go Down
Author Topic: SPI issues... works on the NANO, not on the MEGA. Pin numbers are right. Ideas?  (Read 1091 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Shouldn't that be:
Code:
#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.
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <digitalWriteFast.h>

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Oops.  Now I feel silly. smiley-sad
Logged

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

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

SF Bay area
Offline Offline
Sr. Member
****
Karma: 2
Posts: 266
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I fixed the problem by changing the code to this:

Code:
#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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Thank you for the follow-up.
Logged

Pages: [1]   Go Up
Jump to: