基于单总线的温度传感器设计.docx
- 文档编号:7707696
- 上传时间:2023-05-11
- 格式:DOCX
- 页数:15
- 大小:151.24KB
基于单总线的温度传感器设计.docx
《基于单总线的温度传感器设计.docx》由会员分享,可在线阅读,更多相关《基于单总线的温度传感器设计.docx(15页珍藏版)》请在冰点文库上搜索。
基于单总线的温度传感器设计
procedureTForm1.FormCreate(Sender:
TObject);
Var
ztbuf:
array[0..200]ofChar;
Typebuf:
array[0..200]ofChar;
//RetStr:
array[0..200]ofChar;
SetupDone:
Boolean;
RetValue:
smallint;
PortNum,PortType:
smallint;
begin
//SetupDone:
=FALSE;//TMSetupnotdoneyet
//ReaddefaultPortNumberandPortTypefromregistry
RetValue:
=TMReadDefaultPort(@PortNum,@PortType);
PortNum:
=1;PortType:
=5;//TMReadDefaultPort出错
if(RetValue<1)then
begin
ShowMessage('Pleasesetportfirst');
Halt;
end
else
begin
Get_Version(@ztbuf);//readthetmexversionandtypeversion
Label1.Caption:
=StrPas(ztbuf);
TMGetTypeVersion(PortType,@Typebuf);
Label2.Caption:
=StrPas(Typebuf);
Done:
=False;//attempttogetasession
Repeat
SHandle:
=TMExtendedStartSession(PortNum,PortType,NIL);//0忙
If(SHandle>0)Then
begin
if(TMSetup(SHandle)=1)then//初始化单总线,验证物理层完整性
begin
{ThedevicethatwillbefoundisTemperatureDeviceDS18B20,
soFamilyTypeissetto$28}
select:
=true;//在其他TMEX函数之前调用(除session层)
FindFamily($28,SHandle);
select:
=false;
FindFamily($28,SHandle);
end
else
begin
TMEndSession(SHandle);
ShowMessage('FailtosetupMicroLan!
');
Halt;
end;
end
else
begin
if(SHandle<0)then
Begin
ShowMessage('TheDefaultPortTypedoesnothaveadriver!
');
Halt;
end;
end;
Application.ProcessMessages;//Releasecontrolbacktowindow
until(Done);
end;
end;
procedureTForm1.FindFamily(family:
smallint;SHandle:
longint);//找传感器
var
i,flag:
smallint;
romstr:
string;
rom:
array[0..8]ofsmallint;
begin
flag:
=TMFirst(SHandle,@stateBuf);
ifselect=falsethen
flag:
=TMNext(SHandle,@stateBuf);
if(flag=1)then
begin
{Readtheromnumberbysettingrom[0]=0forreadingandusingTMRom}
rom[0]:
=0;
TMRom(SHandle,@stateBuf,@rom);
romstr:
='';//Checkifcorrecttype
if((familyand$7F)=(rom[0]and$7F))then
begin
fori:
=7downto0do
romstr:
=romstr+IntToHex(ROM[i],2);
ifselectthen
Label3.caption:
='SerialROM2ID:
'+romstr
else
Label21.caption:
='SerialROM2ID:
'+romstr;
ReadTemperature(SHandle);
end
else
begin
ShowMessage('ThereisnoTemperatureDeviceontheport');
halt;
end;
end
else
begin
TMEndSession(SHandle);
ShowMessage('ThereisnoTemperatureDeviceontheport');
halt;
end;
end;
procedureTForm1.ReadTemperature(session_handle:
longint);//读温度
var
i:
smallint;
rbuf:
array[0..8]ofsmallint;
st:
longint;
CRCByte:
Byte;
begin
tmp:
=0.00;
if(TMAccess(session_handle,@StateBuf)=1)then//accessthedevice
begin
{SendtherecallE2command(bysetting$B8tooutbyteinTMTouchByte)
makesureScratchiscorrect}
//TMTouchByte(session_handle,$B8);//读ROM数据到RAM命令
if(TMAccess(session_handle,@StateBuf)=1)then//SendthestartTcommand
begin
//PreparethestrongpullupafternextTMTouchByte
TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_STRONG_PULL_UP,PRIMED_BYTE);
{Sendtheconversioncommand(bysetting$44tooutbyteinTMTouchByte)}
TMTouchByte(session_handle,$44);//发出转换命令
st:
=GetTickCount+1000;//Sleepforasecond
While(GetTickCount TMValidSession(Session_handle); //Disablethestrongpullup TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_NORMAL,PRIMED_NONE); {verifyconversioniscompletebysetting$01tooutbitinTMTouchBitand checkthereturnvaluewith1} if(TMTouchBit(session_handle,$01)=$01)then begin If(TMAccess(session_handle,@StateBuf)=1)then//Accessdevice begin {Sendreadscratchcommandbysetting$BEtooutbyteinTMTouchByte} TMTouchByte(session_handle,$BE);//发出读暂存器RAM命令 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; CRCByte: =Byte(TMTouchByte(session_handle,$FF));//Checkcrc CRC8: =TMCRC(1,@CRCByte,CRC8,0); if(CRC8=0)then begin if((rbuf[1]and$F8)=$F8)then BEGIN//温度为负值 rbuf[1]: =notrbuf[1]; if(rbuf[0]=0)then rbuf[1]: =rbuf[1]+1; rbuf[0]: =(notrbuf[0])+1; case(rbuf[4]and$60)of $00: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$f8)/16;//9位分辨率 $20: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$fc)/16;//10位分辨率 $40: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$fe)/16;//11位分辨率 $60: tmp: =(rbuf[1]and$07)*16+rbuf[0]/16;//12位分辨率 end; tmp: =tmp*(-1); END ELSE BEGIN//温度为正值 case(rbuf[4]and$60)of $00: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$f8)/16;//9位分辨率 $20: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$fc)/16;//10位分辨率 $40: tmp: =(rbuf[1]and$07)*16+(rbuf[0]and$fe)/16;//11位分辨率 $60: tmp: =(rbuf[1]and$07)*16+rbuf[0]/16;//12位分辨率 end; END; tmpf: =(tmp*9)/5+32; ifselectthen begin label4.caption: ='Currenttemp: '+FormatFloat('0.0000',tmp)+'C'+FormatFloat('0.0000',tmpf)+'F'; fori: =0to4do RAM1[i]: =rbuf[i]; iThermometer1.Position: =tmp; label17.Caption: =FormatFloat('0.0000',tmp)+'℃'; end else begin label22.caption: ='Currenttemp: '+FormatFloat('0.0000',tmp)+'C'+FormatFloat('0.0000',tmpf)+'F'; fori: =0to4do RAM2[i]: =rbuf[i]; iThermometer2.Position: =tmp; label18.Caption: =FormatFloat('0.0000',tmp)+'℃'; end; MessageBeep(0);//TMEndSession(session_handle); Done: =True; end; end; end; end; end; end; procedureTForm1.SetRom(session_handle: longint);//定义配置EEPROM过程 var i,flag: smallint; st: longint; rbuf: array[0..2]ofsmallint; begin flag: =0; ifselectthen fori: =0to2do rbuf[i]: =EEPROM1[i] else fori: =0to2do rbuf[i]: =EEPROM2[i]; if(rbuf[0]and$80=$80)then rbuf[0]: =rbuf[0]and$00ff; if(rbuf[1]and$80=$80)then rbuf[1]: =rbuf[1]and$00ff; flag: =TMFirst(SHandle,@stateBuf); ifselect=falsethen flag: =TMNext(SHandle,@stateBuf); if(flag=1)then begin If(TMAccess(session_handle,@StateBuf)=1)then//Accessdevice begin {Sendwritescratchcommandbysetting$4EtooutbyteinTMTouchByte} TMTouchByte(session_handle,$4E); {Readscratch(setting$FFtooutbyteinTMTouchByte)} fori: =0to2do//先写到暂存器中 begin TMTouchByte(session_handle,rbuf[i]);//rbuf[i]: =TMTouchByte(session_handle,$FF); end; end; If(TMAccess(session_handle,@StateBuf)=1)then begin TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_STRONG_PULL_UP,PRIMED_BYTE); TMTouchByte(session_handle,$48); {st: =GetTickCount+1000; While(GetTickCount TMValidSession(Session_handle);} TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_NORMAL,PRIMED_NONE); end; end; end; procedureTForm1.Button6Click(Sender: TObject);//设置EEPROM var H,L: smallint; i: smallint; rbuf: array[0..2]ofsmallint; begin Button4.Enabled: =false; Button2.Enabled: =false; caseCombobox1.Itemindexof//分辨率 0: rbuf[2]: =$1F; 1: rbuf[2]: =$3F; 2: rbuf[2]: =$5F; 3: rbuf[2]: =$7F; end; rbuf[0]: =smallint(strtoint(Edit9.Text)); rbuf[1]: =smallint(strtoint(Edit10.Text)); ifRadioButton1.Checkedthen begin select: =true; fori: =0to2do EEPROM1[i]: =rbuf[i]; end else begin select: =false; fori: =0to2do EEPROM2[i]: =rbuf[i]; end; SetRom(SHandle); FindFamily($28,SHandle); SaveTem(); Button4.Enabled: =true; Button2.Enabled: =true; end; procedureTForm1.display(); var H,L,i: smallint; rbuf: array[0..4]ofsmallint; begin ifGroupbox1.caption='传感器1'then fori: =0to4do rbuf[i]: =RAM1[i] else fori: =0to4do rbuf[i]: =RAM2[i]; Edit5.Text: =inttohex(rbuf[0],2)+'H';//温度及上下限原始数据显示 Edit4.Text: =inttohex(rbuf[1],2)+'H'; Edit6.Text: =inttohex(rbuf[2],2)+'H'; Edit7.Text: =inttohex(rbuf[3],2)+'H'; Edit8.Text: =inttohex(rbuf[4],2)+'H'; if(rbuf[2]and$80=$80)then rbuf[2]: =rbuf[2]or$ff00; if(rbuf[3]and$80=$80)then rbuf[3]: =rbuf[3]or$ff00; Edit1.Text: =intToStr(rbuf[2]); Edit2.Text: =IntToStr(rbuf[3]); case(rbuf[4]and$60)of//分辨率显示 $00: edit3.text: ='9位'; $20: edit3.text: ='10位'; $40: edit3.text: ='11位'; $60: edit3.text: ='12位'; end; end; \ procedureTForm1.SaveTem();//保存温度日志 begin withADOQuery1do begin Close; SQL.Clear; SQL.Add('insertintotem(时间,传感器,温度,温度上限,温度下限)values(now(),: 传感器,: 温度,: 温度上限,: 温度下限)'); ifselectthen begin Parameters.FindParam('传感器').Value: ='1'; Parameters.FindParam('温度').Value: =FormatFloat('0.0000',tmp); Parameters.FindParam('温度上限').Value: =EEPROM1[0]; Parameters.FindParam('温度下限').Value: =EEPROM1[1]; end else begin Parameters.FindParam('传感器').Value: ='2'; Parameters.FindParam('温度').Value: =FormatFloat('0.0000',tmp); Parameters.FindParam('温度上限').Value: =EEPROM2[0]; Parameters.FindParam('温度下限').Value: =EEPROM2[1]; end; ExecSQL; end; end; procedureTForm1.Button2Click(Sender: TObject);//绘温度曲线 begin inherited; iPlot1.XAxis[0].Span: =100; iPlot1.YAxis[0].Span: =30; iPlot1.XAxis[0].Title: ='时间'; iPlot1.YAxis[0].Title: ='温度'; Button3.Enabled: =true; iPlot2.XAxis[0].Span: =100; iPlot2.YAxis[0].Span: =30; iPlot2.XAxis[0].Title: ='时间'; iPlot2.YAxis[0].Title: ='温度'; Timer2.enabled: =true; Button2.Enabled: =false; Button4.Enabled: =false; Button6.Enabled: =false; end; procedureTForm1.Timer2Timer(Sender: TObject); begin FindFamily($28,SHandle); inherited; ifselectthen begin iPlot1.Channel[0].AddYElapsedSeconds(tmp); if(RAM1[3]and$80=$80)then RAM1[3]: =RAM1[3]or$ff00; if(RAM1[2]and$80=$80)then RAM1[2]: =RAM1[2]or$ff00; Iplot1.Channel[1].AddYElapsedSeconds(RAM1[2]); Iplot1.Channel[2].AddYElapsedSeconds(RAM1[3]); end else begin iPlot2.Channel[0].AddYElapsedSeconds(tmp); if(RAM2[3]and$80=$80)then RAM2[3]: =RAM2[3]or$ff00; if(RAM2[2]and$80=$80)then RAM2[2]:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 总线 温度传感器 设计
![提示](https://static.bingdoc.com/images/bang_tan.gif)