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); } }
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.