1、温湿度检测器DHT11程序及显示程序附录1:#include #include #include DHT11.H#include DHT11.Cunsigned char range4=55,75,10,30; /温湿度上下限初值unsigned char Humi_Temp_Tab8=6,9,0,0,7,8,0,0; /数码管显示初值unsigned char numt1=0; /T1中断计数标志unsigned char numt0=0; unsigned char code Tab_Seg10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x
2、6f; /数码管段码 0-9unsigned char code Tab_Dig8=0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe; /位选第一位到第八位unsigned char code units4=0x39,0x71,0x77,0x76; /单位C/F/RH/*/* 温湿度采集函数 */*/void getdata() if(start_DHT11() read_DHT11(); if(check_sum() Humi_Temp_Tab0=DHT_data.DH_H/10; Humi_Temp_Tab1=DHT_data.DH_H%10; Humi_Te
3、mp_Tab2=DHT_data.DH_L/10; /存储湿度数据 if(flag = 0) Humi_Temp_Tab4=DHT_data.T_H/10; Humi_Temp_Tab5=DHT_data.T_H%10; Humi_Temp_Tab6=DHT_data.T_L/10; /存储摄氏温度数据 else Humi_Temp_Tab4=(9*DHT_data.T_H/5+32)/10; Humi_Temp_Tab5=(9*DHT_data.T_H/5+32)%10; Humi_Temp_Tab6=(18*DHT_data.T_H+320)%100%10; /存储华氏温度数据 /*/*
4、主函数 */*/ void main() delay_ms(500); /先进行延时 等待进入稳定状态 P0 = 0; P1 = 0x0C; /初始化P1口 EA = 0; TR1 = 0; TR0 = 0; TMOD = 0x11; /设置定时器 T0和T1,且工作方式都为方式1 TH1 = (65536-5000)/256; TL1 = (65536-5000)%256; TH0 = (65536-2000)/256; TL0 = (65536-2000)%256; /设定初值2ms TR1 = 1; TR0 = 1; EA = 1; ET0 = 1; ET1 = 1; /打开中断定时器T
5、0和T1 PT1 = 0; PT0 = 1; /强制设置优先级 delay_ms(1000); while(1) if(DHT_data.DH_Hrange1) /湿度大于上限 bee = 0; delay_ms(100); bee = 1; delay_ms(100); if(DHT_data.T_Hrange3) /温度大于上限 bee = 0; delay_ms(10); bee = 1; delay_ms(10); /*/* 定时器T0中断 */*/void T0_timer() interrupt 1 unsigned char KData = 0x00; TR0 = 0; /进入T
6、0后将T0中断关闭 TH0 = (65536-2000)/256; TL0 = (65536-2000)%256; switch(numt0) case 0: P0 = 0; Seg_ce = 1; Seg_ce = 0; /段选开关 if(flag2 = 1) P0 = Tab_Segrange0/10; /显示湿度下限的十位 else P0 = Tab_SegHumi_Temp_Tab0; /显示读取的湿度的十位 Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig0; /位选第一位 Dig_ce = 1; Dig_ce = 0; numt0+;break; case
7、1: P0 = 0; Seg_ce = 1; Seg_ce = 0; if(flag2 = 1) P0 = Tab_Segrange0%10; /显示湿度下限的个位 else P0 = Tab_SegHumi_Temp_Tab1; /显示读取的湿度的个位 Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig1; /位选第二位 Dig_ce = 1; Dig_ce = 0; numt0+;break; case 2: P0 = 0; Seg_ce = 1; Seg_ce = 0; if(flag2 = 1) P0 = Tab_Segrange1/10; /显示湿度上限的十位
8、else P0 = units2; /显示单位R Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig2; /位选第三位 Dig_ce = 1; Dig_ce = 0; numt0+;break; case 3: P0 = 0; Seg_ce = 1; Seg_ce = 0; if(flag2 = 1) P0 = Tab_Segrange1%10; /显示湿度上限的个位 else P0 = units3; /显示单位H Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig3; /位选第四位 Dig_ce = 1; Dig_ce = 0; numt0+;b
9、reak; case 4: P0 = 0; Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig4; /位选第五位,且同时拉低键盘第四行 Dig_ce = 1; Dig_ce = 0; Key_ce = 0; KData = P0; /扫描键盘第四行 switch(KData) case 0xfe: case 0xfd: case 0xfb: case 0xf7: default:break; while(KData != 0xff) KData = P0; Key_ce = 1; if(flag2 = 1) P0 = Tab_Segrange2/10; /显示温度下限的十
10、位 else P0 = Tab_SegHumi_Temp_Tab4; /显示读取的温度的十位 Seg_ce = 1; Seg_ce = 0; numt0+;break; case 5: P0 = 0; Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig5; /位选第六位,且同时拉低键盘第三行 Dig_ce = 1; Dig_ce = 0; Key_ce = 0; KData = P0; /扫描键盘第三行 switch(KData) case 0xfe:if(range0range1&flag2=1) range0+;break; /湿度下限加 case 0xfd:if(r
11、ange190&flag2=1) range1+;break; /湿度上限加 case 0xfb:if(range2range3&flag2=1 range2+;break; /温度下限加 case 0xf7:if(range320&flag2=1) range0-;break; /湿度下限减 case 0xfd:if(range00&flag2=1) range2-;break; /温度下限减 case 0xf7:if(range2range3&flag2=1) range3-;break; /温度上限减 default:break; while(KData != 0xff) KData =
12、 P0; Key_ce = 1; if(flag2 = 1) P0 = Tab_Segrange3/10; /显示温度上限的十位 else P0 = Tab_SegHumi_Temp_Tab6; /显示读取温度的小数位的十位 Seg_ce = 1; Seg_ce = 0; numt0+;break; case 7: P0 = 0; Seg_ce = 1; Seg_ce = 0; P0 = Tab_Dig7; /位选第八位,且同时拉低键盘第一行 Dig_ce = 1; Dig_ce = 0; Key_ce = 0; KData = P0; /扫描键盘第一行 switch(KData) case
13、0xfe:flag2 = flag2;TR1 = TR1;break; /进入和退出限制调整模式 case 0xfd:flag = flag;break; /进行华氏摄氏温度的转换设置 case 0xfb: case 0xf7: default:break; while(KData != 0xff) KData = P0; Key_ce = 1; if(flag = 0&flag2 = 0) P0 = units0; /显示单位C else if(flag = 1&flag2 = 0) P0 = units1; /显示单位F else if(flag2 = 1) P0 = Tab_Segran
14、ge3%10; /显示温度上限的个位 Seg_ce = 1; Seg_ce = 0; numt0 = 0;break; default:numt0 = 0;break; TR0 = 1; /打开T0 /*/* 定时器T1中断 */*/void T1_timer() interrupt 3 TR1 = 0; /关闭T0 TH1 = (65536-50000)/256; TL1 = (65536-50000)%256; if(numt1 = 25) getdata(); /采集数据 numt1 = 0; else numt1+; TR1 = 1; /打开T0#ifndef _DHT11_h_#de
15、fine _DHT11_h_#include /*/* 引 脚 定 义 */*/sbit DHT_bus = P20 ; /DHT11数据传输口sbit Key_ce=P13; /按键输出使能sbit Seg_ce=P10; /段选位sbit Dig_ce=P11; /位选位sbit bee = P21; /蜂鸣器控制口/*/* 函 数 声 明 */*/bit start_DHT11(void); /开始void read_DHT11(void); /读取void delay_20us(void); /20us延时void delay_ms(unsigned char m); /N ms延时b
16、it check_sum(void); /和校验/*/* 宏 定 义 */*/#define HIGH 1#define LOW 0/*/* 变 量 定 义 */*/#define DHT_timeover 5 /高电平维持时间,用于识别“数据0”和“数据1”bit flag2 = 0; /设置调节上下限模式转换标志bit flag=0; /设置摄氏和华氏温度模式转换标志/*/* 结 构 体 */*/struct DHT_data unsigned char DH_H; /湿度整数 unsigned char DH_L; /湿度小数 unsigned char T_H; /温度整数 unsig
17、ned char T_L; /温度小数 unsigned char Checksum; /校验和DHT_data;#endif#include DHT11.h#include /*/* 开始 DHT11 温湿度计 */* 输入:无 */* 输出:应答标志 0:应答失败 1:应答成功 */*/bit start_DHT11(void) bit DHT_start; DHT_start = 0; DHT_bus = HIGH; DHT_bus = LOW; /拉低18ms以上 delay_ms(18); TR0 = 0; DHT_bus = HIGH; delay_20us(); delay_20
18、us(); /拉高2040us while(!DHT_bus) DHT_start = 1; /DHT应答,DHT拉低80us后拉高80us,然后开始传输数据 /数据(40bit)=8bit湿度整数+8bit湿度小数+8bit温度整数+8bit 温度小数+8bit校验和 while(DHT_bus); return(DHT_start); /应答成功返回1/*/* 读取 DHT11 温湿度计 */* 读取结果存在DHT_data结构体内 */* 输入:无 输出:无 */*/void read_DHT11(void) unsigned char m,n,timer_dht; unsigned c
19、har *p; p=&DHT_data.DH_H; /数据放在DHT_date的结构体中 for(m=0;m5;m+) for(n=0;nDHT_timeover) *p=1; *p|=0x01; else *p=1; *p&=0xfe; p+; TR0 = 1;/*/* 20us 精 确 延 时 */* 51用在12Mhz晶振下 */* 调用函数使用LCALL和RET指令,共花费4个周期 */* 因此只有16个NOP */*/void delay_20us(void) _nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ (); _nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ (); _nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ (); _nop_ ();/*/* N ms 延 时 */* while()额外占用约5周期 */* 因此内层while(40-)20us 大约1ms */* Nms延时函数(未测试) */*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1