|
請(qǐng)各位大俠看看這個(gè)程序是在51的基礎(chǔ)上修改的修改的部分包括延時(shí)函數(shù)和波特率以及頭文件,發(fā)現(xiàn)在修改前可以通信修改后有數(shù)據(jù)發(fā)出但無法和GSM通訊。使用串口調(diào)試工具查看發(fā)現(xiàn)51和15發(fā)送到串口助手上的數(shù)值是一樣的只是速度快慢有區(qū)別。
- #include <STC15F2K60S2.H>
- #include <intrins.h>
- #include <string.h>
- #include <stdio.h>
- #include "lcd12864.h"
- #define uint unsigned int
- #define uchar unsigned char
- #define CALL_YOU_TELNUMBER "***********"//*號(hào)寫入手機(jī)號(hào)碼
- #define SEND_MESSA_EN "520ss.ssssss.com"
- #define SEND_MESSA_CN "************"//*號(hào)寫入中文對(duì)應(yīng)編碼
- sbit Send_ENGLISH_key=P1^3; //控制發(fā)送英文按鍵
- sbit Send_SMS_PDU_key=P1^2; // 控制發(fā)送中文按鍵
- #define MAXCHAR 70 //串口緩沖最大長(zhǎng)度
- uchar GsmRcv[MAXCHAR];
- #define MAXMSG 15 //接收短信最大長(zhǎng)度
- #define MAXREVMSG MAXMSG+2
- sbit JIDIANQI=P1^0; //繼電器
- sbit FENGMINGQI=P1^1; //蜂鳴器
- uchar j=0;
- int g_timeout=0;
- void Init_UART(void);
- void Init_GSM(void);
- void delay(uint ms);
- void clearBuff(void);
- void send_call(void);
- void send_english(void);
- void hang_up(void);
- void Print_Char(uchar ch);
- void Print_Str(uchar *str);
- void SendUDPMessage(char* pnum, char* context, void (*sendfun)(char*));
- void delay(uint ms);
- int Hand(uchar *a);
- void led(int i);
- void main()
- {
- v_Lcd12864Init_f() ;
- v_Lcd12864PutString_f( 2,0, "") ;
- v_Lcd12864PutString_f( 2,1, "′′˼í¨DÅ") ;
- v_Lcd12864PutString_f( 1,2, "") ;
- v_Lcd12864PutString_f( 1,3, "www.csgsm.com") ;
- Init_UART(); //初始化串口
- Init_GSM(); //初始化GSM
- delay(10);
- Send_ENGLISH_key=1; //發(fā)英文短信按鍵
- Send_SMS_PDU_key=1; //發(fā)中文短信按鍵
- clearBuff();
- send_call(); //打電話
- v_Lcd12864Clear_f() ;
- v_Lcd12864PutString_f( 2,1, "") ;
- v_Lcd12864PutString_f( 1,2, "") ;
- v_Lcd12864PutString_f( 1,3, "") ;
- while(1)
- {
- ////////////////////////////////////////////////////////////////////////
- P2=0XFF;
- if(Send_ENGLISH_key==0) //發(fā)英文短信按鍵
- {
- while(Send_ENGLISH_key==0)
- {
- P2 &= ~0X04; //P2.2點(diǎn)亮
- }
- P2 |= 0X01;
- clearBuff(); //清空緩存
- v_Lcd12864Clear_f() ; //清屏
- v_Lcd12864PutString_f( 0,1, "英文短信發(fā)送....") ;
- send_english();
- v_Lcd12864PutString_f( 0,1, "短信發(fā)送[OK]") ;
- }
- else if(Send_SMS_PDU_key==0) //發(fā)中文短信按鍵
- {
- while(Send_SMS_PDU_key==0)
- {
- P2 &= ~0X08; //點(diǎn)亮P2.3
- }
- P2 |= 0X02;
- clearBuff(); //清空緩存
- v_Lcd12864Clear_f() ; //清屏
- v_Lcd12864PutString_f( 0,1, "中文短信發(fā)送....") ;
- SendUDPMessage(CALL_YOU_TELNUMBER, SEND_MESSA_CN, Print_Str); //·¢ËíÖDÎç¶ìDÅoˉêy
- v_Lcd12864PutString_f( 0,1,"短信發(fā)送[OK]") ;
- }
- if(strstr(GsmRcv,"RING")!=NULL)
- {
- FENGMINGQI=0;
- delay(2);
- clearBuff();
- v_Lcd12864Clear_f() ; //清屏
- v_Lcd12864PutString_f( 0,1, "有電話[..]") ;
- }
- else
- {
- FENGMINGQI=1;
- }
- if(strstr(GsmRcv,"+CMTI")!=NULL) //緩存中有加CMTI字符
- {
- clearBuff();
- while(!Hand("OK"))//與GSM握手判斷是否返回OK
- {
- Print_Str("AT+CMGF=1\r\n");
- delay(10);
- }
- clearBuff();
- Print_Str("AT+CMGR=1\r\n"); //讀取1號(hào)位置短信
- delay(50);
- if(strstr(GsmRcv,"k1")!=NULL||strstr(GsmRcv,"K1")!=NULL)
- {
- v_Lcd12864Clear_f() ; //清屏
- v_Lcd12864PutString_f( 0,1, "繼電器打開[OK]") ;
- JIDIANQI=0;
- }
- else if(strstr(GsmRcv,"k0")!=NULL||strstr(GsmRcv,"K0")!=NULL)
- {
- v_Lcd12864Clear_f() ; //ÇåÆá
- v_Lcd12864PutString_f( 0,1, "繼電器關(guān)閉[OK]") ;
- JIDIANQI=1;
- }
- els
- {
- char pStr[MAXREVMSG];
- int n=0;
- memset(pStr,0,MAXREVMSG);
- v_Lcd12864Clear_f() ;
- v_Lcd12864PutString_f( 0,0, "短信內(nèi)容如下:") ;
- strncpy(pStr,strstr(GsmRcv+14,"\r\n")+2,MAXREVMSG)
- n = strlen(strstr(pStr,"\r\n"));
- pStr[MAXREVMSG-n] = '\0';
- v_Lcd12864PutString_f( 0,1, pStr);
- v_Lcd12864PutString_f( 0,3, "發(fā)十五字以內(nèi)中文") ;
- }
- clearBuff();
- while(!Hand("OK")
- {
- Print_Str("AT+CMGD=1\r\n")
- delay(50);
- }
- clearBuff();
- }
-
- }
- }
- void ser() interrupt 4
- {
- if(RI==1)
- { GsmRcv[j]=SBUF;//Ãüáî′æμ½Ãüáîêy×é
- RI=0; //èí¼tÇå3y½óêÕÖD¶Ï
- j++;
- }
- }
- /*void delay(uint ms)// Ñóê±×ó3ìDò
- {
- uchar i;
- while(ms--)
- {
- for(i=0;i<120;i++);
- }
- }*/
- void delay(uint ms) //@11.0592MHz
- {
- unsigned char a, b;
- while(ms--)
- {
- _nop_();
- _nop_();
- _nop_();
- a = 200;
- b = 200;
- do
- {
- while (--b);
- } while (--a);
-
- }
- }
- void Print_Char(uchar ch
- {
- SBUF=ch;
- while(TI!=1);
- TI=0;
- }
- void Print_Str(uchar *str
- {
- while(*str!='\0')
- {
- Print_Char(*str);
- delay(2);
- str++;
- }
- }
- /*void Init_UART(void)//9600波特率
- {
- SCON = 0x50
- TMOD |= 0x20
- PCON |= 0x80 ;
- TH1 = 0xFA ;
- TL1=0xFA;
- IE |= 0x90 ;
- TR1 = 1 ;
- TI=1;
- ES=1;
- }*/
- void Init_UART(void)//9600波特率
- {
- SCON = 0x50;
- AUXR &= 0xBF;
- AUXR &= 0xFE;
- TMOD &= 0x0F;
- TMOD |= 0x20;
- TL1 = 0xFD;
- TH1 = 0xFD;
- ET1 = 0;
- TR1 = 1;
- ES = 1;
- EA = 1;
- }
- void clearBuff(void)
- {
- for(j=0;j<MAXCHAR;j++)
- {
- GsmRcv[j]=0x00;
- }
- j=0;
- }
- void led(int i)
- {
- P2 &= ~i;
- delay(20);
- P2 |= i;
- delay(20);
- P2 &= ~i;
- }
- int Hand(uchar *a)
- {
- delay(10);
- if(strstr(GsmRcv,a)!=NULL)
- {
- g_timeout=0;
- return 1;
- }
- if(g_timeout>50)
- {
- g_timeout=0;
- return -1;
- }
- g_timeout++;
- return 0;
- }
- void Init_GSM(void)
- {
- clearBuff();
- Print_Str("AT&F\r\n");
- led(0x01);
- while(strstr(GsmRcv,"OK")==NULL)
- {
- delay(10);
- led(0x01);
- clearBuff();
- Print_Str("AT&F\r\n");
- }
- led(0x02);
- {
- clearBuff();
- Print_Str("AT+CREG?\r\n");
- led(0x04);
- if((strstr(GsmRcv,"+CREG: 0,1") || strstr(GsmRcv,"+CREG: 0,5")))
- {
- clearBuff();
- led(0x08)
- break;
- }
- else
- {
- clearBuff();
- led(0x04);
- delay(50);
- }
- }
- while(!Hand("OK")
- {
- Print_Str("AT+CMGD=1\r\n")
- delay(10);
- }
- clearBuff();
- while(!Hand("OK")
- {
- Print_Str("AT+CNMI=2,1\r\n")
- delay(10);
- }
- clearBuff();
- while(!Hand("OK")
- {
- Print_Str("AT+CMGF=1\r\n")
- delay(10);
- }
- clearBuff();
- }
- void send_english(void)
- {
- char temp[20];
- memset(temp,0,20);
- strcat(temp,"AT+CMGS=");
- strcat(temp,CALL_YOU_TELNUMBER);
- //strcat(temp,"\"")
- strcat(temp,"\r\n");
- P2=0XFF;
- while(!Hand("OK")
- {
- Print_Str("AT+CMGF=1\r\n")
- delay(50);
- }
- clearBuff();
- Print_Str(temp);
- led(0x02);
- while(!Hand(">"))
- Print_Str(SEND_MESSA_EN);
- Print_Char(0x1A);
- led(0x04);
- clearBuff();
- }
- void send_call(void)
- {
- char temp[20];
- memset(temp,0,20);
- strcat(temp,"ATD");
- strcat(temp,CALL_YOU_TELNUMBER);
- strcat(temp,";\r\n"); //×é×°ATD18622944083;\r\n
- clearBuff();
- Print_Str(temp); //′òμç»°
- clearBuff();
- }
- /*void hang_up(void)
- {
- clearBuff();
- Print_Str("ATH\r\n"); //′òμç»°
- clearBuff();
- }*/
- void SendUDPMessage(char* pnum, char* context, void (*sendfun)(char*))
- {
- unsigned char i=0,x=0;
- unsigned char con_cnt[3] = {0,0,0};
- unsigned char tabhex[] = "0123456789ABCDEF";
- unsigned char num[13] = "";
- strncpy(num, pnum, strlen(pnum));
- for(i=0; num[i]!=0; i=i+2)
- {
- if(num[i+1] == 0)
- {
- num[i+1] = 'F';
- }
- x = num[i];
- num[i] = num[i+1];
- num[i+1] = x;
- }
- (*sendfun)("AT+CMGF=0\r\n")[/i][/i][/i][i];[/i][i][i][i]
- delay(50);
- (*sendfun)("AT+CMGS=");
- x = 15+strlen(context)/2;
- con_cnt[0] = 0x30 + x/10;
- con_cnt[1] = 0x30 + x%10;
- (*sendfun)(con_cnt);
- (*sendfun)("\r\n");
- delay(50);
- (*sendfun)("0011000D9168")
- (*sendfun)(num);//·¢ËíoÅÂë
- (*sendfun)("0008A0");
- x = strlen(context)/2;
- con_cnt[1] = tabhex[x&0x0f];
- con_cnt[0] = tabhex[(x&0xf0)>>4];
- (*sendfun)(con_cnt);
- delay(50);
- (*sendfun)(context);
- con_cnt[0] = 0x1a;
- con_cnt[1] = 0;
- (*sendfun)(con_cnt)
- delay(50);
- clearBuff();
- }
復(fù)制代碼
|
|