SS = Output with SPI

I am programming the Atmega328 chip with original arduino SPI.

I set the unused pins on the Atmega328 chip as outputs.

But I wasn't sure about the "SS (PB2)" pin. Will there be a problem with SPI when I set this pin as output?

why set them to anything if they are unused?

by defaults, pins are INPUT, otherwise there's potentially two devices driving the connection

I had read somewhere. If we set the pins as outputs, "You protect the chip from electromagnetic pollution" it wrote.

That's why I set unused pins as outputs

You MUST set this pin as an OUTPUT. Otherwise the SPI lib will not function correctly in master mode. See here:

Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.

1 Like

in my 30 years working on embedded systems i've never heard that "myth"

1 Like

Unnecessary or wrong?

interesting attitude

Why did you say "interesting attitude?" I'm sorry if I made a mistake.

"What is the correct code for unused pins?" I just asked to find out.

I assume you're saying "it's okay to have unused pins idle"

Thank you for your help

Hi,

Yes it is, there is in the IC a protection diode on each I/O pin, that will not let the I/O pin go higher than the Vcc supply.
So although the pin is open circuit, any voltage that may damage it, or noise will be suppressed by the diode to Vcc.
There is also one between the I/O pin and gnd.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Because as @MicroBahner pointed out, SS MUST be set to an output as documented here. Or, is that a "myth" also?

is this specifically referring to unused SPI pins?

It's specifically referring to the HW-SS Pin of the SPI at AVR. Leaving this Pin as INPUT in master mode may lead into trouble, because if it is driven low, the SPI HW will switch to slave mode. If it is configured as OUTPUT it doesn' t affect the SPI HW and can be used as a general OUTPUT.
Other architectures may behave differently.

of course it make sense to configure a specific unused SPI pin for the reason you stated. (don't know why the SPI library code wouldn't do it)?

it doesn't make sense to set "unused pins" (which pins) for "You protect the chip from electromagnetic pollution" (??)

i do know SPI pins require an external pull-up resistor since the bus has more that 2 possible devices. without those pull-ups the input is sensitive to EMI which may result in errors

There have been two sentences in the opening post, one statement, one question:

You answered to the first sentence, that this is unneccessary to set all unused pins to OUTPUT - which it obviously is.
I answered to the second sentence, that the SS pin must be set to OUTPUT - even if you don't use it.

So everything is correct and ok :sunglasses:

1 Like

When SPI.h and SPI.begin() are included in the Master sketch, the following definitions are automatically established.

DPin-10 = SS (Slave Select) : OUTPUT and HIGH
DPin-11 = MOSI (Master Out Slave In) : OUTPUT and LOW
DPin-12 = MISO (Master In Slave Out) : INPUT
DPin-13 = SCK (Serial Clock) : OUTPUT and LOW

For lowest power operation, Atmel recommends setting unused pins to Outputs, or Input with pullup resistor enabled. This keeps the inputs from switching randomly and drawing power.
"2.9 State of Digital I/O Pins
All digital I/O pins are by default floating to avoid hardware conflicts. However, since the pins have digital input buffers it is important to ensure that the level on an I/O pin is well-defined to avoid sporadic internal switching and leakage. The leakage caused by floating I/O is relatively small and is mainly observable in sleep, but can be minimized by ensuring that the state of the pins is either high or low.
If a pin is connected to an analog source, the digital input buffer on that pin should be disabled even if it is not configured as an input. This is done by use of the PINnCTRL registers for the individual ports. Please refer to the device manual for information on how to do this configuration.
To minimize power consumption, enable pull-up or -down on all unused pins, and disable the digital input buffer on pins that are connected to analog sources."
Atmel AVR1010 - Minimizing power.pdf (211.4 KB)

"5.2 Enable pull-ups on unused I/O pins and disable modules not used
To remove some power consumption, we will enable pull-ups on unused I/O pins to get a defined logical level and avoid unnecessary switching. To enable pull-ups on the I/O pins, we set the pins to input in the Port Data Direction Register and set the Port Data Register to high level (see section 13.2.1, Configuring the Pin, in the datasheet).
Code:
DDRA = 0x00; // Set direction to input on all pins
PORTA = 0xFF; // Enable pull-ups on pins
Another power-saving feature is to disable unused on-chip modules in the PRR Power Reduction Register (see section 9.10, Minimizing Power Consumption, in the datasheet)."
AtmelAVR4013 picoPower.pdf (92.4 KB)

Is there any internal pull-down resistor with a pin of the ATmega328P MCU? You are advising to enable pull- down resistor with an unused pin.

There are no internal pulldown, only pullups, on the 328/1284/2560 family.
Other families may have pulldowns, I am not familiar with them. As the App Note says, please refer to the manual for the device being used.

The title of the application note is Minimizing the power consumption of
Atmel AVR XMEGA devices
, and they do have pulldowns.