PIC to MAX232

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
it is set 8, but overwritten again to zero because of incorrect use of if and else
Thanks...I will check it though.

@jayanthd Thanks
I will go home around 10 pm

Like to ask regarding extracting the numerical value "18" from "+CSQ: 18.0", and save to do the calculation, is it simple ?
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Regarding Realterm program..
Is it a capture program or hyper terminal.
Do I need to use the hyper terminal together with Realterm ?

In Send TAB there are 2 +CR and 2 +LF...I dunno what to check.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
In the Port Tab if the open button is already pressed then unpress it and select COM port and baudrate and then press it to open port.

In Send Tab there are 2 +CR and 2 +LF because there are two text boxes to send data (numbers or ascii)

If you use the first text box then use the upper +CR and +LF check boxes.

As you have to send ascii data you have to click the "Send Ascii" button

Example

You have to enter

AT and then press send ascii button.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I do not think I need switch case for those two digits.
Just need to extract what ever is in that position and save it to integer.
From there it can be used to do the calculations. Right ?
 

jayanthd

Joined Jul 4, 2015
945
If you have equation to do calculation based on the network strength value then you can do it. That part you have to write the code. I will write code to parse the at responses and extract the required data and convert the required fields to integers.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
First Image
1. Not Registered on Network
2. SIM Removed
1.png

Second Image
Network strength is 99, means no network
2.png

Third Image
Registered on Network and SIM ready
3.png

Fourth Image
Signal strength is "15" - The data which needs to be extracted.
4.png

5th is Not necessary is below. Just AT commands to issue TEXT mode and to clear SMS received just in case
5.png


6 th I forgot.
It Needs to Hang up if any call comes in
Ring.png

This terminal prog explains a lotta thing that I didn't know that happened.

PS.."ATE0 is not issued here. So ATE1 is default
 

jayanthd

Joined Jul 4, 2015
945
Now I will write the code.

In

"+COPS: 0,2,47201

47201 is the network ID ?

Mention your Wavecom modem name. I will check its datasheet.


Explain how do you send AT commands from PIC to Modem. I know you will use UART but to get responses you have to send AT commands and I want to know when and in what sequence you issue the different AT Commands. Code is almost complete. Just have to arrange the instructions in the code.
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
47201 is network ID.
From that I set flag and display to LCD

C:
const char ATC0[] = "AT";          // Checking Response (Just OK)
const char ATC1[] = "ATE0";        // Dissable Echo   (Just is OK)
const char ATC2[] = "AT+CMGF=1";   // Change to Text mode (Just is OK)
const char ATC3[] = "AT+CSQ";      // Read the RX strength (+CSQ: xx,xx & OK)
const char ATC4[] = "AT+COPS?";    // SIM Registeration  (+COPS: x,x,xxxxx & OK)
const char ATC5[] = "AT+CPIN?";    // SIM Status  (+CPIN: READY)
const char ATC6[] = "AT+CMGD=1,4"; // Delete All SMS (Just is OK after 10 sec)


void Send2Modem(const char *s){
  while(*s){
    UART1_Write(*s++);  // Send Command via UART
  }
  UART1_Write(0x0D);  // CR - Carriage Return, 13 in decimal('\r')
  UART1_Write(0x0A);    // LF - Line feed, 10 in decimal ('\n')
//UART1_Write(0x1A);  // CTRL+Z End Character - Decimal 26
}

while(1){
   while(Init_Modem_Flag){
     Modem_Init();
   }


   if(Get_Response() == GSM_OK)break; // OK
  
    Send2Modem(ATC5);
     Wait();
   if(Get_Response() == ERROR)              // AT+CPIN?
     Lcd_Out(3,9,Const2Ram(String,MSLD10)); // "Detecting!";

      Send2Modem(ATC4);
      Wait();
   if(Get_Response() == GSM_ERROR)   // +COPS: 0
       Lcd_Out(4,9,Const2Ram(String,MSLD11)); //"Registering!";
      
   if(Get_Response() == GSM_DhiMobile)   // +COPS: 0,2,47201
       Lcd_Out(4,9,Const2Ram(String,MSLD08)); //"DhiMobile";

   if(Get_Response() == GSM_Ooredoo)   // +COPS: 0,2,47202
       Lcd_Out(4,9,Const2Ram(String,MSLD09)); //"Ooredoo";
  /*
   if(response_rcvd){
     response_rcvd = 0;
     switch(responseID){
       case GSM_OK       : break;
       case GSM_ERROR    :{

                          }
       case GSM_DhiMobile:{
                          Lcd_Out(4,9,Const2Ram(String,MSLD08));
                          }
       case GSM_Ooredoo  : break;
       case SIM_READY    :{
                          Lcd_Out(3,9,Const2Ram(String,MSLD05));
                          }
       case ERROR        : break;
     default :{   // Do Nothing
     }
    }
   }  */

  
  
  
}
}

