SPI Port Pins

I am trying to read/write an SD Card. Am having no luck whatsoever...

But my question is specifically, I have gotten some code from two places which has different assignments for the SPI pins.
To wit:
From http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290279801

// Ports for Arduino Mega //
int PIN_CS = PINB0;	// chip select
int PIN_MOSI = PINB2;    // master out slave in
int PIN_MISO = PINB3;    // master in slave out
int PIN_CLOCK = PINB1;   // clock

and Arduino Playground - SDCARD

// Ports
int PIN_CS = PINB2;      // chip select
int PIN_MOSI = PINB3;    // master out slave in
int PIN_MISO = PINB4;    // master in slave out
int PIN_CLOCK = PINB5;   // clock

Since I am using an Arduino UNO with ATMega328, I should use the second section above, NOT the ones for the Arduino Mega, correct? I guess I was confused at first when I saw the "Mega" I thought it was correct because I was using an AT"Mega". But in composing this post I have discovered there is an Arduino Mega. Ha, I guess I just answered my own question :slight_smile: But please someone tell me that my answer is correct!! :slight_smile:

Thanks,
John

You should use the constants provided by the Arduino environment...

SS
MOSI
MISO
SCK

You shouldn't need to refer to the pins at all, excepting the SS pin. I have examples of reading/writing using SPI here. I don't refer to the pins in it. Leave that to the SPI library.

You should be able to use the SdFat library to access the card without too many problems.

The SS pin is declared in:

#include "pins_arduino.h"

Insert Quote
You shouldn't need to refer to the pins at all, excepting the SS pin. I have examples of reading/writing using SPI here. I don't refer to the pins in it. Leave that to the SPI library.

Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode. In master mode SS is not used/relevant - you can use any pin or pins as the chip selects and you have to set them up by hand.

If you are using slave mode (as when using SPI to control one arduino from another) you have to explicitly set MISO as an output but SS is used automatically by the hardware.

MarkT:
Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode.

Uhhh.. What?

void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);

maniacbug:

MarkT:
Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode.

Uhhh.. What?

void SPIClass::begin() {

// Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);

Exactly, MarkT is correct, they are explicitly set, he wasn't talking using the library though

I agree. Here is a simple test program using the hardware SPI library to test out a new 2 channel 12 bit DAC module I recently obtained. Not the total lack of any pinmode commands and that the SS pin name didn't need to be defined, the library does it all.

// Continous ramping from 0-5vdc analog output on 2 channel DAC module based on MCP4922 13 bit DAC device
// Hardware SPI version
// uses pin 13 (or 52) for SCK, pin 11 (or 51) for MOSI, pin 10 (or 53) for SS
// outputs both channels 0-5vdc in continous loop
// leftyretro 3/31/11

#include <SPI.h>
#include "pins_arduino.h"  // defines hardware SPI pins to use
const char channelA =  0;  // module has two independent D/A outputs, A & B avalible
const char channelB =  1;

void setup() {
 SPI.begin();  // initialize hardware SPI: uses pin 13 (or 52) for SCK and pin 11 (or 51) for MOSI
               // pin 10 (or 53) for slave select, SS 
 }
 
void loop()
 {
   for (int i=0; i < 4096; i++)   // DAC 12bit count value range is 0-4095
   {
    dacWrite(channelA, i);
    dacWrite(channelB, i);
   }
 }

//**********************
// MCP4922 Write Data 
// *********************

void dacWrite(unsigned char DAC_Channel, unsigned int DAC_Data)	  	
{
  switch (DAC_Channel)														
  {
    case 0x00: DAC_Data |= 0x3000;   //selects channal A, gain = 1, + 12 bit DAC value										
  	 break;
    case 0x01: DAC_Data |= 0xB000;   //selects channel B, gain = 1, + 12 bit DAC value										
  	 break;
  }
  digitalWrite(SS,LOW);  //enable Slave Select  SS is pin 10 (or 53)  
  SPI.transfer(highByte(DAC_Data)); // shift out high 8 bits
  SPI.transfer(lowByte(DAC_Data));  // shift out low 8 bits
  digitalWrite(SS,HIGH); // disable Slave Select, DAC value is transfered and latched in module   	  
  }

Lefty