# Interfacing GSM Sim900a with PIC16f887

#### electronicnoob

##### Newbie level 5
Hi, I'm currently working on a project. I'm using a PIC16f887 microcontroller to help me send a sms to the user. It is supposed to allow the user to enter their phone number and it will send a sms to the user. The keypad is working fine but I have problems with the GSM.

The code I've attached below is my main code.
Code:
void main(void)
{
int t,a,b;
t = a = b = 0;
static char Duration[3];
static char PHnum[]='+6          0; //my country code is +6 and i print a 0 at the end to stop the LCD from displaying
gibberish and 10 empty spaces. This will be edited later
char Key = 'n';

LCD_Begin();
InitGSM();
LCD_Cmd(LCD_CLEAR);

LCD_Print("Enter Phone No.");
while(a<12){
Key = switch_press_scan_num();
if ((Key == '#')&&(a>0))
{
PHnum[a-1]=' ';
LCD_Goto(1, 2);
LCD_Printr(PHnum);
__delay_ms(200);
a--;
}
else if (Key != '#')
{
LCD_Goto(1, 2);
PHnum[a] = Key;
LCD_Printr(PHnum);
__delay_ms(200);
a++;
}
};
LCD_Cmd(LCD_CLEAR);
while (b<12){
phoneno[b]=PHnum[b];
b++;
}
sms("Hello World");
}
The code I've attached below is the code for the GSM.
Code:
void InitGSM(){
TRISCbits.TRISC6 = 0; //Set RC6 (TX) as output
TRISCbits.TRISC7 = 1; //Set RC7 (RX) as input
TXSTA = 0b00100100;
RCSTA = 0b10010000;
BAUDCTL.BRG16 = 1;
SPBRG = 16; //Baud rate 115200 @ 8 MHz
TXIF = RCIF = 0; //Disable interrupt(For reseting purpose)
}

void sms(unsigned char *msg){
tx_str("AT\r\n");
__delay_ms(500);

tx_str("AT+CMGF=1\r\n"); //Enter GSM SMS TEXT MODE
__delay_ms(500);

tx_str("AT+CMGS="); //SEND SMS to Phone number
tx('"');

for(int l = 0; l <= 9; l++){
tx(phoneno[l]); //Enter phone number with size 10 array
}

tx('"');
tx_str("\r\n"); //After enter will be entering SMS
__delay_ms(1000);

while(*msg)
tx(*msg++); //Send SMS
tx(0x1A); //CTRL Z ASCII code to send
__delay_ms(500);
}

void tx(char a) //Function to send one byte of data to UART
{
while(TXIF==0) continue; //Hold the program until TX buffer is free to transmit
TXREG=a;
}

unsigned char rx(void){
while(PIR1bits.RCIF == 0); //Hold the program until RX buffer is free to receive
return RCREG;
}

void tx_str(const char *s) //Function to convert string to byte
{
while(*s)
tx(*s++);
}
The code is supposed to allow for 115200 baud rate at 8Mhz

Can anyone help me check my code. Thanks, really appreciate it.

#### FvM

##### Super Moderator
Staff member
Is the modem setup for 115k? If not, tx_str("AT\r\n") doesn't achieve autobaud, you have to send "A" and "T" with pause after each character.

Generally, it's difficult to debug modem code sending blindly. You better monitor RX and TX lines, as suggested in your previous thread. I can't e.g. well recognize if you send a meaningful phone number to the modem. Waiting 1000 ms after +CMGS command may be too short if the network is weak, it's better to wait for the ">" response and generate an error if it's not coming. I also prefer to await each "OK" response in modem communication.

#### thunderboympm

##### Full Member level 5
I have two doubts in your code,
1) is the board rate of modem is 115200?
2) are you sending the phone number array with its ascii code or as decimal numbers?

#### Aussie Susan

Does the modem return the "OK" (or error) message after any of the lines you send to it? If so are you feeding the characters to the Rx side of the UART - if you are and not reading them then (especially with some of these very old MCUs but I'm not certain about this particular one) the UART can stall until the error condition (overrun in this case) is cleared.
As always with problems like this, the key is in the code that you don't show us. I strongly suggest that you create a small but complete app that has the problem and show us all of that. (This has the advantage that often such a small app will NOT have the problem but it shows where the issue is in your original code.)
Susan

#### electronicnoob

##### Newbie level 5
@Aussie Susan, the GSM900 was able to reply Ok when I tried to check for feedback using serial monitor. When I connect my USB to TTL module to the PIC it does not return Ok. After that I tried with 20Mhz external oscillator and serial monitor it, it returns with inverted question marks. What could the problem be

#### KlausST

##### Super Moderator
Staff member
Hi,

* baud rate mismatch
* wrong interface setup (8N1)
* wrong voltage levels
* wrong wiring

Klaus

#### FvM

##### Super Moderator
Staff member
the GSM900 was able to reply Ok when I tried to check for feedback using serial monitor. When I connect my USB to TTL module to the PIC it does not return Ok.
Is it so hard to report the performed tests clearly?

"I tried to check for feedback using serial monitor" - What does this exactly mean? Command was send from PIC or PC? Did you verify correct command send from PIC with monitor?

"When I connect my USB to TTL module to the PIC it does not return Ok" - How did you connect it?

- - - Updated - - -

I already mentioned possible autobauding problem. If modem isn't setup to right Baud rate, you can type AT + carriage return at a terminal and the modem switches to the terminal rate. Sending "AT\r\n" from PIC will not perform autobaud.