How to have a decimal number display on LCD?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 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.

0 Likes
15 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Use sprintf() to print into a char buffer, with the formatting you want. Then you print out that buffer like any other string.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

sprintf( ) ( printf( ) ) eference material, attached.

   

 

   

 

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        Just like this ?But it doesn't work well as before.   

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); }

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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]; ).

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable
        Yeah,your advices are all right.Now i modify the code,as the following,i also can't get the ideal results,like the two-figure decimal,it will display in the ten's digit and hundred's digit,the unit's digit always is 3, for example 95 will display as 953.Like the single-figure decimal,it will display in the hundred's digit,the ten's digit and unit'digit always are 23,for example 1 will display as 123 ,the code:   

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); }

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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...

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This seemed to print OK , attached.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The attached cleans up a line of code you don't need and

   

minor stuff.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 @hli

   

Yeah,this is the key problem of my project.THX.

0 Likes
Anonymous
Not applicable

@danaaknight

   

hah,I just want a project to write and read EEPROM.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

@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?

0 Likes
Anonymous
Not applicable

 @hil

   

Yeah,the code above is a part of the whole main.c file.I just copy the part related this problem.

0 Likes