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.

Receiving routine code for serial driver of 8051

Status
Not open for further replies.

mithila

Newbie level 4
Joined
Nov 30, 2004
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
77
serial driver for 8051

Hi,
I'm developing serial driver for 8051.
I've compltd the code for sending the string as
SendStr( char *Str )
{
while( * Str != '\0' )
{
TI0 = 0;
SBUF0 = ( * Str );
while( !TI0 );
TI0 = 0;
Str++;
}
}

and i've also written for receiving using interrupt.
But now i've to change this receiving method as function Recvstr(char *ptr,int maxlength). Can anyone help?
The ISR routine is
void serialdriver(void) interrupt 4 using 1
{
if(RI0)
{
ch[j++] = SBUF0;
RI0 = 0;
if( ch[j-1] == 0x0d ) // 0x0d - hex value for new line
{
string = 1;
ch[j] = '\0';
}
}

}
 

serial driver for 8051

Which compiler that you're using?
 

Re: serial driver for 8051

Hi,
I'm using Keil
 

serial driver for 8051

I thought the serial communication function/routine should be available in Keil library. Perhaps I was wrong before. Thanks.
 

serial driver for 8051

Why not use puts() and gets?
 

Re: serial driver for 8051

Hi

SendStr( char *Str )
{
while( * Str != '\0' )
{
TI0 = 0;
SBUF0 = ( * Str );
while( !TI0 );
// TI0 = 0; // It is unnecessary
Str++;
}
}

and i've also written for receiving using interrupt.
But now i've to change this receiving method as function Recvstr(char *ptr,int maxlength). Can anyone help?

int Recvstr(char *ptr, int maxlength) // function return -1 if len of str > max
{
int strlen = 0;

while(1) // endless loop
{
while(RIO == 0); // symbol waiting

RI0 = 0;
if ( SBUF0 != 0x0d) && (SBUF0 != 0x0a)
ptr[strlen++] = SBUF0;
if strlen > maxlength
{
ptr[strlen] = 0;
return -1;
}
else
{
ptr[strlen] = 0;
string = 1;
return 0;
}
}
}

Best regards, Kuka
 

Re: serial driver for 8051

hi,
How to receive string using UART interrupt? I've to use function also, similar to that send string. Already posted the send string function.
Pls help.
Mithila
 

Re: serial driver for 8051

hi
This is my RS232 library with interrupt driven.



Sis
 

Re: serial driver for 8051

Hi,
Thks a lot.
Since i'm new to 8051 programming, Cant able to understand ur codes.
Can u pls help in modifying my codes without bugs?
Pls help me.
Mithila.

Added after 2 hours 8 minutes:

hi,
pls help me to find out bugs in the following codes.
I'd given maximum length as 10 for receiving the string. But it exceeds.
Pls help me to find the error.


bit bFlag = 0;
char caString[15];
int index;
char *gBuf;
int glength;

void main(void)
{
int iIndex;

initHW(); // Hardware Initialisation.

SetBaudRate(9600); /* TH1: reload value for 9600 baud @ 25MHz */

while( 1 )
{
RecvStr(caString,10); // Receive String
if( bFlag )
{
bFlag = 0;
SendStr( caString ); // Send String
for( iIndex = 0; iIndex < 15 ; iIndex++)
{
caString[iIndex] = 0;
}
index = 0;
}
}

}


Bool initHW()
{

EA=0; //Disable global interrupt

WDTCN = 0xde; //Watchdog timer
WDTCN = 0xad;

XBR0 = 0x04; //crossbar configuration
XBR1 = 0x14;
XBR2 = 0xC0;

P0 = 0x01;

SCON0 = 0x50; /* SCON0: mode 1, 8-bit UART, enable receiver */
TMOD = 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TR1 = 1; /* TR1: timer 1 run */
TRANSMIT_INTERRUPT = TI0 = 1;

ES0 = 1;
EA = 1; //Enable global interrupt
return 0;

}


Bool SetBaudRate( int iBaudRate )
{
switch( iBaudRate )
{
case 2400:
TH1 = (unsigned int)(TIMERVALUE2400 >> 8); //Values to be loaded based on BR
break;
case 4800:
TH1 = (unsigned int)(TIMERVALUE4800 >> 8); //Values to be loaded based on BR
break;
case 19200:
TH1 = (unsigned int)(TIMERVALUE19200 >> 8); //Values to be loaded based on BR
break;
default:
TH1 = (unsigned int)(TIMERVALUE9600 >> 8); //Values to be loaded based on BR
}
return 0;
}

Bool SendStr( char *Str )
{
while( * Str != '\0' )
{
TI0 = 0;
SBUF0 = ( * Str ); //Send String to Buffer
while( !TI0 );
TI0 = 0;
Str++;
}
return 0;
}

Bool Recvstr(char *ptStr, int iMaxLength)
{
glength = iMaxLength;
gBuf = ptStr;
index = 0;
while(RI0 == 0);
return 0;
}

void serialdriver(void) interrupt 4 using 1
{
if(RI0)
{
RI0 = 0;
if (( SBUF0 != 0x0d) && (SBUF0 != 0x0a) )
{
gBuf[index++] = SBUF0;
if( index > glength )
{
gBuf[index] = 0;
}
bFlag = 1;
}
}
}
 

Re: serial driver for 8051

mithila, try to use tags "Code" to format your sources. Otherwise, it is a pain to read your code.

Compare:

mithila said:
while( 1 )
{
RecvStr(caString,10); // Receive String
if( bFlag )
{
bFlag = 0;
SendStr( caString ); // Send String
for( iIndex = 0; iIndex < 15 ; iIndex++)
{
caString[iIndex] = 0;
}
index = 0;
}
}

and

Code:
while( 1 )
   {
    RecvStr(caString,10);     // Receive String 
    if ( bFlag )
     {
      bFlag = 0;
      SendStr( caString );     // Send String
      for( iIndex = 0; iIndex < 15 ; iIndex++)
        {
         caString[iIndex] = 0;
        }
      index = 0;
     }
 }
 

serial driver for 8051

Hi,
Try the supplied examples from Keil, they r very useful...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top