2018-02-27 12:17 AM
Posted on February 27, 2018 at 09:17
Hello Everyone;
To send ASCII characters via USART I normally use Hal libraries and the function below;
HAL_UART_Transmit(&huart2, (uint_t*)'Hello', 6,10);
But, that one is for characters only.
How can I send the value of an integer over USART as its ASCII characters ? Which functions and libraries should I use ?
. .
. For example (dummy program just to show my purpose):
int i = ADC read value; //Lets say i=15
printf(i); //value should be sent in ASCII characters, so 1 and then 5 should be sent in ASCII.
. .
.
Thanks for your help, Kind regards
Salih
2018-02-27 01:08 AM
You can use sprintf(char *buffer, char *formatted_string) to convert printf-like formatted string to a text (char * buffer for example) then send this buffer through the HAL_UART_Transmit()
You can also implement some primitive function required by printf - I mean putchar() where the actual implementation will contain the HAL_UART_Transmit(). I used both approaches with Keil IDE.
2018-02-27 02:29 AM
,
,
Something like that for the first option.
♯ include <,string.h>,
char buffer[100],
int a=15,
sprintf(buffer, '%d', a),
HAL_UART_Transmit(&,huart1, (uint8_t *)buffer, strlen(buffer), 1000) ,
assumed uart1
---------------------------------------------------------------
In case of using regular printf.
If you use Keil just google retarget.c to find Keil way of redirecting the printf to the UART.
2018-03-03 10:37 AM
This has already been answered in your cross-post on the Keil forum:
2018-03-03 11:44 AM
Seems to be the same solution... or I cannot see the difference...
2018-03-03 11:50 AM
Yes - it is.
2018-03-03 11:56 AM
Great. You know, I closely follow a few people here and try to understand every piece of what you say.
It's the only way for me to learn something from professionals