cancel
Showing results for 
Search instead for 
Did you mean: 

Usart send more than one character

cyril2399
Associate II
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
14 REPLIES 14
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
Up vote any posts that you find helpful, it shows what's working..
trevor23
Associate III
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
Associate II
Posted on August 17, 2011 at 14:55

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

munk2k
Associate II
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

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
Up vote any posts that you find helpful, it shows what's working..
munk2k
Associate II
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 II
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);

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
Up vote any posts that you find helpful, it shows what's working..