Continue to Site

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.

  • 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.

GSM Modem Interfacing with PIC Microcontroller

Status
Not open for further replies.

Sathiesh Kumar

New Member
Hi to all...
I am doing a project in which i will be interfacing a gsm modem with PIC microcontroller(16f877).. when i connect the gsm modem with PC i can able to send and read the messages in the sim using AT commands in hyperterminal...Then i iniatized UART in PIC send and received data to PC through hyperterminal its working properly...
When i interfaced PIC with GSM Modem i can able to send AT command to GSM modem but the reply "OK " from GSM modem to PIC is not received by PIC...

Example:

AT (Enter)-----In Hyperterminal
OK ------Message from GSM Modem

Compiler:MikroC
Baud Rate:9600 bps
GSM MOdem:Wavecom HT-W2101

I can able to send AT and Enter command from UART to PIC... But i cant able see the reply from GSM modem... but it works well when i interface GSM modem with PC hyperterminal...

Help me...

Sathiesh Kumar.V
 
What is the interface on the GSM modem? It sounds like RS232, if this is the case then the PIC needs a level shift IC between the GSM Modem and the PIC, this is usually a MAX232 chip.

Please confirm your hardware setup.

Wilksey
 
Hi Wilksey...
Yes.. I have used max 232 IC to do level shifting operation...

One thing i would like to know is what will be the return message from GSM modem when i send AT and then press enter command.. When i connect GSM MOdem to hyperterminal it returns OK...
 
A Standard GSM Modem response is OK when issuing AT commands.

Once the modem is connected you may see messages like: "CONNECT", "CONNECT 9600" or similar.

You will have to post your code for people to examine and find out if there is anything wrong.

Wilksey
 
Hi Wilksey...

I am posting my cod efor your reference... check this code and reply me if there is any corrections...Help me...



/*Header*****************************************************
Microcontroller=16f877
Clock=10 Mhz
Project Title:Air Pollution Control Using GSM/GPRS
Developed By:Sathiesh Kumar.V
Date:31/01/2010*/
void Calculation(unsigned int ) ;
void Gsm_SMS_Init();
void Gsm_SMS_Read();
void Gsm_SMS_Send();
// 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

unsigned char ch; //
char Receive;
unsigned int adc_rd; // Declare variables
char *text; //
long tlong; //
unsigned int i,j;
char return_gsm[2];
char Gsm_Receive[100];
char Gsm_Msg[10];
char uart_rd[]="AT";
char uart_rd1[]="AT+CMGF=1";
char uart_rd2[]="AT+CMGR=1" ;
char uart_rd3[]="AT+CMGS=";
char Mobile_No[]="+919444721638" ;
char Send_Msg[]="@ON";
void main()
{
INTCON = 0; // All interrupts disabled
ADCON1 = 0x82; // A/D voltage reference is VCC
TRISA = 0xFF; // All port A pins are configured as inputs
TRISC=0x01;
Delay_ms(2000); // Rest of pins are configured as digital
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)

text = "Air Pollution "; // Define the first message
Lcd_Out(1,1,text); // Write the first message in the first line
delay_ms(500);
text = "Sensor Value"; // Define the second message
Lcd_Out(2,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);


text = "GSM"; // Define the third message

while (1)
{
Lcd_Out(1,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
Gsm_SMS_Init();
}
}

void Gsm_SMS_Init()
{
if (UART1_Tx_Idle() == 1)
{
for(i=0;i<=1;i++)
{
UART1_Write(uart_rd); //AT
}
UART1_Write(0x0D); //Enter
Lcd_Out(2,1,"AT"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);

//delay_ms(5000);
while(1)
{
if (UART1_Data_Ready())
{ // If data is received,
for(i=0;i<=1;i++)
{
return_gsm = UART1_Read(); // read the received data, Received OK
}
Lcd_Out(2,1,"OK"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);

/* UART1_Write(return_gsm[0]);
Lcd_Out(2,1,"OK RESEND"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR); */

// UART1_Write(return_gsm[1]);
}
}
/*if(return_gsm[0]==0x4F && return_gsm[1]==0x4B)
{
if (UART1_Tx_Idle() == 1)
{
for(i=0;i<=8;i++)
{
UART1_Write(uart_rd1); //AT+CMGF=1
}
UART1_Write(0x0D); //Enter
Lcd_Out(1,1,"AT+CMGF=1");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
if (UART1_Data_Ready())
{ // If data is received,
for(i=0;i<=1;i++)
{
return_gsm = UART1_Read(); // read the received data,OK
}
Lcd_Out(2,1,"Received OK");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
} */
}
}
 
