Понадобилась поддержка датчика влажности и температуры DHT22 для моего самогонного аппарата, работающего под управлением Raspberry PI.
Но, к сожалению, готового примера на паскале я так и не нашел. В интернетах нашел только на С. Портировал. Берите, вдруг кому нужно.
- Код: Выделить всё
- unit DHT22U;
 interface
 uses
 wiringpi;
 function ReadDHT22Data(Pin : Integer;
 var Temperature : Real;
 var Humidity : Real) : Boolean;
 implementation
 function ReadDHT22Data(Pin : Integer;
 var Temperature : Real;
 var Humidity : Real) : Boolean;
 const
 MAX_TIMINGS = 85;
 var
 initResult : longint;
 data : array [0 ..4] of Integer;
 counter : Integer;
 lastState : Integer;
 i,j : Integer;
 begin
 result := False;
 Temperature := 0;
 Humidity := 0;
 for i := 0 to 4 do
 begin
 data[i] := 0;
 end;
 j := 0;
 initResult := wiringPiSetup();
 if (initResult <> -1) then
 begin
 lastState := HIGH;
 // pull pin down for 20 milliseconds
 pinMode(Pin,OUTPUT);
 digitalWrite(Pin,HIGH);
 delay(500);
 digitalWrite(Pin,LOW);
 delay(20);
 // prepare to read the pin
 pinMode(Pin,INPUT);
 // detect change and read data
 for i := 0 to MAX_TIMINGS - 1 do
 begin
 counter := 0;
 while (digitalRead(Pin) = laststate) do
 begin
 Inc(counter);
 delayMicroseconds(2);
 if (counter = 255) then
 begin
 Break;
 end;
 end;
 laststate := digitalRead(Pin);
 if (counter = 255) then
 begin
 Break;
 end;
 // ignore first 3 transitions
 if ((i >= 4) and (i mod 2 = 0)) then
 begin
 // shove each bit into the storage bytes
 data[j div 8] := data[j div 8] shl 1;
 if (counter > 16) then
 begin
 data[j div 8] := data[j div 8] or 1;
 end;
 Inc(j);
 end;
 end;
 // check we read 40 bits (8bit x 5) + verify checksum in the last byte
 if ((j >= 40) and
 (data[4] = ((data[0] + data[1] + data[2] + data[3]) and $FF))) then
 begin
 Humidity := ((data[0] * 256) + data[1]) / 10;
 Temperature := (((data[2] and $7F) * 256) + data[3]) / 10;
 if ((data[2] and $80) <> 0) then
 begin
 Temperature := -Temperature;
 end;
 result := True
 end;
 end;
 end;
 end.



