一、實(shí)驗(yàn)內(nèi)容 用一個(gè)DHT11溫度/濕度傳感器與單片機(jī)相連,測(cè)量環(huán)境溫度與濕度,并在LCD1602顯示溫度、濕度值,確保調(diào)節(jié)DHT11的溫度與濕度值時(shí),其值會(huì)相應(yīng)變化。
如:T=43°C H=75%
DHT11中一次完整的數(shù)據(jù)為40 bit,即“8 bit濕度整數(shù)”+“8 bit濕度小數(shù)”+“8 bit溫度整數(shù)”+“8 bit溫度小數(shù)”+“8 bit校驗(yàn)和”,從高位開(kāi)始輸出。
DHT11的測(cè)量范圍:溫度0~50 °C,精度為±2 °C,濕度20%~90%,精度為±5%。在實(shí)際應(yīng)用中,溫度、濕度一般取整數(shù)部分就可以了。
二、軟件 1、Keil μVision5 2、Proteus 8.12
三、Proteus 虛擬仿真 (1)在Proteus環(huán)境中完成電路圖的設(shè)計(jì)文件。 (2)在Keil μVision下編寫(xiě)C51程序,經(jīng)過(guò)調(diào)試、編譯,最終生成“.hex”可執(zhí)行文件。 (3)在Proteus中,將“.hex”文件加載到虛擬單片機(jī)中。 (4)在Proteus中,進(jìn)行軟硬件調(diào)試仿真。
具體步驟: 1.在Proteus中創(chuàng)建項(xiàng)目 2.設(shè)計(jì)原理圖 3.源代碼編輯(在Keil C51完成) 4.系統(tǒng)仿真調(diào)試 5.電路與源代碼聯(lián)
Proteus中用到的元件:AT89C52、DHT11、LM016L、RESPACK-8
C51單片機(jī):Proteus仿真DHT11溫度-濕度傳感器、LCD1602顯示溫度-濕度值.png (138.32 KB, 下載次數(shù): 41)
下載附件
C51單片機(jī):Proteus仿真DHT11溫度/濕度傳感器、LCD1602顯示溫度/濕度值
2023-5-28 14:58 上傳
四、Keil C51編程
Keil μVision5創(chuàng)建項(xiàng)目(Project)步驟: 1.建立項(xiàng)目文件 2.選擇單片機(jī)型號(hào) 3.添加源程序文件到項(xiàng)目中 4.設(shè)置項(xiàng)目配置選項(xiàng) 5.編譯項(xiàng)目,生成hex文件
C51代碼:
- #include <reg52.h>
- #define uchar unsigned char
- sbit rs = P2 ^ 0;
- sbit rw = P2 ^ 1;
- sbit en = P2 ^ 2;
- sbit io = P2 ^ 3;
- uchar t, h;
- unsigned long int temp;
- //延時(shí)約xms
- void delay(unsigned int x) {
- uchar i;
- while (x--)
- for (i = 0; i < 125; i++);
- }
- //檢查L(zhǎng)CD忙狀態(tài)
- void lcdbusy() {
- rs = 0;
- rw = 1;
- do {
- en = 0;
- P0 = 0xff;
- en = 1;
- delay(1);
- } while (P0 & 0x80);
- en = 0;
- }
- //寫(xiě)LCD命令或數(shù)據(jù)
- void w_lcd( bit cd, uchar dat) {
- lcdbusy();
- rs = cd;
- rw = 0;
- delay(1);
- P0 = dat;
- en = 1;
- delay(1);
- en = 0;
- }
- //顯示字符串
- void w_str(uchar *s) {
- while (*s)
- w_lcd(1, *s++);
- }
- //初始化LCD
- void init() {
- w_lcd(0, 0x38);
- w_lcd(0, 6);
- w_lcd(0, 0xc);
- w_lcd(0, 1);
- }
- //讀DHT11的32 位濕度和溫度
- void r_dht11() {
- uchar i;
- temp = 0;
- io = 0;
- delay(60);
- io = 1;
- while (io);
- while (~io);
- while (io);
- //讀32位數(shù)據(jù)(8位濕度整數(shù)+8位濕度小數(shù)+8位溫度整數(shù)+8位溫度小數(shù))
- for (i = 0; i < 32; i++) {
- while (~io);
- TL0 = 0;
- TR0 = 1;
- while (io);
- TR0 = 0;
- if (TL0 > 40)
- temp = (temp << 1) | 1; //讀出的數(shù)據(jù)為1
- else
- temp = temp << 1; //讀出的數(shù)據(jù)為0
- }
- }
- void main() {
- delay(500);//延時(shí)使DHT11 工作穩(wěn)定
- init();
- w_lcd(0, 0x85);
- w_str("T=");
- w_lcd(0, 0xC5);
- w_str("H=");
- TMOD = 1; //定時(shí)器T0方式1,根據(jù)定時(shí)時(shí)間判斷從DHT11讀出的是0或1
- TH0 = 0;
- while (1) {
- r_dht11();//讀溫度和濕度
- t = temp >> 8; //取溫度的整數(shù)值
- h = temp >> 24; //取濕度的整數(shù)值
- w_lcd(0, 0x87);
- w_lcd(1, t / 10 | 0x30); //顯示溫度
- w_lcd(1, t % 10 | 0x30);
- w_lcd(1, 0xdf);
- w_lcd(1, 'C');
- w_lcd(0, 0xc7);
- w_lcd(1, h / 10 | 0x30); //顯示濕度
- w_lcd(1, h % 10 | 0x30);
- w_lcd(1, '%');
- delay(200);//實(shí)物可能需要更長(zhǎng)的延時(shí)時(shí)間
- }
- }
復(fù)制代碼
五、實(shí)驗(yàn)結(jié)果
C51單片機(jī):Proteus仿真DHT11溫度-濕度傳感器、LCD1602顯示溫度-濕度值2.png (137.97 KB, 下載次數(shù): 59)
下載附件
C51單片機(jī):Proteus仿真DHT11溫度/濕度傳感器、LCD1602顯示溫度/濕度值
2023-5-28 14:59 上傳
|