我怎样才能digitalRead针是在pinMode OUTPUT? [英] How can I digitalRead a pin that is in pinMode OUTPUT?
问题描述
我有一个非常简单的测试草图在我试图引脚设置为 HIGH
,然后读取它与 digitalRead
。这里是我的草图。
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));
}
串行监测结果:
0
0
0
0
我已经认识到,改变pinMode将被 HIGH
停止。因此,设置引脚 HIGH
在输出
模式,然后更改为输入
模式将其更改为 LOW
。所以digitalRead将始终返回0。如果我不改变pinMode它将不能够读取的销。所以,我怎么能读引脚在输出
模式不失价值?的当前设置
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?
推荐答案
在这种情况下,你只是想访问数据注册。
In this case you just want to access the data register itself.
PORTB和PORTD寄存器包含你正在寻找的针数据。我终于获得一个Arduino弄明白。要使用 bitRead(PORTD,针)
。
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.
读操作了解详情。
这篇关于我怎样才能digitalRead针是在pinMode OUTPUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!