Jag kommer absolut ingenstans så mina tafatta försök är tyvärr inget att visa upp här
![Sad :(](./images/smilies/sad.gif)
Avståndet kan ju vara en orsak, helt klart, om det stämmer så är det ju ett problem.blueint skrev:Har för mig det endast fungerar om:
* Avståndet är några cm.
* Endast en enhet är inkopplad.
* Samt ev kraft tas separat.
Overdrive är en högre hastigheten än 'normal' med vilken man pratar med 1-trådskretsarna.danielr112 skrev:Vad är overdrive av 1-wire kretsar för nåt?.
Det är ju inte speciellt mycket men jag skulle ändå se det fungera, även om det bara fungerar på korta sträckor.mrfrenzy skrev:I databladet för DS1481 så rekommenderar dom högst 3 tum mellan master och slave.
Jo, så här är det.sodjan skrev:> Jag skulle gissa att de flesta undrar varför du envisas med "overdrive" alls...
Om du bara visste hur många gånger jag läst databladet, har blivit gråhårig på köpet.mrfrenzy skrev:Har du provat att läsa databladet för SHT11?
Klistra in ditt schema och din kod här så kanske vi kan se vad som är fel.
Kod: Markera allt
{----------------------------------------------------------------------------------------------}
{ }
{ Reading data from a SHT11 (by SENSIRION) using DS2406 (by Dallas Semiconductor Corporation). }
{ The unit will read temperature and humidity data and verify them using CRC8. }
{ }
{----------------------------------------------------------------------------------------------}
unit UnitSHTxx;
interface
uses SysUtils, WinTypes, Dialogs, MiscApp, LogApp, Forms, Messages;
procedure SHTxx_DataInFile2Memo;
procedure SHTxx_MemoData2file;
procedure SHTxx_ClearGrid;
procedure SHTxx_Update_Grid(te,rh: String);
procedure SHTxx_Do_Both_Measurements;
procedure SHTxx_Check_Registers;
procedure SHTxx_Do_Temp_Measurement;
procedure SHTxx_Do_RH_Measurement;
procedure SHTxx_Idle;
procedure SHTxx_Reset;
procedure SHTxx_TransmissionStart;
procedure SHTxx_Byte_Out(Code: Byte);
function SHTxx_Byte_In(zzz: String; Ack: Byte): Byte; //zzz=MSB/LSB/CRC. DATA-pin = PIO-A
procedure SHTxx_SetLevel_PIO_X(PIO_X: String; Level: Integer);
function SHTxx_ReadStatusMemoryMap_PIO_A(Z: Integer): Integer; //ack: 1= add to memo, 0= don't
procedure SHTxx_DoCRC8(X: Byte);
function ReverseByte(const C: Byte): Byte;
implementation
uses RayUn, TestApp, iBTMEXPW;
const
// Reserved= $xx; //000_0000x; //do not remove
TempCode= $03; //000_00011;
RHCode= $05; //000_00101;
WriteRegisters= $06; //000_00110;
ReadRegisters= $07; //000_00111;
// Reserved= $xx; //000_0101x - 1110x; //do not remove
SoftReset= $1E; //000_11110;
SDA= 'PIO-A';
SCLK= 'PIO-B';
var H, L, CRC: Byte;
tmp,
Temp_C,
RH_Linear,
RH_True: real;
tTemp, tRH,
datumX, tidX: String;
HandleErrorValue: Integer;
SHT11_CRC8: Byte;
SHT11_CRC8_ok: Boolean;
Tranbuff: Array[1..16] of Integer;
{-----------------------------------------------------------------}
{ Clear grid SHT11 }
procedure SHTxx_ClearGrid;
begin
TestApp.TraceData('UnitSHTxx.SHT11ClearGrid');
RayMain.SHT11_StringAlignGrid.Cells[0, 0]:= 'Date';
RayMain.SHT11_StringAlignGrid.Cells[1, 0]:= 'Time';
RayMain.SHT11_StringAlignGrid.Cells[2, 0]:= 'ROM';
RayMain.SHT11_StringAlignGrid.Cells[3, 0]:= 'Temp';
RayMain.SHT11_StringAlignGrid.Cells[4, 0]:= 'RH %';
SHT11col:= 0;
SHT11row:= 1;
RayMain.SHT11_StringAlignGrid.RowHeights[0]:= 17;
RayMain.SHT11_StringAlignGrid.ColWidths[0] := 80;
RayMain.SHT11_StringAlignGrid.ColWidths[1] := 70;
RayMain.SHT11_StringAlignGrid.ColWidths[2] := 125;
RayMain.SHT11_StringAlignGrid.ColWidths[3] := 49;
RayMain.SHT11_StringAlignGrid.ColWidths[4] := 50;
RayMain.SHT11_StringAlignGrid.AutoAdjustLastCol:= TRUE;
end;
{-----------------------------------------------------------------}
{ Do both Temp and Humidity measurements }
procedure SHTxx_Do_Both_Measurements;
var s: String;
begin
TestApp.TraceData('--------------------------------------------------');
TestApp.TraceData('Starting UnitSHTxx.SHTxx_Do_Both_Measurements');
MiscApp.GetDateAndTime(0, datumX,tidX);
HandleErrorValue:= 0;
SHTxx_Do_Temp_Measurement;
RayUn.RayMain.Refresh;
SHTxx_Do_RH_Measurement;
RayUn.RayMain.Refresh;
s:= 'Date:' + datumX + TAB + 'Time:' + tidX + TAB + 'ROM:' + LookingForROM + TAB +
'SHT11 Temp:' + tTemp + '; SHT11 RH:' + tRH;
LogApp.WriteDataToOutFile(s);
TestApp.TraceData('Finished with UnitSHTxx.SHTxx_Do_Both_Measurements');
TestApp.TraceData('--------------------------------------------------');
end;
{-----------------------------------------------------------------}
procedure SHTxx_MemoData2file;
var s, tmpDate: String;
begin
tmpDate:= FormatDateTime('yyyy-mm-dd', Date);
s:= LogDir+tmpDate+SPACE+SHTdata;
AssignFile(SHTdataFile, s);
if FileExists(s)= FALSE then
begin
rewrite(SHTdataFile);
closefile(SHTdataFile);
end;
RayMain.SHT11_StatusMemo.Lines.SaveToFile(s);
end;
{-----------------------------------------------------------------}
procedure SHTxx_DataInFile2Memo;
var s, tmpDate: String;
begin
tmpDate:= FormatDateTime('yyyy-mm-dd', Date);
s:= LogDir+tmpDate+SPACE+SHTdata;
AssignFile(SHTdataFile, s);
if FileExists(s)= TRUE then
RayMain.SHT11_StatusMemo.Lines.LoadFromFile(s);
end;
{-----------------------------------------------------------------}
{ Update SHT11 grid with data }
procedure SHTxx_Update_Grid(te,rh: String);
var AddRow: Boolean;
begin
AddRow:= FALSE;
with RayMain.SHT11_StringAlignGrid do
begin
if (SHT11row= 1) or ((te<> blank) and (rh<> blank)) then
begin
Cells[0, SHT11row]:= datumX;
Cells[1, SHT11row]:= tidX;
Cells[2, SHT11row]:= LookingForROM;
Cells[3, SHT11row]:= te;
Cells[4, SHT11row]:= rh;
AddRow:= TRUE;
end
else
if ((te= blank) or (rh= blank)) then
begin //data existed in row already
if (Cells[3, SHT11row-1]= blank) and (te<> blank) then
begin
Cells[3, SHT11row-1]:= te;
AddRow:= FALSE;
end
else
if (Cells[4, SHT11row-1]= blank) and (rh<> blank) then
begin
Cells[4, SHT11row-1]:= rh;
AddRow:= FALSE;
end
else
begin
Cells[0, SHT11row]:= datumX;
Cells[1, SHT11row]:= tidX;
Cells[2, SHT11row]:= LookingForROM;
Cells[3, SHT11row]:= te;
Cells[4, SHT11row]:= rh;
AddRow:= TRUE;
end;
end
else
begin
if (Cells[0, SHT11row]= blank) then //check if new row is blank
begin
AddRow:= FALSE;
Cells[0, SHT11row]:= datumX;
Cells[1, SHT11row]:= tidX;
Cells[2, SHT11row]:= LookingForROM;
end;
if te= blank then
Cells[3, SHT11row]:= te
else
if rh= blank then
Cells[4, SHT11row]:= rh
else
begin
AddRow:= TRUE;
Cells[0, SHT11row]:= datumX;
Cells[1, SHT11row]:= tidX;
Cells[2, SHT11row]:= LookingForROM;
Cells[3, SHT11row]:= te;
Cells[4, SHT11row]:= rh;
end;
end;
if AddRow= TRUE then
begin
INC(SHT11row);
InsertRow(SHT11row);
end;
Row:= SHT11row;
end;
end;
{-----------------------------------------------------------------}
{ Check registers in SHT11 }
procedure SHTxx_Check_Registers;
begin
RayMain.SHT11_StatusMemo.Lines.Add('Checking Status registers');
SHTxx_Reset;
SHTxx_TransmissionStart;
MiscApp.GetDateAndTime(0, datumX,tidX);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' C''Read Status Reg: ' + tidX+ CRLF;
HandleErrorValue:= 0;
SHTxx_Byte_Out(ReadRegisters);
SHTxx_Idle;
Sleep(100); //wait for values to be written to status register
RayMain.SHT11_StatusMemo.Lines.Add('--');
H:= SHTxx_Byte_In('MSB', 0); // 0= skip ACK to end transmission
SHTxx_Idle;
if HandleErrorValue= 1 then
begin
RayMain.SHT11_StatusMemo.Lines.Add('--');
RayMain.SHT11_StatusMemo.Lines.Add('Status register: '+FloatToStrF(H, ffGeneral, 8,0));
RayMain.SHT11_StatusMemo.Lines.Add('--------------------------------------------------');
end;
SendMessage(RayMain.SHT11_StatusMemo.Handle, EM_SCROLLCARET, 0, 0);
if RayMain.CheckBoxSHTmemoData2file.Checked= TRUE then
SHTxx_MemoData2file;
end;
{-----------------------------------------------------------------}
{ Do Temp measurement }
procedure SHTxx_Do_Temp_Measurement;
var datum, tid: String;
xx: Integer;
SHT11_Temp_Done: Boolean;
begin
MiscApp.GetDateAndTime(0, datumX,tidX);
SHT11_CRC8_ok:= FALSE;
SHT11_Temp_Done:= FALSE;
SHT11_CRC8:= 0;
xx:= 0;
tTemp:= '-99';
RayMain.SHT11_StatusMemo.Lines.Add('Starting Temperature Measurement');
RayMain.SHT11_StatusMemo.Lines.Add('--');
repeat
INC(xx);
SHTxx_Reset;
SHTxx_TransmissionStart;
while HandleErrorValue= 1 do //moved to here 4/1-09
begin
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' C''Soft Reset: ' + tid + CRLF;
SHTxx_Byte_Out(SoftReset);
SHTxx_Idle;
Sleep(250); //wait for values to be written to status register
SHTxx_Reset;
SHTxx_TransmissionStart;
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' C''Measure T: ' + tid + CRLF;
RayMain.SHT11_StatusMemo.Lines.Add('');
SHTxx_Byte_Out(TempCode);
Sleep(750);
RayMain.SHT11_StatusMemo.Lines.Add('--');
while HandleErrorValue= 1 do
begin
H:= SHTxx_Byte_In('MSB', 1); //High byte. 1= wait for ACK
L:= SHTxx_Byte_In('LSB', 1); // Low byte. 1= wait for ACK
CRC:= SHTxx_Byte_In('CRC', 0); // 0= skip ACK to end transmission
BREAK;
end;
SHTxx_DoCRC8(TempCode); //check crc for each byte
SHTxx_DoCRC8(H); //check crc for each byte
SHTxx_DoCRC8(L); //check crc for each byte
CRC:= ReverseByte(CRC);
if CRC= SHT11_CRC8 then SHT11_CRC8_ok:= TRUE;
if xx= 2 then SHT11_Temp_Done:= TRUE;
if SHT11_CRC8_ok= FALSE then
begin
tTemp:= '-99';
end
else
begin
tmp:= int(H)*256+ int(L);
Temp_C:= -40.00 + (0.01 * tmp); //5.0V feed and 14bit resolution
// Temp_C:= -39.55 + (0.01 * tmp); //2.5V feed and 14bit resolution
// Temp_C:= -39.55 + (0.04 * tmp); //2.5V feed and 14bit resolution
if (Temp_C< -40) or (Temp_C> 125) then
Temp_C:= -99
else
SHT11_Temp_Done:= TRUE;
tTemp:= FloatToStrF(Temp_C, ffGeneral, 3, 2);
if length(tTemp)< 3 then tTemp:= tTemp + ',0';
if (copy(tTemp,length(tTemp),1)= comma) or (copy(tTemp,length(tTemp),1)= dot) then
tTemp:= tTemp + '0';
end;
BREAK;
end;
if xx= 2 then SHT11_Temp_Done:= TRUE;
until SHT11_Temp_Done= TRUE;
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Lines.Add('--');
RayMain.SHT11_StatusMemo.Lines.Add('Temp (in C) at ' + tid + ': '+tTemp);
RayMain.SHT11_StatusMemo.Lines.Add('--------------------------------------------------');
SendMessage(RayMain.SHT11_StatusMemo.Handle, EM_SCROLLCARET, 0, 0);
SHTxx_Update_Grid(tTemp,blank);
if RayMain.CheckBoxSHTmemoData2file.Checked= TRUE then
SHTxx_MemoData2file;
end;
{-----------------------------------------------------------------}
{ Do Humidity measurement }
procedure SHTxx_Do_RH_Measurement;
var datum, tid: String;
xx: Integer;
SHT11_RH_Done: Boolean;
begin
MiscApp.GetDateAndTime(0, datumX,tidX);
SHT11_CRC8_ok:= FALSE;
SHT11_RH_Done:= FALSE;
SHT11_CRC8:= 0;
xx:= 0;
tRH:= '-99';
RayMain.SHT11_StatusMemo.Lines.Add('Starting Relative Humidity Measurement');
RayMain.SHT11_StatusMemo.Lines.Add('--');
repeat
INC(xx);
SHTxx_Reset;
SHTxx_TransmissionStart;
while HandleErrorValue= 1 do //moved to here 4/1-09
begin
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' C''Soft Reset: ' + tid + CRLF;
SHTxx_Byte_Out(SoftReset);
SHTxx_Idle;
Sleep(250); //wait for values to be written to status register
SHTxx_Reset;
SHTxx_TransmissionStart;
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' C''Measure H: ' + tid + CRLF;
RayMain.SHT11_StatusMemo.Lines.Add('');
SHTxx_Byte_Out(RHCode);
Sleep(500);
RayMain.SHT11_StatusMemo.Lines.Add('--');
while HandleErrorValue= 1 do
begin
H:= SHTxx_Byte_In('MSB', 1); //High byte. 1= wait for ACK
L:= SHTxx_Byte_In('LSB', 1); // Low byte. 1= wait for ACK
CRC:= SHTxx_Byte_In('CRC', 0); // 0= skip ACK to end transmission
BREAK;
end;
SHTxx_DoCRC8(RHCode); //check crc for each byte
SHTxx_DoCRC8(H); //check crc for each byte
SHTxx_DoCRC8(L); //check crc for each byte
CRC:= ReverseByte(CRC);
if CRC= SHT11_CRC8 then SHT11_CRC8_ok:= TRUE;
if xx= 2 then SHT11_RH_Done:= TRUE;
if SHT11_CRC8_ok= FALSE then
begin
tRH:= '-99';
RH_Linear:= -99;
end
else
begin
//12bit resolution
tmp:= int(H)*256+ int(L); //12bit resolution
RH_Linear:= -4.0 + (0.0405 * tmp) - (2.8e-6 * tmp * tmp); //12bit resolution
//----------------
(*
//8bit resolution
tmp:= int(L); // 8bit resolution
RH_Linear:= -4.0 + (0.648 * tmp) - (7.2e-4 * tmp * tmp); // 8bit resolution
//----------------
*)
if Temp_C<> -99 then
begin
RH_True:= (Temp_C - 25.0) * (0.01 + 0.00008 * tmp) + RH_Linear;
SHT11_RH_Done:= TRUE;
end
else
RH_True:= -99;
if RH_Linear< 0 then RH_Linear:= -99;
if RH_Linear> 100 then RH_Linear:= 100;
if RH_True< 0 then RH_True:= -99;
if RH_True> 100 then RH_True:= 100;
tRH:= FloatToStrF(RH_True, ffGeneral, 3, 2);
if length(tRH)< 3 then tRH:= tRH + ',0';
if (copy(tRH,length(tRH),1)= comma) or (copy(tRH,length(tRH),1)= dot) then
tRH:= tRH + '0';
end;
BREAK;
end;
if xx= 2 then SHT11_RH_Done:= TRUE;
until SHT11_RH_Done= TRUE;
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Lines.Add('--');
RayMain.SHT11_StatusMemo.Lines.Add('RH linear (in %) at ' + tid + ': '+FloatToStrF(RH_Linear, ffGeneral, 3, 2));
RayMain.SHT11_StatusMemo.Lines.Add(' true (in %) at ' + tid + ': '+tRH);
RayMain.SHT11_StatusMemo.Lines.Add('--------------------------------------------------');
SendMessage(RayMain.SHT11_StatusMemo.Handle, EM_SCROLLCARET, 0, 0);
SHTxx_Update_Grid(blank, tRH);
if RayMain.CheckBoxSHTmemoData2file.Checked= TRUE then
SHTxx_MemoData2file;
end;
{-----------------------------------------------------------------}
procedure SHTxx_Idle;
begin
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
SHTxx_SetLevel_PIO_X(SDA, 1); //DATA high
end;
{-----------------------------------------------------------------}
procedure SHTxx_Reset;
var xx: Integer;
datum, tid: String;
begin
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ' SHT Reset: ' + tid + CRLF;
SHTxx_Idle;
while HandleErrorValue= 1 do
begin
for xx:= 1 to 9 do
begin
while HandleErrorValue= 1 do // added 4/1-09
begin
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
BREAK; //added 4/1-09
end;
end;
BREAK;
end;
end;
{-----------------------------------------------------------------}
procedure SHTxx_TransmissionStart;
var datum, tid: String;
begin
MiscApp.GetDateAndTime(0, datum,tid);
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + 'Transmission start: ' + tid + CRLF;
while HandleErrorValue= 1 do
begin
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
SHTxx_SetLevel_PIO_X(SDA, 0); //DATA low
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
SHTxx_SetLevel_PIO_X(SDA, 1); //DATA high
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
BREAK;
end;
end;
{-----------------------------------------------------------------}
procedure SHTxx_Byte_Out(Code: Byte);
var xx: Integer;
begin
while HandleErrorValue= 1 do
begin
for xx:= 1 to 8 do
begin
if (Code AND $80)= 0 then
SHTxx_SetLevel_PIO_X(SDA, 0) //DATA low
else
SHTxx_SetLevel_PIO_X(SDA, 1); //DATA high
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
Code:= Code*2; //shift left 1 pos.
end;
SHTxx_SetLevel_PIO_X(SDA, 1); //DATA high
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
BREAK;
end;
SHTxx_Idle;
end;
{-----------------------------------------------------------------}
function SHTxx_Byte_In(zzz: String; Ack: Byte): Byte; //zzz= MSB/LSB/CRC. DATA-pin= PIO-A
var xx: Integer;
N,Z: Byte;
begin
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + 'Data received ('+zzz+'): ';
Z:= 0;
N:= 0;
// RayMain.SHT11_StatusMemo.Lines.Add('SHT Byte in:');
// while N= 1 do //loop until DATA goes low
// begin
// N:= SHT11_ReadStatusMemoryMap_PIO_A;
// RayMain.SHT11_StatusMemo.Lines.Add('');
// Sleep(5);
// end;
while HandleErrorValue= 1 do
begin
for xx:= 1 to 8 do
begin
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
N:= N*2; //shift left 1 pos.
if SHTxx_ReadStatusMemoryMap_PIO_A(1)= 1 then //add to memo
inc(N);
SHTxx_SetLevel_PIO_X(SCLK, 0); //SCLK low
end;
if Ack= 0 then
SHTxx_SetLevel_PIO_X(SDA, 1) //DATA high
else
SHTxx_SetLevel_PIO_X(SDA, 0); //DATA low
SHTxx_SetLevel_PIO_X(SCLK, 1); //SCLK high
Z:= SHTxx_ReadStatusMemoryMap_PIO_A(0); //1= add to memo, 0=don't
SHTxx_Byte_In:= N;
BREAK;
end;
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + ', ack= '+ IntToStr(Z);
RayMain.SHT11_StatusMemo.Lines.Add('');
SHTxx_Idle;
end;
{-----------------------------------------------------------------}
procedure SHTxx_SetLevel_PIO_X(PIO_X: String; Level: Integer);
var tmp: byte;
xx: Integer;
begin
TestApp.TraceData('UnitSHTxx.SHT11_SetLevel_PortX');
if LookingForROM= '' then
begin
RayMain.SHT11_StatusMemo.Lines.Add('No ROM, exitting');
TestApp.TraceData('No ROM, exitting');
HandleErrorValue:= -99;
EXIT;
end;
{ attempt to start a session on port }
HandleErrorValue:= Get1wHandle(LookingForROM);
if HandleErrorValue<> 1 then
begin
RayMain.SHT11_StatusMemo.Lines.Add('DS2406 handle error in SHT11 SetLevel, exitting');
TestApp.TraceData('DS2406 handle error, exitting');
EXIT;
end
else
begin
(*
RayMain.CheckBoxSHTinOverdrive.Checked:= TRUE;
xx:= SHT11row;
if TMAutoOverDrive(SHandle, @ROMarray, 1)<> 1 then
begin
RayMain.SHT11_StatusMemo.Lines.Add('No overdrive, sigh');
RayMain.CheckBoxSHTinOverdrive.Checked:= FALSE;
end;
SHT11row:= xx;
*)
//(*
TMTouchByte(SHandle, $F5); //Send channel access command
{Send ch. control bytes}
if PIO_X= SDA then //'PIO-A'
TMTouchByte(SHandle, $84) //$84 == d132 = 128+4 (ALR=1,IM=0,TOG=0,IC=0,CHS1=0,CHS0=1,CRC1=0,CRC0=0)
else
if PIO_X= SCLK then //'PIO-B'
TMTouchByte(SHandle, $88); //$88 == d136 = 128+8 (ALR=1,IM=0,TOG=0,IC=0,CHS1=1,CHS0=0,CRC1=0,CRC0=0)
TMTouchByte(SHandle, $FF); //always $FF as 2nd byte
tmp:= TMTouchByte(SHandle, $FF); //receive status byte
status:= tmp;
if Level= 1 then
TMTouchByte(SHandle, $F0) //$F0 to set (1) PIO
else
TMTouchByte(SHandle, $0F); //$0F to clear (0) PIO
(*
Tranbuff[1]:= $F5;
// TMTouchByte(SHandle, Tranbuff[1]); //send command
if PIO_X= SDA then //'PIO-A'
Tranbuff[2]:= $84
else
if PIO_X= SCLK then //'PIO-B'
Tranbuff[2]:= $88;
// TMTouchByte(SHandle, Tranbuff[2]); //1st byte
Tranbuff[3]:= $FF; //2nd byte, always $FF
// TMTouchByte(SHandle, Tranbuff[3]); //send 2nd byte, always $FF
Tranbuff[4]:= $FF; //receive status byte
// status:= TMTouchByte(SHandle, Tranbuff[4]); //always $FF to receive a byte
if Level= 1 then
Tranbuff[5]:= $F0 //$F0 to set (1) PIO
else
Tranbuff[5]:= $0F; //$0F to clear (0) PIO
// TMTouchByte(SHandle, Tranbuff[5]); //
// TMTouchReset(SHandle);
TMBlockStream(SHandle, @Tranbuff, 5); //Send channel access command
// if tmp<> 5 then
// RayMain.SHT11_StatusMemo.Lines.Add('Wrong value read back');
// tmp:= TMBlockIO(SHandle, @Tranbuff, 3); //Send channel access command and the rest
status:= Tranbuff[4];
*)
end;
TMEndSession(SHandle);
TestApp.TraceData(' Finished');
end;
{-----------------------------------------------------------------}
function SHTxx_ReadStatusMemoryMap_PIO_A(Z: Integer): Integer; //Z: 1= add to memo, 0= don't
var xx, tmp: integer;
begin
TestApp.TraceData('UnitSHTxx.ReadStatusMemoryMap_PortA');
SHTxx_ReadStatusMemoryMap_PIO_A:= 0;
if LookingForROM= '' then
begin
RayMain.SHT11_StatusMemo.Lines.Add('No ROM, exitting');
TestApp.TraceData('No ROM, exitting');
HandleErrorValue:= -99;
EXIT;
end;
SHTxx_ReadStatusMemoryMap_PIO_A:= 1; //default Ch.A = 0
{ attempt to start a session on port }
HandleErrorValue:= Get1wHandle(LookingForROM);
if HandleErrorValue<> 1 then
begin
RayMain.SHT11_StatusMemo.Lines.Add('DS2406 handle error in ReadStatusMemoryMap, exitting');
TestApp.TraceData('DS2406 handle error, exitting');
EXIT;
end;
(*
RayMain.CheckBoxSHTinOverdrive.Checked:= TRUE;
xx:= SHT11row;
if TMAutoOverDrive(SHandle, @ROMarray, 1)<> 1 then
begin
RayMain.SHT11_StatusMemo.Lines.Add('No overdrive, sigh');
RayMain.CheckBoxSHTinOverdrive.Checked:= FALSE;
end;
SHT11row:= xx;
*)
{Send read status memory}
TMTouchByte(SHandle, $F5);
{Send ch. control bytes}
TMTouchByte(SHandle, $C4); //Channel A. $C4 == d196 = 128+64+4 (ALR=1,IM=1,TOG=0,IC=0,CHS1=0,CHS0=1,CRC1=0,CRC0=0)
TMTouchByte(SHandle, $FF); //always $FF as 2nd byte
tmp:= TMTouchByte(SHandle, $FF);
if (tmp and 4)= 4 then
status:= 1
else
status:= 0;
SHTxx_ReadStatusMemoryMap_PIO_A:= status;
if Z= 1 then
RayMain.SHT11_StatusMemo.Text:= RayMain.SHT11_StatusMemo.Text + SPACE + IntToStr(status);
TMEndSession(SHandle);
TestApp.TraceData(' Finished');
end;
{-----------------------------------------------------------------}
{ This procedure calculates the cumulative Sensirion SHTxx
CRC8 of all bytes passed to it.
The result accumulates in the global variable SHTxx_CRC8.
}
procedure SHTxx_DoCRC8(X: Byte);
Const
Table : Array[0..255] of Byte = (
0, 49, 98, 83,196,245,166,151,185,136,219,234,125, 76, 31, 46,
67,114, 33, 16,135,182,229,212,250,203,152,169, 62, 15, 92,109,
134,183,228,213, 66,115, 32, 17, 63, 14, 93,108,251,202,153,168,
197,244,167,150, 1, 48, 99, 82,124, 77, 30, 47,184,137,218,235,
61, 12, 95,110,249,200,155,170,132,181,230,215, 64,113, 34, 19,
126, 79, 28, 45,186,139,216,233,199,246,165,148, 3, 50, 97, 80,
187,138,217,232,127, 78, 29, 44, 2, 51, 96, 81,198,247,164,149,
248,201,154,171, 60, 13, 94,111, 65,112, 35, 18,133,180,231,214,
122, 75, 24, 41,190,143,220,237,195,242,161,144, 7, 54,101, 84,
57, 8, 91,106,253,204,159,174,128,177,226,211, 68,117, 38, 23,
252,205,158,175, 56, 9, 90,107, 69,116, 39, 22,129,176,227,210,
191,142,221,236,123, 74, 25, 40, 6, 55,100, 85,194,243,160,145,
71,118, 37, 20,131,178,225,208,254,207,156,173, 58, 11, 88,105,
4, 53,102, 87,192,241,162,147,189,140,223,238,121, 72, 27, 42,
193,240,163,146, 5, 52,103, 86,120, 73, 26, 43,188,141,222,239,
130,179,224,209, 70,119, 36, 21, 59, 10, 89,104,255,206,157,172);
begin
SHT11_CRC8:= Table[SHT11_CRC8 xor X];
end;
{-----------------------------------------------------------------}
function ReverseByte(const C: Byte): Byte;
type ByteSet= set of 0..7;
var b: Byte;
s: ByteSet;
begin
s:= [];
for b:= 0 to 7 do
if 7-b in ByteSet(C) then Include(s, b);
ReverseByte:= Byte(s);
end;
{-----------------------------------------------------------------}
end.