Go Down

Topic: pins_ardunio.h question (Read 1 time) previous topic - next topic


I'm planning on using the microSD shield from sparkfun for a dev project. rather than tie up two DI/O as the arduino require "DIO 10" to be an ouput and the SD shield uses "DIO 8". Can I simply edit the library pins_arduino.h file and change the following
Code: [Select]
const static uint8_t SS   = 10; to 
Code: [Select]
const static uint8_t SS   = 8;

or are there other libraries I need to edit to make the SPI bus default to pin 8 for chip select??


or are there other libraries I need to edit to make the SPI bus default to pin 8 for chip select

No the SPI uses hardware built into the chip to work. You can't change it to use another pin.
If you want to use SPI on another pin you will have to write a bit bang routine to do it.


Mar 17, 2011, 06:39 pm Last Edit: Mar 17, 2011, 06:42 pm by CrossRoads Reason: 1
Not true Mike - he is changing the Slave Select pin, that one the user defines as you seperate Slave Select lines to each SPI part that is communicated with.
User must write SS low & high separate from spi.transfer calls, example from  a sketch of mine:

Code: [Select]

 digitalWrite(SS,LOW);  // take the SS pin low to select the chip
 SPI.transfer(minutes_tens_address);  // select the Address,
 SPI.transfer(number_to_display);      // select the data
 digitalWrite(SS,HIGH);   // take the SS pin high to de-select the chip

The part that is receiving SPI serial data will have to be looked to see if its Chip select line going on/off with no clock/data activity will be a problem.
Same for the shield if the same pin is being used.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


But if you put the hardware slave select pin high when you are using the SPI the Atmega hardware will immediately jump to slave mode and stop transmiting data.


Mar 17, 2011, 07:10 pm Last Edit: Mar 17, 2011, 07:36 pm by davekw7x Reason: 1

.SD shield uses "DIO 8". Can I simply edit the library pins_arduino.h

Do not under any circumstances change the definition of SS in arduino_pins.h.  Really.  Don't.

If you are using the Arduino SD library from the current distribution, look at any of the example sketches and change any occurrences of SD.begin(4) to use the pin number of your hardware slave select.

For example, change line 34 in the SD/examples/ReadWrite sketch from
Code: [Select]

 if (!SD.begin(4)) {

Code: [Select]

 if (!SD.begin(8)) { // The pin number of your SD chip select

Now, the Arduino SD library is a "wrapper" around the excellent SdFat library, but the API doesn't document  access to lots of important things.  (The examples directory for SD is woefully sparse, compared to the SdFat examples.)

If you are using the excellent SdFat library, in their example sketches change stuff like
Code: [Select]

 uint8_t r = card.init(SPI_HALF_SPEED);

to something like

Code: [Select]

 uint8_t r = card.init(SPI_HALF_SPEED, 8);// Use the pin number for your SD chip select

Or, better yet:
Code: [Select]

 uint8_t r = card.init(SPI_FULL_SPEED, 8);// Use the pin number for your SD chip select

If you are using some other library, check to see what you have to do to change the "default" chip select.  (Or, tell us what library you are using.  Maybe we can help dig out the information you need.)

I have tested this for lots of my SD circuits (with Arduino Duemilanove and Uno and other '328 boards) where I (usually) use pin 9 for the SD chip select.




Thanks for the quick replies. When I get that far along in my development I'll look into which library I might use. but from the sounds of it SdFat lib is what I'll end up using as everything I've come across on the web and this forum has high regards for it. 


Mar 17, 2011, 07:24 pm Last Edit: Mar 17, 2011, 08:17 pm by davekw7x Reason: 1

But if you put the hardware slave select pin high when you are using the SPI the Atmega hardware will immediately jump to slave mode and stop transmiting data.

Not if that pin is an output pin.  The Arduino SPI hardware will be in Master mode as long as the SS pin is an output pin, but the ATmega SPI hardware doesn't control it and it doesn't care whether you make it go high or low for any purpose.  (If you make the SS pin an input, then the ATmega SPI hardware will automatically switch to Slave mode if some external device connected to that pin asserts a logic low level, but it stays in Master mode as long as the pin is in output mode.  Really)

Lots of hardware (like the Arduino Ethernet card) also uses the SS pin for the SPI slave device "chip select" signal, but in fact you can use any Arduino output pin as a "chip select" and the various libraries will need to manipulate the "chip select" pin as needed.  With version 5 of the Arduino Ethernet card (the one with the microSD socket) you can use the Ethernet library to access Ethernet stuff (uses SS pin for Ethernet device "chip select"---that's pin 10 on '328 Arduino boards) and you use the SD library to access the SD (uses Arduino pin 4 for the SD "chip select").  Both can have active devices at the same time. (Only one will be active on the SPI bus for a given instruction, but Ethernet and SD access can occur within the same sketch at different times.)

When your program does Ethernet stuff, the SPI port manipulates the SPI signals MISO, MOSI and SCK, and the Ethernet library access functions wiggles the SS pin (Arduino Pin 10 for ATmega328p chips) as required to enable and disable the WIznet Ethernet controller to send/receive Arduino data on the SPI port.  The library functions leave the SS pin high when it is not accessing the Ethernet device.

When your program does SD accesses, the SPI port does it stuff and the SD library access functions wiggle pin 4 as required.  The library functions leave pin 4 high when it is not accessing the SD device.

So: You can have more than one device "active," at the same time, and the device "chip select" determines which device is "on the bus" at a given time.

Regardless of which pin you are going to use as the slave "chip select" pin, your sketch (or any SPI device library you are using) must manipulate the slave "chip select" pin as needed.  (And for sanity's sake, should leave the SS pin as an output.)

At any rate, a decent SPI library will initialize the "SS" pin as an output, regardless of what pin you are using as the slave"chip select."   See Footnote.

Sometimes people specifically put an instruction to set the pin mode to OUTPUT for the SS in their sketch (and sometimes they also set the output bit high). This doesn't hurt, but libraries like the Arduino Ethernet library and the Arduino SD library and the SdFat library (and, in fact, any reasonable library) will do this for you when you use the library's device initialization function.



My definition of "decent" SPI library requires that the library let you specify some pin other than the SPI port hardware "SS" pin to be the slave device "chip select" pin.  My definition of a "decent" library requires that the library initialize and manipulate them during SPI port operations from the library API without making the user sketch work directly on the pins or the SPI hardware registers.

Of course, you don't have to use a library at all; you can put the hardware port access directly into your sketch or into some functions that you write along with your sketch.  For accessing SD memory cards I personally use the SdFat library that I downloaded from http://code.google.com/p/sdfatlib/downloads/list (sdfatlib20101010.zip is the latest one.)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131