/**********************************************設計者:胥學辰時間:2011-11-1——2011-11-6功能:18B20采集溫度,dht11采集濕度,光敏電阻采集光照等,通過GSM接受指令傳輸參數,控制外部器件改變環(huán)境參數。完整代碼下載: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!"; //定義成功發(fā)送要回復的內容char code Huifu_F[]="Fa Song Failure!"; //定義失敗發(fā)送要回復的內容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); //發(fā)送ASCII代碼函數void send_16_Hex(uchar c); //發(fā)送16進制代碼函數bit panduan(uchar *a,uchar *b); //判斷是否返回OK與短信成功代碼void GSM_Text_Message(uchar*num,uchar*Huifu); //Text模式短信發(fā)送函數void GSM_Call(uchar *num); //撥打電話函數void Tiqu_num(); //提取來電號碼函數void init_18b20(); //18b20初始復位函數void write_18b20_dat(uchar dat); //18b20寫入字節(jié)函數uchar read_18b20_dat(); //18b20讀出字節(jié)函數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"); //發(fā)送聯機命令send_16_Hex(0x0d); //發(fā)送回車符 send_16_Hex(0x0a); //發(fā)送換行符P0=0x01; //步驟指示燈1delay_GSM(600); //延時} clear_huancun(); //清除緩存P0=0x02; //步驟指示燈2delay_GSM(600); //延時send_ASCII("AT+CNMI=2,1"); //設置當有新短信到來時提示 send_16_Hex(0x0d); //發(fā)送回車符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時while(!panduan(Huancun,Ok)); //等待設置成功clear_huancun(); //清除緩存內容P0=0x03; //步驟指示燈3delay_GSM(600); //延時 send_ASCII("AT+CMGF=1"); //文本(TEXT)模式 send_16_Hex(0x0d); //發(fā)送回車符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時while(!panduan(Huancun,Ok)); //等待設置成功clear_huancun(); //清除緩存內容P0=0x04; //步驟指示燈4delay_GSM(600); //延時send_ASCII("AT+CMGD=1"); //刪除第一條信息send_16_Hex(0x0d); //發(fā)送回車符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時while(!panduan(Huancun,Ok)); //等待設置成功clear_huancun(); //清除緩存內容while(1) //與GSM模塊聯機成功后的大循環(huán){ P0=0x05; //步驟指示燈5chuli_wen_wet_light(); //實時采集處理溫度if(panduan(Huancun,CMTI)) //若緩存字符串中含有"+CMTI"就表示有新的短信{P0=0x06; //步驟指示燈6clear_huancun(); //清除緩存內容delay_GSM(600); //延時send_ASCII("AT+CMGR=1"); //發(fā)送讀取信息指令send_16_Hex(0x0d); //發(fā)送回車符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時Tiqu_num(); //提取來電號碼if(panduan(Huancun,open_1)) //如果信息內容是"open1"{ P0=0x10; //指示燈GSM_Text_Message(num,Huifu_wsg); //發(fā)送環(huán)境參數信息GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復}else if(panduan(Huancun,open_2)) //如果信息內容是"open2"{P0=0x20; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復}else if(panduan(Huancun,open_3)) //如果信息內容是"open3"{ P0=0x40; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復}else if(panduan(Huancun,open_4)) //如果信息內容是"open4"{ P0=0x80; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復}elseGSM_Text_Message(num,Huifu_F); //發(fā)送失敗短信回復clear_huancun(); //清除緩存內容delay_GSM(600); //延時send_ASCII("AT+CMGD=1"); //刪除讀取后的信息send_16_Hex(0x0d); //發(fā)送回車符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時while(!panduan(Huancun,Ok)); //等待設置成功clear_huancun(); //清除緩存內容}} }/****************************各環(huán)節(jié)延時函數*****************************/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,允許發(fā)送和接收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) //發(fā)送字符串函數{ES=0; //關串口中斷for(b;*b!=