#include "GSMgn.h"
#include "GSMhead.h"
uchar F=0,chinit,chping,chname;
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit LED3=P1^2;
sbit LED4=P1^3;
sbit LED=P1^4;
uint Time,Time1;
void delay1ms(uint x){
for(Time=0;Time<x;Time++)
for(Time1=0;Time1<120;Time1++);
}
/*********************************************************************
* C51中字符串函数的扩充 *
*********************************************************************/
uchar strsearch(uchar *ptr2){//查字符串*ptr2在*ptr1中的位置
//本函数是用来检查字符串*ptr2是否完全包含在*ptr1中
//返回: 0 没有找到
// 1-255 从第N个字符开始相同
uchar i,j,k;
uchar flag;
if(ptr2[0]==0) return(0);
flag=0;
for(i=0,j=0;i<MAX_TM-2;i++){
if(uart_buff[i]==ptr2[j]){//第一个字符相同
for(k=i;k<MAX_TM-2;k++,j++){
if(ptr2[j]==0)//比较正确
return(i+1); //返回值是整数,不含0
if(uart_buff[k]!=ptr2[j]) break;
}
j=0;
}
}
return(0);
}
/*********************************************************************
* AT指令发送程序 *
*********************************************************************/
uchar Send_AT_Command( uchar type){ //发送AT指令
//拨号时号码放在phone.number
//其他用para_temp
AT_Command_Type=type;
send_count=0;
switch(type){
case RESET_TC35: //复位TC35
strcpy(uart_buff,"AT+CFUN=1,1");
break;
case CHECK_PIN: //检查当前是否要输入PIN码
strcpy(uart_buff,"AT+CPIN?");
break;
case TC35_INIT: //TC35初始化命令
strcpy(uart_buff,"ATE0V0");
break;
//0D 0A 54 43 33 35 0D 0A
case SIM_ID:
strcpy(uart_buff,"ATE0V0+CMGF=1+CNMI=2,1");
break;
case AT_IPR:
strcpy(uart_buff,"AT+IPR=9600");
break;
case SMSS_ID:
strcpy(uart_buff,"ATE0V0^SSMSS=1+CNMI=2,1");
break;
case SMS_CMGF: //SMS 接收方式,
strcpy(uart_buff,"ATE0V0+CMGF=0");
break;
case PHONE_WRITE: //存电话号码
strcpy(uart_buff,"AT+CPBW=");
strcat(uart_buff,para_temp);
break;
case PHONE_READ: //读电话本
strcpy(uart_buff,"AT+CPBR=");
strcat(uart_buff,&read_tmp);
uart_buff[9]=0x0d;
uart_buff[10]=0x0a;
uart_buff[11]=0x00;
uart_buff[12]=0x00;
break;
case SMS_CMGR: //读一个短信
strcpy(uart_buff,"AT+CMGR=");
strcat(uart_buff,para_temp);
break;
case SMS_CMGS: //写一个短信
strcpy(uart_buff,"AT+CMGS=");
strcat(uart_buff,para_temp);
break;
case SMS_CMGD: //删除一个短信
strcpy(uart_buff,"AT+CMGD=");
strcat(uart_buff,para_temp);
break;
case CMGS_MUB: //发报警短信内容
strcpy(uart_buff,para_temp);
break;
case AT_COMMAND:
break;
default:
receive_count=0;
return(TRUE);
}
ES=1;
strcat(uart_buff,"\x0d\x00");//在命令后加入CR
send_count=0; //设置发送指针
receive_count=0;
AT_Command_Status=COMMAND_WAIT; //设置接收成功标志
timer_S_cnt=0;
txd_rxd_bit=0;
at_send_bit=0;
TI=1; //开始发送数据
RI=0; //清除接收标志
if (type==TC35_INIT){
while (timer_S_cnt<6){
if (AT_Command_Status!=COMMAND_WAIT||txd_rxd_bit==1)
return(TRUE);
}
}
else if((type==SMS_CMGR)){
while (timer_S_cnt<6){
if (AT_Command_Status!=COMMAND_WAIT||txd_rxd_bit==1)
return(TRUE);
}
}
else if((type==CALL_ID)){
while (timer_S_cnt<2){
if (AT_Command_Status!=COMMAND_WAIT||txd_rxd_bit==1)
return(TRUE);
}
}
else{
while (timer_S_cnt<6){
if (AT_Command_Status!=COMMAND_WAIT||txd_rxd_bit==1){
return(TRUE);
}
}
}
return(FALSE);
}
void Sys_Init(void){//话机启动的初始化程序,包含键盘与定时器,串口等
//TH0=(65536-46080)/256;//重新装入定时25mS的初值到TH0,TL0 56320
//TL0=(65536-46080)%256;
//65536-(11.0592/12)*20MS*1000=To 18432 47104
//1ms定时
//11.0592/12 = 0.9216 M (记数频率)
//1ms记数 921.6次
//16位 2^16 - x = 922 x=64614 FC66
//定时器里不断放fc66,不用怀疑51定时器得到的 1ms的精确性吗
//TH0 = 0xf6;
//TL0 = 0x50;
TH0 = 0xB8;
TL0 = 0x00; //20ms的时钟基准//串口中断
TI=0;
RI=0;
SCON=0x50; //选用方式1
TMOD=0x20;
TMOD = (TMOD & 0xf0) | 1;//MODE 1
TH1=0xfd; //波特率为9600
TL1=0xfd;
PCON=0x00; //倍频0x80
IE = 0xb0; // EA XX ET2 ES ET0 EX0 ET1 EX1 11000000
TR1=1; //enable TIMER1
ES=1;
TR0=1;
ET0=1;
ET1=0;
EA=1;
timer_1S_cnt=0;
receive_count=0;
send_count=0xff; //表示禁止发送,可以接收
}
//======================================================================
//========================进行SIM卡中的1到20条短信号码的读==============================================
//======================================================================
uchar READ_TEL(uchar r){ //读短信号码1-20个
// uchar r;
para_temp[0]=(r/10)+0x30;para_temp[1]=(r%10)+0x30; para_temp[2]=0x0d;para_temp[3]=0x00;
para_temp[4]=0x00;para_temp[5]=0x00;para_temp[6]=0x00;para_temp[7]=0x00;
para_temp[8]=0x00;para_temp[9]=0x00;para_temp[10]=0x00;para_temp[11]=0x00;
// timer_S_cnt=0; while(timer_S_cnt<1&&(AT_Command_Status==COMMAND_WAIT)) watch_dog_clear;
Send_AT_Command(SMS_CMGR);
timer_S_cnt=0;
//while(timer_S_cnt<1&&(AT_Command_Status==COMMAND_WAIT)) watch_dog_clear;
send_count=0;
if(strsearch("CMGR: 0,,0")){ //+CMGR: 0,,0 没有短信返回
// timer_S_cnt=0; while(timer_S_cnt<1) watch_dog_clear;
ES=1; //设置发送指针
receive_count=0;
send_count=0xff;
RI=0 ;
return(FALSE);
}
else if(uart_buff[1]==0x0d&&uart_buff[0]==0x34){
return(FALSE);
}
else{
if(AT_Command_Status==COMMAND_ERROR)
return(FALSE);
else return(TRUE);
}
}
//============================================
//========== 读写短信程序
//====================================================
void read_sms(){
uchar i,r,j,t;
for(r=1;r<20;r++){
timer_tmp=0;
// while(timer_tmp<15) watch_dog_clear;// if(strsearch("+CMTI:")) return; //是否有新短信
num_tmp1=r;
if(READ_TEL(r)!=1){ //读一个短信 at+cmgr=1-19 读指令
if(strsearch("+CMTI:")) return;
continue;
}//TEL_temp[18] 读出对方号码
t=2;
i=0;
for(j=15;j<25;j++){
if((uart_buff[j]==0x22)&&(uart_buff[j+1]==',')&&uart_buff[j+2]==0x22)
break;
}
j=j+3;
if(uart_buff[j]=='+') j=j+3;
for(;j<40;j++){
if(uart_buff[j]==0x22)
break;
para_temp[t++]=uart_buff[j];
TEL_temp[i++]=uart_buff[j];
}
TEL_temp[i++]=0x0d ;
TEL_temp[i++]=0x00 ;
para_temp[t++]=0x0d;
para_temp[t++]=0x00;para_temp[t++]=0x00;
//---------------手机号码设置-------------
if((i=strsearch("CHN"))){ // 发中文//有些比较重要的东西你可以存到卡就,重新开机也没有问题
system_server=SYS_ALARM;
return;
}
else if((i=strsearch("TLA"))){ // 存号码1
para_temp[0]=0x31;
para_temp[1]=',';
Send_AT_Command(PHONE_WRITE); // PHONE_WR// IO_OUT=0;
system_server=SYS_NUMSAVE; //储存电话号码标志
return;
}
else if((i=strsearch("TLB"))){ // 存号码2
para_temp[0]=0x32;
para_temp[1]=',';
Send_AT_Command(PHONE_WRITE); // PHONE_WR// IO_OUT=0;
system_server=SYS_NUMSAVE; //储存电话号码标志
return;
}
else if((i=strsearch("TLC"))){ // 存号码3
para_temp[0]=0x32;
para_temp[1]=',';
Send_AT_Command(PHONE_WRITE); // PHONE_WR// IO_OUT=0;
system_server=SYS_NUMSAVE; //储存电话号码标志
return;
}
else{
para_temp[0]=(num_tmp1/10)+0x30;
para_temp[1]=(num_tmp1%10)+0x30;
para_temp[2]=0x0d;para_temp[3]=0x00