ninja2:
An old thread, but the discussion above is exactly where my concern lies.After reading up (again) on SPI I'm still not clear on difference between CS_pin and SS_PIN.
I'm using the MEGA SPI port (50,51,52) to drive a sparkfun microSD shield
The UNO SPI pins 11,12,13 are re-directed in my design.
The CS_pin is on D8, the microSD default (i.e. SD.begin(CS_pin).
I don't define SS_PIN as it is set to pin53 and an output by the SD library (in sd2PinMap.h) automatically for a MEGA. The SD card is functioning correctly.But I want to use pin 10 for a non-SD related task (backlight on an LCD) but as soon as SD is accessed the backlight fails.
Why is pin 10 unavailable, when the SS_PIN is 53 on MEGA?
thanks
The reason to make the SS pin Output, high has to do with the SPI hardware in the Chip. if the SS pin is currently INPUT, when you start using the SPI functions the chip goes into SLAVE SPI mode instead MASTER SPI mode.
The Master SPI device control the Clock. If the chip is in SLAVE SPI mode, The SPI.transfer() function will hang forever waiting for the 'Master' to provide the clock signal.
So anytime you use SPI, pinMode(SS,OUTPUT); must be executed. The digitaWrite(SS,HIGH); is optional. Personally I use SS(slave Select) as a CS_pin(chip Select) when ever I can. That is what SS was to do.
Because you are using SPI Master mode the SS pin must be an output while any SPI transfers are executing.
A typical SPI transfer:
// SPI has already been initialzed with bit order, modes
// select specific SPI device, multiple device can be connected to SPI(MOSI,MISO,CLK)
// but each device needs its own SS pin, usually call CS_pin
digitalWrite(CS_pin, LOW); // CS_pin may equal SS_Pin, this depends on your specific hardware config
uint8_t inbyte = SPI.transfer(outbyte); // send one byte out, receive one byte in
digitalWrite(CS_pin, HIGH);// finished with SPI transfer, de-select device. all done
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.