基于STC89C52單片機(jī)的溫濕度傳感器_第1頁(yè)
基于STC89C52單片機(jī)的溫濕度傳感器_第2頁(yè)
基于STC89C52單片機(jī)的溫濕度傳感器_第3頁(yè)
基于STC89C52單片機(jī)的溫濕度傳感器_第4頁(yè)
基于STC89C52單片機(jī)的溫濕度傳感器_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

目錄HYPERLINKchapter1Temperatureandhumiditymeasuringprinciple21.1Theschemeselectionoftemperatureandhumiditysensor21.2TheschemeselectionofTemperatureandhumiditydisplay2chapter2Thehardwaredesignofthesystem32.1Theoveralldesignofthesystemhardware32.2Themainschemeselectionanddesignofthecontrolmodule4singlechipSTC89C52introduction6Themaincontrolmodulecircuit72.3TheselectionanddesignOFTheRTCmodulescheme82.4Thedesignofinterfacemodules8chapter3Thedesignofsystemsoftware93.1Systemofthemainprogramandflowchart93.2ThereadoperationflowchartOFDS1302clockchip103.3writeoperationflowchartTheofLCDmodule11chapter4Theexperimentanddebugging124.1Difficultiesandsolutions14chapter5Teamdivisionoflaborandcomments15appendixA:electricschematicdiagram18appendixB:ProgramList19appendixC:externalview32第1章溫濕度測(cè)量原理單片機(jī)的接口信號(hào)是數(shù)字信號(hào)。要想用單片機(jī)獲取溫濕度這類非電信號(hào)的信息,必須將其轉(zhuǎn)換為數(shù)字信號(hào),因?yàn)槿绻娏骰螂妷狠敵鍪悄M信號(hào),還必須進(jìn)行A/D轉(zhuǎn)換,以滿足單片機(jī)接口的需要。如果是數(shù)字信號(hào)就可以直接送往單片機(jī)進(jìn)行數(shù)據(jù)處理。1.1溫濕度傳感器的方案選擇DS18B20+HS1100因?yàn)镠S1100在網(wǎng)上的資料相當(dāng)匱乏,以及老師在課程開(kāi)始前說(shuō)HS1100輸出為模擬信號(hào),需要加A/D轉(zhuǎn)換。以及,分別采用溫度濕度傳感器的話,占用電路板空間會(huì)更大。綜合考慮以上因素,我們認(rèn)為溫濕度傳感器SHT11/10更好。SHT11/10因?yàn)樵谥邪l(fā)電子市場(chǎng),一下子找不到SHT11插針式的傳感器,所以我們選用了精度稍低的SHT10。1.2溫濕度顯示的方案選擇方案一:時(shí)鐘的顯示可以用多位七段LED數(shù)碼管顯示,七段LED數(shù)碼管顯示耗能多,而且顯示位數(shù)有限,每增加一位都要在程序設(shè)計(jì)和硬件設(shè)計(jì)方面增加很多的工作量,不利于電路的擴(kuò)展,而且無(wú)法顯示年、月、日、星期這些漢字,使得顯示不夠直觀,靈活。但是這種設(shè)計(jì)方案在顯示位數(shù)比擬少時(shí)性價(jià)比比擬高,價(jià)格廉價(jià),方案二:采用點(diǎn)陣式數(shù)碼管顯示。點(diǎn)陣式數(shù)碼管是由八行八列的發(fā)光二極管組成,對(duì)于顯示文字比擬適合,而在本課題設(shè)計(jì)中的顯示數(shù)字較多字符較少,而且此種方案耗能多,不符合現(xiàn)代的節(jié)能理念,所以不用此種作為顯示。方案三:采用LCD液晶顯示器顯示。而LCD液晶顯示那么耗能少,能夠顯示年、月、日、星期等漢字,在顯示方面更加靈活,而且改變顯示時(shí)只要改變軟件設(shè)計(jì)就可以,不用改變硬件電路的設(shè)計(jì),易于電路的功能擴(kuò)展。電路的軟件設(shè)計(jì)也很簡(jiǎn)單。另外,這種設(shè)計(jì)硬件更加簡(jiǎn)潔。采用LCD液晶顯示方案的缺點(diǎn)是在顯示位數(shù)比擬少時(shí),價(jià)格也并不貴。比擬上述兩種方案可以看出方案三耗能少,顯示靈活,易于電路擴(kuò)展而且不管是軟件設(shè)計(jì)還是硬件設(shè)計(jì)都比擬簡(jiǎn)單,因此采用第三種設(shè)計(jì)方案。綜上所述,本設(shè)計(jì)采用SHT10做溫濕度傳感器,引入DS1302時(shí)間模塊的設(shè)計(jì)方案,顯示使用LCD液晶顯示。其中LCD1602性價(jià)比高,自帶字模,選用LCD1602。第2章系統(tǒng)硬件設(shè)計(jì)根據(jù)溫濕度傳感器的功能要求,基于單片機(jī)的設(shè)計(jì)方案要運(yùn)用集成SHT10,實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)記錄與讀取:〔1〕根本要求具有讀取溫濕度并顯示的功能〔2〕創(chuàng)新要求具高溫報(bào)警功能具有日歷功能2.1系統(tǒng)硬件的整體設(shè)計(jì)主控芯片使用51系列STC89C52單片機(jī),系統(tǒng)由主控制器STC89C52、時(shí)鐘芯片DS1302、LCD1602液晶顯示電路LED燈高溫報(bào)警電路組成。系統(tǒng)硬件設(shè)計(jì)框圖:系統(tǒng)硬件設(shè)計(jì)框圖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.組員分工及心得體會(huì)組員分工及完成情況:我組十一個(gè)人,其中電商專業(yè)負(fù)責(zé)網(wǎng)站設(shè)計(jì)與制作局部〔3人〕:董笑言郝天琪歐子豪電信工程及管理專業(yè)負(fù)責(zé)溫濕度傳感器電路局部和外形設(shè)計(jì)〔8人〕:焊接:商文熙、劉馳、王珂電路設(shè)計(jì):周雪、鄒嘉桐、趙舒玄仿真設(shè)計(jì):周雪程序設(shè)計(jì):石康采購(gòu):石康、鄒嘉桐AutoCAD以及外觀設(shè)計(jì):郭媛媛

