PIC to MAX232

bwack

Joined Nov 15, 2011
113
Now I get it, ....I am getting "OK" and it is breaking the while(1). Silly me...Why did I not see that. :(

thanks @bwack
Good, no worries. If his code is too large, try figure out what function calls eat much memory by commenting out stuff and rebuilding. For example, use of int and long when you can get past with char, or functions which use software-multiplication/divison when you really don't need it. Another thing you can do is to open the listing window and look for subroutines like __div16x16 __mul16x16 etc. Try avoid using * / % operators or library functions which does use them to eliminate these subroutines. atoi() might actually use one of these subroutines. Maybe the buffers in jay's code can be reduced.

I'd like to add something I forgot regarding the atoi(). Once you have ascii to integer you might want to convert the integer back to decimal string for the lcd. There is a library function for that, but it uses modulo and division. If you have the memory space and time for it is fine (or you already use */% or atoi or similar), but I can give you mine which uses only addition and sub. It wont help though if you have a line somewhere which already calls multiplication or divison ( or modulo). This is a fast integer to five digit decimal string function and doesn't require much program memory. I don't remember what the techique is called. It sort of replaces division by n subroutine by calling subtraction n times instead. (For those who are coming from a google or other search, this is "fast" because we are here using a microcontroller which does not have mul and div instruction opcodes)

C:
void int2dec( unsigned short *array, unsigned int number,
              unsigned short factor, unsigned int offset) {
  unsigned short i=0;

  while(number>=10000u) {
    number=number-10000u;
    i++;
  }
  array[0] = i+0x30;
  i=0;
  while(number>=1000u) {
    number=number-1000u;
    i++;
  }
  array[1] = i+0x30;
  i=0;
  while(number>=100u) {
    number=number-100u;
    i++;
  }
  array[2] = i+0x30;
  i=0;
  while(number>=10u) {
    number=number-10u;
    i++;
  }
  array[3] = i+0x30;
  array[4] = number+0x30;
  array[5]=0;
}
https://github.com/bwack/CASEFANCTRLx8/blob/master/PCFanCTRLx8.c

I can rewrite it for three decimal char. I might have a code for that already somewhere on the other computer, but I need to read now.

Hans
 
Last edited:

bwack

Joined Nov 15, 2011
113
Found the int2dec, or rather char2dec_8 which use no div or mul instruction opcodes

C:
void char2dec( char *array, char _number) {
  char i=0;
  while(_number>=100u) {
    _number=_number-100u;
    i++;
  }
  array[2] = i;
  i=0;
  while(_number>=10u) {
    _number=_number-10u;
    i++;
  }
  array[1] = i;
  array[0] = _number;
}
Warning though, it idoesn't terminate the string with 0.

It was part of a video demonstration to speed up a rotary knob and int2dec routine for pic16f trying all sorts of things..
http://pastebin.com/7FyWwn71
 

bwack

Joined Nov 15, 2011
113
Here is the project.
Thanks. I opened the project settings, and it says pic16f877a. Remember to include the .cfg file for project configuration.
Anyway, I have the mikroC demo version and the 2k program words limit was exceeded. I commented out all LCD_Out and it went down to 1.8k words and compiled fine. I can't understand what takes so much space.. I wish the customer could change the mcu, but if not then a different approach is needed. Could you make the list of const strings shorter ? I mean would it help to just check for +COPS etc and then look for numeric chars at specific array offsetts instead ? For example after checking for +COPS in the received string you could check for length to dermine if it has more info.

Edit: After changing the mcu to pic16f887, I get error undeclared identifier CMCON. Another thing, this is also a 8k program words device and same size memory space as the 877, so maybe it is just that rifa dosn't have the full version of mikroC ... ?
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@bwack I will check your suggestions.
And I did what you asked and the debugger does show that it is parsing all the way until the "CR" . cause I dunno what char to put instead of CR. ?????
I wonder why the original is not parsing and yest I did comment out the break:
It behaves the same way still.

@jayanthd
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[40];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 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 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+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
//const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
//const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "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[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

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;
       
    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 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 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) {

            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;           
        }
    }

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


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:
                 
                  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;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                     
                 
                  gsmBufferIndex = 0;  
                  gsmAttempt = 0;
                  break;           
            case 1:
                 
                  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;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                                         
                     
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 2:
                 
                  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;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                     
                     
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
           
            case 3:           
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) {   //If new SMS  received
                      DelayXSec(1);
                      Extract_Signal_Strength(gsmBuffer, signalStrength);
                     
                      signal_strength = atoi(signalStrength);
                     
                      if(signal_strength == 99) {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));
                          gsmState = 3;
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;
                      }                                      
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }
                              
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:     
                  break;       
        };     
    }
}
I am using F887 not that it is a problem
Problem is the code above compiles but it is missing what I need..
You can see I have commented out most of the part and still it is compiling...I do not understand this.
I think you added most of the stuff to an existing project, if that is the case it won't do any good to me.
I clearly stated what I needed.
I am not complaining here
You took the initiative and said you would help but I am sorry I am got more confused here.