void interrupt(){

  if(INTCON.T0IF){
    INTCON.T0IF = 0;
  //  BLAAAAAAAAH:
  }
  if(CCP1IE_bit){
    if(CCP1IF_bit){
      CCP1IF_bit = 0;

  // More BLAAAAAAH;
    }
  }

//******************************************************************************
  if(RCIE_bit){                   // If RX Interrupt enabled
    if(RCIF_bit){                 // and If RX Interrupt occured
      Uart_Read = UART1_Read();   // Get received byte

     
// Processing reception through state machine
// We are parsing only 'OK', 'ERROR', '+CSQ: XX,XX', '+COPS: 0,2,4720(1or2)' \
   '+CPIN: READY' responses
      switch(gsm_state){
        case 0: {
          response = -1;          // clear response
          if(Uart_Read == 'O')   // we have 'O', it could be "OK"
            gsm_state = 1;        // expecting 'K'
          if(Uart_Read == 'E')   // we have 'E', it could be "ERROR"
            gsm_state = 2;        // expecting 'R'
          if(Uart_Read == '+')   // we have '+', it could be "CSQ,COPS or CPIN"
            gsm_state = 6;        // expecting 'C'
         break;
        }
        case 1:{  // OK
          if(Uart_Read == 'K'){  // we have 'K' ->
            response = GSM_OK;    // we have "OK" response
            gsm_state = 253;      // expecting CR+LF
          }
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 2:{  // ER
          if(Uart_Read == 'R')   // we have 'R', it could be "ERROR"
            gsm_state = 3;        // expecting 'R'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 3:{  // ERR
          if(Uart_Read == 'R')   // we have 'R', it could be "ERROR"
            gsm_state = 4;        // expecting 'O'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 4:{  // ERRO
          if(Uart_Read == 'O')   // we have 'O', it could be "ERROR"
            gsm_state = 5;        // expecting 'R'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 5:{  // ERROR
          if(Uart_Read == 'R'){  // we have 'R',
            response = ERROR;     // we have "ERROR" response
            gsm_state = 253;      // expecting CR+LF
          }
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 6:{  // +C
          if(Uart_Read == 'C')   // we have 'C', it could be "CSQ,COPS or CPIN"
            gsm_state = 7;        // expecting 'S or O or P'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 7:{ // +CO or +CP or +CS
          if(Uart_Read == 'O')   // Iff we have 'O', it could be "COPS"
            gsm_state = 8;        // expecting 'P'
           else if(Uart_Read == 'P')   // Iff we have 'P', it could be "CPIN"
            gsm_state = 99;       // expecting 'I'
           else if(Uart_Read == 'S')   // Iff we have 'S', it could be "CSQ"
            gsm_state = 100;      // expecting 'Q'
           else
           gsm_state = 0;        // reset state machine
         break;
        }
        case 8:{  // +COP
          if(Uart_Read == 'P')   // we have 'P', it could be "COPS"
            gsm_state = 9;        // expecting 'S'
          else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 9:{  // +COPS
          if(Uart_Read == 'S')   // we have 'S', it could be "+COPS: 0,2,4720x
            gsm_state = 10;       // expecting ':'
          else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 10:{  // +COPS:
          if(Uart_Read == ':')   //we have ':', it could be +COPS: 0,2,4720x
            gsm_state = 11;       // expecting ' '
          else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 11:{  // +COPS:_
          if(Uart_Read == 0x20)  //we have ' ', it could be +COPS: 0,2,4720x
            gsm_state = 12;       // expecting '0'
          else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 12:{  // +COPS: 0 ( ONLY )
          if(Uart_Read == '0')   //we have '0', it could be +COPS: 0,2,4720x
            gsm_state = 13;       // expecting ',' or 'CR'
         else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 13:{  // +COPS: 0,  or +COP: 0
          if(Uart_Read == ',')   //we have ',', it could be +COPS: 0,2,4720x
            gsm_state = 14;      // expecting '2'
           else if(Uart_Read == 13){ // we have "CR"
            response = GSM_ERROR;
            gsm_state = 248;      // expecting LF CR+LF OK CR+LF
           }
          else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 14:{  // +COPS: 0,2
          if(Uart_Read == '2')   //we have '2', it could be +COPS: 0,2,4720x
            gsm_state = 15;       // expecting ','
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 15:{  // +COPS: 0,2,
          if(Uart_Read == ',')   //we have ',', it could be +COPS: 0,2,4720x
            gsm_state = 16;       // expecting '4'
         else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 16:{  // +COPS: 0,2,4
          if(Uart_Read == '4')   //we have '4', it could be +COPS: 0,2,4720x
            gsm_state = 17;       // expecting '7'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 17:{  // +COPS: 0,2,47
          if(Uart_Read == '7')   //we have '7', it could be +COPS: 0,2,4720x
            gsm_state = 18;       // expecting '2'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 18:{  // +COPS: 0,2,472
          if(Uart_Read == '2')   //we have '2', it could be +COPS: 0,2,4720x
            gsm_state = 19;       // expecting '0'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 19:{  // +COPS: 0,2,4720x ( X is 1 or 2 )
          if(Uart_Read == '0')   //we have '0', it could be +COPS: 0,2,4720x
            gsm_state = 20;       // expecting '1 or 2'
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 20:{  // +COPS: 0,2,47201 or +COPS: 0,2,47202
          if(Uart_Read == '1'){ //we have '1', it could be +COPS: 0,2,47201
            response  = GSM_DhiMobile;
             gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
           else if(Uart_Read == '2'){ //we have '2', it could be +COPS: 0,2,47202
            response = GSM_Ooredoo;
            gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
          else
         break;
        }

       
// CR + LF
        case 247:{
          if(Uart_Read == 13) // CR
            gsm_state = 248;
         else
            gsm_state = 0;
         break;
        }
        case 248:{
          if(Uart_Read == 10) // LF
            gsm_state = 249;
          else
           gsm_state = 0;
         break;
        }
// CR + LF
        case 249:{
          if(Uart_Read == 13) // CR
            gsm_state = 250;
         else
            gsm_state = 0;
         break;
        }
        case 250:{
          if(Uart_Read == 10) // LF
            gsm_state = 251;
          else
           gsm_state = 0;
         break;
        }
// OK
        case 251:{
          if(Uart_Read == 'O')
            gsm_state = 252;
         else
            gsm_state = 0;
         break;
        }
        case 252:{
          if(Uart_Read == 'K')
            gsm_state = 253;
          else
           gsm_state = 0;
         break;
        }
// CR + LF
        case 253:{// CR
          if(Uart_Read == 13)   // we have CR, it could be CR+LF
            gsm_state = 254;      // expecting LF
           else
            gsm_state = 0;        // reset state machine
         break;
        }
        case 254:{// LF
          if(Uart_Read == 10){  // we have LF, response is complete
            response_rcvd = 1;    // set reception flag
            responseID = response;// set response ID
          }
          gsm_state = 0;          // reset state machine
         break;
        }
        default:{                 // unwanted character
          gsm_state = 0;          // reset state machine
         break;
        }
      }
    }
  }
//******************************************************************************
}
This is what I am working at this moment.
I was stuck at "+COPS: 0" before :oops:
Now It is parsing to "+COPS: 0" fine and LCD shows that but it now it is not going beyond that...o_O
 

jayanthd

Joined Jul 4, 2015
945
code=C]const char ATC0[] = "AT"; // Checking Response (Just OK)
const char ATC2[] = "AT+CMGF=1"; // Change to Text mode (Just is OK)
const char ATC3[] = "AT+CSQ"; // Read the RX strength (+CSQ: xx,xx & OK)
const char ATC4[] = "AT+COPS?"; // SIM Registeration (+COPS: x,x,xxxxx & OK)
const char ATC5[] = "AT+CPIN?"; // SIM Status (+CPIN: READY)
const char ATC6[] = "AT+CMGD=1,4"; // Delete All SMS (Just is OK after 10 sec)

Mention in what sequence you give the above AT commands ? Which commands you give just once and which repeatedly ?


This is the parsing function I have written according to my code.

This works for me.

C:
void Extract_Signal_Strength(char *s1, char *s2) {
    unsigned int i = 0;
   
    asm clrwdt
   
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
   
    asm clrwdt
   
    while(*s1 != ' ') {
           *s1++;
           asm clrwdt
    }

    *s1++;
   
    while(*s1 != ',') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';    
    asm clrwdt
}

What does the Modem_Init() function do ?
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Here is the finished code. test it and reply. It is written for PIC16F887 @ 4 MHz Crsytal.

C:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

#define ON   1
#define OFF  0

#define HIGH 1
#define LOW  0

#define TRUE 1
#define FALSE 0

// GSM module connections
sbit gsmReset at RD7_bit;
sbit gsmReset_Direction at TRISD7_bit;
// End of GSM module connections

sbit LED at RD0_bit;

char gsmBuffer[80];
char buffer1[50];
char sms[30];
char smsIndex[4];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0;

sbit sendSmsFlag at myFlags.B0;

//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS=\"0000000000\"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
const char atCommandB[] = "AT+COPS?\r";
const char atCommandC[] = "AT+CPIN?\r";
const char atCommandD[] = "AT+CSQ?\r";
const char atCommandE[] = "ATH\r";

const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: \"SM\",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING\r\n\r\n";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
const char atResponseA[] = "+COPS: 0";
const char atResponseB[] = "+COPS: 0,2,47201";
const char atResponseC[] = "+COPS: 0,2,47202";
const char atResponseD[] = "+CSQ: ";
const char atResponseE[] = "AT\r\n\r\nOK\r\n";
const char atResponseF[] = "AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n";
const char atResponseG[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n";
const char atResponseH[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n";
const char atResponseI[] = "AT+CPIN?\r\n\r\nERROR\r\n";
const char atResponseJ[] = "AT+CPIN?\r\n\r\n+CPIN: READY\r\n";
const char atResponseK[] = "AT+CSQ\r\n\r\n+CSQ: ";
const char atResponseL[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseM[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseN[] = "ATH\r\n\r\nOK\r\n";

const char msg1[] = "No Network ";
const char msg2[] = "No SIM     ";
const char msg3[] = "No Network And No SIM";
const char msg4[] = "Network and SIM Ready";
const char msg5[] = "Signal Strength = ";
const char msg6[] = "Not Registered On Network";
const char msg7[] = "SIM Removed";
const char msg8[] = "OK         ";
const char msg9[] = "ERROR      ";
const char msgA[] = "SIM Ready  ";
const char msgB[] = "Dhi Mobile ";
const char msgC[] = "Ooredoo    ";
const char msgD[] = "                ";

const char sms1[] = "LED is ON";
const char sms2[] = "LED is OFF";
const char sms3[] = "Invalid Command Received";

//Function Prototypes
void DelayXSec(unsigned long int sec);
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);

void interrupt() {
   
    if((RCIE_bit) && (RCIF_bit)) {
        if(OERR_bit) {
              OERR_bit = 0;
              CREN_bit = 0;
              CREN_bit = 1;
        }

        gsmBuffer[gsmBufferIndex++] = UART1_Read();
        gsmBuffer[gsmBufferIndex] = '\0';

        RCIF_bit = 0;
    }    
}

char *CopyConst2Ram(char *dest, const char *src){
    char *d;
   
    asm clrwdt
    d = dest;
   
    for(;*dest++ = *src++;)asm clrwdt;

    return d;
}

void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}

void GSM_RESET() {
    asm clrwdt
    gsmReset = 1;    
    Delay_ms(20);
    gsmReset = 0;    
}

void Extract_Signal_Strength(char *s1, char *s2) {
    unsigned int i = 0;
   
    asm clrwdt
   
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
   
    asm clrwdt
   
    while(*s1 != ' ') {
           *s1++;
           asm clrwdt
    }

    *s1++;
   
    while(*s1 != ',') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';    
    asm clrwdt
}

void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;
   
    asm clrwdt
   
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
   
    asm clrwdt
   
    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            asm clrwdt
    }

    *s2 = '\0';                        
    asm clrwdt
}

void Get_SMS_Index(char *s1, char *s2) {

    while(*s1 != 'C') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'M') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'T') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'I') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != ',') {
           *s1++;
           asm clrwdt
    }

    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}

void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));        
        DelayXSec(2);        
        if((*gsmAttempt)++ == 3) {
            GSM_RESET();            
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
        }
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));    
    (*gsmAttempt) = 0;
    (*idx) = 0;
}

void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    asm clrwdt
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        DelayXSec(2);
       
        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;            
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }

    (*idx) = 0;

    Extract_SMS(s1, s6);
   
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    asm clrwdt
}

void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        asm clrwdt
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);
       
        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);
       
        DelayXSec(6);
    }

    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    asm clrwdt
}

