|
單片機(jī)源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- #define uint unsigned int
- #define uchar unsigned char
- sbit io=P1^0; //dht11data 端接單片機(jī)的 P1^0 口//
- sbit rw=P0^6; //一下三行是設(shè)置 lcd1602 的使能端 //
- sbit rs=P0^7;
- sbit ep=P0^5;
- typedef bit BOOL; //此聲明一個(gè)布爾型變量即真或假 //
- uchar data_byte;
- uchar RH,RL,TH,TL;
- //*************** 延時(shí)函數(shù) *************************************
- void delay(uchar ms) // 延時(shí)模塊 //
- {
- uchar i;
- while(ms--)
- for(i=0;i<100;i++);
- }
- void delay1() //一個(gè)for循環(huán)大概需要8個(gè)多機(jī)器周期一個(gè)機(jī)器周期為1us
- { //晶振為 12MHz 也就是說(shuō)本函數(shù)延時(shí)8us多
- uchar i; // 此延時(shí)函數(shù)必須德稍微精確一點(diǎn)
- for(i=0;i<1;i++);
- }
- //***************************************************************
- //lcd 模塊 //
- BOOL lcd_bz()// 測(cè)試 lcd 忙碌狀態(tài) 返回值為布爾型數(shù)值 真或假 '1'.'0'
- {
- BOOL result;
- rs=0; // 讀忙信號(hào)
- rw=1;
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- result = (BOOL)(P2&0x80);
- ep=0;
- return result;
- }
- void write_cmd(uchar cmd)// 寫指令 //
- {
- while (lcd_bz());
- rs=0;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P2=cmd ;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- void write_addr(uchar addr)// 寫地址 //
- {
- write_cmd(addr|0x80);//LCD第一行的首地址為0x80第二行的首地址為0x80+0x40=0xc0
- }
- void write_byte(uchar dat) // 寫字節(jié) //
- {
- while (lcd_bz());
- rs=1;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P2=dat ;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- void lcd_init() //lcd 初始化 //
- {
- write_cmd(0x38);// 設(shè)置 LCD 兩行顯示 一個(gè)數(shù)據(jù)由 5*7 點(diǎn)陣表示 ,數(shù)據(jù)由 8 跟線傳輸
- delay(1);
- write_cmd(0x0c);// 清除屏幕顯示
- delay(1);
- write_cmd(0x06);// 設(shè)定輸入方式 增量不移位
- delay(1);
- write_cmd(0x01);// 開(kāi)整體顯示 關(guān)光標(biāo) 不閃爍
- delay(1);
- }
- void display(uchar addr,uchar q)// 在某一地址上顯示內(nèi)容 adder 表示的是地址偏移量 q 表 示顯示的字符或數(shù)字 //
- {
- delay(10);
- write_addr(addr);
- write_byte(q);
- delay(1);// 修改此時(shí)間 可以改變 LCD 上數(shù)值跳變的數(shù)度
- }
- //**************************dht11 測(cè)試某塊 *************************************//
- void start()// 開(kāi)始信號(hào)
- {
- io=1;
- delay1();
- io=0;
- delay(25);// 主機(jī)把總線拉低必須大于 18ms 保證 DHT11 能檢測(cè)到起始信號(hào)
- io=1; //發(fā)送開(kāi)始信號(hào)結(jié)束后 拉高電平延時(shí) 20-40us
- delay1();// 以下三個(gè)延時(shí)函數(shù)差不多為 24us 符合要求
- delay1();
- delay1();
- }
- uchar receive_byte()//接收一個(gè)字節(jié) //
- {
- uchar i,temp;
- for(i=0;i<8;i++)// 接收 8bit 的數(shù)據(jù)
- {
- while(!io);// 等待 50us的低電平開(kāi)始信號(hào)結(jié)束
- delay1();//開(kāi)始信號(hào)結(jié)束之后 延時(shí) 26us-28us 以下三個(gè)延時(shí)函數(shù)
- delay1();
- delay1();
- temp=0;//時(shí)間為 26us-28us 表示接收的為數(shù)據(jù) '0'
- if(io==1)
- temp=1; //如果 26us-28us之后 還為高電平 則表示接收的數(shù)據(jù)為 '1'
- while(io);// 等待數(shù)據(jù)信號(hào)高電平 '0'為 26us-28us '1'為 70us
- data_byte<<=1;// 接收的數(shù)據(jù)為高位在前 右移
- data_byte|=temp;
- }
- return data_byte;
- }
- void receive()// 接收數(shù)據(jù) //
- {
- uchar T_H,T_L,R_H,R_L,check,num_check,i;
- start();// 開(kāi)始信號(hào) //
- io=1; //主機(jī)設(shè)為輸入 判斷從機(jī) DHT11 響應(yīng)信號(hào)
- if(!io)// 判斷從機(jī)是否有低電平響應(yīng)信號(hào) //
- {
- while(!io);// 判斷從機(jī)發(fā)出 80us 的低電平響應(yīng)信號(hào)是否結(jié)束 //
- while(io);// 判斷從機(jī)發(fā)出 80us 的高電平是否結(jié)束 如結(jié)束則主機(jī)進(jìn)入數(shù)據(jù)接收狀態(tài)
- R_H=receive_byte();// 濕度高位
- R_L=receive_byte();// 濕度低位
- T_H=receive_byte();// 溫度高位
- T_L=receive_byte();// 溫度低位
- check=receive_byte();// 校驗(yàn)位
- io=0; // 當(dāng)最后一 bit 數(shù)據(jù)接完畢后 從機(jī)拉低電平 50us//
- for(i=0;i<7;i++)// 差不多 50us 的延時(shí)
- delay1();
- io=1;// 總線由上拉電阻拉高 進(jìn)入空閑狀態(tài)
- num_check=R_H+R_L+T_H+T_L;
- if(num_check==check)// 判斷讀到的四個(gè)數(shù)據(jù)之和是否與校驗(yàn)位相同
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- check=num_check;
- }
- }
- }
- //*****************************************************************************
- void main()// 主函數(shù)模塊 //
- {
- lcd_init();// 初始化 LCD
- while(1)
- {
- receive();// 接收數(shù)據(jù)
- display(0x00,'R');//LCD 的第一行顯示
- display(0x01,':');
- display(0x02,RH/10+0x30); //0x30 表示 帶字庫(kù)的 LCD1602 中 0x30 的位置放有數(shù)字 0 RH/10+0x30 即表示濕度的十位數(shù)字在字庫(kù) RH/10+0x30 的位置處放著
- display(0x03,RH%10+0x30);
- display(0X04,'%');
- display(0x40,'T');//LCD 的第二行顯示
- display(0x41,':');
- display(0x42,TH/10+0x30);
- display(0x43,TH%10+0x30);
- display(0x44,0xdf);// 以下兩個(gè)是溫度單位的處理
- display(0x45,0x43);
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
溫濕度傳感器.rar
(23.79 KB, 下載次數(shù): 100)
2019-6-14 09:13 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|