PIC to MAX232

jayanthd

Joined Jul 4, 2015
945
It will be better if you debug and see if yourself.

Use it like this.

In the first line of the void main() place this code.

C:
char msg[40];
const char msg1[] = "AT+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
void main() {

Extract_Signal_Strength(gsmBuffer, CopyConst2Ram(msg, msg1));

}
then compile the code and start debugger

Now select gsmBuffer variable and then right click it and add it to Watch List.

In watch list select gsmBuffer and click the ellipsis button and set it to display char.

Now use F8 to get to the Extract_Signal... line and then press F7. You end up inside the function. Here use F8 and step slowly and look at the watch list. You will see how the value is extracted.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I am cleaning up the code...have a itsy bitsy q?
C:
void Modem_Init(){
  switch(GSM_State){
   case 0:        
    // DO SOMETHING
      GSM_State = 1;

   case 1:
     // DO SOMETHING
      GSM_State = 2;

   case 2:
    InitModem_Flag = 0;
    GSM_State = 0;
    CheckModem_Flag = 1;
  }
}
This is a one time routine
Runs while(InitModem_Flag){}
At RESET it enters, and exits once done.
Question is ..Do I need a "break;"....if so where ?
 

bwack

Joined Nov 15, 2011
113
How does this work ?
C:
void Extract_Signal_Strength(char *s1, char *s2){
  unsigned int i = 0;
  while(*s1) { // while not at null termination
    if(*s1 == '\n') ++i; // count newlines in s1
    *s1++; // move to next character
    if(i == 2)break; // break if two newlines found
  }
  while(*s1 != ' '){ // move pointer until space char and continue
    *s1++;
  }
  *s1++; // skip space char
  while(*s1 != ','){ // copy first token in the comma delimited list to s2.
    *s2++ = *s1++; // copy character then increment pointers
  }
  *s2 = '\0'; // terminate string s2
}
I added comments. Look up pointers in c programming literature, and for interest sake also look at indirect addressing mode in the PIC16F cpu instruction set. Pointers and arrays are related to that mode. I'm having an exam in FPGA programming on monday. Will get back to this tread after that. I would like to build a gps gsm device for theft protection so this discussion is interesting to me.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
He is using GSM_State in two different switch statements. One outside while(1) and one inside while(1). he is setting GSM_State to 0 before entering while(1) loop for use with the second switch statement.
 

jayanthd

Joined Jul 4, 2015
945
I think he wants to do all the cases in sequence and according to his GSM_State values he doesn't need break statements. Actually he doesn't need the switch statement itself. He can straight away write the code.
 

jayanthd

Joined Jul 4, 2015
945
I think he wants to do all the cases in sequence and according to his GSM_State values he doesn't need break statements. Actually he doesn't need the switch statement itself. He can straight away write the code.

Ok. I understood. He has only one big switch statement inside a function inside the while(1) loop and this is causing the error because stack is overflowing due to the ModemInit() function. He needs to put the switch statement outside that wrapping function.


If he uses my code then he needs break statements because in my code if error response is received then it has to execute the same case again till it gets OK response.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@nerdegutta
Welcome back Buddy..It's never too late. Never realize you became a Mod...Congratulations. :)

The coding is going on nicely..
Ran into a hiccup....
The LCD is showing SIM properly but it is always registering to wrong network. Network is correct but LCD is showing wrong..Triple check the code but nothing is a miss.
After trying a lot I changed


C:
char GSM_Buffer[41]; 
char buffer[41];
const char ATR04B[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n";   // DhiMobile
const char ATR04C[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n";   // Ooredoo
COPS has 41 characters so I changed the buffer to [41] giving space to '/0' character.
Is it OK. :confused:

And Now I can see the Correct Network Name :D
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Aah I see. I read wrong..
so
char GSM_Buffer[37]; ??

By the way I just changed SIM and LCD changed accordingly

This is nice..finally I am getting somewhere.
And now for network strength coding
 

jayanthd

Joined Jul 4, 2015
945
Aah I see. I read wrong..
so
char GSM_Buffer[37]; ??

By the way I just changed SIM and LCD changed accordingly

This is nice..finally I am getting somewhere.
And now for network strength coding
GSM_Buffer[] size should be enough to hold the lengthiest response.

Yes, 37.
 

jayanthd

Joined Jul 4, 2015
945
If you don't specify the size then compiler will do it for the required size.

You have to define sizes for these two

  1. char GSM_Buffer[41];
  2. char buffer[41];
and also for other buffers.
 

jayanthd

Joined Jul 4, 2015
945
40 is enough as 37 fits in it. You have to clear gsmBufferIndex after you comeout of the GSM_Send_Const_AT() function.

Nope I need to specify size. I am getting
"Array dimension must be greater than 0"
For buffer you have to specify size as needed to hold the lengthiest response but for ROM string you don't have to specify size.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I finally go the strength part working
Take a look...
The LCD is updated every 3 sec. The Signal does change a bit, as my modem antenna is just a wire.
D1.jpg
D2.jpg O1.jpg O2.jpg

Nice huh !
Now to send SMS.
 
Top