第30卷第6期2007年12月电子器件ChineseJournalOfElectronDevicesVol.30No.6Dec.2007ErrorAnalyzingofReadoutDataof1wireBusDigitalTemperatureSensorDS18B20*LEIJianlong(DepartmentofElectronicsofWuhanInstituteofShipbuildingTechnology,Wuhan430050,China)Abstract:Forthepurposeofaccuratelydetectingtheenvironmenttemperatureinultrasonicoillevelmeter,the1wirebusdigitaltemperaturesensorDS18B20wasappliedinthesystem.AimedattheproblemofreadingerrorwhenMCUreadDS18B20,byanalyzingthetimingofDS18B20whenreading,thedetectingprogram,especiallythereadingoutprogramisanalyzed,bywhichthecausesofreadingouterrorisdiscovered.IntheIDEofKeilC,theanalysisisapprovedbymanytimesexperiment,andalsothemethodofhowtosolvethisproblemisputforward.ThepracticeapplicationindicatesthattheerrorhasnevertakenplaceafteramendingtheprogramofreadingDS18B20.Keywords:digitaltemperaturesensor;analysisofreadingouterror;MCU;DS18B20;1wirbus;temperaturedetecting.EEACC:7320;7230RDS18B20*雷建龙(,430050):20070110:(2004X170):(1965),,,,ljl62823073@sina.com.:,DS18B20.DS18B20,DS18B20,,DS18B20,,KeilC,,.,DS18B20,.:;;;DS18B20;;:TP212.11;TP311.1:A:10059490(2007)06218303[1],.,[13]:v=331.45+0.607t(m/s),v,t.,,,.1DS18B20AD590,A/D,,DS18B20[4].DS18B20DALLAS.,,,.,,,.DS18B20:-55~125,0.0625;!,9~12;∀750ms(max);#.DS18B2016(2byte),12(),5S.0,S0,1.11,26~2-4,0.0625/LSB.0.0625.,DS18B20,1.图1硬件连接,DS18B20[58].89C20516MHz,:12∃1/6MHz=2s.2,DS18B202.图218B20温度测量子程,:#includereg51.H#includeINTRINS.h#includemath.hsbitDQ=P1^5;bitresetpulse(void)//{%//}voidwritecommandtods18b20(unsignedcharcommand){//%.//}unsignedcharreaddatafromds18b20(void)//{unsignedchari;unsignedcharj;unsignedchartemp;temp=0;for(i=0;i8;i++){temp=_cror_(temp,1);DQ=0;_nop_();DQ=1;//for(j=2;j0;j--);//,5!if(DQ==1){//&1∋temp=temp|0x80;}else{//&0∋temp=temp|0x00;}for(j=100;j0;j--);}return(temp);//}voidmain(void)//{externvoidd1ms();unsignedchargettemperature();unsignedchartemper[5],i,te;for(i=0;i5;i++)//5{while(resetpulse());//writecommandtods18b20(0xcc);//ROMwritecommandtods18b20(0x44);//d1ms();//temper[i]=gettemperature();//}for(i=0;i4;i++)//5,{if(temper[i]temper[i+1]){te=temper[i];temper[i]=temper[i+1];temper[i+1]=te;}}v=33145+sflag*(61*(unsignedint)temper[2])/2;//2184电子器件第30卷,cm/swhile(1);}unsignedchargettemperature()//{unsignedcharreaddata[2];unsignedintresult;while(resetpulse());//writecommandtods18b20(0xcc);//ROMwritecommandtods18b20(0xbe);//DS18B20readdata[0]=readdatafromds18b20();//readdata[1]=readdatafromds18b20();//%.//return(result);}3,1.DS18B20(3):()1s,,DS18B2015s,0,DS18B2015s1,15s,15s,15s,,60s1.,C,,&for(j=2;j0;j--);∋:;----Variable'j'assignedtoRegister'R5'----00127D02MOVR5,#02H;0014?C0029:图3读器件时序0014DDFEDJNZR5,?C0029;C:(1+2j)∃T:T(2s),,KeilC,readdata[2],j210s15s,;j5,DS18B20&∋1,22s.DS18B20,.,DS18B20,CRC,,DS18B20,,,5().4DS18B20,,.,.3j1.1j3j01234567/s261014182226303000001303030:18B20.:[1].[J]..,2006,19(4):11171120.[2].AT89C2051[J].,2003,136(8):1011.[3],.LM567[J].,2000,12(1):3031.[4]DALLASSemiconductor.DS18B20ProgrammableResolution1WireRDigitalThermometer[DB/OL].[5].DS18B20[J].,2002,4,19(2):102105.[6].DS18B20[J].,2004(4):2729.[7].[J].()2005,21(6):57.[8],.C8051FSMBus[J].,2005,(25):5052.2185第6期雷建龙:数字温度传感器DS18B20读出数据错误分析