The question can be applied to any microcontroller with I/O capabilities, but I’m currently working with the popular ATmega328p. Consider the following circuit: A simple SPST Normally Open switch with a 10k pull-up resistor and a ceramic capacitor attached, connected to any I/O port. The specific pin should be obviously configurated as INPUT in the respective DDRx. The datasheet describes, inside the DC Characteristics section, an Input leakage Current I/O pin \$ (I_{IL}/I_{IH})\$ of 1µA, while far in the horizon the DC current per I/O pin caps at 40.0mA. Not a problem at all.
DDRD = 0x00; // Entire port as INPUT
Now consider that I forgot the fact that the given pin has to be set as Input, I set it as Output and set the Port HIGH.
DDRD = 0xFF; // Entire port as OUTPUT
PORTD = 0xFF; // Entire port HIGH
While the datasheet doesn’t provide an Output Impedance it can be estimated to be 25Ω, based on the given graphs.
Now at the time the button is pushed the current finds its way through the microcontroller from the 5V source with a 25Ω resistor plus negligible resistance due to the copper trace. This theoretically produces an overcurrent of 160mA above the DC current per I/O pin ceiling. Could this fry the port and the device?
Analogously, if I switch over to positive logic and set the port to LOW the same problem could be encountered:
DDRD = 0xFF; // Entire port as OUTPUT
PORTD = 0x00; // Entire port LOW
Considering this type of circuit is avidly encouraged, how it doesn't seem to bring any troubles? Now, back to the original question, shouldn’t I be extremely cautious when setting an I/O pin as Output? Or at least attach a small series resistor as a primitive protection?
Note: Not a native English speaker, feel free to edit the post if you see something awkward.