void main() {

    asm clrwdt
    //OPTION_REG = 0x87;
   
    CMCON = 0x07;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x80;
    TRISD = 0x00;
    TRISE = 0x00;
   
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
   
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
       
    UART1_Init(9600);
    Delay_ms(100);
   
    RCIF_bit = 0;
    RCIE_bit = 1;
   
    PEIE_bit = 1;
    GIE_bit = 1;    
           
    while(1) {

        asm clrwdt
       
        switch(gsmState) {
            case 0:
                  while(gsmAttempt < 3) {
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));
                          gsmState = 1;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }                      
                     
                      gsmBufferIndex = 0;                          
                  }
                 
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;            
            case 1:
                  while(gsmAttempt < 3) {
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseJ, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //If new SMS  received
                          DelayXSec(1);                                                      
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 2;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));
                          gsmState = 1;
                          break;
                      }                      
                     
                      gsmBufferIndex = 0;                          
                  }
                 
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 2:
                  while(gsmAttempt < 3) {
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseF, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {   //If new SMS  received
                          DelayXSec(1);                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 2;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) { 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));
                          gsmState = 3;
                          break;
                      }
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) { 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));
                          gsmState = 3;
                          break;
                      }            
                     
                      gsmBufferIndex = 0;                          
                  }
                 
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
           
            case 3:            
                  break;
            case 4:      
                  break;        
        };      
    }
}
 
Top