/**********************************************設(shè)計(jì)者:胥學(xué)辰時(shí)間:2011-11-1——2011-11-6功能:18B20采集溫度,dht11采集濕度,光敏電阻采集光照等,通過(guò)GSM接受指令傳輸參數(shù),控制外部器件改變環(huán)境參數(shù)。完整代碼下載: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"; //定義要撥打的電話號(hào)碼char code Huifu_S[]="Fa Song Success!"; //定義成功發(fā)送要回復(fù)的內(nèi)容char code Huifu_F[]="Fa Song Failure!"; //定義失敗發(fā)送要回復(fù)的內(nèi)容char Huifu_wsg[]="temp:00.00C wet:23%R"; //定義參數(shù)回復(fù)數(shù)組char xdata Huancun[200]; //定義返回值存儲(chǔ)數(shù)組char code Ok[]="OK"; //返回OK值標(biāo)志char code CMTI[]="+CMTI"; //接收短信標(biāo)志char code open_1[]="open1"; //短信控制1標(biāo)志char code open_2[]="open2"; //短信控制2標(biāo)志char code open_3[]="open3"; //短信控制3標(biāo)志char code open_4[]="open4"; //短信控制4標(biāo)志void delay_key(uchar z); //按鍵延時(shí)函數(shù)void delay_GSM(uint y); //GSM模塊延時(shí)函數(shù)void delay_18b20(uchar i); //18b20延時(shí)函數(shù)void delay_dht11_C(uint j); //dht11長(zhǎng)延時(shí)函數(shù)void delay_dht11_D(uchar i); //dht11短延時(shí)函數(shù)void chuankou_init(); //串口初始化函數(shù)void clear_huancun(); //清除緩存函數(shù)void send_ASCII(uchar *b); //發(fā)送ASCII代碼函數(shù)void send_16_Hex(uchar c); //發(fā)送16進(jìn)制代碼函數(shù)bit panduan(uchar *a,uchar *b); //判斷是否返回OK與短信成功代碼void GSM_Text_Message(uchar*num,uchar*Huifu); //Text模式短信發(fā)送函數(shù)void GSM_Call(uchar *num); //撥打電話函數(shù)void Tiqu_num(); //提取來(lái)電號(hào)碼函數(shù)void init_18b20(); //18b20初始復(fù)位函數(shù)void write_18b20_dat(uchar dat); //18b20寫(xiě)入字節(jié)函數(shù)uchar read_18b20_dat(); //18b20讀出字節(jié)函數(shù)uchar read_18b20_temp_zs(); //18b20讀取溫度整數(shù)函數(shù)uchar read_18b20_temp_xs(); //18b20讀取溫度小數(shù)函數(shù)void dht11_init(void); //dht11初始化函數(shù)uchar read_dht11(void); //dht11讀取濕度函數(shù)void chuli_wen_wet_light(); //18b20_dht11_light處理函數(shù)void main() //主函數(shù){chuankou_init(); //串口初始化clear_huancun(); //清除緩存delay_GSM(1000); //延時(shí)while(!panduan(Huancun,Ok)) //判斷返回值{send_ASCII("AT"); //發(fā)送聯(lián)機(jī)命令send_16_Hex(0x0d); //發(fā)送回車(chē)符 send_16_Hex(0x0a); //發(fā)送換行符P0=0x01; //步驟指示燈1delay_GSM(600); //延時(shí)} clear_huancun(); //清除緩存P0=0x02; //步驟指示燈2delay_GSM(600); //延時(shí)send_ASCII("AT+CNMI=2,1"); //設(shè)置當(dāng)有新短信到來(lái)時(shí)提示 send_16_Hex(0x0d); //發(fā)送回車(chē)符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時(shí)while(!panduan(Huancun,Ok)); //等待設(shè)置成功clear_huancun(); //清除緩存內(nèi)容P0=0x03; //步驟指示燈3delay_GSM(600); //延時(shí) send_ASCII("AT+CMGF=1"); //文本(TEXT)模式 send_16_Hex(0x0d); //發(fā)送回車(chē)符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時(shí)while(!panduan(Huancun,Ok)); //等待設(shè)置成功clear_huancun(); //清除緩存內(nèi)容P0=0x04; //步驟指示燈4delay_GSM(600); //延時(shí)send_ASCII("AT+CMGD=1"); //刪除第一條信息send_16_Hex(0x0d); //發(fā)送回車(chē)符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時(shí)while(!panduan(Huancun,Ok)); //等待設(shè)置成功clear_huancun(); //清除緩存內(nèi)容while(1) //與GSM模塊聯(lián)機(jī)成功后的大循環(huán){ P0=0x05; //步驟指示燈5chuli_wen_wet_light(); //實(shí)時(shí)采集處理溫度if(panduan(Huancun,CMTI)) //若緩存字符串中含有"+CMTI"就表示有新的短信{P0=0x06; //步驟指示燈6clear_huancun(); //清除緩存內(nèi)容delay_GSM(600); //延時(shí)send_ASCII("AT+CMGR=1"); //發(fā)送讀取信息指令send_16_Hex(0x0d); //發(fā)送回車(chē)符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時(shí)Tiqu_num(); //提取來(lái)電號(hào)碼if(panduan(Huancun,open_1)) //如果信息內(nèi)容是"open1"{ P0=0x10; //指示燈GSM_Text_Message(num,Huifu_wsg); //發(fā)送環(huán)境參數(shù)信息GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復(fù)}else if(panduan(Huancun,open_2)) //如果信息內(nèi)容是"open2"{P0=0x20; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復(fù)}else if(panduan(Huancun,open_3)) //如果信息內(nèi)容是"open3"{ P0=0x40; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復(fù)}else if(panduan(Huancun,open_4)) //如果信息內(nèi)容是"open4"{ P0=0x80; //指示燈GSM_Text_Message(num,Huifu_S); //發(fā)送成功短信回復(fù)}elseGSM_Text_Message(num,Huifu_F); //發(fā)送失敗短信回復(fù)clear_huancun(); //清除緩存內(nèi)容delay_GSM(600); //延時(shí)send_ASCII("AT+CMGD=1"); //刪除讀取后的信息send_16_Hex(0x0d); //發(fā)送回車(chē)符send_16_Hex(0x0a); //發(fā)送換行符delay_GSM(600); //延時(shí)while(!panduan(Huancun,Ok)); //等待設(shè)置成功clear_huancun(); //清除緩存內(nèi)容}} }/****************************各環(huán)節(jié)延時(shí)函數(shù)*****************************/void delay_key(uchar x) //按鍵控制延時(shí)函數(shù){uchar y;for(;x>0;x--)for(y=110;y>0;y--);}void delay_GSM(uint y) //GSM模塊延時(shí)函數(shù){uint x;for(;y>0;y--){x=1500;while(x--);}}void delay_18b20(uchar i) //18b20延時(shí)函數(shù){while(i--);}void delay_dht11_C(uint j) //dht11長(zhǎng)延時(shí)函數(shù){ uchar i;for(;j>0;j--) for(i=0;i<27;i++);}void delay_dht11_D(uchar i) //dht11短延時(shí)函數(shù){for(;i>0;i--)_nop_();}void chuankou_init() //串口初始化函數(shù){SCON=0x50; //串口:方式1,允許發(fā)送和接收TMOD=0x20; //定時(shí)器1:模式2,8位自動(dòng)重裝模式,用于產(chǎn)生波特率 TH1=0xFD; //11.0592MHZ晶振,波特率為9600TL1=0xFD; TR1=1; //開(kāi)啟定時(shí)器1 ES=1; //開(kāi)啟串口中斷EA=1; //開(kāi)啟全局中斷}/****************************GSM部分函數(shù)*****************************/void clear_huancun() //清除緩存內(nèi)容函數(shù){uchar k;for(k=0;k<200;k++) {Huancun[k]=0; //將緩存內(nèi)容清零}i=0; //接收字符串的起始存儲(chǔ)位置}void send_ASCII(uchar *b) //發(fā)送字符串函數(shù){ES=0; //關(guān)串口中斷for(b;*b!=