How do you display decimal numbers to an LCD using Peter Fleury's Library????

## Displaying decimal Numbers to LCD

itoa() the numbers and then use the LCD print string routines perhaps?

Or wire up printf() to the lowest level LCD put character routine

Cliff

I tried this in part of my program and It always rounds up...I need it to display the whole decimal number!!

char buffer[7]; uint8_t PH = 8.02; .... .... itoa(PH, buffer, 10); lcd_puts(buffer);

SO I put 8.02 into PH and I get 8 out of buffer.

uint8_t PH = 8.02;

You do realise that the .02 in that is completely pointless don't you? As soon as you assign a float constant to an integer variable it will be converted to integer with any fractional decimal part being thrown away.

The easiest solution to this is:

uint16_t PH = 802;

where you are now holding everything in "one hundredths". You now don't need to use 'float' at all and the only tricky bit is when the number finally gets out to the display you may need to output a '.' two digits before the end of the number (or just tell the user you are display 1/100th's of a volt rather than volts (or whatever the unit is).

If you really want the .02 on that then the alternative would be:

float PH = 8.02;

but thn, before you output it, you'll need to do a floating binary to ASCII digit conversion. Often the simplest library function for this is (s)printf and the "%f" format in particular.

Cliff

I wrote this based on Peter Fleury's Library a while ago. It displays floating point (upto 2 dp) at position X and Y. I used it to debug the gyroscope readings from the Balancing Robot I made.

/* -Function displays 2 dp floating at x and y location on Hitachi HD44780 LCD -based on Peter Fleury's library */ void print_dp(float val, int x, int y) { int n = (int)val , c=0; int neg = -1*n; float valab = -1*val; if( 0<=val ){ // for positive lcd_gotoxy( x, y); lcd_puts("+"); lcd_gotoxy( x+1, y); lcd_put_int( n ); while ( n>0 ){ //count digits c++; n/=10; } lcd_gotoxy( x+c+1, y); lcd_puts("."); lcd_gotoxy( x+c+2, y ); lcd_put_int( ( (int)(val*10.0) ) % 10 ); lcd_gotoxy( x+c+3, y); lcd_put_int( ( (int)(val*100.0) ) % 10 ); } else { //for negative lcd_gotoxy( x, y); lcd_puts("-"); lcd_gotoxy( x+1, y); lcd_put_int( neg ); while ( neg>0 ){ c++; neg/=10; } lcd_gotoxy( x+c+1, y); lcd_puts("."); lcd_gotoxy( x+c+2, y ); lcd_put_int( ( (int)( valab*10.0) ) % 10 ); lcd_gotoxy( x+c+3, y); lcd_put_int( ( (int)( valab*100.0) ) % 10 ); } }

A way of formatting a number as fixed point interpetion into a buffer.

// Itoa for Fixed point numbers. Modified itoa from wikipedia.org. void SPrintFixed( char* Buf, uint16_t n, uint8_t Length, uint8_t Precision) { uint8_t Cnt, Digits = 0; char Tmp; do { // Generate digits in reverse order. Buf[Digits++] = n % 10 + '0'; // Get next digit. if( Digits == Precision) // Add fixed point here. Buf[Digits++] = '.'; n /= 10; }while( n); if( Digits < Precision) // Always write '.' and precision. { while(Digits < Precision) Buf[Digits++] = '0'; Buf[Digits++] = '.'; } while(Digits < Length) { Buf[Digits++] = ' '; // Add spaces to the end. } Buf[Digits--] = '\0'; // Reverse digits and leading whitespace. for(Cnt = 0; Cnt < Digits; Cnt++, Digits--) { Tmp = Buf[ Digits]; Buf[ Digits] = Buf[ Cnt]; Buf[ Cnt] = Tmp; } }

With the function above the code below should put the string " 8.04" in the buffer, ready for output to a lcd, uart or whatever.

uint16_t TH = 804; char Buffer[20]; SPrintFixed( Buffer, TH, 5, 2);

A way of formatting a number as fixed point interpetion into a buffer.

For deeper research, digest this thread:

https://www.avrfreaks.net/index.p...

See my utoaz() routine at https://www.avrfreaks.net/index.p... and compare to your approach. You have a % and / for each digit; at the least I'd use div() to save one of the operations.

Will %+/ compare with my "traditional" subtract method? Hmmm... And there is also the reversing loop.

I like my approach for simplicity/straightforwardness. And the cycle counts aren't too bad.

I'll just point out that a 4 year old thread has been resurrected here - make of that what you will ;-)

Hi - forgive my ignorance - where is the function lcd_put_int

Russell

When I google "lcd_put_int" the first hit is this:

https://www.avrfreaks.net/forum/d...

Now that may not be the lcd_put_int() talked about here. But it's certainly one that looks like it should do the job.

The essence of any "put_int" is to take the numeric value, turn that into a string of ASCII digits then send that. The itoa() library function achieves about 90% of that.

**On Tue. Jan 31,**

**2012**- 06:52 PM - clawson wrote:I'll just point out that a 4 year old thread has been resurrected here - make of that what you will ;-)

And, 3 years later, it's resurrected again!

No problem. It shows that Russell did a search first. And any answers will be equally valid.