|
附件文件為c,h的文本格式。
0.png (6.57 KB, 下載次數(shù): 51)
下載附件
2017-12-3 22:12 上傳
單片機(jī)源程序如下:
- /************************************************
- 項(xiàng) 目:溫、濕度測(cè)量程序
- 單片機(jī):STC12C5A60S2
- 傳感器:DHT11
- 日 期:04.09
- ************************************************/
- #include <stc12c5a60s2.h>
- #include "delay.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit io = P2^0; //定義數(shù)據(jù)端口
- uchar data_byte; //定義全局變量
- uchar RH,RL,TH,TL; //定義全局變量
- unsigned char code DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // 顯示段碼值0~9
- uchar TempData[5]; //存儲(chǔ)顯示值的全局變量
- /******************** DHT11測(cè)試模塊**********************/
- void start() //開(kāi)始信號(hào)
- {
- io=1;
- delayus(10);
- io=0;
- delayms(30); //10~60之間
- io=1;
- delayus(100); //關(guān)鍵時(shí)間:35-250之間
- }
- /**********************接收一個(gè)字節(jié)**********************/
- uchar receive_byte()
- {
- uchar i,temp,count;
- for(i=0;i<8;i++)
- {
- count=2;
- while((!io)&&count++)
- temp=0;
- delayus(150); //關(guān)鍵時(shí)間:10~23之間
- if(io==1)temp=1;
- count=2;
- while((io)&&count++);
- if(count==1)break;
- data_byte<<=1;
- data_byte|=temp;
- }
- return data_byte;
- }
- /************************接收數(shù)據(jù)************************/
- void receive()
- {
- uchar T_H,T_L,R_H,R_L,check,num_check;
- uchar count;
- start(); //開(kāi)始信號(hào)
- io=1;
- if(!io) //讀取DHT11響應(yīng)信號(hào)
- {
- count=2;
- while((!io)&&count++); //DHT11高電平80us是否結(jié)束
- count=2;
- while((io)&&count++);
- R_H=receive_byte();
- R_L=receive_byte();
- T_H=receive_byte();
- T_L=receive_byte();
- check=receive_byte();
-
- io=0;
- delayms(1); //拉低延時(shí)
- io=1;
- num_check=R_H+R_L+T_H+T_L;
-
- if(num_check=check)
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- check=num_check;
- }
- }
- }
- /**********************************************************************
- 顯示子函數(shù)
- **********************************************************************/
- void display()
- {
- P2=0xef;
- P0=TempData[0];
- delayms(20);
- P2=0xff;
-
- P2=0xdf;
- P0=TempData[1];
- delayms(20);
- P2=0xff;
- P2=0xbf;
- P0=TempData[2];
- delayms(20);
- P2=0xff;
- P2=0x7f;
- P0=TempData[3];
- delayms(20);
- P2=0xff;
- }
- /*******************************************************************
- 定時(shí)器初始化子函數(shù)
- *******************************************************************/
- void InitTimer(void)
- {
- TMOD |= 0x01; //定義定時(shí)器T0,工作方式1
- TH0=(65536-20000)/256;
- TL0=(65536-20000)%256; //50ms
- ET0=1;
- TR0=1;
- EA = 1; //開(kāi)總中斷
-
- }
- /***************************主函數(shù)**************************/
- void main()
- {
- P0M0=0xFF; //定義P0口為推挽輸出 1111 1111
- // P2M0=0xF0; //定義P2.4 ~ P2.7口為推挽輸出 1111 0000
- InitTimer();
- delayms(10);
-
- while(1)
- {
-
- receive();
-
- TempData[0]=DuanMa[RH/10];
- TempData[1]=DuanMa[RH%10];
-
- TempData[2]=DuanMa[TH/10];
- TempData[3]=DuanMa[TH%10];
-
- delayms(20);
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
程序文件.rar
(1.94 KB, 下載次數(shù): 159)
2017-12-3 12:58 上傳
點(diǎn)擊文件名下載附件
c;h文件 下載積分: 黑幣 -5
|
|