Ok, you really should use the "[ code ]" and "[ /code ]" tags to post your code.

Comment out TRISC=0x01;

You are setting the tristate register of PORTC which is where the hardware USART is, C6 / C7.

C6 Is transmit so it should be an output, C7 is recieve so it should be input.

Let me know if it works

Wilksey
 
Last edited:
i tried by commenting the line TRISC and also tried by initializing TRISC=0x80.. but it doesnt work... By the code which i posted above i know that it transmits AT command to GSM Modem... One thing is that microcontroller doesnt receive the reply from GSM Modem.. But when i connect GSM Modem with PC it works perfectly.. GSM Modem reply with OK ....
What might be the problem...
Help me...

Sathiesh Kumar.V
 
What happens if you connect the PIC to the PC via a serial cable and Hyperterminal?

I tested with Hyperterminal and it worked ok on my setup.

Wilksey
 
when i connect directly PIC with PC Hyperterminal and MODEM with PC Hyperterminal it works perfectly... when i interface microcontroller with GSM Modem then only the problem...
i have modified little bit of my code.. check this code ...
Code:
  /*Header*****************************************************
Microcontroller=16f877
Clock=10 Mhz
Project Title:Air Pollution Control Using GSM/GPRS
Developed By:Sathiesh Kumar.V
Date:31/01/2010*/
void Calculation(unsigned int ) ;
void Gsm_SMS_Init();
void Gsm_SMS_Read();
void Gsm_SMS_Send();
// 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

unsigned char ch;                    //
char Receive;
unsigned int adc_rd;                 // Declare variables
char *text;                          //
long tlong;                          //
unsigned int i,j;
char return_gsm[2];
char Gsm_Receive[100];
char Gsm_Msg[10];
char uart_rd[]="AT";
char uart_rd1[]="AT+CMGF=1";
char uart_rd2[]="AT+CMGR=1" ;
char uart_rd3[]="AT+CMGS=";
char Mobile_No[]="+919444721638" ;
char Send_Msg[]="@ON";
void main()
{
    INTCON = 0;                      // All interrupts disabled
    ADCON1 = 0x82;                   // A/D voltage reference is VCC
    TRISA = 0xFF;                    // All port A pins are configured as inputs
    TRISC=0x80;
    Delay_ms(2000);                    // Rest of pins are configured as digital
    UART1_Init(9600);              // Initialize UART module at 9600 bps
    Delay_ms(100);                 // Wait for UART module to stabilize
    Lcd_Init();                      // LCD display initialization
    Lcd_Cmd(_LCD_CURSOR_OFF);        // LCD command (cursor off)
    Lcd_Cmd(_LCD_CLEAR);             // LCD command (clear LCD)

    text = "Air Pollution ";       // Define the first message
    Lcd_Out(1,1,text);               // Write the first message in the first line
    delay_ms(500);
    text = "Sensor Value";            // Define the second message
    Lcd_Out(2,1,text);               // Define the first message
    delay_ms(500);
    Lcd_Cmd(_LCD_CLEAR);


    text = "GSM";               // Define the third message

   while (1)
   {
       Lcd_Out(1,1,text);               // Define the first message
       delay_ms(500);
       Lcd_Cmd(_LCD_CLEAR);
       Gsm_SMS_Init();
   }
}

