read characters from UART and stop if character 'a' occurs

Started by lpc9xxbeginner January 11, 2009
Hi,

i think it is pretty clear what i want. I want to read characters from
the UART and stop reading if the character 'a' occurs.
I have written a small program, but the program only works the first
time. If i call the function a second time, the program doesn't work.

My program:

int Get_Return_Value()
{
char character;
int i;
unsigned int retry;
char receive_value[4]="\0";
i=0;
do{

if(RI == 1) // if there is a character in SBUF
{
character = uart_receive();
if(character != 0x61) // if the character is not an 'a'
{

receive_value[i] = character; //save the character in the string
i++;

}

if(character == 0x61) //if the character is an 'a'
{

receive_value[i] = '\0';

}

RI = 0;
}

}while(character!= 0x61); //stay in the loop if the received character
is not an 'a'
if(receive_value[0] == '>')
{

printf("\nPrompt");
retry = 0;
}

if(receive_value[0] == 'N' && receive_value[1] == 'D')
{

printf("\nNo Disk");
retry = 2;
}

if(receive_value[0] == 'B' && receive_value[1] == 'C')
{

printf("\nBad Command");
retry = 1;

}

return retry;

}
The Main Program:

void main()
{
int retry;
//initialize the UART at 9600 Baud

for(;;)
{
printf("SCS\n");
retry = Get_Return_Value(); //call the function
}

}
The first time, i call the function, "SCS" is printed on the terminal.
And the program waits for user-input. The second time, "SCS" is
printed on the terminal in a loop - that means, the program doesn't
wait for user-input anymore.

Here is the code of the uart_receive() function:

uart_receive:
jnb ri,$
clr ri
mov r7,sbuf
ret
Thanks for helping!

Hi,

you should initialize char character before entering the while-loop!

...
i=0;
character = 0;
do{
...

The first time you call Get_Return_Value() character has an undefined value
that depends on the stack.
The last value of character before returning from Get_Return_Value() is 'a'.
This value is kept on the stack unless it is overwritten by other function
calls.
The next time you call Get_Return_Value() the 'a' could be still on the
stack. Then the while loop will break at first turn.

You should also test int i in the while loop not to become greater than 3,
because then the buffer receive_value[] will overflow!

Regards

Armin

-----Ursprgliche Nachricht-----
Von: l...
[mailto:l...]Im Auftrag von lpc9xxbeginner
Gesendet: Sonntag, 11. Januar 2009 20:45
An: l...
Betreff: [lpc900_users] read characters from UART and stop if character
'a' occurs
Hi,

i think it is pretty clear what i want. I want to read characters from
the UART and stop reading if the character 'a' occurs.
I have written a small program, but the program only works the first
time. If i call the function a second time, the program doesn't work.

My program:

int Get_Return_Value()
{
char character;
int i;
unsigned int retry;
char receive_value[4]="\0";
i=0;
do{

if(RI == 1) // if there is a character in SBUF
{
character = uart_receive();
if(character != 0x61) // if the character is not an 'a'
{

receive_value[i] = character; //save the character in the string
i++;

}

if(character == 0x61) //if the character is an 'a'
{

receive_value[i] = '\0';

}

RI = 0;
}

}while(character!= 0x61); //stay in the loop if the received character
is not an 'a'
if(receive_value[0] == '>')
{

printf("\nPrompt");
retry = 0;
}

if(receive_value[0] == 'N' && receive_value[1] == 'D')
{

printf("\nNo Disk");
retry = 2;
}

if(receive_value[0] == 'B' && receive_value[1] == 'C')
{

printf("\nBad Command");
retry = 1;

}

return retry;

}
The Main Program:

void main()
{
int retry;
//initialize the UART at 9600 Baud

for(;;)
{
printf("SCS\n");
retry = Get_Return_Value(); //call the function
}

}
The first time, i call the function, "SCS" is printed on the terminal.
And the program waits for user-input. The second time, "SCS" is
printed on the terminal in a loop - that means, the program doesn't
wait for user-input anymore.

Here is the code of the uart_receive() function:

uart_receive:
jnb ri,$
clr ri
mov r7,sbuf
ret
Thanks for helping!

Hi Armin,

thanks for the tip. Now, the program works perfectly.

Thank you!
--- In l..., "ast66" wrote:
>
> Hi,
>
> you should initialize char character before entering the while-loop!
>
> ...
> i=0;
> character = 0;
> do{
> ...
>
> The first time you call Get_Return_Value() character has an
undefined value
> that depends on the stack.
> The last value of character before returning from Get_Return_Value()
is 'a'.
> This value is kept on the stack unless it is overwritten by other
function
> calls.
> The next time you call Get_Return_Value() the 'a' could be still on the
> stack. Then the while loop will break at first turn.
>
> You should also test int i in the while loop not to become greater
than 3,
> because then the buffer receive_value[] will overflow!
>
> Regards
>
> Armin
>
> -----Ursprgliche Nachricht-----
> Von: l...
> [mailto:l...]Im Auftrag von lpc9xxbeginner
> Gesendet: Sonntag, 11. Januar 2009 20:45
> An: l...
> Betreff: [lpc900_users] read characters from UART and stop if character
> 'a' occurs
>
>
> Hi,
>
> i think it is pretty clear what i want. I want to read characters from
> the UART and stop reading if the character 'a' occurs.
> I have written a small program, but the program only works the first
> time. If i call the function a second time, the program doesn't work.
>
> My program:
>
> int Get_Return_Value()
> {
> char character;
> int i;
> unsigned int retry;
> char receive_value[4]="\0";
>
>
> i=0;
> do{
>
> if(RI == 1) // if there is a character in SBUF
> {
> character = uart_receive();
> if(character != 0x61) // if the character is not an 'a'
> {
>
> receive_value[i] = character; //save the character in the string
> i++;
>
> }
>
> if(character == 0x61) //if the character is an 'a'
> {
>
> receive_value[i] = '\0';
>
> }
>
> RI = 0;
> }
>
> }while(character!= 0x61); //stay in the loop if the received character
> is not an 'a'
>
>
> if(receive_value[0] == '>')
> {
>
> printf("\nPrompt");
> retry = 0;
> }
>
> if(receive_value[0] == 'N' && receive_value[1] == 'D')
> {
>
> printf("\nNo Disk");
> retry = 2;
> }
>
> if(receive_value[0] == 'B' && receive_value[1] == 'C')
> {
>
> printf("\nBad Command");
> retry = 1;
>
> }
>
> return retry;
>
> }
>
>
> The Main Program:
>
> void main()
> {
> int retry;
> //initialize the UART at 9600 Baud
>
> for(;;)
> {
> printf("SCS\n");
> retry = Get_Return_Value(); //call the function
> }
>
> }
>
>
> The first time, i call the function, "SCS" is printed on the terminal.
> And the program waits for user-input. The second time, "SCS" is
> printed on the terminal in a loop - that means, the program doesn't
> wait for user-input anymore.
>
> Here is the code of the uart_receive() function:
>
> uart_receive:
> jnb ri,$
> clr ri
> mov r7,sbuf
> ret
>
>
> Thanks for helping!
>
>
>
>
>
>
>
>
>