DHT2151AM2301汇编程序.docx
;DHT21运用范例;单片机:AT89S52或STC89C52RC;功能;串口发送温湖度数据波特率9600;硬件连接:P2.0口为通讯口连接DHTl1.DHTu的电源和地连接单片机的电源和地,单片机串口加MAX232连接电脑;一一变量定义区F1.AGEQU30HU8comdataEQU31HU8T_data_HEQU32HU8T_data_1.EQU33HU8RH_dala_HEQU34HU8RH_data_1.EQU35HU8checkdataEQU36HU8tempEQU37HtempEQU38Horg0000hAJMPSTARTorg0023hAJMPSET_INTSTART:MOVPO,#OFFHMOVTMOD,#20H;T1工作方式2;串口波特率为9600;串口工作于方式1并允许接受;启动Tl;允许串口中断;允许全部外设中断MOVTH1.tfOFDHMOVSCON,#50HSETBTRiSETBESSETBEAMAIN:;调用湿度读取子程序1.C1.1.RH;串口发送程序一一TT:MOVA,U8RH_data_H1.C1.1.TX_DATAMOVA,U8RH_data_1.1.C1.1.TX-DTMOVA,U8T_data_H1.C1.1.TX-DTMOVA,U8T_data_1.1.C1.1.TX-DTMOVA,U8checkdata1.C1.1.TX-DT延时两秒1.C1.1.Delay_2SIJMPMAIN;湿度读取子程序;一一以下变量均为全局变量;温度高8位=U8T_data_H;一一温度低8位=U8T_data_1.;湿度高8位=U8RH_data_H;一一湿度低8位=U8RH_data_1.:校验8位=UScheckdata;一一调用相关子程序如下:Read,DelayOUS,DelayRH:;主机拉低总线,延时50OUSC1.RP2.0MOV,8361.CA1.1.DelaySETBP2.0;延时40us1.C1.1.DelayJOus1.CA1.1.)elay_10us1.C1.1.Delay_10us1.CA1.1.DelayOUS;检测从机是否响应,无响应则跳出JBP2.O,Rl1.EM)MOVF1.AG,tt1.OOPl:INCF1.AGMOVA,F1.AGJBP2.O,1.OOP1_END;为高电平则跳出CJNEA,#0,1.ooPl1.JMPRH_END1.OOP1.END:MOVF1.AG,#01.00P2:INCF1.AGMOVA,F1.AGJNBP2.O,1.00P2_END;为低电平则跳出CJNE,#0,1.00P21.JMPRl1.EYD1.00P2,EN):进入数据接收阶段1.CA1.1.ReadMOVU8RH_data_H,U8comdata;1.CA1.1.ReadMOVU8RH_data_1.,U8comdata;1.CA1.1.ReadMOVU8T_data_H,U8comdata;1.C1.1.ReadMOVU8T_data_1.,U8comdata;1.CA1.1.ReadMOVU8checkdata,U8comdata;SETBP2.0;一一留意:本程序没有加校验程序假如用户须要校验请安以下说明编写校验程序;数据传送正确时校验和UScheckdata的值等于;"8bil海度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据”所得结果的末8位RH_END:RET;读数子程序Read:MOVtemp,#8MOVU8comdata,#01.oOP/0M:;推断低电平是否结束MOVF1.AG,#01.00P3:INCF1.AGMOV,F1.AGJBP2.0,1.OOP3_END;为高电平则跳出CJNE,#0,1.ooP31.JMPCOM_END1.00P3_END:;C1.RP2.1:SETBP2.11.CA1.1.Delay_10us1.C1.1.Delay_10us1.CA1.1.Delay_10us:C1.RP2.1;SETBP2.1MOVU8temp,#0JNBP2.O,HIGl1.ENDMOVU8temp,SlHIGH_END:MOVF1.AG,#01.00P4:INCF1.AGMOVA,F1.AGJNBP2.O,1.ooP4_END;为低电平则跳出CJNE,tt,I.OOP41.JMPCOM_END1.OOP4_END:MOVA,F1.AGCJNEA,no,NO_FI.AG1.JMPCOM_ENDNO_F1.AG:MOVA,U8comdataR1.AOR1.,U8tempMOVU8comdata,A:推断高电平是否结束DJNZtemp,1>OOP_COMCOM_END:RET;延时函数Delay:MOVR4,A;2.5msDl:MOVR5,#0FFHDJNZR5,$DJNZR4,DlRET;延时IOus函数DclayJOus:;IOusNOPNOPNOPNOPNOPNOPRET;延时2S函数Delay_2S:MOVA,#2501.CA1.1.DelayMOVA,#2501.CA1.1.DelayMOVA,#2501.CA1.1.DelayMOVA,#2501.CA1.1.DelayMOVA,#2501.C1.1.DelayMOVA,#2501.CA1.1.DelayMOVA,#2501.CA1.1.DelayMOVA,#2501.C1.1.DelayRET;串口数据发送函数TX_)ATA:C1.RESMOVSBUF,AJNBTI,$C1.RTISETBESRET:串口中断函数SET_INT:C1.RESJNBR,$C1.RRIMOVA,SBUFMOVPO,MOVSBUF,A;禁止串口中断;将同一数据发送到串口;允许串口中断:禁止串口中断;接受串口数据;将串口数据送往POJNBTI,$C1.RTI;将同一数据发送到串口SETBES;允许串口中断RETIEND结束RSBITP3.4RWBITP3.5EBITP3.6DlBITP2.OD2BITP2.1D3BITP2.2IDSBITP2.4IRWBITP2.5IASBITP2.6ICSBITP2.7TTEQU39HFI.AGEQU30HU8comdataEQU31HU8T_data_HEQU32HU8T_data_1.EQU33HU8RH_data_HEQU34HU8RH_data_1.EQU35HU8checkdataEQU3611U8tempEQU37HtempEQU38HORGOOOOH1.JMPMAINORG0040HMAIN:MOVR3,ttMOVTT,#0MOVSP,#5FHCA1.1.INITMOV,#07HCA1.1.WRCTAB:DB,WE1.COME!TABI:DB,TWF&1.NDESIGNMOVRO,#80H+16MOVRl,#8MOVR2,#01.OOP:MOV,#0AHCA1.1.DWRCMOVA,#OBHMOVR6,#26HCA1.1.DWRCMOV,#OBHMOVR6,#26HCA1.1.DWRCMOVA,ROINCROCA1.1.WRCC1.RMOVA,R2INCR2MOVDPTR,ttTABMOVC,©A+DPTRCA1.1.WRDCA1.1.DE1.AYlDJNZRl,1.OOPMOVRO,#0CoH+25MOVRl,#32MOVR2,401.OOPl:MOV,ROINCROCA1.1.WRCC1.RAMOV,R2INCR2MOVDPTRlTABlMOVCA,©A+DPTRCA1.1.WRDCA1.1.DE1.AYlDJNZR1.1.OOP1MOVA,#04HCA1.1.WRCMOVA,#OCHCA1.1.WRCSJ:CA1.1.XSSS:JNBDl,KEY1.JMPSJSHU:DB,0123456789,KEY:CA1.1.DE1.AY1JBDI,SJKEYl:MOVA,ItOAHMOVR6,40ffHCA1.1.DWRCMOV,ftOBHMOVR6,#26HCA1.1.DWRCJNB)3,JIANJNBD2,JIJNBDl,FECA1.1.XS1.JMPKEYlFE:CA1.1.DE1.AYlJBDl,KEYl1.JMPFENJI:CA1.1.DE1.AYlJBD2,KEY1MOVA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256MOVB,#100DIVABMOVDPTRiSHUMOVCA,©A+DPTRCA1.1.WRDMOVA,#8011+12CA1.1.WRCMOVA,BMOVB,#10DIVABMovDptrishuMOVCA,A+DPTRCA1.1.WRDMOVA,#80H+14CA1.1.WRCMOVA,BMOVC,©A+DPTRCA1.1.WRDRETCBUSY:PUSHCCPUSHDPUPUSHDP1.PUSHPSWDWRC:C1.RICSC1.RISSETBIDSSETBIRWSETBIASMOVPO,C1.RTASC1.RIRWMOVPO,R6SETBIRWSETBISSETBICSRETDWRD:C1.RTASSETBIDSSETBIRWC1.RICSSETBISMOVPO,C1.RTASC1.RIDSMOVPO,#OFFHMOVR5,POSETBIDSSETBISSETBICSRETWRC:C1.RRSC1.RRWMOVP1,CA1.1.DE1.AYSETBECA1.1.DE1.AYC1.RERETWRD:SETBRSCA1.1.DE1.AYSETBECA1.1.DE1.AYC1.RERETDE1.AY:MOVR7,#5DE1.A:MOVR6,#0F9HDJNZR6,$DJNZR7,DE1.RETDE1.AY1:MOVR7,#120DE1.1:MOVR6,#0F9HDJNZR6,$DJNZR7,DE1.AlRETRH:MOVA,#361.C1.1.DE1.AY2SETBP2.31.C1.1.Delay_10usI-CA1.I.Delay_IOus1.C1.1.Delay_10us1.CA1.1.Delay_10usRHl:INCF1.AGMOV,F1.AGJBP2.3,RH1.ENDCJNE,#0,RHl1.JMPRH_ENDRH1_END:MOVF1.AG,#0RH2:INCF1.AGMOV,F1.AGJNBP2.3,RH2一ENDCJNE,#0,RH21.JMPRH_ENDRH2END:1.CIJ.ReadMOVU8RH_data_H,U8comdata;1.C1.1.ReadMOVU8RH_data_1.,U8comdata;1.CA1.1.ReadMOVU8T_data_H,U8comdata;1.CA1.1.ReadMOVU8T_data_1.,U8comdata;1.CA1.1.ReadMOVU8checkdata,U8comdata;SETBP2.3RH_END:RETRead:MOVtemp,#8MOVU8comdata,#01.OOP_COM:MOVF1.AG,#01.00P3:INCF1.AGMOVA,F1.AGCJNE,tt,I.OOP31.JMPCOM_END1.OOP3_END:1.C1.1.Delay_10us1.CA1.1.DelayOUS1.C1.1.Delay_10usMOVU8temp,#0JNBP2.3,HIGH_ENDMOVU8temp,#1HIGH_END:MOVF1.AG,n1.00P4:INCF1.AGMOV,F1.AGJNBP2.3,1.00P4_ENDCJNE,#0,1.oOP41.JMPCOM_EM)1.00P4_END:MOV,F1.AGCJNE,#0,NO_F1.AG1.JMPCOM_ENDNOF1.AG:MOV,>,U8comdataR1.OR1.A,U8tempMOVU8comdata,DJNZtemp,.OOP_COMCOM_END:RBTDE1.Y2:MOVR4,ADE1.2:MOVR5,#OFEHDJNZR5,$DJNZR4,DE1.A2RETDelayJOus:NOPNOPNOPNOPNOPNOPRETEND