__delay_ms(500);
Specsheet says in Section 4, must wait 1-second on power up before interacting with this device. This is too short.
Probably, I never used a DHT11, I just focused on the timings
.
When we make TRISIO0 = 0; this puts the pin on output mode, what happens if we make GP0 = 1; and GP0 = 0;? (Also when TRISIO0=1;)
TRISIO0=0 puts GP0 in output mode. Writing to GP0 will set the output state.
TRISIO0=1 puts GP0 in input mode. Writing to GP0 makes no effect. Reading GP0 reflects the state of the pin.
However, I explained why I'm setting GP0 high/low, due a Proteus bug.
Proteus is a bit buggy, after writting 0 to GPIO always reads 0 in input mode. Setting it to 1 in input mode seems to work.
- Why do we shift bits? What does shifting bit means? (I googled it but still...)
Shifting is the same as rotating:
Data = 01000010
Data <<=1;
Data = 10000100
- __delay_us(30); // Wait 30us (1=70us, 0=26us) I am a bit confused. Manuals says every bit of daha begins with a 50 us low-voltage-level and if +70= 1 and +26=0. Why do we wait only for 30 us? what about 50us at the beginning of the bit? I could not get this.
It already waited for the data going low and rising again, that's the part detecting each data bit.
After 30us the data will be 0 again (Next start) if it was a "0” bit (26us), or still 1 for a "1" bit (70us).
uint8_t dht11_read() {
uint8_t data = 0;
for (uint8_t i = 0; i < 8; i++) { // Receive 8 bits
data <<=1; // Shift data
while(DHT11_PIN); // Wait until pin goes low (start of data)
__delay_us(5); // Wait 5us to prevent noise
while(!DHT11_PIN); // Wait until pin goes high (data)
__delay_us(30); // Wait 30us (1=70us, 0=26us) <-----
data |= DHT11_PIN; // Store bit
}
return data;
}
The shift is placed first because we don't want to rotate the last bit.
At first data=0 so it makes no effect