#include<pic.h>
#include"gsm.h"
#include"usart.h"
#include"delay.h"
static bit msg_flag;
static bit ok_flag;
static char msg_num;
static char alert_flag;
void gsm_capture(){
static long v_data;
static char data_rec;
if(alert_flag==1 && data_rec==','){msg_num = RCREG;msg_flag=1;alert_flag=0;}
data_rec = RCREG;
v_data <<= 8;
v_data |= data_rec;
if(v_data == MSG_ALERT){alert_flag=1;}
else if(ok_flag==0 && (v_data &0x0000ffff) == ACK_OK)ok_flag=1;
}
/*
+CMGR:"REC UNREAD","+919995394236", "0", "0",,"09/02/08,12:27:24+22"\r\n2\r\nOK\r\n$h55t02*
*/
char gsm_query(char wt){
while(wt-- > 0){
prints("AT\n\r");
if(ok_flag){
return(1);
}
DelayS(1);
}
return(0);
}
void gsm_init(void){
prints("ATE0\r\n");
DelayS(2);
prints("AT+CMGF=1\r\n");
DelayS(2);
prints("AT+CNMI=2,1,0,0,0\r\n");
DelayS(2);
}
///\r\n+CMTI:"SM",1\r\nOK\r\n
//\r\n+CMGR:"REC UNREAD","+919995394236", "0", "0",,"06/07/21,10:09:38+00"\r\n\r\nOK\r\n
char gsm_process(char *msg){
char c_dat;
char ni=0 ;
char skip;
if(msg_flag){
skip = 0;
DelayS(2);
RCIF = 0;RCIE = 0;
GIE = 0;
prints("AT+CMGR=");
putchs(msg_num);
prints("\r\n");
CREN = 0 ;
CREN = 1 ;
while(getchs()!=',');
ni = 0;
while(c_dat != ','){
c_dat = getchs();
msg[ni++] = c_dat;
}
while(getchs() != '$'){
if(skip++ >70)break;
}
if(skip<70){
while(c_dat != '*'){
c_dat = getchs();
msg[ni++] = c_dat;
}
msg[ni] = '\0';
msg_flag = 0;
DelayS(2);
prints("AT+CMGD=");
putchs(msg_num);
prints("\r\n");
RCIF = 0;
RCIE = 1;
GIE = 1;
return(1);
}
}
return(0);
}
void
gsm_send_msg(const char* num,const char *msg){
prints("AT+CMGS=");
prints(num);
prints("\r\n");
DelayS(2);
prints(msg);
putchs(0X1A);
}