@bwack I am using the MikroC Pro full version. I can compile till the uC spills. :D
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Here is my F887 Project settings

CSS:
/*--------------------------------------------------------------------------------
Configuration Registers ;
CONFIG1 :$2007 : 0x23F4
CONFIG2 :$2008 : 0x0700
Oscillator : Internal 8MHz. RA6 & RA7 as Digital I/O.
Watchdog Timer : Disabled.
Power-up Timer : Disabled.
Master Clear : Enabled.
Code Protection : Disabled.
Data Protection : Disabled.
Brown Out Reset : Enabled at 4.0V.
Int. Ext. Switch : Disabled.
Failsafe CLK : Disabled.
LVP : Disabled.
ICD : Disabled.
Flash Program Self Write: Disabled.
--------------------------------------------------------------------------------
*/
void main(){
  OSCCON = 0x77;                 // 8MHz Internal Osc.
  ANSEL = 0x03;                  // RA0 & RA1 as Analog Rest as Digital IO.
  ANSELH = 0x00;                 // Rest as Digital IO.
  CM1CON0 = 0x07;                // Disable Comparator 1.
  CM2CON0 = 0x07;                // Disable Comparator 2.
  PORTA = 0;                     // Clear PORT A.
  PORTB = 0;                     // Clear PORT B.
  PORTC = 0;                     // Clear PORT C.
  PORTD = 0;                     // Clear PORT D.
  PORTE = 0;                     // Clear PORT E.
  TRISA = 0xFF;                  // PORT A as Input.
  TRISB = 0x00;                  // PORT B as Output.
  TRISC = 0xF8;                  // Later.
  PORTDimg = 0x2F;               // PORT D Shadow, Identical to TRISD.
  TRISD = 0x2F;                  // RD <0 - 3 & 5> as Input, Rest as Output.
  TRISE = 0xFF;                  // PORT E as Input.
//------- Initialte ADC & LCD & UART -------------------------------------------
  ADC_Init();                    // Innitiate ADC.
  VCFG0_bit = 1;                 // Externel Vref +.
  VCFG1_bit = 0;                 // Internal Vref - as GND.
  Lcd_Init();                    // Innitiate LCD.
  Lcd_Cmd(_LCD_CLEAR);           // Clear LCD.
  Lcd_Cmd(_LCD_CURSOR_OFF);      // Cursor OFF.
  Delay_ms(100);                 // Wait to Stabilize.
  UART1_Init(9600);              // Initialize UART module at 9600 baud.
  Delay_ms(100);                 // Wait for UART module to stabilize.
//------- Clear IRQ and Fire up the Timers -------------------------------------
  OPTION_REG = 0x81;             // No pullups, Timer 0 at Prescaler 4.
  INTCON = 0;                    // Disable All Interrupts First.
  PIE1 = 0;                      // Disable All Peripheral Interrupts.
  PIE2 = 0;                      // Disable All Peripheral Interrupts.
  INTCON.T0IE = 1;               // Enable the Timer0 Interrupt.
  INTCON.T0IF = 0;               // Clear Timer 0 Register Overflow Flag.
  TMR0 = TMR0set;                // Load Timer 0 with Preset.
  T1CON = 0x30;                  // No gate, Int Tcyc/8, TMR1 Stopped.
  TMR1H = TMR1L = 0;             // Timer counts up to CCP value.
  CCPR1 = 625;                   // 1usTcyc * 8 Prescale * 625 = 5msec.
  CCP1CON = 0x0B;                // NO Hardware PWM.
  TMR1IF_bit = 0;                // PIR1 reg, Clear the TMR1 Overflow IRQ.
  CCP1IF_bit = 0;                // Clear TMR1 IRQ flag bit.
  T1CON.TMR1ON = 1;              // Start TIMER 1.
  TMR1IE_bit = 1;                // Enable the Timer1 overflow Interrupt.
  CCP1IE_bit = 1;                // Enable the CCP1 Interrupt.
  INTCON.GIE = 1;                // Global Interrupt Enabled.
  Backlit_ON;                    // Turn ON LCD Back light.
  Beep(SU_Beep);                 // Start UP Beep.
while(1){
}
Just in case @bwack
My Project is using without the modem part, 27% of DATA space and 55% of ROM
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
ca any one tell me
C:
const GSM_OK = 0;
what does that mean...?
I know it returns 0 but if I use
C:
char GSM_OK = 0;
Would it work ?
I know char is 0-255..!
so what's the deal ?
 

bwack

Joined Nov 15, 2011
113
const GSM_OK = 0;
GSM_OK is allocated in memory by the compiler, but any attempt to write to it elsewhere in your code will give you an error message in the compiler.

Woops, const GSM_OK seems to be the same as #define GSM_OK 0

#define GSM_OK 0
GSM_OK is now a definition, and wherever you use GSM_OK in your code, the preprocessor will replace GSM_OK with 0 as if you wrote 0

I think you can say the difference is that const GSM_OK uses ram or rom, and #define GSM_OK 0 used in your code, the compiler will put 0 in the program operand next to the opcode in the program memory, like this

#define GSM_OK 0

state = GSM_OK;
// movlw 0 ; move 0 to wreg (working register)
// movwf state; move wreg to location of state variable

but if it was const GSM_OK = 0 then

state = GSM_OK
// movf GSM_OK, 0 ; move GSM_OK memory location to wreg
// movwf state; move wreg to memory location of state.
edit: actually it is much more hairy than this because GSM_OK is now stored in program rom, and to get to it there is a handfull of operations.. unfortunately..


open your listing window, you'll this happen there
Some examples from the listing of jay's code:

Code:
;GSM Based Device Control.c,335 ::                 TRISA = 0x00;
0x02CF        0x0185              CLRF       TRISA
;GSM Based Device Control.c,336 ::                 TRISB = 0x00;
0x02D0        0x0186              CLRF       TRISB
;GSM Based Device Control.c,337 ::                 TRISC = 0x80;
0x02D1        0x3080              MOVLW      128
0x02D2        0x0087              MOVWF      TRISC
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I should know that...it's const and it is in RAM or is it ROM....same way as writing LCD messages.

So how many bytes is that ?
Same as a "char"?
 

bwack

Joined Nov 15, 2011
113
Last edited:

bwack

Joined Nov 15, 2011
113
Ok I tried it. Makes sense now. It doesn't have a size !

const bwack = 39;

and

gsmState = bwack;

The listing that the compiler produces:

Code:
;GSM Based Device Control.c,375 ::  gsmState = bwack;
0x031A  0x3027  MOVLW  39
0x031B  0x1283  BCF  STATUS, 5
0x031C  0x00A8  MOVWF  _gsmState
Notice how 39 is moved into W.. It just says 39 as if it was taken out of the blue, but it is not, it is not stored in a fixed location like variables, but right next to the opcode (the actual machine code is 0x3027 where 30 is movlw and 27 is hex for 39).. :D
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
There is IntToStr() and ByteToStr() in mikroC PRO.

What are you confused with regarding my code ? I wrote the complete code for you. The GSM Library was written 4 years ago and I modified it for your project. Also the code I gave you I wrote yesterday for PIC16F887. Did you connect the modem to PIC and test my code ?

For COPS response I have irectly used the response for displaying LCD messages without extracting the network ID. If you want to extract the different fields of the COPS response then I can do it.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
I am writing comments to the code. It will be easier for you to understand the working.

I will soon post the complete code.

C:
switch(gsmState) {
            case SEND_AT:                  
                  //Send AT and check for OK
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received
                      DelayXSec(1);                                                  
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY                          
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT  
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT                        
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT  
                          break;
                      }              
                  }                      
                 
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case SEND_AT_CPIN_QUERY:
 

