




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
目录HYPERLINKchapter1Temperatureandhumiditymeasuringprinciple21.1Theschemeselectionoftemperatureandhumiditysensor21.2TheschemeselectionofTemperatureandhumiditydisplay2chapter2Thehardwaredesignofthesystem32.1Theoveralldesignofthesystemhardware32.2Themainschemeselectionanddesignofthecontrolmodule4singlechipSTC89C52introduction6Themaincontrolmodulecircuit72.3TheselectionanddesignOFTheRTCmodulescheme82.4Thedesignofinterfacemodules8chapter3Thedesignofsystemsoftware93.1Systemofthemainprogramandflowchart93.2ThereadoperationflowchartOFDS1302clockchip103.3writeoperationflowchartTheofLCDmodule11chapter4Theexperimentanddebugging124.1Difficultiesandsolutions14chapter5Teamdivisionoflaborandcomments15appendixA:electricschematicdiagram18appendixB:ProgramList19appendixC:externalview32第1章温湿度测量原理单片机的接口信号是数字信号。要想用单片机获取温湿度这类非电信号的信息,必须将其转换为数字信号,因为如果电流或电压输出是模拟信号,还必须进行A/D转换,以满足单片机接口的需要。如果是数字信号就可以直接送往单片机进行数据处理。1.1温湿度传感器的方案选择DS18B20+HS1100因为HS1100在网上的资料相当匮乏,以及老师在课程开始前说HS1100输出为模拟信号,需要加A/D转换。以及,分别采用温度湿度传感器的话,占用电路板空间会更大。综合考虑以上因素,我们认为温湿度传感器SHT11/10更好。SHT11/10因为在中发电子市场,一下子找不到SHT11插针式的传感器,所以我们选用了精度稍低的SHT10。1.2温湿度显示的方案选择方案一:时钟的显示可以用多位七段LED数码管显示,七段LED数码管显示耗能多,而且显示位数有限,每增加一位都要在程序设计和硬件设计方面增加很多的工作量,不利于电路的扩展,而且无法显示年、月、日、星期这些汉字,使得显示不够直观,灵活。但是这种设计方案在显示位数比拟少时性价比比拟高,价格廉价,方案二:采用点阵式数码管显示。点阵式数码管是由八行八列的发光二极管组成,对于显示文字比拟适合,而在本课题设计中的显示数字较多字符较少,而且此种方案耗能多,不符合现代的节能理念,所以不用此种作为显示。方案三:采用LCD液晶显示器显示。而LCD液晶显示那么耗能少,能够显示年、月、日、星期等汉字,在显示方面更加灵活,而且改变显示时只要改变软件设计就可以,不用改变硬件电路的设计,易于电路的功能扩展。电路的软件设计也很简单。另外,这种设计硬件更加简洁。采用LCD液晶显示方案的缺点是在显示位数比拟少时,价格也并不贵。比拟上述两种方案可以看出方案三耗能少,显示灵活,易于电路扩展而且不管是软件设计还是硬件设计都比拟简单,因此采用第三种设计方案。综上所述,本设计采用SHT10做温湿度传感器,引入DS1302时间模块的设计方案,显示使用LCD液晶显示。其中LCD1602性价比高,自带字模,选用LCD1602。第2章系统硬件设计根据温湿度传感器的功能要求,基于单片机的设计方案要运用集成SHT10,实现实时数据记录与读取:〔1〕根本要求具有读取温湿度并显示的功能〔2〕创新要求具高温报警功能具有日历功能2.1系统硬件的整体设计主控芯片使用51系列STC89C52单片机,系统由主控制器STC89C52、时钟芯片DS1302、LCD1602液晶显示电路LED灯高温报警电路组成。系统硬件设计框图:系统硬件设计框图2.2ThemainschemeselectionanddesignofthecontrolmoduleItwilldefinitelydecreasecomplexityofhardcircuitsifmainfunctionsarerealizedbysoftware.Thuswemadeoursystemamicrocomputer-basedone.Toimplementaskedfunctions,wejustneedasimplefeaturedmicrocomputer.IchosetheSTC89C52toaccomplishthemissioninthelightofitshighcostperformance.2.2.1DescriptionofSTC89C52TheSTC89C52isalow-power,high-performanceCMOS8-bitmicrocomputerwith8KbytesofFlashprogrammableanderasablereadonlymemory(PEROM).ThedeviceismanufacturedusingAtmel’shigh-densitynonvolatilememorytechnologyandiscompatiblewiththeindustry-standard80C51and80C52instructionsetandpinout.Theon-chipFlashallowstheprogrammemorytobereprogrammedin-systemorbyaconventionalnonvolatilememoryprogrammer.Bycombiningaversatile8-bitCPUwithFlashonamonolithicchip,theSTC89C52isapowerfulmicrocomputerwhichprovidesahighly-flexibleandcost-effectivesolutiontomanyembeddedcontrolapplications.PinConfigurationsPDIPSTC89C52PDIPTheAT89C52providesthefollowingstandardfeatures:8KbytesofFlash,256bytesofRAM,32I/Olines,three16-bittimer/counters,asix-vectortwo-levelinterruptarchitecture,afull-duplexserialport,on-chiposcillator,andclockcircuitry.Inaddition,theAT89C52isdesignedwithstaticlogicforoperationdowntozerofrequencyandsupportstwosoftwareselectablepowersavingmodes.TheIdleModestopstheCPUwhileallowingtheRAM,timer/counters,serialport,andinterruptsystemtocontinuefunctioning.ThePower-downmodesavestheRAMcontentsbutfreezestheoscillator,disablingallotherchipfunctionsuntilthenexthardwarereset.ThemaincontrolmodulecircuitTakingthePOportofsinglechipSTC89C52astheinpurport.ConnectingtheP1,theclockofSHT10anddata,takingthetemperatureandhumiditydataacquisitionandconnectingtheDS1302acquisitiontimeinformation;theP0ConnectstheD0~D7pinofLCD1602,TheXTAL1ofsinglechipconnectstheexternalcrystalandOneendofthetrimmercapacitors.Althoughthereisnostrictrequirementsofthevalueofanexternalcapacitor,ThesizeofthecapacitorwillaffectTheoscillatorfrequency、thestabilityofoscillators、therapidityofthevibrationandthestabilityofthetemperature。Therefore,Inthissystemcircuit,Thevalueofcrystaloscillatoris11.0592MHz.Capacitanceshouldasfaraspossiblethechoiceoftheceramiccapacitor.Capacitanceisabout22μF.The9pinisresetinput.Connectingthecapacity,resisterastheresetcircuit.20pinisthegroundterminal,40pinispowerterminal./EApinconnectsthe+5Vvoltage.Fromthis,Theminimumsystemofsingle-chipmicrocomputercontrolmoduleismakedup,asshowninfigure:2.3TheselectionanddesignOFTheRTCmoduleschemeTheDS1302TrickleChargeTimekeepingChipcontainsarealtimeclock/calendarand31bytesofstaticRAM.Itcommunicateswithamicroprocessorviaasimpleserialinterface.Therealtimeclock/calendarprovidesseconds,minutes,hours,day,date,month,andyearinformation.Theendofthemonthdateisautomaticallyadjustedformonthswithlessthan31days,includingcorrectionsforleapyear.Theclockoperatesineitherthe24–houror12–hourformatwithanAM/PMindicator.ComparedwithDS12887,whichdoesn’tneedanyextrabatterybutkindofexpensiveapproaching30RMB,DS1302canbeworkperfectwitha3.6Vbatteryandthesesumuponly5RMB.Accordingtothehighcostperformance,wechooseDS1302tofinishourRTCmodule.ds1302programflowchart2.4ThedesignofinterfacemodulesThedesignschemeusestheLCDliquidcrystaldisplay,becausethedesigndonotneedalotofcontents.WechosethehighperformancecostratioLCD1602。liquidcrystaldisplayLCD1602introductionomitchapter3Thedesignofsystemsoftware3.1SystemofthemainprogramandflowchartThemainfunctionsoftheprogramaretodisplaytemperatureandhumidityinformation.Inthemainprogram,afterthesystemwillautomaticallyreset,theelectricsystemofliquidcrystaldisplayandtheclockchipDS1302initializedandreadandwriteintheinformationsuchastemperatureandhumidity,time,toreadandwritedataafterthedisplayinformation.Themainprogramflowisshowninfigure:Theintroductionofmainprogram,whenrunthemainprogram,toresetthesensorsfirst,thenputtheLCD1602initialization,andthenitwilldisplaytemperatureandhumidityandtime.3.2ThereadoperationflowchartOFDS1302clockchipFirst,InitializingoftheclockchipDS1302,afterjudgingthestatusregister,toreadintheoperationofDS1302,theoperationusingthecharacteristicswhichtheclockcalendaraddresstheadjacent,thenjudgewhetherthedataisfinishedornot.Iffinished,returntothemainprogram;ifnot,continuetoincreasetheaddress,datauntilthereadingisfinished.Asshowninfigure:3.3writeoperationflowchartTheofLCDmoduleTheLCDmodulethiscircuitusesis1602LCDmodule,itcantransposeASCIIanditiseasytooperate.Intheflowchartofdisplay,thereisnocursoronthedisplay.Thedisplayofdateisfromthesecondlineofthefirstcharacter.chapter4TheexperimentanddebuggingThebestwaytoverifythetheoryispractice,sothisdesignusesexperiment,debuggingrealobjecttoverifythefeasibilityoftheabovedesign.Recentdays,althoughalotofdesignsusethesimulationtosimulateresults,therearestillsomegapswiththereality.Andsomepracticalproblemsarenotshowedbythesimulation,sowemetquiteafewproblemswhileconnectingtherealcircuit.Difficultiesandsolutions:1.thepartofsimulationBecausetheversionofprotelistooold,manycomponentsarenotinthecomponentlibrary.Anditisverytroublesometoaddsomethingtothecomponentlibrary.Therefore,wechooseproteustodosimulation.Ofcourse,wemetsomeproblemsintheprocessofsimulation.Forexample,themicrocontrollerweuseisSTC89C52,butthereisonly89C52there.Foranotherexample,weuseLCD1602todisplay,whichisalsonotinthecomponentlibrary.Throughonlineaccess,weknowthatATandSTCmicrocontrollersonlydifferinmanufacturersandburningmethods.AndLCD1602isreplacedbyanothernumberLM016Linthecomponentlibrary.What’smore,LCD1602missestwointerfacesofadjustingLCDbacklightbrightnessinthesimulationdiagram.Throughthechoiceandconnectionofcircuitelement,wehavecompletedthesimulation.Thefollowingcomponentsareusedinthesimulationdiagram:Singlechip——AT89C52,Sensor——SHT10,CLK——DS1302,Resistance——1KΩand10KΩ,ResistersPacks——RESPACK-8,CrystalOscillator——CRYSTAL(32.768Khz),Powersource、Earthingterminal,Sliderheostat、LCDdisplay——LM016L,NPNTransistor——NPN,LEDlight——LED-RED.CommunicationproblemsbetweenLCDdisplayandsinglechipemergedfromthefirsttryofbreadplate.ThisdesignpassedtheProteussimulationsoftwarewithoutanyproblems,thehardwarevalidationcircuitiscompletedbythestudentsresponsiblefortheoryandlapweldingstudentsintheformofmutualsupervision,toensurethatthecircuitisconnectedcompletelyaccordingtothetheory.With5Vvoltagesourceactingaspowersupply,LCDscreenlightsupnormally,displayingarowofsmallblackgrids,thenstopatthisstate.Whenwefirstlyencounteredthiskindofsituation,wefeltitdifficulttounderstand,becauseinthebeginningwegreatlyunderestimatedthedifferencesbetweenthesimulationandreality,throughdataaccess,weknowthiskindofsituationusuallyexiststwopossibilities:TheimproperadjustmentofLCDbacklightcontrastTheabnormalcommunicationofLCDscreenandsinglechipmicrocomputerFirstly,wesawtheLCDlightedinthedark,withdotmatrixclearlydisplayed,soweeliminatedthefirstpossibility.However,westilltrytoadjustthesliderheostatinthe3rdpin,finallydeterminingthatitisnotbacklitcontrastofLCDtoresultinthisproblem.Thesecondsituationmaybemore,thereasonsofabnormalcommunicationofMCUandLCDmaybeaconnectionproblem,orcode,amongthetwocodemayhaveaproblemofshortdelay.BecausetherefreshabilityofLCDscreenisnotconsideredinthesimulation,butinfact,whenthesinglechipmicrocomputerperformswritingoperationstoLCDscreen,itisliketopourabigbarrelofwaterintoasmallmouthbottleofwater,thedataprocessingabilityofsinglechipmicrocomputerismuchbetterthanthereceptivityofLCDscreen,soapropertimedelaymakestheLCDscreentoworknormally.Bylookinguptosomematerials,weaddedsomeappropriatedelay,finallythetimedelaybetweentwocyclesissetto0.8s,butproblemsstillexist.ThenweusethemultimetertomeasurethecommunicationbetweenthesinglechipmicrocomputerandtheD0~D7mouthofLCD1602,findingthatthevoltagevalueofindividualtubesarenotasimagined,soweconsideredthatapoorcontactcausestheproblem,planningtobuildalineoncemore.Theseconddayafterpitchinglineusinganotherpieceofbreadplate,wecanoccasionallyseeflashesofdigitaltemperatureandhumidity,whichletusbefirmthatlapjointonthebreadplateresultedinmanypoorcontacts,sowestartedtoweldthecircuit.3.ProblemsduringformalweldingAfterthefirstformalweldingwascompleted,wefoundthecircuitthatworkedwellinthetestcan'tdisplayproperlyafterrealwelding,butthistimewecanassertdirectlythatisalineproblem,sowereinforcedthesolderjointsforconsolidation,thistimewecanseenormalsteadytemperatureandhumidity.Althoughtherewasaprobleminthedisplayofthetime,everyonewasveryhappy.Thebuzzerdidn'tworkproperlyInthetestofbuzzercircuit,thebuzzerrangaslongasgivenelectricity.Wecheckedwhetherthebuzzertriodewasnormalonebyone,giventherightanswer,weconsulteddataandtheteachertolearnthathighandlowlevelvoltagesofsinglechipmicrocomputer’seachnozzleisnotthesame,undertheteacher'sadvice,wechangedthecommunicationbuzzercircuitof3nozzlewhichwewillnotgenerallyuseto2nozzle,alsomodifiedsomecode.Thenthebuzzerdidn’tring,sodidn’titwhenarrivingatthealarmingtemperature,finallywefoundtheworkingvoltageofthebuzzerweboughtisgreaterthanthepowersupplyvoltageofthecircuit,wealsotookthehigh-temperaturealarminghumanizedproblemintoconsideration,sowechangedthebuzzertoLEDlight,andallworked.DatedisplayisnotcorrectThefinalproblemthatwasnotsolvedofthewholeprocessisthewrongtimedisplay,westilldarenottotellwhattheproblemwas.Ofthetimedisplay,themonthinformationcanbereadanddisplayednormally,butthedaycan’t,whichwasalwaysaccompaniedbysomemessycode.Andeachtimeweresetthemicrocontroller,displayofthedaywouldbedifferent.Thesituationisroughlyshowedasfollows:Afterdoingthecomparisonoftheexistingonlinecalendarprogram,wefoundtheaddresswhilequeryingcalendarchipintheprogramhadnoerrors,andreadanddisplayofmonthisverynormal,alsowiththesimulation.AtstartwethoughtmaybethetwolatticewhichdisplaythedayinformationontheLCDscreenwerebroken,sowechangedthetheirpositions,butstillonlymonthnormallydisplayed.Wereplacedtheclockchiptogetthesameresult.Atlast,wefoundeverythingwouldbeallrightafterjoiningtheinitializationoftimeintheheaderfilesofds1302,thecauseoftheproblemwasthatifds1302wasnotinitialized,thedaywouldbearandomvalueratherthanamorallysystemtime.组员分工及心得体会组员分工及完成情况:我组十一个人,其中电商专业负责网站设计与制作局部〔3人〕:董笑言郝天琪欧子豪电信工程及管理专业负责温湿度传感器电路局部和外形设计〔8人〕:焊接:商文熙、刘驰、王珂电路设计:周雪、邹嘉桐、赵舒玄仿真设计:周雪程序设计:石康采购:石康、邹嘉桐AutoCAD以及外观设计:郭媛媛
完成情况:除了赵舒玄因赴台湾交流工程无法完成出勤,其余组员均出勤良好。全组上下齐心协力,虽然大体分工如上但是每个局部根本都是组员间相互帮助完成的。最终到达老师根本要求温湿度显示,完成额外功能高温报警。其中额外功能日期显示未完成。心得体会:结束了紧张的期末考试,我们又迎来了小学期实践活动。这次小学期需要我们设计,制作一个温湿度传感器。从电路设计、电路焊接、材料选购、外观设计,到网站设计、宣传出售,全部要我们自己完成。在基于单片机的温湿度传感器电路设计过程中,我们学到了很多重要的东西,其中最重要的是如何将实践和理论相联系,怎样将所学到的知识运用到我以后的工作中去。大学的课堂的学习只是在给我们灌输专业知识,而我们应把所学的用到我们现实的生活中去,此次的温湿度传感器设计给我奠定了一个实践根底。本系统的设计应用到了电路分析,模拟电路根底,数字电路设计,C语言根底等多方面的知识。电路焊接局部听起来容易,但实际操作起来难度很大。不仅要考虑焊接的牢固程度,还要考虑器件布局,导线连接,焊点大小。最难最麻烦的局部是后期的故障排查,因为在茫茫多的焊点、线路中,任何一个小错误都会导致最终电路的工作异常,所以每一个点都要排查,需要极大的耐心。除了耐心,智慧也是必不可少的。有时候,往往多思考一下,改动一个线路的位置,就会大大减少工作量,并且降低电路的错误率。因此,电路焊接大大锻炼了我们的耐心、毅力,而且令我们更深刻的体会到做事要有智慧,有方案,不能头脑一热,说做就做。选择外观时需要综合考虑很多因素:如是否好看、是否易实现、是否具有创意、是否利于结构设计等。最后要对外观进行包装,确保没有电路板裸露在外,并加以装饰。尽量做到外观美、功能全和易操作的统一。总的来说,这次小学期不得不说是对每一个人的挑战,一切知识都是新的。它考验的是我们的学习能力,怎么样快速入门一个自己从未接触过的知识是需要技巧以及好学的精神的。我们在这次小学期学到的很多,至少每个人都能说出基于单片机的设计是怎样的工作原理,至少每个人都不会忘了给我们造成无限麻烦的LCD1602是一个2X16液晶屏每个字块用点阵显示,至少提到时钟芯片,晶振大家都会想起这次忙得不可开交的小学期。由于时间比拟仓促,我们只能做到到达现在这样的水平;其他的希望以后的学习中,能够再做进一步尝试。附录A:电路原理图附录B:程序清单main.c/*************端口定义********************P1.0SCK(SHT10) P1.1DATA(SHT10) P0D0~D7(LCD1602)P2.0RS(LCD1602)P2.1RW(LCD1602)P2.2E(LCD1602)*****************************************/#include<reg52.h>#include<intrins.h>#include<stdio.h>#include<string.h>#include<absacc.h>#include<math.h>#include"ds1302.h"#defineucharunsignedchar#defineuintunsignedint//1602液晶端口定义******sbitLcdRs=P2^0;sbitLcdRw=P2^1;sbitLcdEn=P2^2;sbitLED=P2^7;sbitACC0=ACC^0;sbitACC7=ACC^7;ucharstr[7];unsigneddispbuff=0,OldSecond=0; //dispbuff是显示输出的临时变量unsignedintAlarmVal=40; //设置的报警温度//向LCD写入命令或数据************************************************************#defineLCD_COMMAND 0//Command#defineLCD_DATA 1//Data#defineLCD_CLEAR_SCREEN 0x01//清屏#defineLCD_HOMING 0x02//光标返回原点//设置显示模式************************************************************#defineLCD_SHOW 0x04//显示开#defineLCD_HIDE 0x00//显示关 #defineLCD_CURSOR 0x02 //显示光标#defineLCD_NO_CURSOR 0x00//无光标 #defineLCD_FLASH 0x01#defineLCD_NO_FLASH 0x00//设置输入模式************************************************************#defineLCD_AC_UP 0x02#defineLCD_AC_DOWN 0x00//default#defineLCD_MOVE 0x01//画面可平移#defineLCD_NO_MOVE 0x00//defaultsbitSCK=P1^0;//定义通讯时钟端口sbitDATA=P1^1;//定义通讯数据端口typedefunion{unsignedinti;floatf;}value;enum{TEMP,HUMI};//TEMP=0,HUMI=1#definenoACK0#defineACK1//adrcommandr/w#defineSTATUS_REG_W0x06//00000110#defineSTATUS_REG_R0x07//00000111#defineMEASURE_TEMP0x03//00000011#defineMEASURE_HUMI0x05//00000101#defineRESET0x1e//00011110/****************定义函数****************/voids_transstart(void);//启动传输函数voids_connectionreset(void);//连接复位函数chars_write_byte(unsignedcharvalue);//SHT10写函数chars_read_byte(unsignedcharack);//SHT10读函数chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode);//测量温湿度函数voidcalc_dht90(float*p_humidity,float*p_temperature);//温湿度补偿unsignedcharLCD_Wait(void);voidLCD_Write(bitstyle,unsignedcharinput);/***********延时函数****************/voiddelay(uintz){ uintx,y; for(x=z;x>0;x--) for(y=110;y>0;y--);}/***********LCD写指令/数据****************/voidLCD_Write(bitstyle,unsignedcharinput){ LcdRs=style; //style=0表示写指令,为1表示写数据 P0=input; delay(6); LcdEn=1; delay(6); LcdEn=0; }/***********LCD显示模式****************/voidLCD_SetDisplay(unsignedcharDisplayMode){ LCD_Write(LCD_COMMAND,0x08|DisplayMode); }/***********LCD输入模式****************/voidLCD_SetInput(unsignedcharInputMode){ LCD_Write(LCD_COMMAND,0x04|InputMode);}//初始化LCD************************************************************voidLCD_Initial(){ LcdEn=0; LCD_Write(LCD_COMMAND,0x38);//8位数据端口,2行显示,5*7点阵 LCD_Write(LCD_COMMAND,0x38); LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);//开启显示,无光标 LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);//清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);//AC递增,画面不动}//液晶字符输入的位置************************voidGotoXY(unsignedcharx,unsignedchary){ if(y==0) LCD_Write(LCD_COMMAND,0x80|x); if(y==1) LCD_Write(LCD_COMMAND,0x80|(x-0x40));}//将字符串输出到液晶显示voidPrint(unsignedchar*str){ while(*str!='\0') { LCD_Write(LCD_DATA,*str); str++; }}//将字符串输出到液晶的指定位置显示voidLCD_WriteString(unsignedcharX,unsignedcharY,char*p){ EA=0;GotoXY(X,Y);//设置XY坐标while(*p){LCD_Write(LCD_DATA,*p); //显示字符p++;} EA=1;}//将字符输出到液晶的指定位置显示voidLCD_SramChar(unsignedcharX,unsignedcharY,unsignedcharp){ EA=0;GotoXY(X,Y);//设置XY坐标LCD_Write(LCD_DATA,p); //显示字符 EA=1;}voidzhuanhuan(floata)//浮点数转换成字符串函数{ memset(str,0,sizeof(str)); //字符串清零 sprintf(str,"%f",a); //浮点数转换成字符串函数}/*;模块名称:delay_n10us();;功能:延时函数,延时约n个10us;*/voiddelay_n10us(uintn)//延时n个10us@12M晶振{uinti;for(i=n;i>0;i--){ _nop_();_nop_();_nop_(); _nop_();_nop_();_nop_(); }}/*;模块名称:s_transstart();;功能:启动传输函数;*/voids_transstart(void){DATA=1;SCK=0;//Initialstate_nop_();SCK=1;_nop_();DATA=0;_nop_();SCK=0;_nop_();_nop_();_nop_();SCK=1;_nop_();DATA=1;_nop_();SCK=0;}/*;模块名称:s_connectionreset();;功能:连接复位函数;*/voids_connectionreset(void){unsignedchari;DATA=1;SCK=0;//Initialstatefor(i=0;i<9;i++)//9SCKcycles{SCK=1;SCK=0;}s_transstart();//transmissionstart}/*;模块名称:s_write_byte();;功能:SHT10写函数;*/chars_write_byte(unsignedcharvalue)////writesabyteontheSensibusandcheckstheacknowledge{unsignedchari,error=0;for(i=0x80;i>0;i/=2)//shiftbitformasking{if(i&value)DATA=1;//maskingvaluewithi,writetoSENSI-BUSelseDATA=0;SCK=1;//clkforSENSI-BUS_nop_();_nop_();_nop_();//pulswithapprox.3usSCK=0;}DATA=1;//releaseDATA-lineSCK=1;//clk#9forackerror=DATA;_nop_();_nop_();_nop_();SCK=0;DATA=1;//releaseDATA-linereturnerror;//error=1incaseofnoacknowledge//返回:0成功,1失败}/*;模块名称:s_read_byte();;功能:SHT10读函数;*/chars_read_byte(unsignedcharack)//readsabyteformtheSensibusandgivesanacknowledgeincaseof"ack=1"{unsignedchari,val=0;DATA=1;//releaseDATA-linefor(i=0x80;i>0;i/=2)//shiftbitformasking{SCK=1;//clkforSENSI-BUSif(DATA)val=(val|i);//readbit _nop_();_nop_();_nop_();//pulswithapprox.3usSCK=0;}if(ack==1)DATA=0;//incaseof"ack==1"pulldownDATA-LineelseDATA=1;//如果是校验(ack==0),读取完后结束通讯_nop_();_nop_();_nop_();//pulswithapprox.3usSCK=1;//clk#9forack_nop_();_nop_();_nop_();//pulswithapprox.3usSCK=0;_nop_();_nop_();_nop_();//pulswithapprox.3usDATA=1;//releaseDATA-linereturnval;}/*;模块名称:s_measure();;功能:测量温湿度函数;*/chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode){unsignederror=0;unsignedinti;s_transstart();//transmissionstartswitch(mode){//sendcommandtosensorcaseTEMP:error+=s_write_byte(MEASURE_TEMP);break;caseHUMI:error+=s_write_byte(MEASURE_HUMI);break;default:break;}for(i=0;i<65535;i++)if(DATA==0)break;//waituntilsensorhasfinishedthemeasurementif(DATA)error+=1;*(p_value)=s_read_byte(ACK);//readthefirstbyte*(p_value+1)=s_read_byte(ACK);//readthesecondbyte*p_checksum=s_read_byte(noACK);returnerror;}/*;模块名称:calc_sht10();;功能:温湿度补偿函数;*/voidcalc_sht10(float*p_humidity,float*p_temperature)//calculatestemperature[C]andhumidity[%RH]//input:humi)//temp//output:humi//temp{constfloatC1=-4.0;constfloatC2=+0.0405;constfloatC3=-0.0000028;constfloatT1=+0.01;constfloatT2=+0.00008;floatrh=*p_humidity;//rh:Humidity[Ticks]12Bitfloatt=*p_temperature;//t:Temperature[Ticks]14Bitfloatrh_lin;//rh_lin:Humiditylinearfloatrh_true;//rh_true:Temperaturecompensatedhumidityfloatt_C;//t_C:Temperature[C]t_C=t*0.01-40;//calc.temperaturefromticksto[C]rh_lin=C3*rh*rh+C2*rh+C1;//calc.humidityfromticksto[%RH]rh_true=(t_C-25)*(T1+T2*rh)+rh_lin-4;//calc.temperaturecompensatedhumidity[%RH]if(rh_true>100)rh_true=100;//cutifthevalueisoutsideofif(rh_true<0.1)rh_true=0.1;//thephysicalpossiblerange*p_temperature=t_C;//returntemperature[C]*p_humidity=rh_true;//returnhumidity[%RH]}//显示当前时间voidShowTime(void){ unsignedchari=0,j=0,k=0; //EA=0; ds1302_read_time();//读取时间 if(OldSecond!=time_buf[6]) //判断当前秒钟和上次显示是的秒钟是否相同,不同,更改显示 { OldSecond=time_buf[6]; // 保存当前秒钟时间 dispbuff=(time_buf[0]>>4)+'0'; LCD_SramChar(3,1,dispbuff);//年 dispbuff=(time_buf[0]&0x0F)+'0'; LCD_SramChar(4,1,dispbuff); dispbuff=(time_buf[1]>>4)+'0'; LCD_SramChar(5,1,dispbuff); dispbuff=(time_buf[1]&0x0F)+'0'; LCD_SramChar(6,1,dispbuff); LCD_SramChar(7,1,'/'); dispbuff=(time_buf[2]>>4)+'0'; LCD_SramChar(8,1,dispbuff);//月 dispbuff=(time_buf[2]&0x0F)+'0'; LCD_SramChar(9,1,dispbuff); //LCD_SramChar(10,1,'/'); //dispbuff=(time_buf[3]>>4)+'0'; //LCD_SramChar(11,1,dispbuff);//日 //dispbuff=(time_buf[3]&0x0F)+'0'; //LCD_SramChar(12,1,dispbuff); }}voidmain(void){ valuehumi_val,temp_val; unsignedcharerror,checksum; LcdRw=0;LED=0; s_connectionreset();//复位传感器 LCD_Initial(); //液晶初始化 while(1) { error=0; error+=s_measure((unsignedchar*)&humi_val.i,&checksum,HUMI);//获得湿度值 error+=s_measure((unsignedchar*)&temp_val.i,&checksum,TEMP);//获得温度值 if(error!=0) { s_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农用土地流转合同样本
- 公司顾问聘用合同样本
- app定制采购合同样本
- 2025建筑工程分包合同范本下载合同
- 写物业收费合同标准文本
- 供求合同样本
- 供水工程合同标准文本
- 上海市房屋租赁合同样本
- 归纳总结教学经验计划
- 农村土屋出售合同样本
- TCI 535-2024 铝合金液态模锻模具技术条件
- 胰岛素泵护理管理规范
- 2025年河南工业贸易职业学院单招职业适应性测试题库及答案参考
- 9.1.1 西亚 第1课时 课件 七年级地理下册 人教版
- 校外培训机构预收费资金托管协议书范本
- DB37-T4827-2025 水利工程运行管理标牌设置指南
- 2025山东能源集团中级人才库选拔高频重点模拟试卷提升(共500题附带答案详解)
- 《餐厅托盘的使用技》课件
- 【化学】化学与可持续发展教学设计-2024-2025学年九年级化学人教版下册
- Unit 2 Know your body(说课稿) -2024-2025学年外研版(三起)(2024)英语三年级下册
- 断绝父母关系协议书
评论
0/150
提交评论