2

On an ATmega2560, can ports have some pins used for analog inputs but others as digtial i/o? Specifically, I only need to use 3-5 analog inputs but have many digital inputs. I would like to use the remaining 3-5 pins as digital outputs if possible.

Additionally, does it make a difference if the port in question is Port F vs. Port K? For Port F, the datasheet seems to imply that all 8 of the pins are analog only if the ADC is enabled:

Port F serves as analog inputs to the A/D Converter.

Port F also serves as an 8-bit bi-directional I/O port, if the A/D Converter is not used.

For Port K, however, the datasheets port description does not have the distinction:

Port K serves as analog inputs to the A/D Converter.

Port K is a 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).

It seems that this is definitely possible for the Arduino Uno/ATmega328P ADC port (except for the ADC6/7 pins for 32-pin form factors), but I can't seem to find anything confirming for the Arduino Mega/ATmega2560 or distinguishing between whether this is done on Port F or Port K

2
  • 2
    please provide a link to the datasheet that you are reading ..... also, which section in the datasheet are you referring to regarding port F?
    – jsotola
    Apr 3, 2019 at 1:28
  • Datasheet: ww1.microchip.com/downloads/en/devicedoc/… I was looking at section 2.3.8
    – Matthew
    Apr 3, 2019 at 21:14

1 Answer 1

3

On an ATmega2560, can ports have some pins used for analog inputs but others as digtial i/o?

Yes, the ADC MUX controls the selection of the pin that is connected to the converter. This is independent of the digital pin control.

Cheers!

PS: See the Arduino core source code for further details on the ADC MUX control registers.

4
  • Are you afraid to say that the datasheet is wrong? The part "if the A/D Converter is not used" for port F should not be there.
    – Jot
    Apr 4, 2019 at 1:38
  • 1
    @Jot The ports (F and K) are digital but may also be selected to the ADC via the MUX and sample-and-hold. I guess that you interpret the datasheet as "to use port F for digital IO the ADC module must be turned off"? The is unfortunately wrong IMHO. Apr 4, 2019 at 5:35
  • The digital input buffer (per pin) can be turned off when used for an analog input signal. The control register DIDRx controls this functionality. It is used to "reduce power consumption in the digital input buffer". Apr 4, 2019 at 5:47
  • That is indeed how I read it. They can be used at the same time and the datasheet tells otherwise. Matthew had a good reason to ask this question. Even more so, because sometimes the digial pins are disconnected. For example for a atmega328p, when the x-tal is selected, the digital pins are disconnected for those x-tal pins. Beside the jtag, I think the description for the F and K port should be the same. @Matthew please mark the answer as a good answer.
    – Jot
    Apr 4, 2019 at 9:12

Your Answer

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

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