單片機源程序 #include <REGX52.H> #include "eeprom52.H" #define uchar unsigned char #define uint unsigned int sbit key_1=P1^0; //布防按鍵 sbit key_2=P1^1; //撤防按鍵 sbit key_3=P1^2; //緊急報警按鍵 sbit LED_1=P2^1; //短信指示燈 黃色 sbit LED_2=P2^2; //布防指示燈 綠色 sbit LED_3=P2^3; //報警指示燈 紅色 sbit beep=P2^4; bit police_flag=0; sbit Shock=P0^0; uchar Shock_time=0; //布防狀態下,震動報警,發送短信后一分鐘才可以再次觸發 bit Shock_flag=0,Shock_ok_set=0; bit Urgent=0; //緊急報警按鍵標志位 bit Defence_Disarm=1; // Defence布防-0 撤防Disarm-1 uchar Defence_Vaule=0; uchar phone[11]= "15737324461"; //手機號數組 uchar xdata GSM_dat[100]; uchar subscript_1,subscript_2,await_time; bit ir_ok_set=0; uchar ms; //定時器定時變量 void delay(uint T) //延時函數 { while(T--); } void Uart1Data(uchar dat) { SBUF=dat; while(!TI); TI=0; } void UartData_Byte(uchar *byte) { while(*byte != '\0') { Uart1Data(*byte++); } } uchar verify_GSMdat(uchar *dat)//讀取服務器返回的數據 { uchar i=0; while(*dat != 0){ if(*dat != GSM_dat){ return 0; } i++; dat++; } GSM_dat[0]=0; return 1; } void gsm_init() //GSM初始化 中心號碼設置 { UartData_Byte("ATE0\r\n"); //AT無回顯指令 await_time=0; do { if(await_time>20) { await_time=0; UartData_Byte("ATE0\r\n"); } }while(verify_GSMdat("OK")!=1); //延時等待 delay(2000); UartData_Byte("AT+CNMI=3,2,0,0,0\r\n"); await_time=0; do { if(await_time>20) { await_time=0; UartData_Byte("AT+CNMI=3,2,0,0,0\r\n"); } }while(verify_GSMdat("OK")!=1); delay(2000); UartData_Byte("AT+CMGF=1\r\n"); await_time=0; do { if(await_time>20) { await_time=0; UartData_Byte("AT+CMGF=1\r\n"); } }while(verify_GSMdat("OK")!=1); delay(2000); UartData_Byte("AT+CMGDA=\"DEL ALL\"\r\n"); await_time=0; do { if(await_time>20) { await_time=0; UartData_Byte("AT+CMGDA=\"DEL ALL\"\r\n"); } }while(verify_GSMdat("OK")!=1); } void GSM_send_dispose() { uchar i; if(ir_ok_set==1) //接收手機號設置短信 { LED_1=0; ir_ok_set=0; SectorErase(0x2200); byte_write(0x2200,phone[0]); byte_write(0x2201,phone[1]); byte_write(0x2202,phone[2]); byte_write(0x2203,phone[3]); byte_write(0x2204,phone[4]); byte_write(0x2205,phone[5]); byte_write(0x2206,phone[6]); byte_write(0x2207,phone[7]); byte_write(0x2208,phone[8]); byte_write(0x2209,phone[9]); byte_write(0x220A,phone[10]); resend_English:UartData_Byte("AT+CMGF=1\r\n"); await_time=0; do { if(verify_GSMdat("ERROR")==1 || await_time>20) { await_time=0; UartData_Byte("AT+CMGF=1\r\n"); } }while(verify_GSMdat("OK")!=1); delay(20000); UartData_Byte("AT+CSCS=\"GSM\"\r\n"); await_time=0; do { if(verify_GSMdat("ERROR")==1 || await_time>20) { await_time=0; UartData_Byte("AT+CSCS=\"GSM\"\r\n"); } }while(verify_GSMdat("OK")!=1); delay(20000); UartData_Byte("AT+CMGS=\""); //信息發送指令 AT+CMGS=// for(i=0;i<11;i++) { Uart1Data(phone); } UartData_Byte("\"\r\n"); //信息發送指令 AT+CMGS=// await_time=0; do { if(verify_GSMdat("ERROR")==1 || await_time>20) { await_time=0; goto resend_English; } }while(verify_GSMdat("> ")!=1); UartData_Byte("SET END"); //信息發送指令 AT+CMGS=// Uart1Data(0x1a); //信息發送指令 AT+CMGS=// await_time=0; do { if(verify_GSMdat("ERROR")==1 || await_time>200) { goto resend_English; } }while(verify_GSMdat("+CMGS:")!=1&&verify_GSMdat("OK")!=1); UartData_Byte("AT+CMGDA=\"DEL ALL\"\r\n"); LED_1=1; } if((Shock_ok_set&&Shock_flag)||Urgent) { LED_1=0; Shock_flag=0; if(Urgent==0) { Defence_Vaule=120; } Urgent=0; UartData_Byte("AT+CMGF=0\r\n"); delay(40000); UartData_Byte("AT+CMGS=41\r\n"); delay(40000); UartData_Byte("0011000D9168"); Uart1Data(phone[1]); Uart1Data(phone[0]); Uart1Data(phone[3]); Uart1Data(phone[2]); Uart1Data(phone[5]); Uart1Data(phone[4]); Uart1Data(phone[7]); Uart1Data(phone[6]); Uart1Data(phone[9]); Uart1Data(phone[8]); Uart1Data('F'); Uart1Data(phone[10]); delay(40000); UartData_Byte("0008A9"); UartData_Byte("1A"); //以下是26 // UartData_Byte("8BF76CE8610FFF0C67094EBA8FDB516562A58B66830356F4FF01"); //經度:134.56321 緯度:150.36523 Uart1Data(0x1a); delay(40000); UartData_Byte("AT+CMGF=1\r\n"); gsm_init(); LED_1=1; } } void interrunt_Init(void) { TMOD=0x21; //定義兩個定時器 TH1=0xFD; TL1=0xFD; //定時器1用于產生波特率 晶振11.0592 TL0 = 0x00; //設置定時初值 TH0 = 0x4C; //設置定時初值 SCON=0x50; PCON=0; EA=1; //開總中斷 ES=1; //ES-串行中斷允許控制位 ES = 1 允許串行中斷。 TR1=1; //啟動定時器開始工作 ET0=1; TR0=1; } void dispose(void) { if(Defence_Disarm==0&&Defence_Vaule==0) //布防狀態下 { police_flag=1; if(Shock) //這一段是為了確定只執行一次 { Shock_ok_set=1; } else { Shock_flag=1; Shock_ok_set=0; } } if(Defence_Disarm==0&&police_flag) { if(Shock) //這一段是為了確定只執行一次 { Shock_time=10; } } } void key_dispose() { if(!key_1) { delay(200); if(!key_1) { while(!key_1); Defence_Disarm=0; Defence_Vaule=40; } } if(!key_2) { delay(200); if(!key_2) { while(!key_2); Defence_Disarm=1; Shock_time=0; } } if(!key_3) { delay(200); if(!key_3) { while(!key_3); if(Defence_Disarm==0&&police_flag) Urgent=1; } } } void main() { uchar i=0; interrupt_Init(); for(i=0;i<11;i++) { phone=byte_read(0x2200+i); if(phone>0x39 || phone<0x30) phone=0x30; } UartData_Byte(phone); while(verify_GSMdat("+COPS: 0,0,\"CHINA")==1 || verify_GSMdat("+COPS: 0,0,\"CHN-UNICOM\"")==1) { UartData_Byte("AT+COPS?\r\n"); delay(40000); } gsm_init(); while(1) { key_dispose(); dispose(); GSM_send_dispose(); } } void int_t0(void) interrupt 1 { TL0 = 0x00; //設置定時初值 TH0 = 0x4C; //設置定時初值 ms++; if(Defence_Vaule!=0) { if(Defence_Disarm==0&&(ms%10==0)) { Defence_Vaule--; LED_2=~LED_2; } } if(Defence_Disarm) LED_2=1; else if(Defence_Disarm==0&&Defence_Vaule==0) LED_2=0; if(Shock_time!=0&&(ms%10==0)) { Shock_time--; beep=~beep; LED_3=~LED_3; } if(Shock_time==0) { beep=1; LED_3=1; } if(ms>=20) { ms=0; } await_time++; } void Uart_1(void) interrupt 4 { if(RI) { RI=0; GSM_dat[subscript_1]=SBUF; if(GSM_dat[subscript_1-20]=='P'&&GSM_dat[subscript_1-19]=='H'&&GSM_dat[subscript_1-18]=='O'&&GSM_dat[subscript_1-17]=='N'&&GSM_dat[subscript_1-16]=='E'&& GSM_dat[subscript_1-15]==' '&&GSM_dat[subscript_1-14]=='S'&&GSM_dat[subscript_1-13]=='M'&&GSM_dat[subscript_1-12]=='S'&&GSM_dat[subscript_1-11]==':' ) { Uart1Data(phone[0]=GSM_dat[subscript_1-10]); Uart1Data(phone[1]=GSM_dat[subscript_1-9]); Uart1Data(phone[2]=GSM_dat[subscript_1-8]); Uart1Data(phone[3]=GSM_dat[subscript_1-7]); Uart1Data(phone[4]=GSM_dat[subscript_1-6]); Uart1Data(phone[5]=GSM_dat[subscript_1-5]); Uart1Data(phone[6]=GSM_dat[subscript_1-4]); Uart1Data(phone[7]=GSM_dat[subscript_1-3]); Uart1Data(phone[8]=GSM_dat[subscript_1-2]); Uart1Data(phone[9]=GSM_dat[subscript_1-1]); Uart1Data(phone[10]=GSM_dat[subscript_1]); ir_ok_set=1; // UartData_Byte(phone); } subscript_1=(subscript_1+1)%100; if(GSM_dat[(subscript_1+100-1)%100] == '\n'&&GSM_dat[(subscript_1+100-2)%100] == '\r') { subscript_1=0; } }
|