完成情況:除了趙舒玄因赴臺(tái)灣交流工程無(wú)法完成出勤,其余組員均出勤良好。全組上下齊心協(xié)力,雖然大體分工如上但是每個(gè)局部根本都是組員間相互幫助完成的。最終到達(dá)老師根本要求溫濕度顯示,完成額外功能高溫報(bào)警。其中額外功能日期顯示未完成。心得體會(huì):結(jié)束了緊張的期末考試,我們又迎來(lái)了小學(xué)期實(shí)踐活動(dòng)。這次小學(xué)期需要我們?cè)O(shè)計(jì),制作一個(gè)溫濕度傳感器。從電路設(shè)計(jì)、電路焊接、材料選購(gòu)、外觀設(shè)計(jì),到網(wǎng)站設(shè)計(jì)、宣傳出售,全部要我們自己完成。在基于單片機(jī)的溫濕度傳感器電路設(shè)計(jì)過(guò)程中,我們學(xué)到了很多重要的東西,其中最重要的是如何將實(shí)踐和理論相聯(lián)系,怎樣將所學(xué)到的知識(shí)運(yùn)用到我以后的工作中去。大學(xué)的課堂的學(xué)習(xí)只是在給我們灌輸專業(yè)知識(shí),而我們應(yīng)把所學(xué)的用到我們現(xiàn)實(shí)的生活中去,此次的溫濕度傳感器設(shè)計(jì)給我奠定了一個(gè)實(shí)踐根底。本系統(tǒng)的設(shè)計(jì)應(yīng)用到了電路分析,模擬電路根底,數(shù)字電路設(shè)計(jì),C語(yǔ)言根底等多方面的知識(shí)。電路焊接局部聽(tīng)起來(lái)容易,但實(shí)際操作起來(lái)難度很大。不僅要考慮焊接的牢固程度,還要考慮器件布局,導(dǎo)線連接,焊點(diǎn)大小。最難最麻煩的局部是后期的故障排查,因?yàn)樵诿C6嗟暮更c(diǎn)、線路中,任何一個(gè)小錯(cuò)誤都會(huì)導(dǎo)致最終電路的工作異常,所以每一個(gè)點(diǎn)都要排查,需要極大的耐心。除了耐心,智慧也是必不可少的。有時(shí)候,往往多思考一下,改動(dòng)一個(gè)線路的位置,就會(huì)大大減少工作量,并且降低電路的錯(cuò)誤率。因此,電路焊接大大鍛煉了我們的耐心、毅力,而且令我們更深刻的體會(huì)到做事要有智慧,有方案,不能頭腦一熱,說(shuō)做就做。選擇外觀時(shí)需要綜合考慮很多因素:如是否好看、是否易實(shí)現(xiàn)、是否具有創(chuàng)意、是否利于結(jié)構(gòu)設(shè)計(jì)等。最后要對(duì)外觀進(jìn)行包裝,確保沒(méi)有電路板裸露在外,并加以裝飾。盡量做到外觀美、功能全和易操作的統(tǒng)一??偟膩?lái)說(shuō),這次小學(xué)期不得不說(shuō)是對(duì)每一個(gè)人的挑戰(zhàn),一切知識(shí)都是新的。它考驗(yàn)的是我們的學(xué)習(xí)能力,怎么樣快速入門一個(gè)自己從未接觸過(guò)的知識(shí)是需要技巧以及好學(xué)的精神的。我們?cè)谶@次小學(xué)期學(xué)到的很多,至少每個(gè)人都能說(shuō)出基于單片機(jī)的設(shè)計(jì)是怎樣的工作原理,至少每個(gè)人都不會(huì)忘了給我們?cè)斐蔁o(wú)限麻煩的LCD1602是一個(gè)2X16液晶屏每個(gè)字塊用點(diǎn)陣顯示,至少提到時(shí)鐘芯片,晶振大家都會(huì)想起這次忙得不可開(kāi)交的小學(xué)期。由于時(shí)間比擬倉(cāng)促,我們只能做到到達(dá)現(xiàn)在這樣的水平;其他的希望以后的學(xué)習(xí)中,能夠再做進(jìn)一步嘗試。附錄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是顯示輸出的臨時(shí)變量unsignedintAlarmVal=40; //設(shè)置的報(bào)警溫度//向LCD寫入命令或數(shù)據(jù)************************************************************#defineLCD_COMMAND 0//Command#defineLCD_DATA 1//Data#defineLCD_CLEAR_SCREEN 0x01//清屏#defineLCD_HOMING 0x02//光標(biāo)返回原點(diǎn)//設(shè)置顯示模式************************************************************#defineLCD_SHOW 0x04//顯示開(kāi)#defineLCD_HIDE 0x00//顯示關(guān) #defineLCD_CURSOR 0x02 //顯示光標(biāo)#defineLCD_NO_CURSOR 0x00//無(wú)光標(biāo) #defineLCD_FLASH 0x01#defineLCD_NO_FLASH 0x00//設(shè)置輸入模式************************************************************#defineLCD_AC_UP 0x02#defineLCD_AC_DOWN 0x00//default#defineLCD_MOVE 0x01//畫面可平移#defineLCD_NO_MOVE 0x00//defaultsbitSCK=P1^0;//定義通訊時(shí)鐘端口sbitDATA=P1^1;//定義通訊數(shù)據(jù)端口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/****************定義函數(shù)****************/voids_transstart(void);//啟動(dòng)傳輸函數(shù)voids_connectionreset(void);//連接復(fù)位函數(shù)chars_write_byte(unsignedcharvalue);//SHT10寫函數(shù)chars_read_byte(unsignedcharack);//SHT10讀函數(shù)chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode);//測(cè)量溫濕度函數(shù)voidcalc_dht90(float*p_humidity,float*p_temperature);//溫濕度補(bǔ)償unsignedcharLCD_Wait(void);voidLCD_Write(bitstyle,unsignedcharinput);/***********延時(shí)函數(shù)****************/voiddelay(uintz){ uintx,y; for(x=z;x>0;x--) for(y=110;y>0;y--);}/***********LCD寫指令/數(shù)據(jù)****************/voidLCD_Write(bitstyle,unsignedcharinput){ LcdRs=style; //style=0表示寫指令,為1表示寫數(shù)據(jù) 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位數(shù)據(jù)端口,2行顯示,5*7點(diǎn)陣 LCD_Write(LCD_COMMAND,0x38); LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);//開(kāi)啟顯示,無(wú)光標(biāo) LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);//清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);//AC遞增,畫面不動(dòng)}//液晶字符輸入的位置************************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);//設(shè)置XY坐標(biāo)while(*p){LCD_Write(LCD_DATA,*p); //顯示字符p++;} EA=1;}//將字符輸出到液晶的指定位置顯示voidLCD_SramChar(unsignedcharX,unsignedcharY,unsignedcharp){ EA=0;GotoXY(X,Y);//設(shè)置XY坐標(biāo)LCD_Write(LCD_DATA,p); //顯示字符 EA=1;}voidzhuanhuan(floata)//浮點(diǎn)數(shù)轉(zhuǎn)換成字符串函數(shù){ memset(str,0,sizeof(str)); //字符串清零 sprintf(str,"%f",a); //浮點(diǎn)數(shù)轉(zhuǎn)換成字符串函數(shù)}/*;模塊名稱:delay_n10us();;功能:延時(shí)函數(shù),延時(shí)約n個(gè)10us;*/voiddelay_n10us(uintn)//延時(shí)n個(gè)10us@12M晶振{uinti;for(i=n;i>0;i--){ _nop_();_nop_();_nop_(); _nop_();_nop_();_nop_(); }}/*;模塊名稱:s_transstart();;功能:啟動(dòng)傳輸函數(shù);*/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();;功能:連接復(fù)位函數(shù);*/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寫函數(shù);*/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讀函數(shù);*/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;//如果是校驗(yàn)(ack==0),讀取完后結(jié)束通訊_nop_();_nop_();_nop_();//pulswithapprox.3usSCK=1;//clk#9forack_nop_();_nop_();_nop_();//pulswithapprox.3usSCK=0;_nop_();_nop_();_nop_();//pulswithapprox.3usDATA=1;//releaseDATA-linereturnval;}/*;模塊名稱:s_measure();;功能:測(cè)量溫濕度函數(shù);*/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();;功能:溫濕度補(bǔ)償函數(shù);*/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]}//顯示當(dāng)前時(shí)間voidShowTime(void){ unsignedchari=0,j=0,k=0; //EA=0; ds1302_read_time();//讀取時(shí)間 if(OldSecond!=time_buf[6]) //判斷當(dāng)前秒鐘和上次顯示是的秒鐘是否相同,不同,更改顯示 { OldSecond=time_buf[6]; // 保存當(dāng)前秒鐘時(shí)間 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();//復(fù)位傳感器 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論