Hi guys,
I'm using Keil V5.8 and I'm trying to write a C function that converts an integer to a string which is then passed onto another function that displays it on an LCD. The code I'm using is
unsigned int lengthOfDigits = 20; int IntergerToBeDisplayed = 106; char StringNumber [lengthOfDigits]; sprintf(StringNumber, "%d", IntergerToBeDisplayed); LCDSendAString(StringNumber);
Using the above code does not work at all, it builds without any errors or warnings but the display does not respond. But using a constant value for the array size instead of passing a value works. char StringNumber[20];
From what I have researched, before C99 using an array in such a way was not possible in C. I have made sure that Keil is configured for C99 and I have also tested a version of the code in code blocks ide which works fine
#include <stdio.h>#include <stdlib.h>
int main(){ unsigned int lengthOfDigits = 20; int numberOfElements = 120; char digits [lengthOfDigits]; sprintf(digits, "%d", numberOfElements); printf("%s", digits); return 0;}
Am I missing something here? I'm still new at embedded programming so if my question does not make sense please let me know and I will change it.
Hello Anubus, when i'm need converts integer to a string i do this operation:integer + 0x30 = integer on ascii (this is only for a digit, you have to convert the numer in a digit secuence, and for each digit you needed do the operation)
if you need the inverse operation, convert an character array ,only if necessary do this operation:
character - 0x30 = integer
Perhaps disconnect the issue from the display? Subdivide the problem. What it does on a PC is immaterial.
Check that the data in your digits buffer actually looks correct in the debugger.
Test the display directly LCDSendAString("Hello");
Test it also with your string sprintf(StringNumber, "INT %d", IntegerToBeDisplayed);
Consider using itoa() function, it is less resource heavy.
Thanks for the answers guys. I think I have solved the problem. the ARM compiler setting was set to; use default compiler version 5. changing it to V6.12 seems to solve the problem.