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); } }
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?
do you mean to say that i should use interrupt method to receive data?
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.
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?
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...
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..
"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?
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?
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..
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?
I have found that interruptifobia is a baaaad disease.
polled serial is far more likely to cuse timing errors
Erik
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.
all connections are right...i didnt get 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.
// 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:
www.8052.com/.../162556