- #include <STC12C5A60S2.H>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]="TEMP:";
- uchar code table1[]="HUMI:";
- //请根据自己的dht11接的IO口来改动位定义
- sbit dht11 = P3^2;
- sbit rw=P2^5;
- //防止在与硬件通信时发生死循环的计数范围
- #define NUMBER 2000
- #define SIZE 5
- unsigned char status;
- //存放五字节数据的数组
- unsigned char value_array[SIZE];
- /*可在其他的文件引用温湿度值,实际是温度的整数的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--;
- }
- /*读一个字节的数据*/
- unsigned char ReadValue(void)
- {
- unsigned char count, value = 0, i;
- status = 1; //设定标志为正常状态
- for(i = 8; i > 0; i--)
- {
- //高位在先
- value <<= 1;
- count = 0;
- //每一位数据前会有一个50us的低电平时间.等待50us低电平结束
- while(dht11 == 0);
- //26-28us的高电平表示该位是0,为70us高电平表该位1
- Delay_10us();
- Delay_10us();
- //延时30us后检测数据线是否还是高电平
- if(dht11 != 0)
- {
- //进入这里表示该位是1
- value++;
- //等待剩余(约40us)的高电平结束
- while(dht11 != 0)
- {
- dht11 = 1;
- }
- }
- }
- return (value);
- }
- //读一次的数据,共五字节
- uchar rdata()
- {
- uchar i = 0, check_value = 0,count = 0;
- Delay_1ms(2);
- dht11 = 0; //拉低数据线大于18ms发送开始信号
- Delay_1ms(20); //需大于18毫秒
- dht11 = 1; //释放数据线,用于检测低电平的应答信号
- //延时20-40us,等待一段时间后检测应答信号,应答信号是从机拉低数据线80us
- Delay_10us();
- Delay_10us();
- if(dht11 != 0) //检测应答信号,应答信号是低电平
- {
- //没应答信号
- return 0;
- }
- else
- {
- //有应答信号
- while(dht11 == 0 ); //等待应答信号结束
- count = 0;
- dht11 = 1;//释放数据线
- //应答信号后会有一个80us的高电平等待高电平结束
- while(dht11 != 0);
- //读出湿.温度值
- for(i = 0; i < SIZE; i++)
- {
- value_array[i] = ReadValue();
- if(status == 0)//调用ReadValue()读数据出错会设定status为ERROR
- {
- dht11 = 1;
- return 0;
- }
- //读出的最后一个值是校验值不需加上去
- if(i != SIZE - 1)
- {
- //读出的五字节数据中的前四字节数据和等于第五字节数据表示成功
- check_value += value_array[i];
- }
- }//end for
- //在没用发生函数调用失败时进行校验
- if(check_value == value_array[SIZE - 1])
- {
- //将温湿度扩大10倍方便分离出每一位
- humi = value_array[0] * 10;
- temp = value_array[2] * 10;
- dht11 = 1;
- return 0; //正确的读出dht11输出的数据
- }
- else
- {
- //校验数据出错
- 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');
-
- }
-
- }
复制代码 |