Hey guys, I have been googling this whole day, but seems that no answer is working for me.
In my code I use one library that sends High or Low to pin 13 (OUTPUT).
I need to get this Pin state in my code so I can do some other things with that state.
I tried to reach Port Registers with this code:
bitRead(PORTB,5)
But in my Serial monitor I get only:
������������������������������������������������������������������
I use Arduino UNO
Any Ideas?
The code you posted doesn't print anything. So, it's impossible to tell where are the ? symbols are coming from.
gfvalvo:
The code you posted doesn't print anything. So, it's impossible to tell where are the ? symbols are coming from.
Serial.println(bitRead(PORTB,5));
The following diagram (Fig-1) may help you to know the value (undisturbed) that you have just written into an output port/pin.
Figure-1:
Assume that you have written HIGH at DPin-13 which drives a low impedance load causing VOH to drop at 2.5V (below VIH), which one of the following two instructions are you going to execute to know that you wrote HIGH at DPin-13 and not LOW?
bool n = PINB5; //same as: bool n = bitRead(PINB, 5);
bool n = PORTB5; //same as: bool n = bitRead(PORTB, 5);
In my code I use one library that sends High or Low to pin 13 (OUTPUT).
I need to get this Pin state in my code so I can do some other things with that state.
byte pinState = digitalRead(13);
Yes, you read that correctly, you can read the state of an output pin
UKHeliBob:
byte pinState = digitalRead(13);
Yes, you read that correctly, you can read the state of an output pin
No, it can not give the correct state.
Assume that the OP has written HIGH at DPin-13 which drives a low impedance load causing VOH to drop at 2.5V (below VIH = 3.0V), then execution of digitalRead(13) would return LOW (0); but, it is HIGH that was written on DPin-3. Therefore, the correct instruction (better safe than sorry) is:
bool pinState = bitRead(PORTB, 5);
The execution of digitalRead(13) on an output pin/port is equivalent to the execution of bitRead(PINB, 5) which reads the pin-value (Fig-1).
Figure-1:
it can not give the correct state.
I must be imagining that this works then
byte ledPin = 13;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.println(digitalRead(ledPin) ? "HIGH" : "LOW");
digitalWrite(ledPin, LOW);
Serial.println(digitalRead(ledPin) ? "HIGH" : "LOW");
}
void loop()
{
}
UKHeliBob:
I must be imagining that this works then
You are still reading the pin-value of DPin-13 (which could be even 0.7V if driving the base of a transistor for example) and not the undisturbed value that was written on the pin for which we need to execute this code: bool n = PORTB5 or bool n = bitRead(PORTB, 5) and not bool n = digitalRead(13) or bool n = bitRead(PINB, 5).
The AVR processor reads the output data latch (internal), not the pin buffer output (which is connected to the output pin), when it reads from a pin that is configured for output.
aarg:
The AVR processor reads the output data latch (internal), not the pin buffer output (which is connected to the output pin), when it reads from a pin that is configured for output.
The above quote agrees with the following conceptual diagram for Bit-5 of Port-B Register (Fig-1); where, the 'output data latch is PORTB', and the 'pin buffer output is PINB/G5ID'.
Figure-1:
digitalRead() doesn't differ from your port read in how it acquires the input state:
if (*portInputRegister(port) & bit) return HIGH;
return LOW;
is not functionally different than your:
bool n = bitRead(PINB, 5)