標(biāo)題:
STC12C5A60S2+DH11溫濕度傳感器 11.0592晶振 延時是用示波器掐的
[打印本頁]
作者:
⑨掌門
時間:
2019-1-14 10:20
標(biāo)題:
STC12C5A60S2+DH11溫濕度傳感器 11.0592晶振 延時是用示波器掐的
#include <STC12C5A60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="TEMP:";
uchar code table1[]="HUMI:";
//請根據(jù)自己的dht11接的IO口來改動位定義
sbit dht11 = P3^2;
sbit rw=P2^5;
//防止在與硬件通信時發(fā)生死循環(huán)的計數(shù)范圍
#define NUMBER 2000
#define SIZE 5
unsigned char status;
//存放五字節(jié)數(shù)據(jù)的數(shù)組
unsigned char value_array[SIZE];
/*可在其他的文件引用溫濕度值,實際是溫度的整數(shù)的10倍
如dht11讀回的溫度是26,則temp_value = 260, 濕度同理*/
int temp=12,humi=12;
sbit lcden=P2^7;
sbit lcdrs=P2^6;
uchar num,a;
uint c,b,d,e;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=500;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
void write_string(uchar hang,uchar add,uchar *p)
{
if(hang==1)
write_com(0x80+add);
else
write_com(0x80+0x40+add);
while(1)
{
if(*p == '\0') break;
write_data(*p);
p++;
}
}
void init_1602()
{
rw=0;
lcden=0;
dht11=1;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void Delay_1ms(unsigned int ms)
{
unsigned int x, y;
for(x = ms; x > 0; x--)
{
for(y = 1000; y > 0; y--);
}
}
void Delay_10us(void)
{
unsigned char i;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--; i--; i--;
i--; i--;
i--; i--; i--; i--;
i--; i--;
}
/*讀一個字節(jié)的數(shù)據(jù)*/
unsigned char ReadValue(void)
{
unsigned char count, value = 0, i;
status = 1; //設(shè)定標(biāo)志為正常狀態(tài)
for(i = 8; i > 0; i--)
{
//高位在先
value <<= 1;
count = 0;
//每一位數(shù)據(jù)前會有一個50us的低電平時間.等待50us低電平結(jié)束
while(dht11 == 0);
//26-28us的高電平表示該位是0,為70us高電平表該位1
Delay_10us();
Delay_10us();
//延時30us后檢測數(shù)據(jù)線是否還是高電平
if(dht11 != 0)
{
//進(jìn)入這里表示該位是1
value++;
//等待剩余(約40us)的高電平結(jié)束
while(dht11 != 0)
{
dht11 = 1;
}
}
}
return (value);
}
//讀一次的數(shù)據(jù),共五字節(jié)
uchar rdata()
{
uchar i = 0, check_value = 0,count = 0;
Delay_1ms(2);
dht11 = 0; //拉低數(shù)據(jù)線大于18ms發(fā)送開始信號
Delay_1ms(20); //需大于18毫秒
dht11 = 1; //釋放數(shù)據(jù)線,用于檢測低電平的應(yīng)答信號
//延時20-40us,等待一段時間后檢測應(yīng)答信號,應(yīng)答信號是從機拉低數(shù)據(jù)線80us
Delay_10us();
Delay_10us();
if(dht11 != 0) //檢測應(yīng)答信號,應(yīng)答信號是低電平
{
//沒應(yīng)答信號
return 0;
}
else
{
//有應(yīng)答信號
while(dht11 == 0 ); //等待應(yīng)答信號結(jié)束
count = 0;
dht11 = 1;//釋放數(shù)據(jù)線
//應(yīng)答信號后會有一個80us的高電平等待高電平結(jié)束
while(dht11 != 0);
//讀出濕.溫度值
for(i = 0; i < SIZE; i++)
{
value_array[i] = ReadValue();
if(status == 0)//調(diào)用ReadValue()讀數(shù)據(jù)出錯會設(shè)定status為ERROR
{
dht11 = 1;
return 0;
}
//讀出的最后一個值是校驗值不需加上去
if(i != SIZE - 1)
{
//讀出的五字節(jié)數(shù)據(jù)中的前四字節(jié)數(shù)據(jù)和等于第五字節(jié)數(shù)據(jù)表示成功
check_value += value_array[i];
}
}//end for
//在沒用發(fā)生函數(shù)調(diào)用失敗時進(jìn)行校驗
if(check_value == value_array[SIZE - 1])
{
//將溫濕度擴(kuò)大10倍方便分離出每一位
humi = value_array[0] * 10;
temp = value_array[2] * 10;
dht11 = 1;
return 0; //正確的讀出dht11輸出的數(shù)據(jù)
}
else
{
//校驗數(shù)據(jù)出錯
return 0;
}
}
}
void main()
{
uchar x;
x=1;
init_1602();
for(num=0;num<5;num++)
{
write_data(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<5;num++)
{
write_data(table1[num]);
delay(5);
}
while(1)
{
rdata();
write_com(0x80+0x05);
write_data(temp/100+'0');
delay(x);
write_data(temp%100/10+'0');
delay(x);
write_com(0x80+0x45);
write_data(humi/100+'0');
delay(x);
write_data(humi%100/10+'0');
delay(x);
write_string(1,8,"com");
// write_com(0x80+0x09);
// delay(x);
// write_data(c/10000+'0');
// delay(x);
// write_data(c%10000/1000+'0');
// delay(x);
// write_data(c%10000%1000/100+'0');
}
}
復(fù)制代碼
作者:
admin
時間:
2019-1-14 15:13
用示波器掐的?
作者:
⑨掌門
時間:
2019-1-18 16:50
admin 發(fā)表于 2019-1-14 15:13
用示波器掐的?
嗯 對時間要求有點小嚴(yán)格 所以用示波器一點一點試的
作者:
Zonaa
時間:
2021-11-28 15:55
我的怎么不顯示
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1