arduino / ArduinoCore-megaavr

Arduino Core for the ATMEGA4809 CPU
103 stars 62 forks source link

digitalWrite() backwards compatibility problem #86

Open benwillcocks opened 3 years ago

benwillcocks commented 3 years ago

Line 164 of wiring_digital.c has the comment "old implementation has side effect when pin set as input - pull up is enabled if this function is called. Should we purposely implement this side effect?". Actually, a more important feature of the old implementation of digitalWrite(), which is missing from the new implementation, is that it can be used to set the output register whilst the pin is configured as an input. Here are two situations where this is useful: 1] if a pin is used for bidirectional communication, it can be necessary to change direction from input to output without changing the state of the pin. To achieve this one needs to read the input, set the output register accordingly, and then make the pin an output. 2] if multiple pins (not necessarily on the same port) are commoned for extra drive strength, then one must be careful to avoid the outputs fighting one another. When changing the state it is necessary to make all the pins inputs, then change the output registers, then make all the pins outputs.