温湿度传感器、光照传感器、空气质量传感器
单片机源程序如下:
- #include "stc15.h"
- #include "string.h"
- #include "i2c.h"
- #include "stdio.h"
- #include "uart.h"
- #include "normal.h"
- #include "string.h"
- #include <stdio.h>
- #include <intrins.h>
- unsigned char buf[40]={0};
- #define uchar unsigned char
- #define uint unsigned int
- uint hum,temp;
- uchar hum_h,hum_l,temp_h,temp_l,check;
- uchar bai,shi,ge,bail,shil,gel;
- char i=0;
-
- sbit dht=P1^3;
- sbit in1=P2^1;
- sbit in2=P2^2;
- sbit in3=P2^3;
- sbit in4=P2^4;
- sbit zongxian=P2^5;
- sbit fenxian=P2^6;
- void delay(unsigned int time)
- {
- unsigned char a,b;
- for(;time>0;time--)
- for(b=4;b>0;b--)
- for(a=113;a>0;a--);
- }
- void delay2(uint t)
- {
- unsigned char i;
- for(t;t>0;t--)
- {
- _nop_();
- i = 25;
- while (--i);
- }
- }
- //void delay2(uint i)
- //{
- // while(i--);
- //}
- void delay1(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void star() //开始接受信号
- {
- dht=0;
- delay2(5);
- dht=1;
- delay2(4);
- while(!dht);
- while(dht);
- }
- uchar read_byte() //读取接受的信号
- {
- uchar n,byte=0,dat;
- for(n=0;n<8;n++)
- {
- while(!dht);
- delay2(4);
- dat=0;
- if(dht)
- dat=1;
- while(dht);
- byte<<=1;
- byte|=dat;
-
- }
- return byte;
- }
- void read_hum_temp()
- {
- star();
- hum_h=read_byte();
- hum_l=read_byte();
- temp_h=read_byte();
- temp_l=read_byte();
- check=read_byte();
- while(!dht);
- dht=1;
- }
- void shujvzhuanhua() //数据转化
- {
- uchar a;
- a=hum_h+hum_l+temp_h+temp_l;
- if(a==check)
- {
- hum=temp=0;
- hum=((hum|hum_h)<<8)|hum_l;
- temp=((temp|temp_h)<<8)|temp_l ;
- bai=temp/100+0x30;
- shi=temp%100/10+0x30;
- ge=temp%10+0x30;
- bail=hum/100+0x30;
- shil=hum%100/10+0x30;
- gel=hum%10+0x30;
- }
- else
- bai=shi=ge=bail=shil=gel=0;
- }
- void chuankou2() interrupt 8
- {
- R2I_RST();
- T2I_RST();
- i=S2BUF;
- if(i=='s')
- {
- P53=0;
-
-
- }
- if(i=='j')
- {
- P52=0;
- }
-
- if(i=='g')
- {
- zongxian=0;
- delay_ms(1);
- fenxian=0;
-
- }
- if(i=='m')
- {
- zongxian=1;
- fenxian=1; //检测下降沿
-
- }
- }
- //void delay1(unsigned int x)
- //{
- // while(x--);
- //}
- void main()
- {
- int i;
- unsigned char raw_data[13]={0};
- uint32_t Lux=0;
- uint16_t data_16[2]={0};
- CLK_DIV=0x00;
- P1M0=0x00;
- P1M1=0x00;
- P2M0=0xff;
- P2M1=0x00;//驱动电机模块需要大电流,推挽输出
- P3M0=0x00;
- P3M1=0x00;
- P5M0=0xff;
- P5M1=0x00;
- P53=1;
- P52=1;
- uart1_int();
- uart2_int();
- IE2=0x00;
- SCL=1;
- SDA=1;
- EA_ON();
- ES_OFF();
- ES2_OFF();
- zongxian=1;
- fenxian=1;
- while(1)
- {
-
- if(Single_ReadI2C(0xb6,0x00,raw_data,4)) //光照
- {
- data_16[0]=(((uint16_t)raw_data[0])<<8)|raw_data[1];
- data_16[1]=(((uint16_t)raw_data[2])<<8)|raw_data[3];
- Lux=(((uint32_t)data_16[0])<<16)|data_16[1];
- }
- delay_ms(1100);
-
- delay_ms(1);
- read_hum_temp();
- shujvzhuanhua();
- ES2_OFF();
- P53=1;
- P52=1;
- printf("t3.txt=\"%f\"\xff\xff\xff",hum/10.0);
- printf("t1.txt=\"%f\"\xff\xff\xff",temp/10.0);
- printf("t5.txt=\"%f\"\xff\xff\xff",Lux/100.0);
- printf("add 1,0,%f\xff\xff\xff",hum/10.0);
- printf("add 1,1,%f\xff\xff\xff",temp/10.0);
- printf("add 1,3,%f\xff\xff\xff",Lux/100.0);
- delay_ms(1);
- sprintf(buf,"*%f~?%f!+%f-",Lux/100.0,temp/10.0,hum/10.0);//转化成字符
- delay_ms(1);
-
- for(i=0;i<strlen(buf);i++)
- {
- delay_ms(1);
- uart2_sendata(buf[i]);
- }
- ES2_ON();
- delay_ms(1);
- memcpy(buf,"\0",40);//数组清零
- delay_ms(1);
-
- }
- }
复制代码
所有资料51hei提供下载:
智能家居.zip
(76.06 KB, 下载次数: 52)
|