/**********************************************
设计者:胥学辰时刻:2011-11-1——2011-11-6功用:18B20收集温度,dht11收集湿度,光敏电阻收集光照等,经过GSM承受指令传输参数,操控外部器材改动环境参数。完好代码下载:http://www.51hei.com/f/gpsdd.rar***********************************************/#include#include #include #include #define uchar unsigned char#define uint unsigned intsbit DQ_wen=P2^0;sbit DQ_shi=P2^1;uchar i=0,j=0,flag,tm,temp,wet,dat;char num[]="15282983798"; //界说要拨打的电话号码char code Huifu_S[]="Fa Song Success!"; //界说成功发送要回复的内容char code Huifu_F[]="Fa Song Failure!"; //界说失利发送要回复的内容char Huifu_wsg[]="temp:00.00C wet:23%R"; //界说参数回复数组char xdata Huancun[200]; //界说回来值存储数组char code Ok[]="OK"; //回来OK值标志char code CMTI[]="+CMTI"; //接纳短信标志char code open_1[]="open1"; //短信操控1标志char code open_2[]="open2"; //短信操控2标志char code open_3[]="open3"; //短信操控3标志char code open_4[]="open4"; //短信操控4标志void delay_key(uchar z); //按键延时函数void delay_GSM(uint y); //GSM模块延时函数void delay_18b20(uchar i); //18b20延时函数void delay_dht11_C(uint j); //dht11长延时函数void delay_dht11_D(uchar i); //dht11短延时函数void chuankou_init(); //串口初始化函数void clear_huancun(); //铲除缓存函数void send_ASCII(uchar *b); //发送ASCII代码函数void send_16_Hex(uchar c); //发送16进制代码函数bit panduan(uchar *a,uchar *b); //判别是否回来OK与短信成功代码void GSM_Text_Message(uchar*num,uchar*Huifu); //Text形式短信发送函数void GSM_Call(uchar *num); //拨打电话函数void Tiqu_num(); //提取来电号码函数void init_18b20(); //18b20初始复位函数void write_18b20_dat(uchar dat); //18b20写入字节函数uchar read_18b20_dat(); //18b20读出字节函数uchar read_18b20_temp_zs(); //18b20读取温度整数函数uchar read_18b20_temp_xs(); //18b20读取温度小数函数void dht11_init(void); //dht11初始化函数uchar read_dht11(void); //dht11读取湿度函数void chuli_wen_wet_light(); //18b20_dht11_light处理函数void main() //主函数{chuankou_init(); //串口初始化clear_huancun(); //铲除缓存delay_GSM(1000); //延时while(!panduan(Huancun,Ok)) //判别回来值{send_ASCII("AT"); //发送联机指令send_16_Hex(0x0d); //发送回车符 send_16_Hex(0x0a); //发送换行符P0=0x01; //过程指示灯1delay_GSM(600); //延时} clear_huancun(); //铲除缓存P0=0x02; //过程指示灯2delay_GSM(600); //延时send_ASCII("AT+CNMI=2,1"); //设置当有新短信到来时提示 send_16_Hex(0x0d); //发送回车符send_16_Hex(0x0a); //发送换行符delay_GSM(600); //延时while(!panduan(Huancun,Ok)); //等候设置成功clear_huancun(); //铲除缓存内容P0=0x03; //过程指示灯3delay_GSM(600); //延时 send_ASCII("AT+CMGF=1"); //文本(TEXT)形式 send_16_Hex(0x0d); //发送回车符send_16_Hex(0x0a); //发送换行符delay_GSM(600); //延时while(!panduan(Huancun,Ok)); //等候设置成功clear_huancun(); //铲除缓存内容P0=0x04; //过程指示灯4delay_GSM(600); //延时send_ASCII("AT+CMGD=1"); //删去第一条信息send_16_Hex(0x0d); //发送回车符send_16_Hex(0x0a); //发送换行符delay_GSM(600); //延时while(!panduan(Huancun,Ok)); //等候设置成功clear_huancun(); //铲除缓存内容while(1) //与GSM模块联机成功后的大循环{ P0=0x05; //过程指示灯5chuli_wen_wet_light(); //实时收集处理温度if(panduan(Huancun,CMTI)) //若缓存字符串中含有"+CMTI"就表明有新的短信{P0=0x06; //过程指示灯6clear_huancun(); //铲除缓存内容delay_GSM(600); //延时send_ASCII("AT+CMGR=1"); //发送读取信息指令send_16_Hex(0x0d); //发送回车符send_16_Hex(0x0a); //发送换行符delay_GSM(600); //延时Tiqu_num(); //提取来电号码if(panduan(Huancun,open_1)) //假如信息内容是"open1"{ P0=0x10; //指示灯GSM_Text_Message(num,Huifu_wsg); //发送环境参数信息GSM_Text_Message(num,Huifu_S); //发送成功短信回复}else if(panduan(Huancun,open_2)) //假如信息内容是"open2"{P0=0x20; //指示灯GSM_Text_Message(num,Huifu_S); //发送成功短信回复}else if(panduan(Huancun,open_3)) //假如信息内容是"open3"{ P0=0x40; //指示灯GSM_Text_Message(num,Huifu_S); //发送成功短信回复}else if(panduan(Huancun,open_4)) //假如信息内容是"open4"{ P0=0x80; //指示灯GSM_Text_Message(num,Huifu_S); //发送成功短信回复}elseGSM_Text_Message(num,Huifu_F); //发送失利短信回复clear_huancun(); //铲除缓存内容delay_GSM(600); //延时send_ASCII("AT+CMGD=1"); //删去读取后的信息send_16_Hex(0x0d); //发送回车符send_16_Hex(0x0a); //发送换行符delay_GSM(600); //延时while(!panduan(Huancun,Ok)); //等候设置成功clear_huancun(); //铲除缓存内容}} }/****************************各环节延时函数*****************************/void delay_key(uchar x) //按键操控延时函数{uchar y;for(;x>0;x--)for(y=110;y>0;y--);}void delay_GSM(uint y) //GSM模块延时函数{uint x;for(;y>0;y--){x=1500;while(x--);}}void delay_18b20(uchar i) //18b20延时函数{while(i--);}void delay_dht11_C(uint j) //dht11长延时函数{ uchar i;for(;j>0;j--) for(i=0;i<27;i++);}void delay_dht11_D(uchar i) //dht11短延时函数{for(;i>0;i--)_nop_();}void chuankou_init() //串口初始化函数{SCON=0x50; //串口:方法1,答应发送和接纳TMOD=0x20; //定时器1:形式2,8位主动重装形式,用于发生波特率 TH1=0xFD; //11.0592MHZ晶振,波特率为9600TL1=0xFD; TR1=1; //敞开定时器1 ES=1; //敞开串口中止EA=1; //敞开大局中止}/****************************GSM部分函数*****************************/void clear_huancun() //铲除缓存内容函数{uchar k;for(k=0;k<200;k++) {Huancun[k]=0; //将缓存内容清零}i=0; //接纳字符串的开始存储方位}void send_ASCII(uchar *b) //发送字符串函数{ES=0; //关串口中止for(b;*b!=;b++){SBUF=*b;while(TI!=1); //等候发送完结TI=0; //铲除发送中止标志位} ES=1; //开串口中止}