代码:
#include <REG51.H>
#include <string.H>
#define uchar unsigned char
#define uint unsigned int
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
#define FOSC_110592M
//#define FOSC 12000000L
uchar rec_data[50];
uchar rec_num;
sta
tic uchar phonenum[50]="454646564";
//uchar sendsms[25];
uchar flag;
void SerialInti()
{
TMOD=0x20;
#ifdef FOSC_12M
TH1=0xf3;
TL1=0xf3;
#else
TH1=0xfd;
TL1=0xfd;
#endif
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
}
void DelaySec(int sec)
{
uint i , j= 0;
for(i=0; i<sec; i++)
{
for(j=0; j<65535; j++)
{
}
}
}
void Serial_interrupt() interrupt 4
{
uchar temp;
temp=SBUF;
rec_data[rec_num++]=temp;
if(rec_num>=50)
rec_num=0;
else
;
RI=0;
//¢P16=0;
//DelaySec(1);
//P16=1;
}
void Uart1Sends(uchar *str)
{
while(*str!=' ')
{
SBUF=*str;
while(!TI);
TI=0;
str++;
}
}
void Uart1BYTE(uchar temp)
{
SBUF=temp;
while(!TI);
TI=0;
}
uchar hand(uchar *ptr)
{
if(strstr(rec_data,ptr)!=NULL)
return 1;
else
return 0;
}
void clear_rec_data()
{
uchar i;
for(i=0;i<strlen(rec_data);i++)
{
rec_data[i]='0';
}
rec_num=0;
}
void main()
{
uchar i = 0;
SerialInti();
P10=0;
P11=0;
P12=0;
P13=0;
P14=1;
P15=1;
P16=1;
flag =1;
//Uart1Sends(num);
//????????GSM???,???? AT ??,??????????????
Uart1Sends("ATI
"); //??
while(!hand("OK"))
{
clear_rec_data();
i++;
Uart1Sends("ATI
");//
DelaySec(1);//??
if(i>=5)
{
P10=1;
break;
//return;
}
else
;
}
clear_rec_data();//?????GSM???????,?????????
DelaySec(1);//??
Uart1Sends("AT+CSQ
");//????
DelaySec(1);//??
i=0;
while(!hand("OK")) //??????GSM??????OK
{
clear_rec_data();
i++;
Uart1Sends("AT+CSQ
");//
DelaySec(1);//??
if(i>=5)
{
P11=1;// ÃðµÆûÓеõ½Êý¾Ý
break;
//return;
}
else
;
}
// P11=0;
clear_rec_data();
DelaySec(1);//??
Uart1Sends("AT+CPIN?
");//?????????
DelaySec(1);//??
i=0;
while(!hand("READY")) //??SIM??????SIM?READY
{
clear_rec_data();
i++;
Uart1Sends("AT+CPIN?
");//???????
DelaySec(1);//??
if(i>=5)
{
P12=1;
break;
//return;
}
else
;
}
//P12=0;
Uart1Sends("AT+COPS?
");//???????
DelaySec(1);//??
i=0;
while(!hand("CHINA")) //???????????CHINA
{
clear_rec_data();
i++;
Uart1Sends("AT+COPS?
");//???????
DelaySec(1);//??
if(i>=5)
{
P13=1;
break;
//return;
}
else
;
}
// P13=0;
clear_rec_data();
//DelaySec(1);//??
//???????????
//Uart1Sends("AT+CMMI=2,1
"); //???
//DelaySec(1);//??
//Uart1Sends("AT+CMGF=1
"); //??1
//DelaySec(1);//??
/*Uart1Sends("AT+CMGS="1252015002940813"
"); //????????????
DelaySec(1);//??
Uart1Sends("ye");
DelaySec(1);//??
Uart1BYTE(0X1A);
DelaySec(1);*/
//DelaySec(1);//??
//DelaySec(1);//??
// DelaySec(1);//??
//????????
Uart1Sends("AT+CMGD=1
");//??SIM?????
DelaySec(1);//??
DelaySec(1);
P14=0;
//phonenum[11]=' ';
clear_rec_data();
Uart1Sends("AT+CSCS="GSM"
"); //???
DelaySec(1);//??
Uart1Sends("AT+CMGF=1
"); //??1
DelaySec(1);//??
Uart1Sends("AT+CMGS="1252015002940813"
"); //????????????
DelaySec(1);//??
Uart1Sends(phonenum);
DelaySec(1);//??
Uart1BYTE(0X1A);
}//deng dai zhu ce duanxin
图片中 454646564 是STC89C51在与GSM模块连接之前,串口接收的数据。而接上GSM模块之后变成了00000000 这是为啥啊??
GSM模块是SIM900A v3.8.2
-
一周热门 更多>