|
- #include <STC12C5A60S2.H>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]="TEMP:";
- uchar code table1[]="HUMI:";
- //請(qǐng)根據(jù)自己的dht11接的IO口來改動(dòng)位定義
- sbit dht11 = P3^2;
- sbit rw=P2^5;
- //防止在與硬件通信時(shí)發(fā)生死循環(huán)的計(jì)數(shù)范圍
- #define NUMBER 2000
- #define SIZE 5
- unsigned char status;
- //存放五字節(jié)數(shù)據(jù)的數(shù)組
- unsigned char value_array[SIZE];
- /*可在其他的文件引用溫濕度值,實(shí)際是溫度的整數(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--;
- }
- /*讀一個(gè)字節(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ù)前會(huì)有一個(gè)50us的低電平時(shí)間.等待50us低電平結(jié)束
- while(dht11 == 0);
- //26-28us的高電平表示該位是0,為70us高電平表該位1
- Delay_10us();
- Delay_10us();
- //延時(shí)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ā)送開始信號(hào)
- Delay_1ms(20); //需大于18毫秒
- dht11 = 1; //釋放數(shù)據(jù)線,用于檢測低電平的應(yīng)答信號(hào)
- //延時(shí)20-40us,等待一段時(shí)間后檢測應(yīng)答信號(hào),應(yīng)答信號(hào)是從機(jī)拉低數(shù)據(jù)線80us
- Delay_10us();
- Delay_10us();
- if(dht11 != 0) //檢測應(yīng)答信號(hào),應(yīng)答信號(hào)是低電平
- {
- //沒應(yīng)答信號(hào)
- return 0;
- }
- else
- {
- //有應(yīng)答信號(hào)
- while(dht11 == 0 ); //等待應(yīng)答信號(hào)結(jié)束
- count = 0;
- dht11 = 1;//釋放數(shù)據(jù)線
- //應(yīng)答信號(hào)后會(huì)有一個(gè)80us的高電平等待高電平結(jié)束
- while(dht11 != 0);
- //讀出濕.溫度值
- for(i = 0; i < SIZE; i++)
- {
- value_array[i] = ReadValue();
- if(status == 0)//調(diào)用ReadValue()讀數(shù)據(jù)出錯(cuò)會(huì)設(shè)定status為ERROR
- {
- dht11 = 1;
- return 0;
- }
- //讀出的最后一個(gè)值是校驗(yàn)值不需加上去
- if(i != SIZE - 1)
- {
- //讀出的五字節(jié)數(shù)據(jù)中的前四字節(jié)數(shù)據(jù)和等于第五字節(jié)數(shù)據(jù)表示成功
- check_value += value_array[i];
- }
- }//end for
- //在沒用發(fā)生函數(shù)調(diào)用失敗時(shí)進(jìn)行校驗(yà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
- {
- //校驗(yàn)數(shù)據(jù)出錯(cuò)
- 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ù)制代碼 |
|