[51]dht11溫溼模塊
/*說明:這個程序需要把data連到P1^0端,然後只有當L8亮起時校驗通過,數據準確*/
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint led[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e }; //p0端0到f,0x7f爲點
uint ledx[] = { 0x00,0x0e,0x0d,0x0b,0x07,0xff }; //p2端都亮,到都滅
sbit echo = P1 ^ 0;//定義data端口
unsigned char hum1 = 0, hum2 = 0, tem1 = 0, tem2 = 0;//分別對應溼度整數、小數和溫度整數、小數。
uint t1a, t1b; //對應數碼管位數
uint t2c, t2d; //對應數碼管位數
uint h1a, h1b; //對應數碼管位數
uint h2c, h2d; //對應數碼管位數
unsigned char check;
sbit checkled = P1 ^ 7; //校驗通過則亮燈
sbit k1 = P3 ^ 4; //顯示溼度
sbit k2 = P3 ^ 5; //顯示溫度
uint displaytem = 1; //顯示模式,默認顯示溫度
uint i = 0;
void delay(unsigned int x) //延時 x ms
{
unsigned int i, j;
for (i = x; i > 0; i--)
for (j = 110; j > 0; j--);
}
void delay_10()//延時10us
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void start()
{
tem1 = 0;
tem2 = 0;
hum1 = 0;
hum2 = 0;
echo = 0;//再將DATA輸出爲低電平,持續時間大於18ms,此時DHT檢測到後從低功耗模式->高速模式
delay(50);
echo = 1;//再拉高
while (echo == 1);
_nop_();
while (echo == 0); //一個應答
_nop_();
while (echo == 1); //標誌即將傳輸數據
_nop_();
}
void read()
{
/*以下是溼度整數部分的數據讀入*/
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
hum1 = 1;
while (echo == 1);
}
else if (echo == 0)
{
hum1 = 0;
}
for (i = 2; i <= 8; i++)
{
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
hum1 = hum1 << 1;
hum1 += 1;
while (echo == 1);
continue;
}
else if (echo == 0)
{
hum1 = hum1 << 1;
continue;
}
}
/*以下是溼度小數部分的數據讀入*/
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
hum2 = 1;
while (echo == 1);
}
else if (echo == 0)
{
hum2 = 0;
}
for (i = 2; i <= 8; i++)
{
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
hum2 = hum2 << 1;
hum2 += 1;
while (echo == 1);
continue;
}
else if (echo == 0)
{
hum2 = hum2 << 1;
continue;
}
}
/*以下是溫度整數部分的數據讀入*/
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
tem1 = 1;
while (echo == 1);
}
else if (echo == 0)
{
tem1 = 0;
}
for (i = 2; i <= 8; i++)
{
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
tem1 = tem1 << 1;
tem1 += 1;
while (echo == 1);
continue;
}
else if (echo == 0)
{
tem1 = tem1 << 1;
continue;
}
}
/*以下是溫度小數部分的數據讀入*/
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
tem2 = 1;
while (echo == 1);
}
else if (echo == 0)
{
tem2 = 0;
}
for (i = 2; i <= 8; i++)
{
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
tem2 = tem2 << 1;
tem2 += 1;
while (echo == 1);
continue;
}
else if (echo == 0)
{
tem2 = tem2 << 1;
continue;
}
}
/*以下是校驗部分的數據讀入*/
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
check = 1;
while (echo == 1);
}
else if (echo == 0)
{
check = 0;
}
for (i = 2; i <= 8; i++)
{
if (echo == 0)while (echo == 0);
delay_10();
delay_10();
delay_10();
delay_10();
if (echo == 1)
{
check = check << 1;
check += 1;
while (echo == 1);
continue;
}
else if (echo == 0)
{
check = check << 1;
continue;
}
}
if (check == hum1 + hum2 + tem1 + tem2)checkled = 0;
}
void main()
{
echo = 1;//設置DATA引腳爲輸出狀態並輸出高電平
delay(1500);//開機等待1.5S的時間穩定
while (1)
{
checkled=1;//校驗燈復位
start();
read();
/*溫度計算部分,小數位無用*/
t1a = tem1 / 10;
t1b = tem1 - t1a * 10;
t2c = tem2 / 100;
t2d = tem2 / 10 - t2c * 10;
/*溼度計算部分,小數位無用*/
h1a = hum1 / 10;
h1b = hum1 - h1a * 10;
h2c = hum2 / 100;
h2d = hum2 / 10 - h2c * 10;
if (displaytem == 1)
while (1)
{
P0 = led[t1a];
P2 = ledx[1];
delay(5);
P0 = led[t1b];
P2 = ledx[2];
delay(5);
P0 = 0x9C;
P2 = ledx[3];
delay(5);
P0 = 0xC6;
P2 = ledx[4];
delay(5);
if (k1 == 0) { P0 = 0xff; displaytem = 0; break; }
else if (k2 == 0) { P0 = 0xff; displaytem = 1; break; }
}
else if (displaytem == 0)
while (1)
{
P0 = led[h1a];
P2 = ledx[1];
delay(5);
P0 = led[h1b];
P2 = ledx[2];
delay(5);
P0 = 0x9C;
P2 = ledx[3];
delay(5);
P0 = 0x8E;
P2 = ledx[4];
delay(5);
if (k2 == 0) { P0 = 0xff; displaytem = 1; break; }
else if (k1 == 0) { P0 = 0xff; displaytem = 0; break; }
}
delay(1000);//等待18ms
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.