jayanthd

Joined Jul 4, 2015
945
Slowly the commenting is completing.

C:
        switch(gsmState) {
            case SEND_AT:                  
                  //Send AT and check for OK
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received
                      DelayXSec(1);                                                  
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY                          
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT  
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH OK
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT                        
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT  
                          break;
                      }              
                  }                      
                 
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case SEND_AT_CPIN_QUERY:
                  //Send AT+CPIN? and check for response +CPIN: READY
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseJ, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //OK received
                      DelayXSec(1);                                                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));   //SIM Ready on LCD
                      gsmState = 2;   //Set state AT_COPS_QUERY                        
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) { //ERROR received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));  //Print SIM Removed on LCD
                      gsmState = 1;     //Set state to SEND_AT_CPIN_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH OK
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received 
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE)); //Print OK on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY                          
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY 
                          break;
                      }              
                  }                                          
                     
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case AT_COPS_QUERY:
 

jayanthd

Joined Jul 4, 2015
945
Here is the latest code with comments.

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

#define SEND_AT 0
#define SEND_AT_CPIN_QUERY  1
#define AT_COPS_QUERY 2
#define AT_CSQ_QUERY 3

// 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[40];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 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+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";
const char atResponseP[] = "AT+COPS?\r\n\r\n+COPS: ";
const char atResponseQ[] = "ATH\r\n\r\n";
const char atResponseR[] = "AT\r\n\r\n";
const char atResponseS[] = "AT+CPIN?\r\n\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[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

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;
       
    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);        
        (*idx) = 0;
       
        /*        
        if((*gsmAttempt)++ == 2) {
            //GSM_RESET();            
            //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;            
        }
        */        
    }

    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 SEND_AT:                  
                  //Send AT and check for AT\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received                                                                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY                          
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT  
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received                                                                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT                        
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT  
                          break;
                      }              
                  }                      
                 
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case SEND_AT_CPIN_QUERY:
                  //Send AT+CPIN? and check for response AT+CPIN?\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseS, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //OK received                                                                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));   //SIM Ready on LCD
                      gsmState = 2;   //Set state AT_COPS_QUERY                        
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) { //ERROR received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));  //Print SIM Removed on LCD
                      gsmState = 1;     //Set state to SEND_AT_CPIN_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                                           
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received                                                                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE)); //Print OK on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY                          
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY 
                          break;
                      }              
                  }                                          
                     
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case AT_COPS_QUERY:
                  //Send AT+COPS? and check for "AT+COPS?\r\n\r\n+COPS: "
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1); 
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {  //"AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n"; received                                                
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                      gsmState = 2;   //Set state to AT_COPS_QUERY                        
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));  //Print "Dhi Mobile" on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY 
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));  //Print Oordeoo on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received                                                                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY                          
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY
                          break;
                      }              
                  }                      
                     
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break                              
            case AT_CSQ_QUERY:            
                  //Send AT+CSQ? and check for response AT+CSQ\r\n\r\n+CSQ: 
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) { //AT+CSQ\r\n\r\n+CSQ:  received                    
                      Extract_Signal_Strength(gsmBuffer, signalStrength); //Extract Singnal Strength
                     
                      signal_strength = atoi(signalStrength);   //Convert Signal Strength Ascii to integer
                     
                      if(signal_strength == 99) {       //Signal Strength is 99
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));   //Print No Network on LCD
                          gsmState = 3;   //Set state to AT_CSQ_QUERY
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;   //Set state to next state - (not coded)
                      }                                      
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) { //ATH OK received                                                                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY                          
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) { //ERROR received                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY
                          break;
                      }              
                  }
                               
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:      
                  break;        
        };      
    }
}
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Ummm ! when I try to compile it does say F877.
But I can change that.

