# [SOLVED]receiving sms problem using uart1_read command

Status
Not open for further replies.

#### zeckrey J

##### Newbie level 5
problem, i cannot receive the data in rx, the rcreg show me a junk data each time at+cmgr is entered, i try to show the message on lcd, but some times other come out, such as other memory, like at+cmgr that i stored in atcmd2..
please take a look at my coding and tell me where i did wrong...

PHP:
int CMGR()
{

unsigned char recv;
//recv=&msg;

for(i=0; i<=8; i++)
UART1_write(atcmd2[i]); // write the at+cmgr=2
UART1_write(0x0D);      // send cr
delay_ms(10);        //delay10ms
{
uart1_write(recv);           // send back data

Lcd_Init();                      //initial lcd
Lcd_Cmd(_LCD_CLEAR);     //clear lcd
Lcd_Cmd(_LCD_CURSOR_OFF);     //no cursor
Lcd_Out(1,1,recv);       //display stored msg
Delay_ms(2000);           //delay 2000ms
}
}

#### ckshivaram

does it work with hyperterminal???????????

zeckrey J

### zeckrey J

points: 2

#### zeckrey J

##### Newbie level 5
does it work with hyperterminal???????????
dear ckshivaram, thanks for the fast reply...
i test it using hyperterminal,and send a character, but the rx line not receive the data(im using the proteus virtual terminal tool to detect the character i send.

#### horace1

it is possible you are using the incorrect baud rate in particular if you are receiving junk data.
try at various baud rates 2400, 4800, 9600, 19200,38400, 57600, 115200.
try sending AT followed by a carriage return character (make sure hyperterm sends carriage return - many modems require it \r in C)

zeckrey J

### zeckrey J

points: 2

#### zeckrey J

##### Newbie level 5
it is possible you are using the incorrect baud rate in particular if you are receiving junk data.
try at various baud rates 2400, 4800, 9600, 19200,38400, 57600, 115200.
try sending AT followed by a carriage return character (make sure hyperterm sends carriage return - many modems require it \r in C)
i already double check, the baud rate i set it to 9600, the source code, compim in proteus simulation, hyperterminal... all i use 9600 baud rate... it is anywhere that i forget to check the baudrate, please tell me...
by the way i am using 20Mhz crystal, in my simulation... does it have to do whit this?

#### zeckrey J

##### Newbie level 5
i have update on my problem, i have watch the rcreg pin, it seem that it only able to receive 1 byte data at a time, while my rxterminal scope show that my modem send the data(message about 64byte)...
the rx terminal on my pic only take the 1st value "0x0A" which is <LF>...
any solution ?

#### hussain_kiet

##### Full Member level 5
i have update on my problem, i have watch the rcreg pin, it seem that it only able to receive 1 byte data at a time, while my rxterminal scope show that my modem send the data(message about 64byte)...
the rx terminal on my pic only take the 1st value "0x0A" which is <LF>...
any solution ?
definitely in your code you are receiving only one character, however modem sends a stream of characters in the message you need to set a loop and receive the whole message

#### zeckrey J

##### Newbie level 5
definitely in your code you are receiving only one character, however modem sends a stream of characters in the message you need to set a loop and receive the whole message
thank you for u reply hussain_keit...

this code that i try but only get 1 byte from the rcreg
Code:
void store()
{

//strcpy(copy,&msg[i]);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,msg);
delay_ms(2000);

}
and this code i try to do loop also not work..
can suggest me a way?
thanks

Code:
void store()
{

for(i=0; i<80; i++)
{

}
//strcpy(copy,&msg[i]);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,msg);
delay_ms(2000);

}

##### Member level 3
Hi zeckrey,
First ensure your cable is OK,
- shorting the 2 and 3 rd pin D9 connector whereas the other terminal is connected with the computer
-open hyperterminal,set baud rate as 9600 or 115200
-connect it and enable ascii and echo settings in properties of hyperterminal, if you type a character the same character have to be received(seems to be echo of character).
Once your connection is OK, connect the modem directly to the com port connector then proceed with the commands directly,
Then go to program.

#### horace1

does Lcd_Out() display a single character or a string?
assuming a single character try
Code:
void store()
{
int i;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
for(i=0;i<80;i++)
{
// print line 1 character position i+1
Lcd_Out(1,i+1,msg);
delay_ms(2000);
}
}

#### zeckrey J

##### Newbie level 5
Hi zeckrey,
First ensure your cable is OK,
- shorting the 2 and 3 rd pin D9 connector whereas the other terminal is connected with the computer
-open hyperterminal,set baud rate as 9600 or 115200
-connect it and enable ascii and echo settings in properties of hyperterminal, if you type a character the same character have to be received(seems to be echo of character).
Once your connection is OK, connect the modem directly to the com port connector then proceed with the commands directly,
Then go to program.
does Lcd_Out() display a single character or a string?
assuming a single character try
i guess my connection is ok, i try to upload my circuit picture for u to see, but sadly my internet connection not gud enough..
i can see from my virtual terminal scope the modem is reponding and sending data... the problem is after using uart_read command only one bit of my data is receive at rcreg... the other is loss...

i also tried the code that horace suggest me, the lcd only show the same character repeatly on the lcd screen..

i have asking around on other forum, one of them suggest me that i need to control the interrupt to receive the whole data "a sms in my case"... but i am totaly newbie in pic programing.. i dont have idea how to control the interrupt to be able receive the whole sms data... i would appreciate if anyone can guide me understand the interrupt.. thanks..

##### Member level 3
Hi,
suspent the problem is with the LCDOUT() routine, can you post that routine here. you may get solution.

#### zeckrey J

##### Newbie level 5
Hi,
suspent the problem is with the LCDOUT() routine, can you post that routine here. you may get solution.
thanks e shade for reply.. this is my whole code... hope u can guide my where i did it wrong... thanks...

Code:
/*
* Project name:
sms traffic reporting

*/

sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;

char txt1[]="Trafic reporting";
char txt2[]="system using sms";
char copy[16];
char msg[50];

char atcmd[]="AT";
char atcmd0[]="ATE0";
char atcmd1[]="AT+CMGF=1";
char atcmd2[]="AT+CMGR=2";

int i;
int AT();
int CMGF();
int CMGR();
int ATEO();
int extract();
int count;
int wait();
int LCD();
void store();
void   setInterrupt();
void   setCommunication();
void   interruptme();

void main()
{

//RCSTA=0b10010000;
//TXSTA=0b00100100;
TRISD=0;
PORTD=0XFF;
TRISD=0XFF;
TRISB=0x00;
PORTB=0;

UART1_Init(9600);

Delay_ms(100);

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
Lcd_Out(2,1,txt2);
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);

while(1)
{
if (AT())
{
if (ATEO())
{
if(CMGF())
{
if(CMGR())
{
if(LCD())
{

}

}
}
}
}
}

}

