单总线控制的温度传感器ds18b20芯片.docx
《单总线控制的温度传感器ds18b20芯片.docx》由会员分享,可在线阅读,更多相关《单总线控制的温度传感器ds18b20芯片.docx(29页珍藏版)》请在冰豆网上搜索。
单总线控制的温度传感器ds18b20芯片
unitTempmain;
interface
uses
SysUtils,WinTypes,WinProcs,Messages,Classes,Graphics,Controls,
Forms,Dialogs,iBTMEXPW,StdCtrls,ExtCtrls,ComCtrls,DBCtrls,DB,
DBTables,Grids,DBGrids;
type
TForm1=class(TForm)
Label1:
TLabel;
Label2:
TLabel;
Label3:
TLabel;
Label4:
TLabel;
Image1:
TImage;
Label5:
TLabel;
PageControl1:
TPageControl;
TabSheet1:
TTabSheet;
TabSheet2:
TTabSheet;
TabSheet3:
TTabSheet;
TabSheet4:
TTabSheet;
PageControl2:
TPageControl;
TabSheet5:
TTabSheet;
TabSheet6:
TTabSheet;
Label12:
TLabel;
Label13:
TLabel;
Label14:
TLabel;
Edit1:
TEdit;
Edit2:
TEdit;
Edit3:
TEdit;
Button1:
TButton;
Label7:
TLabel;
Label15:
TLabel;
Label8:
TLabel;
Label16:
TLabel;
Label9:
TLabel;
Label17:
TLabel;
Edit4:
TEdit;
Edit5:
TEdit;
Edit6:
TEdit;
Edit7:
TEdit;
Edit8:
TEdit;
Edit9:
TEdit;
Button2:
TButton;
Label10:
TLabel;
Label11:
TLabel;
ProgressBar1:
TProgressBar;
ProgressBar2:
TProgressBar;
Label18:
TLabel;
Label19:
TLabel;
Label20:
TLabel;
Label21:
TLabel;
GroupBox1:
TGroupBox;
RadioButton1:
TRadioButton;
RadioButton2:
TRadioButton;
GroupBox2:
TGroupBox;
RadioButton3:
TRadioButton;
RadioButton4:
TRadioButton;
DBGrid1:
TDBGrid;
Table1:
TTable;
DataSource1:
TDataSource;
Button3:
TButton;
PaintBox1:
TPaintBox;
GroupBox3:
TGroupBox;
RadioButton5:
TRadioButton;
RadioButton6:
TRadioButton;
Button4:
TButton;
Timer1:
TTimer;
Button5:
TButton;
procedureFormCreate(Sender:
TObject);
procedureFindFirstFamily(family:
smallint;SHandle:
Longint);
procedureReadTemperature1(session_handle:
Longint);
procedureFindSecondFamily(family:
smallint;SHandle:
longint);
procedureReadTemperature2(session_handle:
Longint);
procedureImage1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
procedureButton3Click(Sender:
TObject);
procedureButton1Click(Sender:
TObject);
procedureButton4Click(Sender:
TObject);
procedureTimer1Timer(Sender:
TObject);
procedureButton5Click(Sender:
TObject);
{Privatedeclarations}
public
{Publicdeclarations}
SHandle:
longint;
fenbianlv1,fenbianlv2:
smallint;
keke1,keke2:
string;
diwei1,gaowei1,xiaxian1,shangxian1,diwei2,gaowei2,shangxian2,xiaxian2,yuanshi1,yuanshi2:
extended;
StateBuf:
array[0..5120]ofsmallint;
CRC8:
Word;
Done,dieer:
boolean;
end;
Const
LEVEL_SET=1;
LEVEL_STRONG_PULL_UP=$01;
PRIMED_BYTE=2;
PRIMED_NONE=0;
LEVEL_NORMAL=$00;
var
Form1:
TForm1;
implementation
{$R*.DFM}
procedureTForm1.FormCreate(Sender:
TObject);
Var
ztbuf:
array[0..200]ofChar;
Typebuf:
array[0..200]ofChar;
i,k,RetValue:
smallint;
RetStr:
array[0..200]ofChar;
SetupDone:
Boolean;
PortNum,PortType:
smallint;
begin
dieer:
=FALSE;
SetupDone:
=FALSE;{TMSetupnotdoneyet}
Label4.Caption:
='';
{ReaddefaultPortNumberandPortTypefromregistry}
RetValue:
=TMReadDefaultPort(@PortNum,@PortType);
if(RetValue<1)then
begin
ShowMessage('Pleasesetportfirst');
Halt;
end
else
begin
{readthetmexversionandtypeversion}
Get_Version(@ztbuf);
Label1.Caption:
=StrPas(ztbuf);
TMGetTypeVersion(PortType,@Typebuf);
Label2.Caption:
=StrPas(Typebuf);
{attemptogetasession}
Done:
=False;
Repeat
SHandle:
=TMExtendedStartSession(PortNum,PortType,NIL);
If(SHandle>0)Then
begin
if(TMSetup(SHandle)=1)then
{ThedevicethatwillbefoundisTemperatureDeviceDS18B20,
soFamilyTypeissetto$28}
FindFirstFamily($28,SHandle)
else
begin
TMEndSession(SHandle);
ShowMessage('FailtosetupMicroLan!
');
Halt;
end;
end
else
begin
if(SHandle<0)then
Begin
ShowMessage('TheDefaultPortTypedoesnothaveadriver!
');
Halt;
end;
end;
{Releasecontrolbacktowindow}
Application.ProcessMessages;
until(Done);
Done:
=False;
Repeat
SHandle:
=TMExtendedStartSession(PortNum,PortType,NIL);
If(SHandle>0)Then
begin
if(TMSetup(SHandle)=1)then
{ThedevicethatwillbefoundisTemperatureDeviceDS18B20,
soFamilyTypeissetto$28}
FindSecondFamily($28,SHandle)
else
begin
TMEndSession(SHandle);
ShowMessage('FailtosetupMicroLan!
');
Halt;
end;
end
else
begin
if(SHandle<0)then
Begin
ShowMessage('TheDefaultPortTypedoesnothaveadriver!
');
Halt;
end;
end;
{Releasecontrolbacktowindow}
Application.ProcessMessages;
until(Done);
label18.Caption:
=keke1;
label19.caption:
=keke2;
table1.Active:
=true;
iftable1.CanModifythen
begin
table1.Append;
table1.Fields[0].AsDateTime:
=now;
table1.Fields[1].AsString:
=floattostr(diwei1);
table1.Fields[2].AsFloat:
=shangxian1;
table1.Fields[3].AsFloat:
=xiaxian1;
table1.Fields[4].AsString:
=floattostr(diwei2);
table1.Fields[5].AsFloat:
=shangxian2;
table1.Fields[6].AsFloat:
=xiaxian2;
table1.Post;
end;
end;
end;
procedureTForm1.FindFirstFamily(family:
smallint;SHandle:
longint);
var
i,flag:
smallint;
romstr:
string;
rom:
array[0..8]ofsmallint;
begin
{Setuptofindthefirstdevicewiththefamily'family'}
if(TMFamilySearchSetup(SHandle,@stateBuf,family)=1)then
begin
{Getfirstdevice}
if(TMNext(SHandle,@stateBuf)=1)then
begin
{Readtheromnumberbysettingrom[0]to0forreadingandusingTMRom}
rom[0]:
=0;
TMRom(SHandle,@stateBuf,@rom);
{Checkifcorrecttype}
romstr:
='';
if((familyand$7F)=(rom[0]and$7F))then
begin
fori:
=7downto0do
romstr:
=romstr+IntToHex(ROM[i],2);
Label3.caption:
='SerialROMID1:
'+romstr;
ReadTemperature1(SHandle);
end
else
begin
ShowMessage('ThereisnoTemperatureDeviceontheport');
halt;
end;
end
else
begin
TMEndSession(SHandle);
ShowMessage('ThereisnoTemperatureDeviceontheport');
halt;
end;
end
else
begin
TMEndSession(SHandle);
ShowMessage('ThereisnoTemperatureDeviceontheport');
halt;
end;
end;
procedureTForm1.ReadTemperature1(session_handle:
longint);
var
tsht,i,tmp1:
smallint;
cr,cpc,tmpf,tmp:
Extended;
rbuf:
array[0..9]ofsmallint;
st:
longint;
CRCByte,xiaxianbyte:
Byte;
begin
tmp:
=0.00;
{accessthedevice}
if(TMAccess(session_handle,@StateBuf)=1)then
begin
{SendtherecallE2command(bysetting$B8tooutbyteinTMTouchByte)
makesureScratchiscorrect}
TMTouchByte(session_handle,$B8);
{SendthestartTcommand}
if(TMAccess(session_handle,@StateBuf)=1)then
begin
{PreparethestrongpullupafternextTMTouchByte}
TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_STRONG_PULL_UP,PRIMED_BYTE);
{Sendtheconversioncommand(bysetting$44tooutbyteinTMTouchByte)}
TMTouchByte(session_handle,$44);
{Sleepforasecond}
st:
=GetTickCount+1000;
While(GetTickCountTMValidSession(Session_handle);
{Disablethestrongpullup}
TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_NORMAL,PRIMED_NONE);
{verifyconversioniscompletebysetting$01tooutbitinTMTouchBitand
checkthereturnvaluewith1}
if(TMTouchBit(session_handle,$01)=$01)then
begin
{Accessdevice}
If(TMAccess(session_handle,@StateBuf)=1)then
begin
{Sendreadscratchcommandbysetting$BEtooutbyteinTMTouchByte}
TMTouchByte(session_handle,$BE);
CRC8:
=0;
{Readscratch(setting$FFtooutbyteinTMTouchByte)andcheckcrcfor
eachbyte}
fori:
=0to7do
begin
rbuf[i]:
=TMTouchByte(session_handle,$FF);
CRCByte:
=Byte(rbuf[i]);{thebytetorunthroughCRC8routine}
CRC8:
=TMCRC(1,@CRCByte,CRC8,0);
end;
{Checkcrc}
CRCByte:
=Byte(TMTouchByte(session_handle,$FF));{thebytetorunthroughCRC8routine}
CRC8:
=TMCRC(1,@CRCByte,CRC8,0);
if(CRC8=0)then
begin
{Calculatethetemperarure
tsht:
=rbuf[0];
if((rbuf[1]and$01)=1)then
tsht:
=tshtor(-256);
tmp1:
=Round((tsht)/2);
tmp:
=tmp1;
cr:
=rbuf[6];
cpc:
=rbuf[7];
if(rbuf[7]<>0)then
tmp:
=tmp-(0.25)+(cpc-cr)/cpc;}
if((rbuf[1]and$F8)=$F8)then
BEGIN//温度为负值
END
ELSE
BEGIN//温度为正值
case(rbuf[4]and$60)of
$00:
begin
fenbianlv1:
=9;
tmp:
=(rbuf[1]and$07)*16+(rbuf[0]and$f8)/16;//9位分辨率
diwei1:
=(rbuf[0]and$f8)/16;
end;
$20:
begin
fenbianlv1:
=10;
tmp:
=(rbuf[1]and$07)*16+(rbuf[0]and$fc)/16;//10位分辨率
diwei1:
=(rbuf[0]and$fc)/16;
end;
$40:
begin
fenbianlv1:
=11;
tmp:
=(rbuf[1]and$07)*16+(rbuf[0]and$fe)/16;//11位分辨率
diwei1:
=(rbuf[0]and$fe)/16;
end;
$60:
begin
fenbianlv1:
=12;
tmp:
=(rbuf[1]and$07)*16+rbuf[0]/16;//12位分辨率
diwei1:
=rbuf[0]/16;
end;
end;
END;
tmpf:
=(tmp*9)/5+32;
label4.caption:
='Currenttemp1:
'+FormatFloat('0.0000',tmp)+
'Cor'+FormatFloat('0.0000',tmpf)+'F';
keke1:
=FormatFloat('0.0000',tmp);
yuanshi1:
=rbuf[4];
shangxian1:
=rbuf[2]and$7f;
xiaxianbyte:
=rbuf[3]and$80;
ifxiaxianbyte=1then
xiaxian1:
=(rbuf[3]and$7f)*(-1)
else
xiaxian1:
=rbuf[3]and$7f;
MessageBeep(0);
TMEndSession(session_handle);
Done:
=True;
end;
end;
end;
end;
end;
end;
procedureTForm1.FindSecondFamily(family:
smallint;SHandle:
longint);
var
i,flag:
smallint;
romstr:
string;
rom:
array[0..8]ofsmallint;
begin
{Setuptofindthefirstdevicewiththefamily'family'}
if(TMFamilySearchSetup(SHandle,@stateBuf,family)=1)then
begin
{Getfirstdevice}
if(TMNext(SHandle,@stateBuf)=1)then
if(TMNext(SHandle,@stateBuf)=1)then
begin
{Readtheromnumberbysettingrom[0]to0forreadingandusingTMRom}
rom[0]:
=0;
TMRom(SHandle,@stateBuf,@rom);
{Checkifcorrecttype}
romstr:
='';
if((familyand$7F)=(rom[0]and$7F))then
begin
fori:
=7downto0do
romstr:
=romstr+IntToHex(ROM[i],2);
Label10.caption:
='SerialROMID2:
'+romstr;
Read