• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing GSM Sim900a with PIC16f887

Swsean

New Member
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[11];
    char Key = 'n';
    
    LCD_Begin();
    InitKeypad();
    InitGSM();
    LCD_Cmd(LCD_CLEAR);
    
    LCD_Print("Enter Phone No.");
    while(a<10){
        Key = switch_press_scan_num();
        if ((Key == '#')&&(t>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<10){
        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 |= 0b00001000;
    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.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Why 115200 baud?, GSM is extremely slow, and if you want to waste the serial speed at 115200 you MUST use a crystal oscillator, in order to get the required accuracy.

Why not just use 9600 baud?, it's still loads faster than you need, and works perfectly from the internal oscillator.

Without excessively analysing your existing code, I would suggest your delays are much too short - it's also best to wait until you get the response back from the modem, rather than just a fixed delay - in my experience sometimes delays can be much longer than you expect.

I would also suggest using interrupt driven serial routines, particularly the receive one, as there's little buffering otherwise.
 

Latest threads

EE World Online Articles

Loading

 
Top