//AT指令的定义
char code AT_Tc35[]="AT+"; //连机
char code Bps_Tc35[]="IPR=38400"; //波特率
char code Text_Tc35[]="CMGF=1"; //文本模式
char code Read_Tc35[]="CMGR="; //读信息
char code Erase_Tc35[]="CMGD="; //删除信息
char code Send_Tc35[]="CMGS="; //发送信息
char code Creg_Tc35[]="CREG?"; //注册
//模式设置
void UART_Init(void)
{
SCON=0x50; //01010000b=>1模式scon,#11011000b;CEDN论坛2 a _5 N) Z& ]1 X
ES=l;
}
//发送-个ASCII
void SendASC(unsigned char ASC)
{
bit es;
es=ES;
EES=0;//关闭中断
TI=0;
SBUF=ASC;
while(!TI);
TI=0;
ES=es;
}
//发送命令到TC35
void SendToTc35(unsigned char* p,unsigned char Long)
{
while(Long--)
{
SendASC(*p++);
}
}
// 通讯中断接收程序
void Rs485_Do(void) interrupt 4 using 1
{
if(RI==l)
{
RI=0;
RsBuq[RsPoint++]=SBUF;
if fRsPoint>=sizeof(RsBuf))
{
RsPoint=0;//FlagRs485=0;
} //数据处理
}
}
// 发送AT连机命令
char code AT_Code[]="OK";
void Send_AT(void)
{
unsigned char *p;
while(1)
{
ClrRsBuf(RsBuf,sizeof(RsBuf));
SendToTc35(AT_Tc35,2); //"AT"
SendASC(OVER);
//************等待应答“ok",
ES=1; //必须中断
Delay(50);
P=strstr(RsBuf,AT_Code);
if(p!=NULL) break;
}
}
//发送bps连机命令
void Send_BPS(void)
{
SendToTc35(AT_Tc35,3); //"AT+"
SendToTc35(Bps_Tc35,sizeof(Bps_Tc35)-1);//"IPR=19200"
SendASC(OVER);
}
//设置文本
void SetText(void)
{
SendToTc35(AT_Tc35,3); //"AT+"
SendToTc35(Text_tc35,sizeof(Text_tc35)-1);//"IPR=19200"
SendASC(OVER);
Delay(100);
}
//删除短信息
unsigned char EraseMsg(unsigned char index)
{
unsigned char *p,i=20;
SendToTc35(AT_Tc35,3); //"AT+"
SendToTc35(Erase_Tc35,sizeof(Erase_Tc35)-1);//"IPR=19200";
SendASC(index);
SendASC(OVER);
ES=1;
while(i--)
{
Delay(200);
P=strstr(RsBuf,AT_Code);
if(P!=NULL) {return 1;}
}
return 0;
}
//读取短信息
char code Ask_No[]="+CMGR:0,,0";
char code ERROR{]="ERROR";
char code Ask_Tc35[]="/";
unsigned char ReadMsg(unsigned char index)
{
unsigned char *p,i;
unsigned char Buf[40];
SendToTc35(AT_Tc35,3); //"AT+" _
SendToTc35(Read_Tc35,sizeof(Read_Tc35)-1);
SendASC(index);
SendASC(OVER);
ES=1; //必须中断
Delay(600);
/*-------
ES=0:
SendToTc35(RsBuf,99);
ES=1;
*/
p=strstr(RsBuf,ERROR);
if(P!=NULL)
{
Send_AT();return 0;
p=strstr(RsBuf,Ask_No);//无信息
if(p!=NULL)return 0;
p=strstr(RsBuf,Ask_Tc35);
if(p==NULL) return 0;
p=p+21;
for(i=0;i