SdFat lib for mega 2560 -- AnalogBinLogger

Hi,

I am having trouble getting the SD card to communicate using the SdFat libaries. Basically I think it is because of the pin numbers, I am not sure how to set them. I was able to get it working with the SD libraries from Arduino so I know the card is not the issue.
This is what I have so far. I have tried basically every combination.

// SD chip select pin.
const uint8_t SD_CS_PIN = 53;
...
  pinMode(53,OUTPUT); // pin 53 must be left as an OUTPUT for the SD library to work
  pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip
  pinMode(stopSwitch,INPUT);
  digitalWrite(stopSwitch,LOW);
  if (!sd.begin(SD_CS_PIN, SPI_HALF_SPEED)) {
    sd.initErrorPrint();
    fatalBlink();
  }
Errors:
FreeStack: 7223
Can't access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0X0

I have been trying to use the AnalogBinLogger program. My question is if the SdFat libraries will work with the Mega 2560? Ive looked at the SdFat.h and it doesnt seem to take more than 2 parameters like the SD.h file does so I'm wondering how to correctly set the pin numbers.

Im using adafruit datalogging shield, mega 2560 and SanDisk 16GB SDHC UHS-1

Thanks

ckcompton:
Hi,

I am having trouble getting the SD card to communicate using the SdFat libaries. Basically I think it is because of the pin numbers, I am not sure how to set them. I was able to get it working with the SD libraries from Arduino so I know the card is not the issue.
This is what I have so far. I have tried basically every combination.

// SD chip select pin.

const uint8_t SD_CS_PIN = 53;
...
  pinMode(53,OUTPUT); // pin 53 must be left as an OUTPUT for the SD library to work
  pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip
  pinMode(stopSwitch,INPUT);
  digitalWrite(stopSwitch,LOW);
  if (!sd.begin(SD_CS_PIN, SPI_HALF_SPEED)) {
    sd.initErrorPrint();
    fatalBlink();
  }
Errors:
FreeStack: 7223
Can't access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0X0




I have been trying to use the AnalogBinLogger program. My question is if the SdFat libraries will work with the Mega 2560? Ive looked at the SdFat.h and it doesnt seem to take more than 2 parameters like the SD.h file does so I'm wondering how to correctly set the pin numbers. 

Im using adafruit datalogging shield, mega 2560 and SanDisk 16GB SDHC UHS-1

Thanks

If you are talking about the AdaFruit Produce 1141, you have to use their SPI on Any Pin Library. If you use it with A MEGA.

The UNO has the hardware SPI interface on D10..D13 whereas the MEGA has them on D50..53.

to make it work on a Mega AdaFruit wrote a Library that emulates the SPI hardware. the card still use Pins 10..13 for the SPI Function.

Chuck.

...whereas the MEGA has them on D50..53

AFAIK, pin 53 is not in use for SPI purposes on the Mega by default (CMIIW):

MEGA MISO 50
MEGA MOSI 51
MEGA SCK 52

so pin 53 actually should work for SD_cs IMO!

You must run wires from the hardware SPI pins to use the Adafruit Datalogging Shield with AnalogBinLogger. The Datalogging shield is a very old design and does not connect to the ISP pins for hardware SPI.

Pins 11, 12, 13 on the shield must be connected to 51, 52, 50 on the Mega. Chip select for the Datatlogging shield will be pin 10 unless you rewire it.

The Adafruit library is based on a very old version version of SdFat. The Adafruit SD library is too slow with software SPI and does not have features needed by AnalogBinLogger.

You might be able to configure a newer version of SdFat to use software SPI. Software SPI in newer versions of SdFat is much faster than the Adafruit library. Software SPI will still be slower than hardware SPI.

Don't add this kind of stuff to AnalogBinLogger unless you have another SPI device connected to the Mega and then only set chip select for that device HIGH. SdFat and AnalogBinLogger handles the correct state of pins it uses.

  pinMode(53,OUTPUT); // pin 53 must be left as an OUTPUT for the SD library to work
  pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip

ArthurD:
AFAIK, pin 53 is not in use for SPI purposes on the Mega by default (CMIIW):

MEGA MISO 50
MEGA MOSI 51
MEGA SCK 52

so pin 53 actually should work for SD_cs IMO!

You are wrong, 53 is the HARDWARE SS pin, If as you say, it is not used by SPI, Set it to Input, connect it to GND and see if your Mega hangs when you try SPI transactions.

It is part of the SPI function. You should use it when you are using SPI, the error of using SS as an input while SPI transmission are occurring will cause unexpected fault and failures.

As long as it is either HIGH or an OUTPUT everything functions a expected, but if the pin status changes to INPUT LOW, The SPI master function is terminated, and the Arduino goes into Slave SPI mode. It nolonger produces SCK, instead it waits for SCK to be supplied by the 'external Master'. The way the SPI library code is written causes a endless loop waiting for SPDR to fill. It never will.

Chuck.

so pin 53 actually should work for SD_cs IMO!

Yes pin 53 is a good choice as chip select for a SPI device since it can't be used as an input when the Arduino is a SPI master.

As Chuck said, master mode SPI will fail if SS goes low in input mode.

The reason is that AVR SPI expects SPI will be in slave mode when SS is an input. In slave mode SS is Slave Select for the Arduino and the master connected to the slave Arduino generates clock.

You don't need to initialize SS since the SPI library will initialize it to an output and set it high when you call begin() for SD.h or SdFat.

Thank you guys for the responses as I am very new to the arduino world. I tried working with it for a few days but could not getting it working correctly so I went ahead and bought the Uno and its working great.

Thanks,
Cameron