2011-08-17 01:39 AM
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? Thanks2011-08-17 05:31 AM
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.2011-08-17 05:32 AM
2011-08-17 05:55 AM
2011-08-17 05:56 AM
2011-08-17 07:06 AM
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
2011-08-17 07:52 AM
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.2011-08-18 06:14 PM
Yep you put mine to shame.. :p
I will replace it with your one,
thanks.
2011-09-29 04:39 AM
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);2011-09-29 05:14 AM
Wouldn't you just use a routine like the one Trevor posted?
send_string(''\r\n+STWMOD=0\r\n''); ...