ninja2:
Chuck
That all makes sense, but why can't I use pin 10?
and either way pin 10 seems to be unavailable for other non-SD tasks.
(again my term SS_pin does not override SS_PIN in the library)
Now I don't bother making pin 53 an output, because it seems to be done for me.
Still not quite adding up!
On the Mega2560, the SS pin is digital pin 53, for SPI to function correctly pinMode(53,OUTPUT); must be executed. You can use pin 53 for any task as long as it it configured as an OUTPUT. It can be LOW or HIGH The library does not care.
The SPI Library does two configuration step to the SS pin in SPI.begin();
First it check if SS pin is configured as input if it is an input is turns on the Pullup resister, If the pin is not connected to anything the Pullup resistor pull the pin HIGH. ( every thing is happy as long as the pin is high). This configuration work is incase you use the SPI in SLAVE mode, just incase.
The Second thing is it changes the pinMode of SS to output. It does not change the value(HIGH,LOW) Just forces it to output. Most people use SPI in Master mode, configuring to work makes peoples live better.
That is the only time the SPI library messes with SS. If later in your program you change pinMode(SS) to INPUT AND use SPI AND SS is LOW the SPI hardware will automagically switch from MASTER Mode to SLAVE Mode and your SKETCH with Hang.
I use SS as a chip select in my latest Kickstarter Project Memory Panes
It works fine.
Here is the SPI.begin() function from the latest release.
void SPIClass::begin()
{
uint8_t sreg = SREG;
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
if (!initialized) {
// Set SS to high so a connected chip will be "deselected" by default
uint8_t port = digitalPinToPort(SS);
uint8_t bit = digitalPinToBitMask(SS);
volatile uint8_t *reg = portModeRegister(port);
// if the SS pin is not already configured as an output
// then set it high (to enable the internal pull-up resistor)
if(!(*reg & bit)){
digitalWrite(SS, HIGH); ****************** this is where is sets the PULLUP Resistor ON
}
// 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); **************************** This is were is Sets pinMode
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
}
initialized++; // reference count
SREG = sreg;
}
I don't understand what you meant by:
[quote author=ninja2 link=msg=2279182 date=1434500599
I assume pin 53 is "the hardware SS" and cannot be changed on a MEGA.
I've tested with and without following in my code and it makes no difference to SD functionality
Code: [Select]
define SS_pin 53
pinMode(SS_pin, OUTPUT);
and either way pin 10 seems to be unavailable for other non-SD tasks.
(again my term SS_pin does not override SS_PIN in the library)
Now I don't bother making pin 53 an output, because it seems to be done for me.
[/quote]
How is pin 10 unavailable for other tasks?
The only way it would be unavailable is if it was used by some hardware.
What do you mean 'pin 10 seems unavailable'? Where is it unavailable?
Chuck.
Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.