Arduino SPI not transmitting

arduinospi

I am trying to drive a MAX7219 LED controller and 8×8 LED Matrix via SPI from my Arduino MEGA 2560. I can control the MAX7219 via SPI using my Bus Pirate, but for whatever reason, when I try to control it from Arduino (code below) I get no response. Furthermore, I connected my Bus Pirate to my Arduino SPI pins and put it in sniffing mode, and it shows no communication, whether or not CS is low. Does anyone have any idea why this is not working?

Code:

#include "SPI.h"

void setup() {
  pinMode(10, OUTPUT);
  lcd.begin(20,4);
  SPI.begin();
  pinMode(13, INPUT);
}

void spiWrite(byte reg, byte data){
 digitalWrite(10, LOW);
  SPI.transfer(reg);
  SPI.transfer(data);
  digitalWrite(10, HIGH); 
}

void resetMAX(){
  spiWrite(0x0f, 1);
  spiWrite(0x0c, 1);
  spiWrite(0x02, 13);  
}


void loop() {  

  if(digitalRead(13)==HIGH){
    resetMAX();
  } else {
    spiWrite(0x0f, 0);
  }
}

I am using the SPI pins on the ICSP header (Clock = 3, MOSI = 4) and Pin 10 (on the PWM side of the arduino) for the Slave Select. On Pin 13, I have a switch to enable sending different commands to the MAX7219, though that is currently having no effect, since it seems I am sending no SPI commands at all. Thoughts?

Best Answer

I found this on the Arduino SPI page http://arduino.cc/en/Reference/SPI

Note about Slave Select (SS) pin on AVR based boards

All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative. It is, however, possible to use any pin as the Slave Select (SS) for the devices. For example, the Arduino Ethernet shield uses pin 4 to control the SPI connection to the on-board SD card, and pin 10 to control the connection to the Ethernet controller.

Not sure if it will fix the problem, so far most of my boards have used the SS pin as an output regardless.