- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear all,
Now I want to read datas from EEPROM to LCD,I use the API——LCD_Char_PrintNumber(),it can diplay three-figure decimal correctly,like 110,122.However,it can't read the two-figure decimal and single-figure decimal right,like 11,9.It maybe caused by it said"left-justified ASCII characters",isn't it?But ,why the three-figure decimal can be display right?How can i read all the datas right on display?THX.
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Use sprintf() to print into a char buffer, with the formatting you want. Then you print out that buffer like any other string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are several different ways to accomplish conversion of a binary number to ascii characters. As hli suggested sprintf() is the most convinient and easiest way, but will consume the most resources. Using itoa() is another ready to use function in C.
Lastly a do-it-yourself solution by constantly dividing an integer by 10 until the result is zero and converting the remainder to an ascii char (by adding '0') will do the job as well.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
char* da=(char*)malloc(1); int ta; for(index=0;index<100;index++) { ta=sprintf(da,"%d",CY_GET_REG8(CYDEV_EE_BASE+index)); LCD_Char_1_PrintString(da); if(index==99) index=0; CyDelay(1000); }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The 'malloc(1)' is wrong. You need to allocate as many characters as your printed string can be, plus one (for the terminating '\0').
Its also easier to just allocate a static buffer ( char* da=char[10]; ).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, there ARE benefits from using malloc(), but you are requested to use free() whwn you don't need the allocated memory any more. Additionally there is an overhead for malloc's hosekeeping which is at least 4 bytes for each requested chunk of memory. So in your case, as hli stated, using a global, static or automatic) buffer would be the best solution.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
char da[4]; uint8 lut[100] = { 127,134,142,150,158,166,173,181,188,195,201,207,213,219,224,229, 234,238,241,245,247,250,251,252,253,253,253,252,251,250,247,245, 241,238,234,229,224,219,213,207,201,195,188,181,173,166,158,150, 142,134,126,119,111,103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2, 3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95,103,111,119 }; int index,ta; for(index=0;index<100;index++) { LCD_Char_1_Position(1,strlen("Success ")); ta=sprintf(da,"%d",CY_GET_REG8(CYDEV_EE_BASE+index)); LCD_Char_1_PrintString(da); if(index==99) index=0; CyDelay(1000); }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just add an LCD_Char_1_Clear() inbetween - the extraneous charactaers you see is because they just stay in the display, and _PrintString() doesn't overwrite them...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Change in my code example -
ta = sprintf( da, "%d", lut[ index ] );
to
ta = sprintf( da, "%3d", lut[ index ] );
That will take care of the width option to implement 3 digit right justified field.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@hli
Yeah,this is the key problem of my project.THX.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@danaaknight
hah,I just want a project to write and read EEPROM.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@mark: it was really non-obvious. Took me an hour or so of testing to see what the real reason was...
Did you look at the EEPROM component sample projects and the data sheet?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@hil
Yeah,the code above is a part of the whole main.c file.I just copy the part related this problem.