-->

How can I digitalRead a pin that is in pinMode OUT

2019-01-14 06:08发布

问题:

I have a very simple test sketch in which I'm trying to set a pin to HIGH and then read its state with digitalRead. Here is my sketch.

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    delay(1000);

    pinMode(3, OUTPUT);
    digitalWrite(3, HIGH);
    delay(1000);

    pinMode(3, INPUT);
    Serial.println(digitalRead(3));
}

Serial monitor result:

0
0
0
0

I have come to understand that changing the pinMode will stop it from being HIGH. So setting a pin to HIGH in OUTPUT mode and then changing to INPUT mode will change it to LOW. So the digitalRead will always return 0. If I don't change the pinMode it won't be able to read the pin. So how can I read the current setting of a pin that is in OUTPUT mode without losing the value?

回答1:

In this case you just want to access the data register itself.

PORTB and PORTD registers contain the pin data you are looking for. I finally got access to an Arduino to figure it out. You want to use bitRead(PORTD, pin).

Serial.println(bitRead(PORTD,3)); //Reads bit 3 of register PORTD which contains the current state (high/low) of pin 3.

Reference Bit Read Operation for more information.



回答2:

Your sketch should be

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    delay(1000);

    pinMode(3, OUTPUT);
    digitalWrite(3, HIGH);
    delay(1000);

    // pinMode(3, INPUT); // get rid of this line
    Serial.println(digitalRead(3));
}

That's all. Then it reads the pin's state which in your case is "HIGH". If you set the pinMode to input it will read the input depending on what is connected. If you are writing "HIGH" to an input pin the internal pullup will be activated. It does not matter if you write HIGH before setting it to input mode or after setting it to input mode. Unless of course you are driving a load that is to high for the output pin (e.g. a switch to ground). Then this would probably kill the pin.

If you have written a low and set the pin to low it might float which may lead to any kind of unpredictable behaviour.



回答3:

digitalWrite(3,HIGH);
digitalRead(3);


回答4:

Why do you want to do that? If you are doing this to validate that the pin is really high, this will not confirm it to you, because maybe there is a short circuit on the high pin from the external circuit, the best way is to create a feedback through another pin; configure another pin as input, and connect the output pin to the new input pin, and read its value. Reading the internal register will always return for you what the controller is trying to put on the pin, not the actual pin value.



回答5:

Keep your pinMode() selection in the setup() function, and try the digitalWrite() and digitalRead() functions.

setup() will be executed when controller starts and loop() will be the function which keep executing.

int pin22 = 22;
void setup()
{
  Serial.begin(9600);
  pinMode(pin22,output);
}

void loop()
{
  digitalWrite(pin22,HIGH);
  digitalRead(pin22);
  digitalWrite(pin22,LOW);
  digitalRead(pin22);
}


回答6:

Didn't like any of the previous answers, because they would not be cross-arduino-platform compatible. You need to access through the pin reference tables. The following expression does the trick.

bool value = (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));

Let me break that down for better understanding

digitalPinToPort(pin) look up you the gpio bank [port] that the pin is assigned to on your selected hardware

portOutputRegister(...) gives you a pointer to the port containing the value you are looking for. * dereferences the pointer and gives the full value of all 8 pins assigned to that port. It is not yet uniquely useful, but the bit you are looking for is in there.

&digitalPinToBitMask(pin) selects only the bit you are interested in for the pin, all other bits will be zero.

0!= tests to see if the resulting expression is zero, or something else. If it is zero, then your output is zero on that pin. Otherwise the output is one.



回答7:

Are you trying to set the default input to HIGH?
If so you are looking to activate the pull-up register:

void setup() 
{
    Serial.begin(9600);
}

void loop() 
{
    delay(1000);

    pinMode(3,INPUT);         // default mode is INPUT  
    digitalWrite(3, HIGH);    // Turn on the internal pull-up resistor, default state is HIGH  

    delay(1000);
    Serial.println(digitalRead(3));
}

Excerpt from DigitalWrite:

If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor.



回答8:

Keep a separate boolean map of the output pin states.

If a microcontroller GPIO pin is set as an input, then its value, when read, depends on what it's connected to externally. That's kind of the point.



回答9:

ALways keep in mind. If you are trying to Configure anything ,Just keep it in setup file. Since setup file get executed once , If you are setting In loop . it execute continuous.and try to keep it remain it start state.That is active low state



回答10:

I wrote a routine for flashing four different LEDs for different purposes but I also wanted to retain their initial state before I flash them, as their steady state also tells me something is occurring in my code, so here is my Flash code, you call it by sending the pin number, the number of times to flash it and how long to flash it for.

inline void Flash (byte pinNum, byte times, byte flashSpeed)
  {
    bool state;                     // Local var for State of pin
    state = digitalRead(pinNum);    // Read the current state as set elsewhere
    int x;                          // Local var for repeat flash
    for (x = 0; x < times; x++)
      {
        digitalWrite(pinNum, HIGH); // Turn on LED
        delay(flashSpeed);
        digitalWrite(pinNum, LOW);  // Turn off LED
        delay(flashSpeed * 2);      //  leave off twice as long as on
      }                             //  due to persistence of Vision   
    digitalWrite(pinNum, state);    // Restore the original state of the LED
  }


回答11:

You can try:

int Pin22 = 22;
int valuePin22 = 0;

void setup() {

    pinMode(Pin22, OUTPUT);
    digitalWrite(Pin22, LOW);

}

void loop() {

    digitalWrite(Pin22, HIGH)
    valuePin22 = 1;
    Serial.println(valuePin22);
    delay(100);

    digitalWrite(Pin22, LOW)
    valuePin22 = 0;
    Serial.println(valuePin22);
    delay(1000)

}


标签: arduino