Skip to main content
cyril2399
Associate III
August 17, 2011
Question

Usart send more than one character

  • August 17, 2011
  • 14 replies
  • 3338 views
Posted on August 17, 2011 at 10:39

Hello,

I saw that in usart.c there is the function   USART_SendData(USART2, 'a');

but this is for only one character (here 'a').

I want to send more than one character but I can't find how.

I think that USART_SendData(USART2, 'hello'); won't work.

Coud anyone help me please?

Thanks
    This topic has been closed for replies.

    14 replies

    Tesla DeLorean
    Guru
    August 17, 2011
    Posted on August 17, 2011 at 14:31

    Send it one character at a time, pretty fundamental C.

    char str[] = ''hello''

    char *s;

    s = str;

    while(*s)

    {

     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);

     USART_SendData(USART2, *s++);

    }

    Or you could get it suitable hosted, and use putchar/puts/printf.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    trevor23
    Associate III
    August 17, 2011
    Posted on August 17, 2011 at 14:32

    void send_string(const char *str)

    {

        while (*str)

        {

            while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);

            USART_SendData(USART2, *str++);

        }

    }

    send_string(''hello'');

    EDIT: posted at same time as Clive's post above - didn't mean to duplicate.

    cyril2399
    cyril2399Author
    Associate III
    August 17, 2011
    Posted on August 17, 2011 at 14:55

    cyril2399
    cyril2399Author
    Associate III
    August 17, 2011
    Posted on August 17, 2011 at 14:56

    munk2k
    Associate
    August 17, 2011
    Posted on August 17, 2011 at 16:06

    This is another way of doing it;

    #######START CODE#######

    void SendData(u8 USARTx,u16 ch)

    {

    u16 Value[5],i;

    Value[0] = (ch /1000 ) ; // divide number by 1000

    ch = (ch % 1000) ; // take remainder number of 1000

    Value[1] = (ch / 100 ) ; // divide this now by 100

    ch = (ch % 100 ) ; // take remainder of 100

    Value[2] = (ch / 10) ;  // divide remainder by 10

    Value[3] = (ch % 10 ) ; // load remainder of final Dimension into unit variable

      switch(USARTx)

      {

         case 1:

      for (i=0; i<5; i++)

      {

      Value[i]=(Value[i]+48); //convert to ASCI numbers

      while (!(USART1->SR & USART_FLAG_TXE)); // check that USART is ready

      USART_SendData(USART1, Value[i]); //send data

      }//end for

      USART_SendData(USART1, '\n'); //send newline

         break;

         case 2:

      for (i=0; i<5; i++)

      {

      Value[i]=(Value[i]+48); //convert to ASCI numbers

      while (!(USART2->SR & USART_FLAG_TXE)); // check that USART is ready

      USART_SendData(USART2, Value[i]); //send data

      }//end for

      USART_SendData(USART2, '\n'); //send newline

     break;

      }

    }

    #########end code################

    Its a little crude but allows you to send numbers (and only numbers) back to usart1 or 2 to a maximum value of 9999

    Tesla DeLorean
    Guru
    August 17, 2011
    Posted on August 17, 2011 at 16:52

    I think he asked for strings. For 32-bit numbers the following is less crude, and works with all USARTs.

    void USART_SendData_Numeric(USART_TypeDef* USARTx, u32 x)

    {

      char Value[10];

      int i;

      i = 0;

      do

      {

        value[i++] = (char)(x % 10) + '0';

        x /= 10;

      } while(x);

      while(i)

      {

        while (!(USARTx->SR & USART_FLAG_TXE)); // check that USART is ready

        USART_SendData(USARTx, Value[--i]); //send data

      }

      while (!(USARTx->SR & USART_FLAG_TXE)); // check that USART is ready

      USART_SendData(USARTx, '\n'); //send newline

    }

    If the footprint permits sprintf() you could use that to generate a formatted string, and then output the string. Which would be the way I'd go when using floating point numbers.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    munk2k
    Associate
    August 19, 2011
    Posted on August 19, 2011 at 03:14

    Yep you put mine to shame.. :p

    I will replace it with your one, 

    thanks.

    sam239955
    Associate III
    September 29, 2011
    Posted on September 29, 2011 at 13:39

    and how can i do that with stm32 ??

    sendBlueToothCommand = is send to USART2

        delay(1000);

        sendBlueToothCommand(''\r\n+STWMOD=0\r\n'');

        sendBlueToothCommand(''\r\n+STNA=SeeeduinoBluetooth\r\n'');

        sendBlueToothCommand(''\r\n+STAUTO=0\r\n'');

        sendBlueToothCommand(''\r\n+STOAUT=1\r\n'');

        sendBlueToothCommand(''\r\n +STPIN=0000\r\n'');

        delay(2000);

        sendBlueToothCommand(''\r\n+INQ=1\r\n'');

        delay(2000);

    Tesla DeLorean
    Guru
    September 29, 2011
    Posted on September 29, 2011 at 14:14

    Wouldn't you just use a routine like the one Trevor posted?

    send_string(''\r\n+STWMOD=0\r\n'');

    ...

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mubinicyer
    Associate III
    March 25, 2014
    Posted on March 25, 2014 at 11:24

    Hi clive1,

    In your function the parameter u32 x is being changed rather than staying constant. Won't it change the original x as well?

    I think it would be better if we write it so:

    void USART_SendData_Numeric(USART_TypeDef* USARTx, const u32 x)

    {

    u32 a = x;

          // ...

      do

      {

        value[i++] = (char)(a % 10) + '0';

        a /= 10;

      } while(a);

    //....

    //....

    }