I have some question about atoi() function. I'm trying to convert string data received from HAL_UART_Receive_IT function to integer and next to sent again to terminal. And what I have at the exit it Looks like garbage
enter image description here
This is part of my code
uint8_t UART_Rx_Data;
----------------------
while (1){
HAL_UART_Receive_IT(&huart2, (uint8_t*)&UART_Rx_Data, sizeof(UART_Rx_Data));
}
----------------------
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if (huart == &huart2){
int i = atoi((char *)&UART_Rx_Data);
HAL_UART_Transmit_IT(&huart2, (uint8_t *)i, sizeof(i));
}
}
How to get integer from UART_Rx_Data?
*I really believed that there was a problem with atoi function, because another part of my code looks like
if (huart == &huart2){
unsigned int Left_El = atoi((char*) &UART_Rx_Data);
for(unsigned int i = 0; i <= 4095; i++){
if(i == Left_El){
unsigned int ADC_Rx_Data = round((i * (100.0 / 4095.0)) + 25);
__HAL_TIM_SET_COMPARE(&htim21, TIM_CHANNEL_1, ADC_Rx_Data);
}
}
}
I need just to receive integer data from UART and add duty cycle to TIM, but it doesn't work. I tried to check why, and when I send throw UART data it turned out that there have garbage. Now I will try remade by loop "for" for the reason like mentioned below that atoi waits for "\0"
This line of your posted code passes the value of the variable
icast to an address to the functionHAL_UART_Transmit_IT(), not the address of the value contained ini:Per the documentation for
HAL_UART_Transmit_IT()that I found:The second argument -
pDatain the documentation - is a pointer to (or the address of) the data to be sent.This would match that documentation:
Note the addition of the
&"address-of" operator toi.