Keil Logo Arm Logo

8051 interfacing with sim300

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
varun bhat
Posted
19-Feb-2013 08:16 GMT
Toolset
C51
New! 8051 interfacing with sim300

i am able to send msg from gsm to the mobile phone but am not able to receive data from the GSM.
eg..in this code when i send AT to the gsm , i get garbage value on the LCD instead of OK..but with the hyperterminal, the communication is proper with both the 8051 and GSM.

#include<stdio.h>
#include<at89x51.h>
#define port P1
#define dataport P2                             // Data port for LCD
sbit rs = port^2;
sbit rw = port^3;
sbit en = port^4;
 int p;
 unsigned char msg[20];
//generates delay in milli seconds
void delay_ms(unsigned int i)
{
unsigned int j;
        while(i-->0)
        {
                for(j=0;j<500;j++)
                {
                        ;
                }
        }
}

void command(unsigned char dost)
   {
   dataport=dost;                  //put the value on pin
   rs=0;
   rw=0;
   en=1;         //strobe the enable pin
   delay_ms(5);
   en=0;
   }



void lcddisplay(unsigned char word)
   {
   dataport=word;           //put the value on pin
   rs=1;
   rw=0;
   en=1;          //strobe the enable pin
   delay_ms(5);
   en=0;
   }

                void lcd_data_string(unsigned char *str)        // Function to display string on LCD
{
        int i=0;
        while(str[i]!='\0')
        {
          lcddisplay(str[i]);
          i++;
          delay_ms(1);
        }
        return;
}

//Function to initialize RS232 Serial Port
void serial_init()
{
        SCON=0x50;   //setup for 8-bit data
        TMOD=0x20;       //setup timer 1 for auto-reload
        TH1=0xfd; //Baud Rate 9600
                TL1=0xfd;
        TR1=1;       //turn on timer 1
        TI=1;            //indicate ready to transmit
}



//This function displays a null-terminated string on the RS232 port
void send_serial(unsigned char *s)
{
while(*s!=0x0)
{
SBUF=*s;
while(!TI)
{
}
TI=0;
s++;
}
}

int i;

void tx0(unsigned char x) //send data to serial port 0
{
EA=0;
SBUF=x;
while(TI==0);
TI=0;
EA=1;
}
void lcd()
{
        command(0x38);                                          // For using 8-bit 2 row LCD
        delay_ms(1);
        command(0x0F);                                          // For display on cursor blinking
        delay_ms(1);
        command(0x80);                                          // Set the cursor on first position of LCD
        delay_ms(1);
}
    char receive(void)
   { // unsigned char m;
        while (RI != 1) {;}     //wait to receive data

 msg[++p]=SBUF;           // save value of data
 RI=0;
 return (msg[p]);

}

void main()
{
p=(-1);
delay_ms(80);
lcd();
delay_ms(50);
serial_init();   //Initialize Serial port.

while(1)
{ int m;
send_serial(" AT");
delay_ms(400);
tx0(0x0D);

m=receive();
command(0x06);
lcddisplay(m);

}
}
Read-Only
Author
Per Westermark
Posted
19-Feb-2013 09:06 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

Why are you using a busy-loop to delay the modem communication? Don't you think it is much better to have a function that sends a string while watching for the result?

Read-Only
Author
varun bhat
Posted
19-Feb-2013 09:31 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

do you mean to say that i should use interrupt method to receive data?

Read-Only
Author
Per Westermark
Posted
19-Feb-2013 09:52 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

It doesn't matter if you poll or use interrupts.

But you shouldn't base your communication on delays. You should synchronize the communication based on what characters you get back from the modem. Your code doesn't seem to care about any responses from the modem. Do you get any echo back? Do you get any OK back if you send AT? The communication with a modem is a dialogue. So treat it as such instead of just sending out a string and then dummy-wait before starting to listen.

Read-Only
Author
varun bhat
Posted
19-Feb-2013 10:13 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

i get garbage value on the LCD(echo is turned off)...i also tried checking only the 'o' and 'k' characters that should be received from GSM and tried to display them on LCD..but even that isnt working.where am I going wrong?

Read-Only
Author
Per Westermark
Posted
19-Feb-2013 10:30 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

