#include <REGX52.H>#include <intrins.h>#include <stdio.h>#include "oled.h"#define uchar unsigned char#define uint unsigned int
sbit CS=P1^0; //adc0832引脚sbit CLK=P1^1;
sbit DIO=P1^2;
sbit CS1=P1^3; //adc0832引脚sbit CLK1=P1^4;
sbit DIO1=P1^5;
sbit Temp_data=P2^6; //DHT11sbit out1=P3^6;
sbit out2=P3^7;
sbit key1=P1^6;
sbit key2=P1^7;
sbit key3=P3^2;
sbit key4=P3^3;
sbit key5=P3^4;unsigned char rec\_dat\_lcd0\[6\];unsigned char rec\_dat\_lcd1\[6\];unsigned char rec\_dat\_lcd2\[6\];unsigned char rec\_dat\_lcd3\[6\];unsigned int rec\_dat\[4\];static uchar u,U; //定义变量static flagpeople=0,flagwd=0,flagsd=0,flaggz=0;static uchar wd,sd;static int wdyz=37,sdyz=80,gzyz=100;static int flag=0;static char Dat\_rxd;static int flag1 = 0;static int num=0;static int flag2 = 0;void DHT11\_delay\_us(unsigned char n);void DHT11\_delay\_ms(unsigned int z);void DHT11\_start();unsigned char DHT11\_rec\_byte();void DHT11\_receive();void beep_warning();void cshq();void xxpxs();void ajjc();void ckjs();void Delay(unsigned int xms){ unsigned char i, j; while(xms--)
{
i = 2;
j = 239; do
{ while (--j);
} while (--i);
}
}//延时msvoid DHT11\_delay\_ms(unsigned int z){ unsigned int i,j; for(i=z; i>0; i--) for(j=110; j>0; j--);
}//延时us --2*n+5usvoid DHT11\_delay\_us(unsigned char n){ while(--n);
}//DHT11起始信号void DHT11_start(){
Temp\_data=1; DHT11\_delay_us(10);
Temp\_data=0; DHT11\_delay_ms(50);//这个延时不能过短,18ms以上,实际在仿真当中要想读到数据延时要在延时参数要在40以上才能出数据
Temp\_data=1; DHT11\_delay\_us(30);//这个延时不能过短}//接收一个字节unsigned char DHT11\_rec_byte(){ unsigned char i,dat=0; for(i=0; i<8; i++)
{ while(!Temp\_data); DHT11\_delay_us(8);
dat <<=1; if(Temp_data==1)
{
dat +=1;
} while(Temp_data);
} return dat;
}//接收温湿度数据void DHT11\_receive(){ unsigned int R\_H,R\_L,T\_H,T\_L; unsigned char RH,RL,TH,TL,revise; DHT11\_start();
Temp\_data=1; if(Temp\_data==0)
{ while(Temp_data==0); //等待拉高
DHT11\_delay\_us(40); //拉高后延时80us
R\_H=DHT11\_rec_byte(); //接收湿度高八位
R\_L=DHT11\_rec_byte(); //接收湿度低八位
T\_H=DHT11\_rec_byte(); //接收温度高八位
T\_L=DHT11\_rec_byte(); //接收温度低八位
revise=DHT11\_rec\_byte(); //接收校正位
DHT11\_delay\_us(25); //结束
if((R\_H+R\_L+T\_H+T\_L)==revise) //校正
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
} /*数据处理,方便显示*/
rec_dat\[0\]=RH;
rec_dat\[1\]=RL;
rec_dat\[2\]=TH;
rec_dat\[3\]=TL;
}
}void dht11(){ DHT11\_delay\_ms(150); DHT11\_receive(); sprintf(rec\_dat\_lcd0,"%d",rec\_dat\[0\]); sprintf(rec\_dat\_lcd1,"%d",rec\_dat\[1\]); sprintf(rec\_dat\_lcd2,"%d",rec\_dat\[2\]); sprintf(rec\_dat\_lcd3,"%d",rec\_dat\[3\]); DHT11\_delay_ms(100);
wd = rec\_dat\[3\]*10 + rec\_dat\[2\];
sd = rec\_dat\[1\]*10 + rec\_dat\[0\];
}uchar get\_AD\_Res() //ADC0832启动读取函数{
uchar i, data1=0, data2=0;
CS=0;
CLK=0;DIO=1;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=1;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=0;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=1;\_nop\_();
for(i=0; i<8; i++)
{
CLK=1;\_nop\_();
CLK=0;\_nop\_();
data1=(data1<<1)|(uchar)DIO;
}
for(i=0; i<8; i++)
{
data2=data2|(uchar)DIO<<i;
CLK=1;\_nop\_();
CLK=0;\_nop\_();
}
CS=1;
return(data1 == data2)?data1:0;
}uchar get\_AD\_Res1() //ADC0832启动读取函数{
uchar i, data1=0, data2=0;
CS1=0;
CLK1=0;DIO1=1;\_nop\_();
CLK1=1;\_nop\_();
CLK1=0;DIO1=1;\_nop\_();
CLK1=1;\_nop\_();
CLK1=0;DIO1=0;\_nop\_();
CLK1=1;\_nop\_();
CLK1=0;DIO1=1;\_nop\_();
for(i=0; i<8; i++)
{
CLK1=1;\_nop\_();
CLK1=0;\_nop\_();
data1=(data1<<1)|(uchar)DIO1;
}
for(i=0; i<8; i++)
{
data2=data2|(uchar)DIO1<<i;
CLK1=1;\_nop\_();
CLK1=0;\_nop\_();
}
CS1=1;
return(data1 == data2)?data1:0;
}void beep_warning()//蜂鸣器警报并且电机转动{ if(U<100)
{ OLED_ShowCHinese(48,6,6);//有
OLED_ShowCHinese(64,6,7);//人
} else
{ OLED_ShowCHinese(48,6,8);//无
OLED_ShowCHinese(64,6,7);//人
}
if(flag == 0) //自动模式
{ if(flagpeople == 0) //可以判断有人
{ if(U<100) //有人
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0;
flagpeople = 1;
}
} if(U>=100)
{
flagpeople = 0;
}
if(flagwd == 0)
{ if(wd>wdyz)
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0;
flagwd = 1;
}
} if(wd<=wdyz)
{
flagwd = 0;
}
if(flagsd == 0)
{ if(sd>sdyz)
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0;
flagsd = 1;
}
} if(sd<=sdyz)
{
flagsd = 0;
}
if(flaggz == 0)
{ if(u>gzyz)
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0;
flaggz = 1;
}
} if(u<=gzyz)
{
flaggz = 0;
}
}
}void main() //主函数{
out1 = 0;
out2 = 0; OLED_Init(); //初始化OLED
OLED_Clear();
OLED_ShowCHinese(0,0,0);//温 横坐标11, 纵坐标0,第0个汉字
OLED_ShowCHinese(16,0,1);//度
OLED_ShowCHinese(0,2,4);//湿
OLED_ShowCHinese(16,2,5);//度
OLED_ShowCHinese(0,4,2);//光
OLED_ShowCHinese(16,4,3);//照
while(1)
{
cshq(); //参数获取
dht11(); //温湿度获取
ajjc(); //按键检测
xxpxs(); //显示屏显示
beep_warning(); //状态判断
if(flag2==0)
{ OLED_ShowCHinese(0,6,9);//自
OLED_ShowCHinese(16,6,10);//动
} else
{ OLED_ShowCHinese(0,6,11);//手
OLED_ShowCHinese(16,6,10);//动
}
}
}void ajjc() //按键检测{ if(flag == 0)
{ if(!key1) //打开
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0; while(!key1);
}
if(!key2) //关闭
{
out1 = 0;
out2 = 1; Delay(1000);
out2 = 0; while(!key2);
}
}
if(!key3) //切换
{
flag++;
flag2++; if(flag > 1)
{
flag = 0;
} if(flag2>1)
{
flag2 = 0;
} while(!key3);
}
if(flag==1) //手动
{ if(!key4) //打开
{
out1 = 1;
out2 = 0; Delay(1000);
out1 = 0; while(!key4);
}
if(!key5) //关闭
{
out1 = 0;
out2 = 1; Delay(1000);
out2 = 0; while(!key5);
}
}
}void xxpxs() //显示屏显示{ OLED_ShowNum(32,0,wd,2,16);//温度
OLED_ShowNum(32,2,sd,2,16);//湿度
OLED_ShowNum(32,4,u,3,16);//光强}void cshq() //参数获取{
u=get\_AD\_Res();
U=get\_AD\_Res1(); //红外}