Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Controlling Serial communication in 8051

Status
Not open for further replies.
O

omar.nasir123

Guest
Hi, im trying to serially interface sim548c module with microcontroller 89c52. i have successfully sent AT commands via TX of microcontroller to RX of GSM part of sim 548c module. As soon as the commands are sent, i want to immediately start receiving the replies from the GSM part via RX of microcontroller. The replies are stored in a char array and then checked to see whether the AT command was successfully passed. The problem is each time when i output my array it gives null value (it is not storing anything). Here is my code. can anyone please identify the problem in it?

Code:
int check=0;
int out=0;
unsigned char info[20]={"00000000000000000000"};
unsigned char  *s;
unsigned char a[3],b[3];

void transmit_data(unsigned char str)        
{
        SBUF=str;                                  
        while(TI==0);
        TI=0;                          
}

void send_serial(unsigned char *s)
{
delay(50);
while(*s!=0x0)
	{
	SBUF=*s;
	while(TI==0)
		{
		}
	TI=0;
	s++;
	}
}

void receive_data()            interrupt 4                      
{
if(RI)
{
	info[check++]=SBUF;
	RI=0;
	}
	if(TI)
	TI=0;
}
 
void search(unsigned char b[])
{
int l=0;
for(l;l<18;l++)
	{
	if(info[l]==b[0] && info[l+1]==b[1] && info[l+2]==b[2])
		{
		out=1;
		break;
		}
	}
}

void compare(unsigned char *s, unsigned char a[]) //for CIPSEND
{
while(1)
	{
	out=0;
	check=0;
	delay(50);
	send_serial("AT+CIPSEND\r");
	delay(100);
	send_serial(s);
	transmit_data(0x0D);
	transmit_data(0x0A);
	transmit_data(0x1A);
	IE=0x90;
	delay(200);
	IE=0x88;
	search(a);	
	if (out==1)
	break;
	}
}

Use CODE tags when posting your code


in the main function im passing all AT commands like this:
compare("EHLO lavabit.com","250");

250 is the status code that is replied if the command is succesfully to the lavabit server. I have already established gprs sessions with the server. If i try to output info array during search function it gives zeros and because nothing is storing in it, the control remains in the compare function and the same command is sent over and over again.
 
Last edited by a moderator:

Hai

I find a mistake in transmit_data();

you do not check for the TI == 0 in a while loop. Because, when interrupt occurs, you will be in routine with TI = 1. and before you come out of the routine, you will clear the flag TI = 0. Once you return to the transmit function, you will again check for TI == 0 in while. Obviously, it is now TI = 0. So you will be within that loop and never send next data to SBUF. So SIM wont receive data properly and you will get junk

I have done some changes in your code only at the transmit_data() and interrupt routine. Please copy this and let me know what is the result


Code:
int status_tx = 0,check=0;
int out=0;
unsigned char info[20]={"00000000000000000000"};
unsigned char  *s;
unsigned char a[3],b[3];

void transmit_data(unsigned char str)        
{
        SBUF=str;                                  
        while(status_tx==0); 
	status_tx = 0;                        
}

void send_serial(unsigned char *s)
{
delay(50);
while(*s!=0x0)
	{
	SBUF=*s;
	while(TI==0)
		{
		}
	TI=0;
	s++;
	}
}

void receive_data()            interrupt 4                      
{
	if(RI)
	{
		info[check++]=SBUF;
		RI=0;
	}
	if(TI)
	{
		TI=0;
		status_tx = 1;
	}
}
 
void search(unsigned char b[])
{
	int l=0;
	for(l;l<18;l++)
	{
		if(info[l]==b[0] && info[l+1]==b[1] && info[l+2]==b[2])
		{
		out=1;
		break;
		}
	}
}

void compare(unsigned char *s, unsigned char a[]) //for CIPSEND
{
while(1)
	{
	out=0;
	check=0;
	delay(50);
	send_serial("AT+CIPSEND\r");
	delay(100);
	send_serial(s);
	transmit_data(0x0D);
	transmit_data(0x0A);
	transmit_data(0x1A);
	IE=0x90;
	delay(200);
	IE=0x88;
	search(a);	
	if (out==1)
	break;
	}
}

All the best

Thanks
 

I tested your program. the execution did not go beyond transmit_data.

No i think i did not explain properly. The transmit commands are executed without problem, but the microcontroller does not receive properly. I am communicating with SMTP server. my sequence of commands is EHLO lavabit.com, then i send AUTH LOGIN then username and then password. All these commands are executed properly meaning that the program comes out of the transmit_data function back to main program. but nothing is stored in info.

When i send commands with delay about 500msec all commands are executed successfully. But when we try to implement a check function to see if a successful reply was recieved, the program remains in the compare() function and sends only one command again and again. (because info has not been updated and returns false value when checked against the expected reply).
 

Hai

The code which i have given you is not stalling at transmit_data. It transmitts correct data and also it receives the data which i have sent.

i have tested the following code. Try this

Code:
unsigned char status_tx = 0,check=0;
int out=0;
unsigned char info[20];
unsigned char  *s;
unsigned char a[3],b[3];

void transmit_data(unsigned char str)        
{
        SBUF=str;                                  
        while(status_tx==0); 
		status_tx = 0;                        
}

void send_serial(unsigned char *s)
{
//delay(50);
while(*s!= 0x00)
	{
	SBUF=*s;
	while(status_tx==0); 
	status_tx = 0;
	s++;
	}
}

void receive_data()            interrupt 4                      
{
	if(RI)
	{
	
		info[check]= SBUF;
		check++;
		RI=0;
	}
	if(TI)
	{
		TI=0;
		status_tx = 1;
	}
}
 
void search(unsigned char b[])
{
	int l=0;
	for(l;l<18;l++)
	{
		if(info[l]==b[0] && info[l+1]==b[1] && info[l+2]==b[2])
		{
		out=1;
		break;
		}
	}
}

May i know why do you enable and disable the serial interrupt in compare function? and also you are trying to get interrupt for the timer which is supposed to be used for baud generation of serial communication?

Code:
void compare(unsigned char *s, unsigned char a[]) //for CIPSEND
{
while(1)
	{
	out=0;
	check=0;
	delay(50);
	send_serial("AT+CIPSEND\r");
	delay(100);
	send_serial(s);
	transmit_data(0x0D);
	transmit_data(0x0A);
	transmit_data(0x1A);
	IE=0x90;                                 //Enable serial communication interrupt
	delay(200);
	IE=0x88;                                //Disable serial communication interrupt and enable timer 1 interrupt?
	search(a);	
	if (out==1)
	break;
	}
}

Please send me the configuration code and clear the above doubts to help you further.

All the best

Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top