We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
By "HAL", I assume you mean the STM32 HAL here?
Note that the STM32 HAL has nothing to do with Keil - it is entirely ST's thing.
ST Document it here:
UM1725 User Manual Description of STM32F4 HAL and LL drivers www.st.com/.../jcr:content/translations/en.DM00105879.pdf
"HAL_UART_Transmit ... But, that one is for characters only"
No, it isn't - it just transmits a buffer of any arbitrary data. That's why it takes a pointer & length as parameters - rather than a string.
But, if you want to convert numbers to strings, that is a standard 'C' question - nothing to do with ST or Keil. Take a look in your 'C' textbook at the section on the Standard Library functions...
Or here's an online listing of the Standard 'C' Library functions: www.cplusplus.com/.../
char buffer[16]; int i = 15; HAL_UART_Transmit(&huart2, (uint8_t*)buffer, sprintf(buffer, "%d", i), 500);
itoa() might be another choice. Breaking down a number into decimal digits should be something basic math skills should cover.
hello mr pier , thanks for sharing the code
i have already tried your code by using sprintf() and itoa() and the result is same.let me explain it, the number will split into buffer one by one.
num = 49 // sprinf() or itoa() method buffer [0] = 4 buffer [1] = 9
maybe what he meant was to send a buffer with one variable
buffer [0] = 49
Why are you waking-up a thread that has been dead for over 2 years?
You seem to be confusing the numerical values with ASCII character codes.
If you want to send the value 49 (ie, 0x31) as ASCII coded, human-readable text, then you need to send the character '4' followed by the character '9'
ie, in your example:
buffer [0] = '4'; buffer [1] = '9';
That's not what the question asks, reread it
uint8_t ctrlZin = 0x1A;
HAL_UART_Transmit(&huart1, &ctrlZin, 1, 100);
this has worked for while sending ctrl+z (hex value 1A) on uart to GSM board, for SMS sending.
I honestly don't know why these old threads are kept open for years, and subject to irrelevant answers