📄 gsm1.c
字号:
#include <intrins.h> // 外部函数头文件
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<intrins.h>
/////////////////////////////////////
/*采用P1口作控制
// 11.0592的晶体
// RST=20K*/
////////////////////////////////////
#define uchar unsigned char
#define uint unsigned int
sbit bCancel=P1^0; //取消
sbit bCall=P1^1; //拨号/接听
sbit bLed=P1^2; //LED指示
sbit bintr=P1^3
//sbit bPro=P1^3; //编程指示
sbit bStart=P3^4; //启动GSM
sbit bMusic=P3^5; //提示音控制
bit smsFlag=0;
uint time=0;
uchar temp;
uchar xdata pdu1[]="5BA2538567094EBA975E6CD58FDB5165";
//12345678901234567890123456789012
uchar xdata pdu2[]="003053F7673A51FA73B04EE559160021";
uchar xdata smsCenter_pdu[]="3108701905F0";
uchar xdata Phone_pdu[]="3178603180F4";
uchar xdata Phone[]="13870613084;\r";
uchar xdata rsbuf[100]="abcdef";
uchar sbuf,rsPoint;
/****************************************************************************/
//延迟函数
void Delay(uchar n)
{
uchar i=204;
while(n--)
{
while(i--);
}
}
/****************************************************************************/
//串口初始化
//89C51在11.0592MHz下以9600Bs的波特率工作*/
void uart_init(void)
{
SCON=0x50; //SM1=1,REN=1.UART工作于模式1,并允许接收
TMOD=0x21; //TIMER1工作于模式2,TIMER0工作于模式1,
PCON&=0x7f; //SMOD=0;
TH1=0xFD; //9600bs
TR1=1; //开定时器1
}
/****************************************************************************/
//LED闪烁
void Led_SS(uchar n)
{
n=n*2;
while(n--)
{
bLed=!bLed;
Delay(100);
}
}
/****************************************************************************/
void Music(uchar n)
{
bMusic=0;
while(n--)
{
Delay(64);//Delay(64);
}
bMusic=1;
}
/****************************************************************************/
//启动GSM模块即使GSM的IGT保持100ms以上的低电平
void Start_GSM()
{
bStart=0;
Delay(120);
bStart=1;
}
//发送一个ASCII
void SendASC(uchar ASC)
{
bit es;
es=ES;//
ES=0;//关闭串行中断
TI=0;
SBUF=ASC;
while(!TI);
TI=0;
ES=es;
}
/****************************************************************************/
//发送AT命令到TC35I
void SendCmdToTc35i(uchar *p)
{
uchar len;
len=strlen(p);
while(len--)
{
SendASC(*p++);
}
}
/****************************************************************************/
//删除sim卡内短信息
void EraseMsg(uchar index)
{
SendCmdToTc35i("AT+CMGD=");
SendASC(index);//指出第几个
SendASC(0x0d);
ES=1;
}
/****************************************************************************/
//读取短信息
/*uchar ReadMsg(uchar index)
{
uchar *p,i;
uchar Buf[40];
SendCmdToTc35i("AT+CMGF=1\r");
Delay(10);
SendCmdToTc35i("AT+CMGR=");
SendASC(index);//指出第几个
SendASC(0x0d);
ES=1; //必须中断
Delay(200);
//-----------------------------
p=strstr(rsBuf,"ERROR");
if (p !=NULL) return 0;
//-----------------------------
p=strstr(rsBuf,"+CMGR:0,,0");//无信息
if (p !=NULL) return 0;
//--------------------------
p=strstr(rsBuf,"/");//20个字节后是MSG
if (p ==NULL) return 0;
//---------------------------------------
p=p+21;
//-----------------------------------
for (i=0;i<strlen(Buf);i++)
{
Buf[i]=*p++;
}
//-----------------------------------
p=strcpy(rsBuf,Buf); //放回RsBuf
return 1;
}
/****************************************************************************/
//发送短消息
void Send_SMS(uchar *p)
{
uchar tmp=0;
ES=0;
SendCmdToTc35i("AT+CMGF=0\r");
Delay(50);
SendCmdToTc35i("AT+CMGS=38\r");
SendCmdToTc35i("089168");
SendCmdToTc35i(smsCenter_pdu);
SendCmdToTc35i("11000B81");
SendCmdToTc35i(Phone_pdu);
SendCmdToTc35i("0008A7188BF76CE8610FFF1A");
SendCmdToTc35i(p);
SendASC(0X1A);
}
/****************************************************************************/
//外部中断0处理函数
void Intr0_int(void) interrupt 0
{
uchar i,n=0;
EX0=0;
Delay(10);
bintr=1;
Delay(100);
EX0=1;
}
/****************************************************************************/
//外部中断1处理函数
void T0_intsvr(void) interrupt 1
{
TH0=0x4C; //Timer0产生50ms的中断
TL0=0;
time++;
}
/****************************************************************************/
void serial_int()interrupt 4
{
if(TI)
{
TI=0;
}
else nop()
}
/****************************************************************************/
void main(void)
{
uchar i;
EA=0;
bMusic=1;
bintr=0;
startflag=0;
Start_GSM();//启动GSM模块
uart_init();
Led_SS(5);
ES=1;
EX0=1;
EX1=1;
TI=1;
Delay(200);
Delay(200);
SendCmdToTc35i("ATE\n");
EA=1;
Music(2);
while(1)
{
if(intr=1)
Send_SMS(pdu1);
Music(2);
else nop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -