2

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?

MrBit
  • 1,973
  • 4
  • 27
  • 54

2 Answers2

1

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.

Ignacio Vazquez-Abrams
  • 48,282
  • 4
  • 73
  • 102
0

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.

skrrgwasme
  • 828
  • 8
  • 32
  • 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 – MrBit Nov 19 '14 at 20:47
  • 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? – MrBit Nov 19 '14 at 20:56
  • 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?? – MrBit Nov 19 '14 at 21:10
  • 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 – MrBit Nov 19 '14 at 21:16
  • @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. – skrrgwasme Nov 19 '14 at 21:41
  • Technically ADC pins that have a GPIO function can still be read from unless you explicitly disable the digital input functionality with `DIDRx`. – Ignacio Vazquez-Abrams Nov 19 '14 at 22:00
  • @IgnacioVazquez-Abrams Agreed. I mentioned GPIO functionality is still available even while special functions are used and referenced your answer. – skrrgwasme Nov 19 '14 at 22:02
  • What confuses me about what you said in your answer about how the peripheral will automatically configure the port when it is enable, is that I found with the ATtiny84, it requires the pin with the clock to be set as an output for the clock signal to come out. If the pin is not set as an output then no clock signal will be there regardless of the peripheral being set or not. So I’m not 100% sure what you mean about the peripheral automatically setting up the port. – Kalcifer May 03 '21 at 07:11