int AT()
{
for(i=0; i<=1; i++)
UART1_write(atcmd[i]);
UART1_write(0x0D);
Delay_ms(2000);

}

int ATEO()
{
for(i=0; i<=3; i++)
UART1_write(atcmd0[i]);
UART1_write(0x0D);
Delay_ms(2000);
}

int CMGF()
{
for(i=0; i<=8; i++)
UART1_write(atcmd1[i]);
UART1_write(0x0D);
Delay_ms(2000);
}

int CMGR()
{

for(i=0; i<=8; i++)
UART1_write(atcmd2[i]);
UART1_write(0x0D);
delay_ms(10);
store();
//setInterrupt();
// setCommunication();
//interruptme();

}

int LCD()
{

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
for(i=0; i<=16; i++)
{
Lcd_Out(1,1+i,msg[i]);
delay_ms(500);
}
for(i=16; i<=32; i++)
{
Lcd_Out(2,1+i,msg[i]);
delay_ms(500);
}

}

void store()
{

// print line 1 character position i+1

}

#### kiranvarma3

##### Junior Member level 1
hi! can u tell me what type of controller ur using in ur simulation.
r u using interrupted based serial txion.

---------- Post added at 07:30 ---------- Previous post was at 07:25 ----------

if ur using polling method to receive character. ur r not able received the next char. bcz LCD take more time to initialize and to run.
so do one thing first receive the character serial and store it into an array then display them it into LCD screen.
i hope this will work.
and i am sure that ur serial line is working correctly. bcz ur gettiing atleast one char.no prob with ur hardware. the only thing is u have to modify ur code.

#### zeckrey J

##### Newbie level 5
hi! can u tell me what type of controller ur using in ur simulation.
r u using interrupted based serial txion.

---------- Post added at 07:30 ---------- Previous post was at 07:25 ----------

if ur using polling method to receive character. ur r not able received the next char. bcz LCD take more time to initialize and to run.
so do one thing first receive the character serial and store it into an array then display them it into LCD screen.
i hope this will work.
and i am sure that ur serial line is working correctly. bcz ur gettiing atleast one char.no prob with ur hardware. the only thing is u have to modify ur code
i am using pic16f877a as the mcu...
in the code that i post before, i have not modify the interupt.. i already, try to put it in array, but as a result it still the same... it only come out one character... and some time there junk memory, inside my array..
i have no idea, what is the problem... but there are someone told me that i have to control the interupt to receive the whole data... but im newbie in pic programmming... i have no idea how to do it.. i have read the uart chap in the mcu data sheet, and i know that the interrupt occurs in every 1/clockfrequency so this is maybe the cause of data loss.. but i am still not sure.. pls help me.. i am using microc pro for pic v4.60.00 as the compiler.. thanks..

#### zeckrey J

##### Newbie level 5
hi all,
thanks for guiding me...
i have complete my project about70-80%...
just left to configure the input for my program...
thanks again!!

Status
Not open for further replies.