基于单总线的温度传感器设计Word下载.docx
- 文档编号:8590199
- 上传时间:2023-05-11
- 格式:DOCX
- 页数:15
- 大小:151.24KB
基于单总线的温度传感器设计Word下载.docx
《基于单总线的温度传感器设计Word下载.docx》由会员分享,可在线阅读,更多相关《基于单总线的温度传感器设计Word下载.docx(15页珍藏版)》请在冰点文库上搜索。
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
if(TMSetup(SHandle)=1)then//初始化单总线,验证物理层完整性
{ThedevicethatwillbefoundisTemperatureDeviceDS18B20,
soFamilyTypeissetto$28}
select:
=true;
//在其他TMEX函数之前调用(除session层)
FindFamily($28,SHandle);
=false;
TMEndSession(SHandle);
FailtosetupMicroLan!
'
end;
if(SHandle<
0)then
Begin
TheDefaultPortTypedoesnothaveadriver!
Application.ProcessMessages;
//Releasecontrolbacktowindow
until(Done);
end;
procedureTForm1.FindFamily(family:
SHandle:
longint);
//找传感器
var
i,flag:
romstr:
string;
rom:
array[0..8]ofsmallint;
flag:
=TMFirst(SHandle,@stateBuf);
ifselect=falsethen
=TMNext(SHandle,@stateBuf);
if(flag=1)then
{Readtheromnumberbysettingrom[0]=0forreadingandusingTMRom}
rom[0]:
=0;
TMRom(SHandle,@stateBuf,@rom);
='
;
//Checkifcorrecttype
if((familyand$7F)=(rom[0]and$7F))then
fori:
=7downto0do
=romstr+IntToHex(ROM[i],2);
ifselectthen
Label3.caption:
SerialROM2ID:
'
+romstr
Label21.caption:
+romstr;
ReadTemperature(SHandle);
ThereisnoTemperatureDeviceontheport'
halt;
procedureTForm1.ReadTemperature(session_handle:
//读温度
i:
rbuf:
array[0..8]ofsmallint;
st:
longint;
CRCByte:
Byte;
tmp:
=0.00;
if(TMAccess(session_handle,@StateBuf)=1)then//accessthedevice
{SendtherecallE2command(bysetting$B8tooutbyteinTMTouchByte)
makesureScratchiscorrect}
//TMTouchByte(session_handle,$B8);
//读ROM数据到RAM命令
if(TMAccess(session_handle,@StateBuf)=1)then//SendthestartTcommand
//PreparethestrongpullupafternextTMTouchByte
TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_STRONG_PULL_UP,PRIMED_BYTE);
{Sendtheconversioncommand(bysetting$44tooutbyteinTMTouchByte)}
TMTouchByte(session_handle,$44);
//发出转换命令
=GetTickCount+1000;
//Sleepforasecond
While(GetTickCount<
st)do
TMValidSession(Session_handle);
//Disablethestrongpullup
TMOneWireLevel(session_handle,LEVEL_SET,LEVEL_NORMAL,PRIMED_NONE);
{verifyconversioniscompletebysetting$01tooutbitinTMTouchBitand
checkthereturnvaluewith1}
if(TMTouchBit(session_handle,$01)=$01)then
If(TMAccess(session_handle,@StateBuf)=1)then//Accessdevice
{Sendreadscratchcommandbysetting$BEtooutbyteinTMTouchByte}
TMTouchByte(session_handle,$BE);
//发出读暂存器RAM命令
CRC8:
{Readscratch(setting$FFtooutbyteinTMTouchByte)andcheckcrcfor
eachbyte}
=0to7do
rbuf[i]:
=TMTouchByte(session_handle,$FF);
=Byte(rbuf[i]);
//thebytetorunthroughCRC8routine
=TMCRC(1,@CRCByte,CRC8,0);
=Byte(TMTouchByte(session_handle,$FF));
//Checkcrc
if(CRC8=0)then
if((rbuf[1]and$F8)=$F8)then
BEGIN//温度为负值
rbuf[1]:
=notrbuf[1];
if(rbuf[0]=0)then
=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:
=(rbuf[1]and$07)*16+(rbuf[0]and$fc)/16;
//10位分辨率
$40:
=(rbuf[1]and$07)*16+(rbuf[0]and$fe)/16;
//11位分辨率
$60:
=(rbuf[1]and$07)*16+rbuf[0]/16;
//12位分辨率
tmp:
=tmp*(-1);
END
ELSE
BEGIN//温度为正值
//12位分辨率
END;
tmpf:
=(tmp*9)/5+32;
label4.caption:
Currenttemp:
+FormatFloat('
0.0000'
tmp)+'
C'
+FormatFloat('
tmpf)+'
F'
fori:
=0to4do
RAM1[i]:
=rbuf[i];
iThermometer1.Position:
=tmp;
label17.Caption:
=FormatFloat('
tmp)+'
℃'
;
label22.caption:
tmp)+'
RAM2[i]:
iThermometer2.Position:
label18.Caption:
end;
MessageBeep(0);
//TMEndSession(session_handle);
=True;
procedureTForm1.SetRom(session_handle:
//定义配置EEPROM过程
i,flag:
rbuf:
array[0..2]ofsmallint;
flag:
=0;
ifselectthen
=0to2do
=EEPROM1[i]
else
=EEPROM2[i];
if(rbuf[0]and$80=$80)then
=rbuf[0]and$00ff;
if(rbuf[1]and$80=$80)then
=rbuf[1]and$00ff;
ifselect=falsethen
if(flag=1)then
If(TMAccess(session_handle,@StateBuf)=1)then//Accessdevice
{Sendwritescratchcommandbysetting$4EtooutbyteinTMTouchByte}
TMTouchByte(session_handle,$4E);
{Readscratch(setting$FFtooutbyteinTMTouchByte)}
=0to2do//先写到暂存器中
TMTouchByte(session_handle,rbuf[i]);
//rbuf[i]:
If(TMAccess(session_handle,@StateBuf)=1)then
TMTouchByte(session_handle,$48);
{st:
}
procedureTForm1.Button6Click(Sender:
//设置EEPROM
H,L:
smallint;
i:
Button4.Enabled:
Button2.Enabled:
caseCombobox1.Itemindexof//分辨率
0:
rbuf[2]:
=$1F;
1:
=$3F;
2:
=$5F;
3:
=$7F;
=smallint(strtoint(Edit9.Text));
=smallint(strtoint(Edit10.Text));
ifRadioButton1.Checkedthen
=true;
EEPROM1[i]:
EEPROM2[i]:
SetRom(SHandle);
SaveTem();
procedureTForm1.display();
H,L,i:
array[0..4]ofsmallint;
ifGroupbox1.caption='
传感器1'
then
=RAM1[i]
=RAM2[i];
Edit5.Text:
=inttohex(rbuf[0],2)+'
H'
//温度及上下限原始数据显示
Edit4.Text:
=inttohex(rbuf[1],2)+'
Edit6.Text:
=inttohex(rbuf[2],2)+'
Edit7.Text:
=inttohex(rbuf[3],2)+'
Edit8.Text:
=inttohex(rbuf[4],2)+'
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//分辨率显示
edit3.text:
='
9位'
10位'
11位'
12位'
\
procedureTForm1.SaveTem();
//保存温度日志
withADOQuery1do
Close;
SQL.Clear;
SQL.Add('
insertintotem(时间,传感器,温度,温度上限,温度下限)values(now(),:
传感器,:
温度,:
温度上限,:
温度下限)'
Parameters.FindParam('
传感器'
).Value:
1'
温度'
).Value:
tmp);
温度上限'
=EEPROM1[0];
温度下限'
=EEPROM1[1];
2'
=EEPROM2[0];
=EEPROM2[1];
ExecSQL;
procedureTForm1.Button2Click(Sender:
//绘温度曲线
inherited;
iPlot1.XAxis[0].Span:
=100;
iPlot1.YAxis[0].Span:
=30;
iPlot1.XAxis[0].Title:
时间'
iPlot1.YAxis[0].Title:
Button3.Enabled:
iPlot2.XAxis[0].Span:
iPlot2.YAxis[0].Span:
iPlot2.XAxis[0].Title:
iPlot2.YAxis[0].Title:
Timer2.enabled:
Button6.Enabled:
procedureTForm1.Timer2Timer(Sender:
inherited;
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]);
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)