2014-09-12 11:33 PM
I'm trying to get a string from usart to stm8l, but when i got 2byte char, the RXNE is clear, and i can't get anymore char.
My code is:main{int8_t count = 0;
getdata = ''''; while(1) { getdata[count++] = getchar(); if(getdata[0]==0x0D&&getdata[1]==0x0A) { getdata[0]=getdata[1]=NULL; count=0; } if(getdata[count-1]==0x0A) { getdata[count-1]=NULL; getdata[count]=NULL; break; } }}GETCHAR_PROTOTYPE{ char c = 0; /* ??????? */ while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); /* ??????? */ c = USART_ReceiveData8(USART1); USART_ClearFlag(USART1, USART_FLAG_RXNE); return (c); }Please help if you have any idea!Thanks so much!2014-09-13 01:25 AM
c = USART_ReceiveData8(USART1);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
I think you clear the RXNE flag twice here as reading data reg also clears it.It may cause some problem.Edit:
if(getdata[0]==0x0D&&getdata[1]==0x0A)
{ getdata[0]=getdata[1]=NULL; count=0; } if(getdata[count-1]==0x0A) { getdata[count-1]=NULL;The above code can actually try to access getdata[-1], it's really not good.2014-09-13 01:58 AM
I have deleted line '' USART_ClearFlag(USART1, USART_FLAG_RXNE);'' in GETCHAR prototype.
With code:if(getdata[0]==0x0D&&getdata[1]==0x0A)
{
getdata[0]=getdata[1]=NULL; count=0;
}
if(getdata[count-1]==0x0A)
{
getdata[count-1]=NULL;
I try to get the breakpage symbol, and in line ''getdata[count++] = getchar();''I have increased count variable, so i won't get access ''getdata[-1] error''.I try very much, evenwhen i dont care about RXNE flag, but I always get only 2 char. I'm very grateful if you can help me get a string to stm8l.2014-09-13 03:07 AM
int8_t count = 0;
getdata = ''''; What is the actual size of getdata buffer, where declared.If it's not the buffer problem then I don't know what.With code:
if(getdata[0]==0x0D&&getdata[1]==0x0A)
{
getdata[0]=getdata[1]=NULL; count=0;
}
if(getdata[count-1]==0x0A)
{
getdata[count-1]=NULL;}
I try to get the breakpage symbol, and in line ''getdata[count++] = getchar();''
I have increased count variable, so i won't get access ''getdata[-1] error''.
OK, but when you get 0x0d+0x0a, count is reset to zero ant then you access getdata[0-1].Baybe it should be:if(getdata[0]==0x0D&&getdata[1]==0x0A)
{
getdata[0]=getdata[1]=NULL; count=0;
continue;
}
2014-09-13 08:03 AM
Thank you so much knik, you're so gracious!
I declared char getdata[32]. And i deleted all of code except line ''getdata[count++] = getchar();'' in while(1) to get data, but i just got 2 char when I debug.Do u have any idea to resolve this problem.2014-09-14 02:17 AM
Maybe baud rate is not set correctly, make sure you receive correct chars.
It may happen that you receive some garbage when data rates don't match.2014-09-14 05:04 AM
Yes, I recieve the correct char. Baudrate is valid.
I think the data buffer of stm8l just only 2byte. But if it's right, how to recieve a string from usart! So difficult!