單片機(jī)DTH11溫濕度測(cè)量仿真
關(guān)于單片機(jī)DTH11溫濕度測(cè)量仿真,過(guò)去大多無(wú)法進(jìn)行,都用的SHT11來(lái)代替,但實(shí)際上,DHT11的驅(qū)運(yùn)程序與SHT11的驅(qū)運(yùn)程序有著本質(zhì)上的區(qū)別,DHT11只需要接線(xiàn)3根線(xiàn)就可以工作,VCC、GND、DATA,工作時(shí)由單片機(jī)通過(guò)DATA線(xiàn)向DHT11發(fā)送啟動(dòng)信號(hào),DHT11接到信號(hào)后返回一個(gè)應(yīng)答信號(hào),單片機(jī)收到到應(yīng)答信號(hào)便可開(kāi)始接收溫濕度數(shù)據(jù)(8bit濕度整數(shù)+8bit濕度小數(shù)+8bit溫度整數(shù)+8bit溫度小數(shù)+8bit校驗(yàn)和),SHT11是四線(xiàn)驅(qū)動(dòng)。價(jià)格DHT11優(yōu)越于SHT11,讀取上SHT11會(huì)更加方便一些,SHT11,是四線(xiàn)驅(qū)動(dòng),直接4根插針數(shù)據(jù)時(shí)鐘電源+ - 。DHT11和SHT11是同一個(gè)系列的,只不過(guò)它的測(cè)量精度不同。后者的測(cè)量精度比較高一些!目前,兩款的溫度濕度傳器在PROTEUS中都可以進(jìn)行仿真的。下面是仿真圖。源程序及仿真。
源程序:
#include "REGX51.H"
#define LCD_DB P0
unsigned char s1[5];
unsigned char s2[5];
sbit LCD_RS=P0^7;
sbit LCD_RW=P0^6;
sbit LCD_E=P0^5;
sbit io = P1^0;
sbit moshi=P3^2;
sbit INC=P3^3;
sbit DEC=P3^4;
sbit SPK=P3^5;
#define uchar unsigned char
#define uint unsigned int
typedef unsigned charU8;
typedef unsigned intU16;
uchar count1=70,count2=35;
uchar moshicount=0;
uchar TD=0;//定時(shí)次數(shù)
U8U8FLAG;
U8U8count,U8temp;
U8U8T_data_H,U8T_data_L,U8RH_data_H,
U8RH_data_L,U8checkdata;
U8U8T_data_H_temp,U8T_data_L_temp,
U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8U8comdata;
void LCD_init(void);//初始化函數(shù)
void LCD_write_command(uchar command); //寫(xiě)指令函數(shù)
void LCD_write_data(uchar dat);
//寫(xiě)數(shù)據(jù)函數(shù)
void LCD_disp_char(uchar x,
uchar y,uchar dat);
//在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-16),y(1-2)
void delay_n40us(uint n);//延時(shí)函數(shù)
//********************************
//*******液晶初始化函數(shù)***************
void LCD_init(void)
{
LCD_write_command(0x38);
//設(shè)置 8 位格式,2 行,5x7
LCD_write_command(0x0c);
//整體顯示,關(guān)光標(biāo),不閃爍
LCD_write_command(0x06);
//設(shè)定輸入方式,增量不移位
LCD_write_command(0x03);
//清除屏幕顯示
delay_n40us(100);
}
//********************************
//*******定時(shí)器初始化函數(shù)**********
void timerinit()
{
TMOD=0x01;
TH0=-50000/256;
TL0=-50000%6;
EA=1;
ET0=1;
}
//********************************
//********寫(xiě)指令函數(shù)************
void LCD_write_command(uchar dat)
{
LCD_DB=dat;
LCD_RS=0;//指令
LCD_RW=0;//寫(xiě)入
LCD_E=1;//允許
delay_n40us(1);
LCD_E=0;
delay_n40us(1);
}
//*******************************
//********寫(xiě)數(shù)據(jù)函數(shù)*************
void LCD_write_data(uchar dat)
{
LCD_DB=dat;
LCD_RS=1;//數(shù)據(jù)
LCD_RW=0;//寫(xiě)入
LCD_E=1;//允許
delay_n40us(1);
LCD_E=0;
delay_n40us(1);
}
//********************************
//*******顯示一個(gè)字符函數(shù)*********
void LCD_disp_char(uchar x,
uchar y,uchar dat)
{
uchar address;
if(y==1)
address=0x80+x;
else
address=0xc0+x;
LCD_write_command(address);
LCD_write_data(dat);
}
//********************************
//********延時(shí)函數(shù)***************
void delay_n40us(uint n)
{
uint i;
uchar j;
for(i=n;i>0;i--)
for(j=0;j<2;j++);
}
voidDelay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void Delay(U16 j)
{
U8 i;
for(;j>0;j--)
for(i=0;i<27;i++);
}
//*******一字節(jié)數(shù)據(jù)傳送函數(shù)*********
voidCOM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!io)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(io)U8temp=1;
U8FLAG=2;
while((io)&&U8FLAG++);
//超時(shí)則跳出for循環(huán)
if(U8FLAG==1)break;
//判斷數(shù)據(jù)位是0還是1
// 如果高電平高過(guò)預(yù)定0高電平值則數(shù)據(jù)位為 1
U8comdata<<=1;
U8comdata|=U8temp;
}
}
void RH(void)
{
//主機(jī)拉低18ms
io=0;
Delay(180);
io=1;
//總線(xiàn)由上拉電阻拉高 主機(jī)延時(shí)20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//主機(jī)設(shè)為輸入判斷從機(jī)響應(yīng)信號(hào)
io=1;
//判斷從機(jī)是否有低電平響應(yīng)信號(hào)如不響應(yīng)則跳出,響應(yīng)則向下運(yùn)行
if(!io)//T !
{
U8FLAG=2;
//判斷從機(jī)是否發(fā)出 80us 的低電平響應(yīng)信號(hào)是否結(jié)束
while((!io)&&U8FLAG++);
U8FLAG=2;
//判斷從機(jī)是否發(fā)出 80us 的高電平,如發(fā)出則進(jìn)入數(shù)據(jù)接收狀態(tài)
while((io)&&U8FLAG++);
//數(shù)據(jù)接收狀態(tài)
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
io=1;
//數(shù)據(jù)校驗(yàn)
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}
//濕度整數(shù)部分
s1[0] = (char)(0X30+U8RH_data_H/10);
s1[1] = (char)(0X30+U8RH_data_H);
//濕度小數(shù)部分
s1[2] = (char)(0X30+U8RH_data_L/10);
//溫度整數(shù)部分
s2[0] = (char)(0X30+U8T_data_H/10);
s2[1] = (char)(0X30+U8T_data_H);
//溫度小數(shù)部分
s2[2] = (char)(0X30+U8T_data_L/10);
}
}
//*******************************
//液晶顯示函數(shù)
void disp()
{
LCD_disp_char(0,1,'s');
LCD_disp_char(1,1,'h');
LCD_disp_char(2,1,'i');
LCD_disp_char(3,1,'d');
LCD_disp_char(4,1,'u');
LCD_disp_char(5,1,':');
LCD_disp_char(6,1,s1[0]);
LCD_disp_char(7,1,s1[1]);
LCD_disp_char(8,1,'.');
LCD_disp_char(9,1,s1[2]);
LCD_disp_char(10,1,'%');
LCD_disp_char(11,1,'R');
LCD_disp_char(12,1,'H');
LCD_disp_char(0,2,'w');
LCD_disp_char(1,2,'e');
LCD_disp_char(2,2,'n');
LCD_disp_char(3,2,'d');
LCD_disp_char(4,2,'u');
LCD_disp_char(5,2,':');
LCD_disp_char(6,2,s2[0]);
LCD_disp_char(7,2,s2[1]);
LCD_disp_char(8,2,'.');
LCD_disp_char(9,2,s2[2]);
LCD_disp_char(10,2,0xDF);
LCD_disp_char(11,2,'C');
}
//閾值設(shè)置函數(shù)
void shezhi()
{
//初值
s1[3] = (char)(0X30+count1/10);
s1[4] = (char)(0X30+count1);
//初值
s2[3] = (char)(0X30+count2/10);
s2[4] = (char)(0X30+count2);
moshi=1;
if(moshi==0)
{
Delay_10us();
while(moshi==0);
moshicount++;
}
switch(moshicount)
{
case 1:
{
INC=1;DEC=1;
if(INC==0)
{
Delay_10us();
while(INC==0);
count1++;
}
else if(DEC==0)
{
Delay_10us();
while(DEC==0);
count1--;
}
LCD_disp_char(14,1,s1[3]);
LCD_disp_char(15,1,s1[4]);
LCD_disp_char(14,2,s2[3]);
LCD_disp_char(15,2,s2[4]);
}break;
case 2:
{
//moshicount=0;
INC=1;DEC=1;
if(INC==0)
{
Delay_10us();
while(INC==0);
count2++;
}
else if(DEC==0)
{
Delay_10us();
while(DEC==0);
count2--;
}
LCD_disp_char(14,1,s1[3]);
LCD_disp_char(15,1,s1[4]);
LCD_disp_char(14,2,s2[3]);
LCD_disp_char(15,2,s2[4]);
}break;
case 3:
{
moshicount=0;
LCD_disp_char(14,1,' ');
LCD_disp_char(15,1,' ');
LCD_disp_char(14,2,' ');
LCD_disp_char(15,2,' ');
}break;
default :break;
}
}
//蜂鳴器報(bào)警程序
void laba()
{
if((U8RH_data_H>=count1)||(U8T_data_H>=count2))
SPK=1;
else
SPK=0;
}
//定時(shí)器0中斷程序,每次定時(shí)50ms
timer0() interrupt 1
{
TD++;
if(TD>=100)
{
TH0=-50000/256;
TL0=-50000%6;
TR0=0;
TD=0;
}
TH0=-50000/256;
TL0=-50000%6;
}
//*********主函數(shù)*****************
void main(void)
{
LCD_init();
timerinit();
Delay(4);
while(1)
{
RH();
disp();
laba();
TR0=1;
while((TD>=1)&&(TD<=100))
{
shezhi();
}
}
}