This question refers specifically to an ATmega328P, but probably can be generalized to most or all AVR MCUs.

Known 1: When the Data Direction Register (DDR) for a pin is set as input mode, it is still possible to write to the PORT for that pin; instead of producing an output, the value written to PORT will just change the pin between pullup and tri-state mode.

Known 2: A pin can be configured to toggle its output state along with the waveform generation module in a timer.

Question: When the DDR for a pin is configured as input, and that same pin is also configured to change state along with a timer, will the timer waveform effectively just rapidly switch the pin between tri-state and pullup modes? Or will the pin behave more reasonably, staying in tri-state/pullup according to the PORT value?

I guess the question could be restated as: Does the timer write to the PORT register in order to change the pin's state?

The only thing remotely relevant I could find in the datasheet is the following line:

...However, note that the Data Direction Register (DDR) bit corresponding to the [timer] pin must be set in order to enable the output driver.


The "I/O-Ports" section of each AVR datasheet gives a detailed treatment of what each "virtual" output affects. In the case of OCnx, PVOV is directly affected, and PVOV is defined as "Port Value Override Value - If PVOE is set, the port value is set to PVOV, regardless of the setting of the PORTxn Register bit.". PVOE is set to OCnx ENABLE, controlled by COMnx. Therefore the OCnx value is not written to PORTx, and will always be output on the pin with an appropriate value in DDRx regardless of the setting of PORTx.

  • \$\begingroup\$ Thank you. That makes good sense, and I see that in the block diagram in the I/O section of the datasheet. Are you saying that setting COMnx to the right value(s) will cause the timer to directly drive PVOV? \$\endgroup\$ – Yankee Jul 18 '17 at 4:27
  • \$\begingroup\$ OCnx drives PVOV directly, and COMnx drives PVOE indirectly. \$\endgroup\$ – Ignacio Vazquez-Abrams Jul 18 '17 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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