@jayanthd I did not mean to offend, bu I apologize if it felt that way.
I got confused that after commenting out so much messages, it compiled.

The commenting u did above did put a lot in perspective.
It makes sense to me. You know those long functions confuses me. I never use long ones, I try to keep the as short as possible.

I hope you can take time to comment on rest of the coding. It will help a lot
Thanks a lot of the effort.
I appreciate it.
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@jayanthd I will not try your but I will write a fresh one looking at yours. This will help me a lot.
This code I can post here if any thing comes up.
I hope you will be here to help me out on this. :)
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@jayanthd
Few Questions
C:
const char atResponseQ[] = "ATH\r\n\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";
The above two are same but I dunno how the modem response to the ATH command.
I will check tonight with Realterm, and use the result..OK?


C:
const char atCommandD[] = "AT+CSQ\r";
There are 2 ways the modem responses to the above command
One
C:
const char atResponse[] = "AT+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n;
This means the no network or there is an error.
The first "99" is the characters that need to be extracted or compared to detect the error
Basically I will display infinity character on LCD instead of decimal digits if it is 99

Second
C:
const char atResponse[] = "AT+CSQ\r\n\r\n+CSQ: 18,0\r\nOK\r\n;
This means the Signal strength is "18" and it is these two characters that I need to use to convert to show signal strength in LCD

This part is a bit confusing from your code.
Can you please clarify these parts.
 

