这个程序是本人写的,可在1602显示,烟雾与温湿度显示程序。经测试,可用
单片机源程序如下:
- #include"main.h"
- sbit DHT22_DATA = P2^0;
- unsigned char a,b,c,d,check=1; //8bit温度整数数据+8bit温度小数数据+8bit湿度整数数+8bit湿度小数数据+8bit校验和
- unsigned int R,T;
-
- unsigned char n;
- void delay500us(void) //误差 -0.054253472222us
- {
- unsigned char a,b;
- for(b=251;b>0;b--)
- for(a=4;a>0;a--);
- _nop_(); //if Keil,require use intrins.h
- }
- void delay20us(void) //误差 -0.016637731481us
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=52;a>0;a--);
- _nop_(); //if Keil,require use intrins.h
- }
- char Read_Byte()
- {
- char temp,tmp;
- unsigned char i;
- for(i=0;i<8;i++)
- {
- // delay20us();
- n = 2;
- while((!DHT22_DATA)&&n++); //等待50us间隔低电平响应结束
- if(n == 1) return 0;
- delay20us();delay20us(); //26-28us高电平为‘0’,70us高电平为‘1’
- tmp = 0;
- if(DHT22_DATA) tmp = 1;
- n = 2;
- while(DHT22_DATA&&n++); //若位为1,等待高电平结束
- if(n == 1) return 0;
- temp <<= 1;
- temp |= tmp;
- }
- return temp;
- }
- unsigned char DHT22_Read()
- {
- DHT22_DATA = 0;
- delay500us(); //拉低500us
- DHT22_DATA = 1;
- delay20us();delay20us(); //延时20-40us等待响应信号
- DHT22_DATA = 1;
- if(!DHT22_DATA)
- {
- n = 2;
- while((!DHT22_DATA)&&n++); //等待80us低电平响应结束
- if(n == 1) return 0;
- delay20us();delay20us();delay20us();
- n = 2;
- while(DHT22_DATA&&n++); //等待80s高电平等待结束
- if(n == 1) return 0;
- a = Read_Byte();
- b = Read_Byte();
- c = Read_Byte();
- d = Read_Byte();
- check = Read_Byte();
- DHT22_DATA = 1;
- if(check != a+b+c+d) return 0;
- }
- R = a*256+b;
- T = c*256+d;
- return 1;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
烟雾温湿度可用.zip
(60.03 KB, 下载次数: 181)
|