《DHT2151AM2301汇编程序.docx》由会员分享,可在线阅读,更多相关《DHT2151AM2301汇编程序.docx(56页珍藏版)》请在第壹文秘上搜索。
1、;DHT21运用范例;单片机:AT89S52或STC89C52RC;功能;串口发送温湖度数据波特率9600;硬件连接:P2.0口为通讯口连接DHTl1.DHTu的电源和地连接单片机的电源和地,单片机串口加MAX232连接电脑;一一变量定义区F1.AGEQU30HU8comdataEQU31HU8T_data_HEQU32HU8T_data_1.EQU33HU8RH_dala_HEQU34HU8RH_data_1.EQU35HU8checkdataEQU36HU8tempEQU37HtempEQU38Horg0000hAJMPSTARTorg0023hAJMPSET_INTSTART:MOVPO
2、,#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.
3、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;检测从机
4、是否响应,无响应则跳出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.,U8com
5、data;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:;推断低电平是否结
6、束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;为低电平则跳出C
7、JNE,tt,I.OOP41.JMPCOM_END1.OOP4_END:MOVA,F1.AGCJNEA,no,NO_FI.AG1.JMPCOM_ENDNO_F1.AG:MOVA,U8comdataR1.AOR1.,U8tempMOVU8comdata,A:推断高电平是否结束DJNZtemp,1OOP_COMCOM_END:RET;延时函数Delay:MOVR4,A;2.5msDl:MOVR5,#0FFHDJNZR5,$DJNZR4,DlRET;延时IOus函数DclayJOus:;IOusNOPNOPNOPNOPNOPNOPRET;延时2S函数Delay_2S:MOVA,#2501.CA1.1
8、.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;禁止串口中断;将同一数据发送到串口;允许串口中断:禁止
9、串口中断;接受串口数据;将串口数据送往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.EQU35HU8checkdataEQU3611U8tempEQU37HtempEQU3
10、8HORGOOOOH1.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.W
11、RDCA1.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.
12、DWRCMOV,ftOBHMOVR6,#26HCA1.1.DWRCJNB)3,JIANJN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