Skip to main content
Visitor II
September 13, 2014
Question

Help me get string for stm8l152c8 through usart communication

  • September 13, 2014
  • 6 replies
  • 1581 views
Posted on September 13, 2014 at 08:33

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!
    This topic has been closed for replies.

    6 replies

    Visitor II
    September 13, 2014
    Posted on September 13, 2014 at 10:25

      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.

    chuanut4Author
    Visitor II
    September 13, 2014
    Posted on September 13, 2014 at 10:58

    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.

    Visitor II
    September 13, 2014
    Posted on September 13, 2014 at 12:07

    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;
    }

    chuanut4Author
    Visitor II
    September 13, 2014
    Posted on September 13, 2014 at 17:03

    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.

    Visitor II
    September 14, 2014
    Posted on September 14, 2014 at 11:17

    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.

    chuanut4Author
    Visitor II
    September 14, 2014
    Posted on September 14, 2014 at 14:04

    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!