If in an AVR microcontroller there's at least two functions for example SPI, Analog Comp and GPIO pins in one 8bit PORT what's the priority of the functions? for example: If SPI enable first, and then using DDRx trying to set one PIN which used from SPI as GPIO (such as input or output) what's the final function?

EDIT: Can i setup in a port a multible functions? For example: i want to enable SPI and use the other pins normally as a GPIO pins. or, to enable UART and using other rest pins as GPIO. How can i do that?

  • \$\begingroup\$ Your edit makes no sense. Nothing prevents you from using GPIOs as GPIOs except using them for something else. \$\endgroup\$ Commented Nov 28, 2014 at 22:54
  • \$\begingroup\$ and the alternative function of port remain intact? \$\endgroup\$
    – MrBit
    Commented Nov 28, 2014 at 23:01
  • \$\begingroup\$ Are you sure you read the answers? \$\endgroup\$ Commented Nov 28, 2014 at 23:03

2 Answers 2


The "Alternate Port Functions" subsection of the "I/O-Ports" section of the datasheet describes exactly how each pin will behave given the peripherals enabled. In the case of SPI, only either MISO (master) or the other three pins (slave) are forced to a specific direction (input in both cases); the other pins can have their direction configured to either conform to SPI or not as required/desired.

Note that DDRx, PORTx, and PINx can always be read from and written to regardless of the current functions of the corresponding pins, even if such an action does not make sense given their configuration.


AVR chips will have their IO pins default to Digital I/O inputs on startup. All "alternate functions" will be inactive, meaning that the pins will just be digital input GPIOS until you activate a component that requires one of the pins.

For example, if you want to use the ADC on the ATTiny85, you write 0x80 to the register ADCSRA to enable it (that places a "1" in the enable bit of the ADC control register). When you do that, the ADC controller will essentially take over the pins it needs and configure them as needed. Depending on your ADC configuration, it may take over several pins. Let's say you're using an external voltage reference for your ADC, which is on pin PB0. At this point, PB0 can no longer be used as a digital input pin because the alternate function is active, and pins can only do one thing at a time.

As Ignacio mentioned in his answer, you can always read/write from/to the pins regardless of the function selected, but you are likely to interfere with the operation of whatever is using that pin.

Now let's say I want to use an external interrupt. I will have to choose one besides PCINT0, because that interrupt also uses PB0, which is already taken by my ADC. There is no "priority" - alternate functions will interfere with each other if you try to enable multiple peripherals that use the same pins.

Now, if I go disable the ADC, then the pins it used are free again. I don't think the pins will be returned to their previous states, so if you want to use them as digital input again, I would write to the necessary registers first to confirm they are in that mode.

  • \$\begingroup\$ You mean: if i enable an alternative function first this functions will remaining? I tried to set a port as output and then to enable I2C and works fine. this mean for me that I2C had most priority (in decleration) than DDRx \$\endgroup\$
    – MrBit
    Commented Nov 19, 2014 at 20:47
  • \$\begingroup\$ For "the most recent bit pattern" theory: if i try to config a port to using the alternative module (such as SPI on PORB of ATMEGA32) the spi will enabled until to setup some different? and how can i declare some other pin of PORT to be an input or output with DDRx register? if select DDRB = 0x07 to set PB0 PB1 PB2 as output the others will take the zero value because 0x07 = 0b0000111, thus, msb (including SPI pins) will be inputs? and SPI will stop working? \$\endgroup\$
    – MrBit
    Commented Nov 19, 2014 at 20:56
  • \$\begingroup\$ this which i mean it's: Can i config a port as output/input and then to enable an alternative function? (USART,SPI,I2C) and inversely? I tried before to setup a port as output and then i enable I2C. so, i consider that to config a port as output or input when an alternative function it's already enabled it's achievable. it is true?? \$\endgroup\$
    – MrBit
    Commented Nov 19, 2014 at 21:10
  • \$\begingroup\$ and how can i config a port to use a half as alternative function (UART,SPI,I2C,ADC) and a half as GPIO (input/output) port? is there an influence in this configuration? For example: ATMEGA32 (SPI on PORTB). SPI is enabled and DDRB = 0x07 this seting three LSB as output and others as input but there's a SPI already enabled and i worry for this affect \$\endgroup\$
    – MrBit
    Commented Nov 19, 2014 at 21:16
  • \$\begingroup\$ @GeoG392 You don't need to set your pins in any special way before enabling a peripheral that uses an alternate pin function. The peripherals will take over and configure the pins they need when you enable them. Once you enable a peripheral and it takes over the pins it needs, other pins on that port can still be configured and manipulated as they normally could be. It won't change how they behave. Just make sure you use a read-modify-write cycle when you write those pins' control registers. I've rewritten my answer, so give it another look and see if it's more clear now. \$\endgroup\$
    – skrrgwasme
    Commented Nov 19, 2014 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.