int DHTpin=2;
byte dat[5];
//------------------------------------------------------
byte read_data()
{ byte data;
for(int i=0;i<8;i++)
{
if(digitalRead(DHTpin)==LOW)
{
while(digitalRead(DHTpin)==LOW) ;
delayMicroseconds(30);
if(digitalRead(DHTpin)==HIGH)
data|=(1<<(7-1));
while(digitalRead(DHTpin)==HIGH) ;
}
}
return data;
}
//------------------------------------------------------
void start_test(){
digitalWrite(DHTpin,LOW);
delay(30);
digitalWrite(DHTpin,HIGH);
delayMicroseconds(40);
pinMode(DHTpin,INPUT);
while(digitalRead(DHTpin)==LOW) ;
delayMicroseconds(80);
for(int i=0;i<5;i++)
dat[i]=read_data();
pinMode(DHTpin,OUTPUT);
digitalWrite(DHTpin,HIGH);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(DHTpin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
start_test();
for(int i=0;i<5;i++)
{
Serial.print("dat[");
Serial.print(i);
Serial.print("]=");
Serial.print(dat[i]);
Serial.print(' ');
}
delay(700);
}
为什么串口监视器上一直显示0
read_data()里的代码该怎么写?