Pages: [1]   Go Down
Author Topic: SD examples and chip select definition  (Read 3237 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Using a SD card, I want to try the examples given with the libraries. As warning, it's often written "you have to define correctly the CS pin as output, event if it's not used as the CS pin..."
But, in all example sketch, a bad programming usage is used... here the "Files.ino" sketch fragment:
Code:
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
The code above a no chance to be executed correctly, because CS is defined as 10 in pinmode, but as 4 in SD.begin!!
Why not using the correct reference to, set in Sd2Card.h, as SS_PIN in all occurrences? This is more clean and professionnal.
Have a good day,
//Yves
Logged

Regards,
// Yves

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The SS pin referred to in the comment is the SS pin for the Ethernet functionality. The value in the parentheses in the call to SD.begin() is defining which pin is used as the SS pin FOR THE SD CARD.

Different things. The SS pin (pin 10) needs to be defined as an OUTPUT to make SPI work, regardless of whether or not you are using pin 10 as the SS pin for anything.

Quote
The code above a no chance to be executed correctly, because CS is defined as 10 in pinmode, but as 4 in SD.begin!!
Have you actually run the code? What failed to be executed properly? You are talking apples and oranges.
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The SD library has a long history and many of the examples are out of date or just plain wrong.

This comment is wrong.
Code:
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

The SD library always sets SS high as an output so no code is needed for that purpose.  Here is the code from Sd2Card.cpp in IDE version 1.01.
Code:
  // SS must be in output mode even it is not chip select
  pinMode(SS_PIN, OUTPUT);
  digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin

Future versions of the SD.h library will use the SPI.h library and it also makes SS as an output.  Here is code from the 1.5 SPI.h library.
Code:
  // Set SS to high so a connected chip will be "deselected" by default
  digitalWrite(SS, HIGH);

  // 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(SS, OUTPUT);

Here is the reason you may need to make some pins outputs and high.  If you have more than one device on the SPI bus, you must disable all devices other than the SD by making their chip select pin an output and high.

For the Ethernet shield, pin 10 is chip select for the Ethernet controller and pin 4 is chip select for the SD card.  So you should make pin 10 an output and set it high to disable the Ethernet controller, not because pin 10 is SS.

Much confusion results from the fact that on 328 Arduinos pin 10 is also SS.  On the Mega, people change the 10 to 53, the SS pin for the Mega, and then the SD on the Ethernet shield will not initialize.  The problem is that the Ethernet controller is no longer disabled on the Mega.

For the Ethernet shield you must always make pin 10 an output and high before initializing the SD card.  It has nothing to do with SS.


Logged

Pages: [1]   Go Up
Jump to: