Input/Output Switching for Arduino

Asked 2 years ago, Updated 2 years ago, 369 views

Arduino is a program and
every pin I will decide in advance what kind of pin to choose, but is it not possible to input and output by program depending on the situation of the program?

For example, when an LED is connected,
Turn it on/off and whether it's on or not
Can't you check it?
If possible, I can switch with one pin and use fewer pins.

Thank you for your cooperation.

c arduino

2022-09-30 21:55

1 Answers

Arduino UNO's microcomputer is ATmega328, and according to the description in 18.2.4, you can see the 18.2 port configuration diagram in the volunteer manual Ore Translation and

  • You can read the PIN register regardless of the DD register setting
  • PIN register gets pin signal latched value

So if you digitalRead() the pin you output in pinMode(), you can read the value you output in digitalWrite() as it is (you can't deny the possibility that the circuit will be different, but it's a rare case).

Note that there is a delay of one clock because there is a latch on the input circuit. You don't have to worry about using digitalWrite() and digitalRead() because more than one clock must pass through API function calls, but NOP waits as per the sample documentation when accessing the port register directly.

This kind of "hardware constraint" is always present in every microcomputer, so programmers who write so-called device drivers must read the hardware manual carefully.If you pull out your hand, you'll get into trouble later.

With a larger and faster microcomputer (such as Renesas RX), the peripheral circuit register is slower than the CPU's calculation circuit, so if you want to get the value you output, it's faster to store it on RAM when you output it.


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.