PIC24FJ128GA306 + GM862 Quad

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Tue Apr 17, 2012 1:52 am

Hello everybody,

I'm very new with the GSM module and I'm trying to connect it with a PIC microcontroller. I got a tiny problem because I want to create functions depending of what the module answers to the AT commands sent by the PIC. For example if I ask it : "AT+CPIN?" I want to know if it answers +CPIN:SIM READY or +CPIN:SIM PIN...

Unfortunately, I can't receive more than 6 caracters with my "get string from UART" function. Actually if I put a 10 caracters string I just receive the first 6 caracters and then the last one is repeated 4 time (to have the 10 caracters string). For example if I right AZERTYAZER, it will answer me AZERTTTTTT. This is the code I'm using:

Code: Select all

char *getstringUART( char *s, int len)
{
    char *p = s;            // copy the buffer pointer 
    do{
        *s = getcharUART();       // wait for a new character
              
        if (( *s==BACKSPACE)&&( s>p))
        {
            putcharUART( ' ');     // overwrite the last character
            putcharUART( BACKSPACE);
            len++;
            s--;            // back the pointer
            continue;
        }
        if ( *s=='\n')      // line feed, ignore it
            continue;
        if ( *s=='\r')      // end of line, end loop
            break;          
        s++;                // increment buffer pointer
        len--;
    } while ( len>1 );      // until buffer full
 
    *s = '\0';              // null terminate the string 
    
    return p;               // return buffer pointer
} // getstringUART

// send a character to the UART2 serial port
int putcharUART( int c)
{
	while ( U2STAbits.UTXBF);   // wait while Tx buffer full
	U2TXREG = c;
	return c;
} // putcharUART
Then, this is the code to get the string in the main:

Code: Select all

putstringUART("AT+CPIN?");
	tempo();
	}
	getstringUART(s,7);
	putstringUART(s);
I hope you understand my problem.

Thank you very much for any help!!

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Tue Apr 24, 2012 12:06 am

No one to help me ? Anyone have another getString from UART function ?

Thank you

Joeisi
Posts: 418
Joined: Thu Apr 26, 2012 6:59 pm
Location: Lima, OH

Re: PIC24FJ128GA306 + GM862 Quad

Post by Joeisi » Thu Apr 26, 2012 7:04 pm

I would use a buffer instead of a string. You get more flexible with what you can do. You can buffer what comes in and out by reading and writing to the buffers in an ISR when the UART flags are set. This may be vague but if you get what I'm saying, you will be well on your way to a solution.

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Wed May 02, 2012 7:15 am

First of all thank you for your answer. So, you think that I should create an interrupt when I receive or send a char with a flag (flag=1 when a char is coming for example)and I put this char into a buffer. I'm pretty new with all this so if you can be even more precise it would be great for me :).

Thank you again for you answer !

Joeisi
Posts: 418
Joined: Thu Apr 26, 2012 6:59 pm
Location: Lima, OH

Re: PIC24FJ128GA306 + GM862 Quad

Post by Joeisi » Thu May 03, 2012 5:16 pm

Exactly. This may be a bit difficult at first but an important tool for the future.

Code: Select all

char pointer;
char data[10];
bit done;
void interrupt isr(void){
if(RCIF&RCIE){
data[pointer] = RCBUFF;
pointer++;
if(pointer == 10){
done = 1;
}
}
}

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Fri May 04, 2012 4:48 am

For this example, I can only receive a 10 characters string ? Not 9 or 11 ? So I'll have an interrupt with your code

Code: Select all

char pointer;
char data[10];
bit done;

void interrupt isr(void){
if(RCIF&RCIE){
data[pointer] = RCBUFF;
pointer++;
if(pointer == 10){
done = 1;
}
}
}
How can I adapt my getStringfromUart function with this ISR() to get it work in my code ?

Thank you for your time and sorry if I ask some newbie questions :)

Joeisi
Posts: 418
Joined: Thu Apr 26, 2012 6:59 pm
Location: Lima, OH

Re: PIC24FJ128GA306 + GM862 Quad

Post by Joeisi » Fri May 04, 2012 8:21 pm

Code: Select all

if(pointer != 0){
for(char i = 0, i<pointer,i++){
//Do all of your switches and such here with the character being read being data[pointer];
pointer--;
}
}
This should work.

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Mon May 07, 2012 2:19 am

Joeisi wrote:

Code: Select all

if(pointer != 0){
for(char i = 0, i<pointer,i++){
//Do all of your switches and such here with the character being read being data[pointer];
pointer--;
}
}
This should work.
Ok thank you, do you think this can work then ?

Code: Select all

/*ISR*/
char pointer=0;
char data[10];
bit done;

void interrupt isr(void)
{
	if(RCIF&RCIE)
	{
		data[pointer] = RCBUFF;
		pointer++;
		if(pointer == 10)
		{
			done = 1;
		}
	}
}