void Gsm_SMS_Init()
{
if (UART1_Tx_Idle() == 1)
        {
     Lcd_Out(2,1,"AT");               // Define the first message
         delay_ms(500);
         Lcd_Cmd(_LCD_CLEAR);

         for(i=0;i<=1;i++)
          {
          UART1_Write(uart_rd[i]);           //AT
          }
        UART1_Write(0x0D);                   //Enter
        
        //delay_ms(5000);
        while(1)
        {
     Lcd_Out(2,1,"In while");               // Define the first message
           delay_ms(500);
           Lcd_Cmd(_LCD_CLEAR);

         if (UART1_Data_Ready())
        {                         // If data is received,
      Lcd_Out(2,1,"In IF");               // Define the first message
           delay_ms(500);
           Lcd_Cmd(_LCD_CLEAR);

          for(i=0;i<=1;i++)
          {
           return_gsm[i] = UART1_Read();      //   read the received data, Received OK
          }
           Lcd_Out(2,1,"OK");               // Define the first message
           delay_ms(500);
           Lcd_Cmd(_LCD_CLEAR);

          /* UART1_Write(return_gsm[0]);
           Lcd_Out(2,1,"OK RESEND");               // Define the first message
           delay_ms(500);
           Lcd_Cmd(_LCD_CLEAR);  */

          // UART1_Write(return_gsm[1]);
        }
        }
        /*if(return_gsm[0]==0x4F && return_gsm[1]==0x4B)
          {
            if (UART1_Tx_Idle() == 1)
            {
               for(i=0;i<=8;i++)
                {
                 UART1_Write(uart_rd1[i]);   //AT+CMGF=1
                }
               UART1_Write(0x0D);            //Enter
               Lcd_Out(1,1,"AT+CMGF=1");
               delay_ms(500);
               Lcd_Cmd(_LCD_CLEAR);
            }
            if (UART1_Data_Ready())
            {                             // If data is received,
                for(i=0;i<=1;i++)
                 {
                   return_gsm[i] = UART1_Read();      //   read the received data,OK
                 }
                 Lcd_Out(2,1,"Received OK");
                 delay_ms(500);
                 Lcd_Cmd(_LCD_CLEAR);
            }
                }  */
           }
  }
 
Last edited by a moderator:
when i check the above code.. LCD displays...
Air Pollution
Sensor Value
GSM
AT
in while

it doesnt go to if statement.. which means the data is not received from MODEM when i send AT command...

Sathiesh Kumar.V
 
Strange!

It appears that the PIC is working, as if you can get a response from hyperterminal, it is functioning correctly.

It must be the modem side of it.

A few things to try, however, AT is not really a valid modem command, it is more of an "identifier", how about trying something like "ATI", and make sure AT&V is set to enable return codes.

One other thing, you are sending 0x0d for enter, try sending 0x0a + 0x0d (one after the other) for a full CRLF see if that responds any different.

Obviously for the ATI you will need to increase your FOR loop counter from 1 to 2.

Wilksey
 
i have interchanged the pin TX and RX from microcontroller to max 232... so i can able to receive the response from MODEM... then i initialize the received value to a variable and tried to display it in LCD... but it shows 26f=1.. but the actual response should be OK...

What might be the problem....

Help me....
 
When you connect PIC with hyperterminal of the PC, does it work as expected?

You can test with connecting it and then reading data on hyperterminal.

If the PIC sends the above commands (one at a time), you can manually send the OK response from the hyperterminal and test your PIC's code that way.

The PIC should wait for the OK command to be received right? before transmitting the next command. That you can provide manually.

Also what type of data cable are you using ( does it contain RX, TX and GND only) + are you using the same cable that you use to connect with computer with PIC?

I have done mobile module before but it was using assembly and in 8051 using nokia 6100.
 
Also i noticed one more thing.

Once you send the AT command, you update the LCD first and then you try to read response from the Modem.. This should not be happening.

Maybe you miss the data while you are trying to update the LCD.

I recommend that first you send AT command and immediately try to read response from the MODEM. Another way that would be more reliable would be to use Rx serial interrupt but i guess it will take more effort :)
 
I wouldn't have thought the LCD timings would interfere with the UART, but you could try by commenting out the LCD_xxxx functions.

I have interfaced wavecom / sony ericsson GR64 modems with A pic using a MAX 232 level shift chip, I used an 18F series, but the pricipal is the same.

That response is very strange!

What you could try to do is connect the PIC to the PC and see if the request is what you sent from the PIC.

Use TerraTerm, or a favourite of mine is the MOXA DataScope utility to find the hex representation of what is being sent out and post that.

So "pretend" the PC is the modem, send "OK" back to the PIC and see what you get on the LCD.

Wilksey
 
Hi.. Thanx for the reply...

whats the hex value for Ctrl+z command and enter command...

I think for enter its 0x0d...

I will send you the code shortly of interfacing pic with hyperterminal thinking that their will be a reply from hyperterminal...

check down the code and give me suggestions...

And one more question.. how to post code in this forum... please send me the procedure...
 
code for ASCII control-Z is 0x1A

You can go to "Go advanced" in the reply box which will give option for code tags [ CODE ] [ /CODE ]

Use these tags so that the indentation is kept.
 
Last edited:
Hi to all..
At last i have send the sms using a microcontroller .. thanx for all...

but i have a problem in reading a sms using microcontroller.. how can i do it...
I have used the command AT+CMGR=1 to read the sms... but i have a doubt how to store the value which is returned by modem... i have declared an array to store the message and read the message using UART1_Read() function...

Sathiesh Kumar.V
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top