Ignore the LCD. Do you get any O and K characters? The LCD isn't the only way to verify what characters you get from the modem...

Read-Only
Author
varun bhat
Posted
19-Feb-2013 11:19 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

on hyperterminal i get proper OK response...I am also able to send msg to the cell phone from 8051 through sim300. The only problem is while reading data from sim300 on LCD..

Read-Only
Author
Per Westermark
Posted
19-Feb-2013 12:10 GMT
Toolset
C51
New! RE: 8051 interfacing with sim300

"The only problem is while reading data from sim300 on LCD.."

Yes, but stop reading data from sim300 on LCD then, and figure out if your progrem is reading any answers from the modem, or sending data to the modem or if the problem is to present the data on the LCD.

You (!) really have to start split problems into smaller problems and find out which sub-problem that fails.

"The only problem" here seems to be that you haven't given us any information indicating that you are actually trying to debug your program. You think your problems will magically go away?

Read-Only
Author
Erik malund
Posted
19-Feb-2013 14:01 GMT
Toolset
C51
New! finding a needle in a haystack

You (!) really have to start split problems into smaller problems and find out which sub-problem that fails.
finding a needle in a haystack is a lot easier when the haystack is amaller

split you code into pieces

can you write messages from flash to the LCD?
can you write messages from hyperterm to the LCD?

Read-Only
Author
varun bhat
Posted
19-Feb-2013 16:35 GMT
Toolset
C51
New! RE: finding a needle in a haystack

I can write msgs from flash and hyperterm to the LCD..vl again go through the code by part and vl post if any queries..

Read-Only
Author
Per Westermark
Posted
19-Feb-2013 18:24 GMT
Toolset
C51
New! RE: finding a needle in a haystack

So: why should _we_ spend time going through the code?

Have you verified yet that you actually manage to send anything to the modem?

- correct pin connections?
- correct logic levels?
- correct baudrate?

Have you verified if the modem tries to send any answer back? What happens on the RX and TX pins?

Any hardware handshake required?

And another thing. You do a long busy-loop. How do you know that the modem haven't already given the answers while you were busy with your loop?

Read-Only
Author
Erik malund
Posted
19-Feb-2013 19:59 GMT
Toolset
C51
New! also

I have found that interruptifobia is a baaaad disease.

polled serial is far more likely to cuse timing errors

Erik

Read-Only
Author
Per Westermark
Posted
19-Feb-2013 20:02 GMT
Toolset
C51
New! RE: also

Interrupts are the way to go to be able to comfortably catch unsolicited messages like "RING".

But at lower baudrates, a polled solution can work reasonably well too.

But a polled solution with a dummy delay will almost always lead to tears. Because that dummy delay is a window where the modem can (and will) send characters that will be lost. And the code have no way to synchronize the communication process with the lost characters.

Read-Only
Author
varun bhat
Posted
20-Feb-2013 04:53 GMT
Toolset
C51
New! RE: finding a needle in a haystack

all connections are right...i didnt get the meaning of long busy loop

Read-Only
Author
Andrew Neil
Posted
20-Feb-2013 07:07 GMT
Toolset
None
New! RE: the meaning of long busy loop

A "busy loop" is a loop where you keep the processor busy doing nothing - as a time delay.

Your delay_ms() function uses a "busy loop".

A "long busy loop" is such a loop used to give a "long" delay.

Read-Only
Author
Andrew Neil
Posted
20-Feb-2013 07:13 GMT
Toolset
None
New! HLL timing
// generates delay in milli seconds
void delay_ms( unsigned int i )
{
   unsigned int j;

   while( i-- >0 )
   {
      for( j=0; j<500; j++ )
      {
         ;
      }
   }
}

This will only generate millisecond delays under certain very specific conditions - of which you do not have full control:

http://www.8052.com/forum/read/162556

Read-Only
Author
ghulam mujtaba
Posted
15-May-2013 19:38 GMT
Toolset
None
New! RE: HLL timing

hi everyone i want to make a project to control relays by sending sms any one help me and send kiel c code to interface sim300 with 89c52 thanks

Read-Only
Author
Andrew Neil
Posted
15-May-2013 19:47 GMT
Toolset
None
New! RE: send kiel (sic) c code

No, people are not going to do your job for you for free!

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings