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.

Problem in using Xdata Memory

Status
Not open for further replies.

gauravkothari23

Advanced Member level 2
Advanced Member level 2
Joined
Mar 21, 2015
Messages
642
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,298
Activity points
6,934
Hi all...
I have a very small issue....
i have written a code to send messages through SIM800 using SST89E516RD2 microcontroller.
code is:-
Code:
#include <sst89e516rd2.h>
sbit row0=P2^4;	
sbit row1=P2^5;	
sbit row2=P2^6;	
sbit row3=P2^7;	
sfr COL=0xA0;	

unsigned char *message = "Hi Everybody ";   // Content of the message to be sent

int keypad();
void sendsms();
void puts(unsigned char* ptr);
void putc(unsigned char chr);
void initialize();
unsigned char *std = "+91";   			  // std

unsigned char CTRLZ = 0x1A; 

unsigned char  xdata phoneno[10]; //Array to store the phone no.

void main()						// MAIN STARTS HERE
{
unsigned int i,j;
 while(1)                
   {	
     for(j=0;j<10;j++)
	{	
        phoneno[i]=keypad();   // Entering new company phone no
	LCD_putc(phoneno[i]);
        i++;
        }
	i=0;
       	initialize();
	msdelay(1000);
	sendsms();	
    }
}

void sendsms()
{
 puts("ATE0\r"); 
 msdelay(1000);
 puts("AT\r");
 msdelay(1000); 
 puts("AT+CMGF=1\r"); 
 msdelay(1000);
 puts("AT+CMGS=\"");
 puts(std);
 puts(phoneno);
 puts("\"");
 puts("\r");
 msdelay(1000);
 puts(message);
 msdelay(1000);
 putc(CTRLZ);
}

int keypad() 
 {
   unsigned char dat[4][4]={'1','2','3','A',	 // assigning key matrix
                            '4','5','6','B',
						    '7','8','9','-',
						    '*','0','#','+',
						               };
 unsigned char colloc,rowloc;
 COL=0xFF;
 row0=0;
 row1=0;
 row2=0;
 row3=0;
   /* setting LCD screen*/ 
  while(1)
 {
/* reading character from keyboard */
   do
   {
    row0=0;
 	row1=0;
 	row2=0;
 	row3=0;
   	colloc=COL;
	colloc&=0x0F;
	}while(colloc!=0x0F);
	do
	{
	 do
	 {
	  msdelay(10);
	  colloc=COL;
	  colloc&=0x0F;
	  }
while(colloc==0x0F);
	  msdelay(10);
	  colloc=COL;
	  colloc&=0x0F;
	  }
  while(colloc==0x0F);
  while(1)
	 {
	 row0=0;
 	 row1=1;
     row2=1;
     row3=1;
	 msdelay(5);
	 colloc=COL;
	 colloc&=0x0F;
	 if(colloc!=0x0F)
	 { 
	  rowloc=0;
	  break;
	  }
	  row0=1;
 	  row1=0;
      row2=1;
      row3=1;
	  msdelay(5);
	  colloc=COL;
	  colloc&=0x0F;
	   if(colloc!=0x0F)
	   {
	    rowloc=1;
		break;
		}
	  row0=1;
      row1=1;
      row2=0;
      row3=1;
      msdelay(5);
	  colloc=COL;
	  colloc&=0x0F;
	   if(colloc!=0x0F)
	   {
	    rowloc=2;
		break;
		}
	  row0=1;
 	  row1=1;
 	  row2=1;
 	  row3=0;
	  msdelay(5);
	  colloc=COL;
	  colloc&=0x0F;
	if(colloc!=0x0F)
	  {  
	      rowloc=3;
	      break;
	    }
	   }
   if(colloc==0x0E)
  
   return(dat[rowloc][0]);

   else if(colloc==0x0D)
   return(dat[rowloc][1]);
   else if(colloc==0x0B)
   return(dat[rowloc][2]);
   else
    return(dat[rowloc][3]);
	}
	}

void puts(char* p)
{
 char *temp = p;          /*temp pointer so that the actual pointer is not displaced */
 while(*temp != 0x00)
 {
  putc(*temp);  
  temp++;
 } 
}

void putc(unsigned char chr)
{
 SBUF = chr;
 while(TI==0);            /*Wait until the character is completely sent */
 TI=0;                    /*Reset the flag */
}
								// MAIN ENDS HERE
void initialize()
{
 SCON  = 0x50;   /*SCON: mode 1, 8-bit UART, enable receive      */
 TMOD |= 0x20;   /*TMOD: timer 1, mode 2, 8-bit                  */
 TH1   = 0xFD;   /*TH1:  for 9600 baud                           */
 TR1   = 1;      /*TR1:  timer 1 run                             */ 
}

void msdelay(unsigned int value)
{
 unsigned int i,j;
 for(i=0;i<value;i++)
 for(j=0;j<100;j++);
}

Now my problem is when i change
unsigned char Xdata phone[10]; to unsigned char phoneno[10];
i GSM module send me the messages...
but if i write
unsigned char Xdata phoneno[10];
the GSM modem does not send any messages.
the system only works and sends messages when array phoneno[10] is stores in DATA instead of XDATA.
why does this happen....
can you please help me....
 

Not specifically related to xdata versus data usage, but you are sending a string without a terminating null character. Thus program operation depends on accidental memory content after phoneno[9].
 

    V

    Points: 2
    Helpful Answer Positive Rating
Not specifically related to xdata versus data usage, but you are sending a string without a terminating null character. Thus program operation depends on accidental memory content after phoneno[9].

Thanks...
But as i am entering the values through keypad... then too do i need to send a null character for termination.
and if this is a problem, then why do the same thing work when i change the memory location from xdata to data of array phone[10];
 

I can't imagine how your keypad generates a null character, it must appended to the string explicitly in your code, needing phoneno array being one character larger than the number of printable characters. A popular error in string handling code.

To be honest, I don't know if the missing null character is actually causing your code to fail, but its sufficient to achieve it. The point of unpredictability is that there may be a null character by accidence, or might be not, depending on the compiler's memory utilization and previous code execution.

I don't notice a different coding error at first sight.
 

I can't imagine how your keypad generates a null character, it must appended to the string explicitly in your code, needing phoneno array being one character larger than the number of printable characters. A popular error in string handling code.

To be honest, I don't know if the missing null character is actually causing your code to fail, but its sufficient to achieve it. The point of unpredictability is that there may be a null character by accidence, or might be not, depending on the compiler's memory utilization and previous code execution.

I don't notice a different coding error at first sight.

Not specifically related to xdata versus data usage, but you are sending a string without a terminating null character. Thus program operation depends on accidental memory content after phoneno[9].

Thanks a lot FVM.... you are right..... i added a null character as you said after taking the entries from the keypad... and the things are working perfect.
but can you please let me know that.... then why do it works perfect without adding a null character when i placed the array to data memory.

- - - Updated - - -

as well as can you please explain me how can i read the message received to GSM module and then store it to an array and display it on the LCD.
 

why do it works perfect without adding a null character when i placed the array to data memory.
If you typed in 10 characters, there's apparently a null character in the next memory location. If less than 10 characters, because the memory has been zeroed by the run time library at startup.

as well as can you please explain me how can i read the message received to GSM module and then store it to an array and display it on the LCD.
Not so simple. I use to have an interrupt driven routine that stores received data in a FIFO. If a complete line is received (CR termination), start to decode the data.
 

Thanks FVM...
Again i have written somw code to receive message and then display it on the LCD...
I am using seriel interrupt.
can you please check and let me know what's wrong with the code.
why i am not able to Display the message on LCD.

Code:
Code:
#include <p89V51Rx2.h>

#define buffer 110

sbit LCD_en=P0^1;
sbit LCD_rs=P0^0;

#define LCD_DELAY 800 /* Delay for 1 ms */
#define lcdclear() LCD_command(0x1)        /* Clear display LCD */
#define LCD_origin() LCD_command(0x2)      /* Set to origin LCD */
#define lcdrow1() LCD_command(0x80)        /* Begin at Line 1 */
#define lcdrow2() LCD_command(0xC0)        /* Begin at Line 2 */
#define lcdrow3() LCD_command(0x94)        /* Begin at Line 3 */
#define lcdrow4() LCD_command(0xD4)        /* Begin at Line 4 */
#define Shiftcursorleft() LDC_command(0x04)

void LCD_delay(unsigned char ms);
void LCD_enable();
void LCD_command(unsigned char command);
void LCD_putc(unsigned char ascii);
void LCD_puts(unsigned char *lcd_string);
void LCD_init();

void delay(unsigned int i);
void tx0(unsigned char);
void SMSString(char*text) ;
void init();
void clear(void);
void read_text(unsigned char * , unsigned char * , unsigned char *);
void read_notification();
void clear_mybuff();

unsigned char abc;
unsigned char idata msg1[buffer];
unsigned char rec_no[10];
unsigned char time_date[20];
unsigned char code Response[] = "+CMTI";
unsigned char MyBuff[20], k = 0;
bit NewMessage = 0;

void delay(unsigned int i)
{
        unsigned int k, l;
        for(k=0;k<i;k++)
                for(l=0;l<1000;l++);
}

        

void LCD_delay(unsigned char ms)
{
      unsigned char n;
      unsigned int i;
      for (n=0; n<ms; n++)
      {
              for (i=0; i<LCD_DELAY; i++); /* For 1 ms */
      }
 
}

void LCD_enable()
{
    LCD_en = 0; /* Clear bit P2.4 */
    LCD_delay(1);
    LCD_en = 1; /* Set bit P2.4 */
}
 
void LCD_command(unsigned char command)
{
    LCD_rs = 0; /* Clear bit P2.5 */
    P1 = (P1 & 0xF0)|((command>>4) & 0x0F);
    LCD_enable();
    P1 = (P1 & 0xF0)|(command & 0x0F);
    LCD_enable();
    LCD_delay(1);
}
 
void LCD_putc(unsigned char ascii)
{
    LCD_rs = 1; /* Set bit P2.5 */
    P1 = (P1 & 0xF0)|((ascii>>4) & 0x0F);
    LCD_enable();
    P1 = (P1 & 0xF0)|(ascii & 0x0F);
    LCD_enable();
    LCD_delay(1);
}
 
void LCD_puts(unsigned char *lcd_string)
{
      while (*lcd_string)
      {
              LCD_putc(*lcd_string++);
      }
}
 
void LCD_init()
{
    LCD_en=1;       /* Set bit P2.4 */
    LCD_rs=0; 		/* Clear bit P2.5 */
    LCD_command(0x33);
    LCD_command(0x32);
    LCD_command(0x28);
    LCD_command(0x0C);
    LCD_command(0x06);
    LCD_command(0x01); /* Clear */
    LCD_delay(256);
}


void main ()
{
clear();
LCD_init();
lcdrow1();
LCD_putc('K');
delay(10);
init();

SMSString("AT\r"); // AT commands to initialize gsm modem
delay(50);

SMSString( "ATe0\r"); // turn off echo
delay(50);

SMSString( "AT&W\r"); // save settings
delay(50);

SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay(50);
  
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms

while(1)
{
EA=1;
ES0=1;
//IE=0x90;
k=0;
lcdrow2();
LCD_putc('R');
delay(100);
while(!NewMessage);
EA=0;
ES0=0;
//IE=0x00;
lcdrow1();
LCD_putc('I');
delay(10);
NewMessage = 0;
read_notification();
delay(100);
read_text(msg1,rec_no,time_date);
clear();
clear_mybuff();
}
}

void read_notification()
{
	LCD_putc('Z');
	delay(50);
	clear();
	SMSString("AT+CMGR=");
    tx0(MyBuff[12]);
    SMSString("\r");
	EA=1;
    ES0=1;
//    IE=0x90;	
}

void init()
{
	abc=0;
	TL1=0XFD; //9600 @ 11.0592
	TH1=0xFD;
	TMOD=0x20;
	SCON=0x50;
	TR1=1;
}

void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
	while (*text)
	{
		tx0(*text++);
	}
} 

void tx0(unsigned char x) //send data to modem 
{
	SBUF=x;
	while(TI==0);
	TI=0;
}


void serial () interrupt 4 
{
unsigned char ch;
if(RI)
 {
  ch = SBUF;   //character is read
  if((ch == Response[k]) || (k > 4))
  {
   if(ch != 0x0D) // 0x0D means data received..
   { 
    MyBuff[k++] = ch;
   }
   else
    {
     MyBuff[k] = '\0';
     NewMessage = 1;
     k = 0; 
    }
  }
  else 
  	{
	 msg1[abc]=ch;   //received msg is stored.
	 abc++;
     k = 0;
    }
 } 
  if(TI)
  TI = 0;
  RI = 0; //clear flag
}

void clear_mybuff()
{
	unsigned int a;
	for(a=0;a<25;a++)
		MyBuff[a]=0x00;
}

void clear(void)
{
	unsigned int a;
	for(a=0;a<buffer;a++)
		msg1[a]=0x00;
}
 
void read_text( unsigned char *msg,unsigned char *no ,unsigned char *time)
{
unsigned char *temp;
temp=msg;        
do
msg++;
while(*msg!='+');         //  reaching at no
do
*no++=*msg++;
while(*msg!='"');         
*no++='\0';
msg++;
msg++;
msg++;
do
msg++;
while(*msg!=',');         //  reaching at time
msg++;
msg++;
do
*time++=*msg++;
while(*msg!='+');         // raching at message
*time='\0';
do
msg++;
while(*msg!='\n');
msg++;
do
*temp++=*msg++;
while(*msg!='\r');       // reaching at end of message
*temp='\0';

lcdrow1();
LCD_puts(rec_no);          // array having receipt no
lcdrow2();
LCD_puts(time_date);       // array having date and time
lcdrow3();
LCD_puts(msg1);            // array having message
}
 

If you typed in 10 characters, there's apparently a null character in the next memory location. If less than 10 characters, because the memory has been zeroed by the run time library at startup.


Not so simple. I use to have an interrupt driven routine that stores received data in a FIFO. If a complete line is received (CR termination), start to decode the data.

as i have mentioned "AT+CMGR="
do i need to mention the message no like
"AT+CMGR=1"
let me try...

- - - Updated - - -

as i have mentioned "AT+CMGR="
do i need to mention the message no like
"AT+CMGR=1"
let me try...

as i wrote "AT+CMGR=1"
and when i send a new message to GSM modem... The LCD prints something...
+CMTI:

first two of third row is garbage and then it prints OK.
what does this mean...
please help me out of this...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top