jayanthd

Joined Jul 4, 2015
945
Hi

There was a mistake in one of the blocks. Fixed it. Here is the new code. Have checked full code. No more bugs.

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

#define SEND_AT 0
#define SEND_AT_CPIN_QUERY  1
#define AT_COPS_QUERY 2
#define AT_CSQ_QUERY 3

// 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[40];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 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+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";
const char atResponseP[] = "AT+COPS?\r\n\r\n+COPS: ";
const char atResponseQ[] = "ATH\r\n\r\n";
const char atResponseR[] = "AT\r\n\r\n";
const char atResponseS[] = "AT+CPIN?\r\n\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[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

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;
     
    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);      
        (*idx) = 0;
     
        /*      
        if((*gsmAttempt)++ == 2) {
            //GSM_RESET();          
            //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;          
        }
        */      
    }

    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;
 
    CM1CON0 = 0x00;
    CM2CON0 = 0x00;
 
    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 SEND_AT:                
                  //Send AT and check for AT\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
               
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received                                                                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY                        
                  }                    
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                   
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received                                                                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT                      
                      }                    
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                        
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT
                          break;
                      }            
                  }                    
               
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case SEND_AT_CPIN_QUERY:
                  //Send AT+CPIN? and check for response AT+CPIN?\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseS, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
               
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //OK received                                                                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));   //SIM Ready on LCD
                      gsmState = 2;   //Set state AT_COPS_QUERY                      
                  }                    
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) { //ERROR received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));  //Print SIM Removed on LCD
                      gsmState = 1;     //Set state to SEND_AT_CPIN_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                                         
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received                                                                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE)); //Print OK on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY                        
                      }                    
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                        
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY
                          break;
                      }            
                  }                                        
                   
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case AT_COPS_QUERY:
                  //Send AT+COPS? and check for "AT+COPS?\r\n\r\n+COPS: "
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseP, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
               
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {  //"AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n"; received                                              
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                      gsmState = 2;   //Set state to AT_COPS_QUERY                      
                  }                    
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));  //Print "Dhi Mobile" on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));  //Print Oordeoo on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                   
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received                                                                        
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY                        
                      }                    
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY
                          break;
                      }            
                  }                    
                   
                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break                            
            case AT_CSQ_QUERY:          
                  //Send AT+CSQ? and check for response AT+CSQ\r\n\r\n+CSQ:
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
               
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) { //AT+CSQ\r\n\r\n+CSQ:  received                  
                      Extract_Signal_Strength(gsmBuffer, signalStrength); //Extract Singnal Strength
                   
                      signal_strength = atoi(signalStrength);   //Convert Signal Strength Ascii to integer
                   
                      if(signal_strength == 99) {       //Signal Strength is 99
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));   //Print No Network on LCD
                          gsmState = 3;   //Set state to AT_CSQ_QUERY
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;   //Set state to next state - (not coded)
                      }                                    
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                   
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) { //ATH OK received                                                                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY                        
                      }                    
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) { //ERROR received                        
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY
                          break;
                      }            
                  }
                             
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:    
                  break;      
        };    
    }
}



Here is the explanation of a block

C:
const char atCommand1[] = "AT\r";
const char atResponseR[] = "AT\r\n\r\n";
const char atResponse1[] = "AT\r\n\r\nOK\r\n";


case SEND_AT:                
                  //Send AT and check for AT\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);
               
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received                                                                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY                        
                  }                    
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                        
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);
                   
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received                                                                          
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT                      
                      }                    
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received                        
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT
                          break;
                      }            
                  }
In the line

[cpde=c]
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
[/code]

AT\r command is sent and checked for AT\r\n\r\n and so if either
AT\r\n\r\nOK\r\n

or

AT\r\n\r\nERROR\r\n is received then buffer contains the actual response and then in the outer

C:
if(strstr(...
else if(strstr(...
block it is tested if

AT\r\n\r\nOK\r\n was received or
AT\r\n\r\nERROR\r\n was received

and based on that gsmState is set.


If you need more explanation then I can do it.


Similarly when it is testing for a response if Rin\r\n\r\n was received then

ATH\r is issued and checked if ATH\r\n\r\n is received.

If ATH\r\n\r\n was received then it is further tested if

ATH\r\n\r\nOK\r\n or

ATH\r\n\r\nERROR\r\n was received and based on this LCD messages are outputted and gsmState is set.


You can modify the code inside ISR to use ring buffer. It will be better.
 
Top