Pages: [1]   Go Down
Author Topic: Can only send one SPI byte!?  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an Arduino Mega and a wifly shield.  The wifly shield has a SPI->UART IC which is supposed to be the interface to the wifly.  Right now I'm just trying to get the SPI IC configured correctly.  I've copied the transparent terminal sketch from the "Talking wireless server tutorial".  When I run it the code hangs.

I striped everything down to a bare sketch so you can clearly see the problem:


Code:
char clr = 0;

#define SS   10
#define MOSI 11
#define MISO 12
#define SCLK 13

char spi_transfer( volatile char data )
{
  SPDR = data;
  while( !( SPSR & (1<<SPIF) ) );
  Serial.println( "Completed" );
  return SPDR;
}

void setup()
{
  pinMode( MISO, INPUT  );
  pinMode( MOSI, OUTPUT );
  pinMode( SS,   OUTPUT );
  pinMode( SCLK, OUTPUT );
  
  digitalWrite( SS, HIGH );
  
  SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);
  clr  = SPSR;
  clr  = SPDR;
  delay( 10 );
  
  Serial.begin( 9600 );
  Serial.println( "Initializing SPI..." );
  
  digitalWrite( SS, LOW );
  spi_transfer( 0x00 ); // Dummy data
  spi_transfer( 0x00 ); // Dummy data
  digitalWrite( SS, HIGH );
}

void loop()
{
}

When I run this I only see one "Completed" and the code hangs when trying to send the second byte. The second "Completed" never comes.

Here's a copy from the terminal:

Initializing SPI...
Completed


Is there a problem with my code? Do I have a bad Arduino or peripheral?

Can someone else run this simple code and see if they get the same results?
« Last Edit: April 10, 2010, 02:20:50 pm by trobertson » Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bear in mind that the SPI pins on the Mega (50-53) are not the same as those on the Duemilanove or other boards (10-13).

So unless the shield was designed for the Mega it won't be getting the SPI signals from the board. (of course you'd have to change the definitions of the pins in your code as well).
Logged

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

Wow, I guess that would do it. Yeah I switched the pins and it worked as expected.

This brings up another question.  Since the wifly shield expects pins 10-13 to be used for SPI, can I connect wires from my mega 50-52 to the shield 10-13 WHILE it is stacked onto my mega?  I guess I would need to invert the direction of pins 10-13 match the direction of pins 50-52.  This would essentially be shorting the pins 50-52 to pins 10-13 since stacking the shield will cause the shield pins to match the mega pins. Would this damage the arduino?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting question... I don't really know the answer, but I would hazard a guess that if you set the unused Mega pins to INPUT (high-impediance) it might work.  Since the input pins are high impedance adding them onto the SPI lines might not affect them.

At least it shouldn't damage anything;  pins set to inputs are ordinarily connected to +5V or ground all the time.
Logged

Pages: [1]   Go Up
Jump to: