cancel
Showing results for 
Search instead for 
Did you mean: 

ascii message throught uart on keil not working perfectly like on cubeide

salss_emb
Associate

when i downloaded the code from cube ide and then try to visualize it with putty it works perfectly but when i moved the project to keil and then download it from there i didn't got the same result ascii is stuck at number 8 and it's not showing the write output related to volatge what should i do ?? , do i need some new configurations on keil cause i'm not getting any error neither warnings !!!! 

 

 

here's the function of to convert an integer to ascii :

char* shift_digits(int input_integer) {
// Convert the integer to a string
char input_str[20]; // Assuming a reasonable maximum length for the string
sprintf(input_str, "%d", input_integer);

// Allocate memory for the result string
char* result_str = malloc(3 * strlen(input_str)); // Each digit is shifted by 3 characters, and 2 extra characters for spaces and null terminator

// Shift each digit by +48 and join them with spaces
int index = 0;
for (int i = 0; i < strlen(input_str); i++) {
int shifted_digit = input_str[i] ;
index += sprintf(result_str + index, "%d ", shifted_digit);
}

// Remove the trailing space and add the null terminator
result_str[index - 1] = '\0';

return result_str;
}

1 ACCEPTED SOLUTION

Accepted Solutions

Do you ever bother to free() the malloc() ?

Check startup.s for Heap size, check you don't get a NULL returned, and free() the allocation.

Perhaps consider using a static allocation you can return.

ie static char result_str[32]; in the function

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

Do you ever bother to free() the malloc() ?

Check startup.s for Heap size, check you don't get a NULL returned, and free() the allocation.

Perhaps consider using a static allocation you can return.

ie static char result_str[32]; in the function

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

yasss thank you !!

i added :  free(msg2);

and it works perfectly!