char *getstringUART( char *data, int len)
{
 	char *p = data[pointer];	//copy the buffer pointer 

	if(pointer != 0)
	{
		for(char i = 0, i<pointer,i++)
		{
	   	    do
			{
		        *data[pointer] = getcharUART();       // wait for a new character
		        if (( *data[pointer]==BACKSPACE)&&( data[pointer]>p))
		        {
		            putcharUART( ' ');     // overwrite the last character
		            putcharUART( BACKSPACE);
		            len++;
		            pointer--;            // back the pointer
		            continue;
		        }
		        if ( *data[pointer]=='\n')      // line feed, ignore it
		            continue;
		        if ( *data[pointer] =='\r')      // end of line, end loop
		            break;          
		        pointer++;                // increment buffer pointer
		        len--;
			} while ( len>1 );      // until buffer full
			 
			    *data[pointer] = '\0';              // null terminate the string 
			    return p;               // return buffer pointer
		
				//Do all of your switches and such here with the character being read 
				//being data[pointer];
			
			}
	}
}

Joeisi
Posts: 418
Joined: Thu Apr 26, 2012 6:59 pm
Location: Lima, OH

Re: PIC24FJ128GA306 + GM862 Quad

Post by Joeisi » Mon May 07, 2012 3:32 pm

It looks like it. Try it out and tell me how it works!

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Wed May 09, 2012 7:40 am

Joeisi wrote:It looks like it. Try it out and tell me how it works!
I'm sorry to bother you but I'm having some troubles with the code:

Code: Select all

	if(pointer != 0)
	{
		for(i = 0;i<pointer;i++)
		{
	   	    do
			{
		        *data = getcharUART();       // wait for a new character
Do I have to indicate *date[pointer] or *data or *data ? In the first code, it was just "*s" but now that I have an array this is not exactly the same, isnt it ?

Code: Select all

    if (( *data==BACKSPACE)&&( *data>p))
Same question for this case

Code: Select all

	{
		            putcharUART(' ');     // overwrite the last character
		            putcharUART( BACKSPACE);
		            len++;
		            [color=#0000FF]data--;            // back the pointer[/color]
		            continue;
Do I have to decrease "data" or maybe "i" or "pointer"

Code: Select all

  }
		        if ( *data =='\n')      // line feed, ignore it
		            continue;
		        if ( *data =='\r')      // end of line, end loop
		            break;
				data++;          
				len--;
			} 
			while ( len>1 );      // until buffer full
			 
			*data = '\0';              // null terminate the string 
			return p;               // return buffer pointer
Same question here, do I have to keep *data or *data or *data[pointer]

Code: Select all

pointer--;                // decrement buffer pointer
When do I use the "i" variable ? And when do I use the "pointer" variable, because I use a for(i...)

I hope you understand my questions. Thank you very much again for your time !

jremington
Support Volunteer
Posts: 2137
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: PIC24FJ128GA306 + GM862 Quad

Post by jremington » Wed May 09, 2012 1:36 pm

You don't need interrupts. The example code that you first posted appears to contain errors.
You should start with working example code, and study it carefully to learn about pointers and arrays in C,
which can be very confusing. Here are a couple of links to on-line tutorials; I'm sure there are many more.
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
http://boredzo.org/pointers/

Joeisi
Posts: 418
Joined: Thu Apr 26, 2012 6:59 pm
Location: Lima, OH

Re: PIC24FJ128GA306 + GM862 Quad

Post by Joeisi » Wed May 09, 2012 2:25 pm

Well, I tried. You get the point though right?

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Wed May 09, 2012 11:55 pm

jremington wrote:You don't need interrupts. The example code that you first posted appears to contain errors.
You should start with working example code, and study it carefully to learn about pointers and arrays in C,
which can be very confusing. Here are a couple of links to on-line tutorials; I'm sure there are many more.
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
http://boredzo.org/pointers/
I should learn a bit more about pointers that is true but if I remember correctly the example code I first posted come from Microchip example sources. Can you tell me what is wrong about this code ?

Thank you :)

jremington
Support Volunteer
Posts: 2137
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: PIC24FJ128GA306 + GM862 Quad

Post by jremington » Thu May 10, 2012 6:51 am

In your main program, how is "s" declared?
You say you want to input 10 characters, but what do you suppose the "7" is doing in the following line?

Code: Select all

getstringUART(s,7);

NeZo
Posts: 9
Joined: Tue Apr 17, 2012 12:07 am

Re: PIC24FJ128GA306 + GM862 Quad

Post by NeZo » Thu May 10, 2012 7:35 am

jremington wrote:In your main program, how is "s" declared?
You say you want to input 10 characters, but what do you suppose the "7" is doing in the following line?

Code: Select all

getstringUART(s,7);
I declare "s" in the void main :

Code: Select all

int main(void) 
{	
	UART2Init();	// Initialize UART2 for 19200,8,N,1 TX/RX

	char s[30];
	getstringUART(s,11);
	putstringUART(s);

	return 0;
}
I took s[30] as an example and I want to be able to write the word "AZERTAZER" which has 10 letters + the "/0" so 11 characters. Because if I indicate

Code: Select all

getstringUART(s,sizeof(s))
, it will expect 30 characters until it works and I just need 10.
In my first example I took 7 but it's better to try with 11 to see the real problem.

I hope you understand my point

Post Reply