简体   繁体   中英

Converting 'uint8_t' 'float' and 'unsigned char' into 'char' for LCD display

I'm working on AtMega88PA with well-working LCD display.

I've got 2 working methods from the internet:

extern void lcd_putc(char c);          // send one 'char' to LCD display e.g. lcd_putc('A')
extern void lcd_puts(const char *s);   // send more chars e.g lcd_puts("something")

And those are working great. However if I want to send uint8_t LCD is showing weird symbol - 4 horizontal lines.

I've tried to project this integer using:

lcd_putc((char) integer); 

Both outside and inside function, with same result. I also tried to convert a number from BCD to Decimal and otherwise. It's same for unsigned char - for some reason when I try to display unsigned char same character appears on LCD display.

How do I convert other data types into char in C?

To convert unsigned , signed , float and double on AVR controllers you can use following functions:

Unsigned 2 ASCII

unsigned char data = 100;
char buffer[9];

ultoa(data, buffer, base);

Signed 2 ASCII

signed char data = -10;
char buffer[9];

ltoa(data, buffer, base);

The base can be:

  • 2 for binary
  • 10 for decimal
  • 16 for hexadecimal

Buffer size has to be adapted within the width of the number (eg for unsigned char 0-255 (decimal) 4 digits are necessary max. number + escape sequence '\\0').

Double 2 ASCII

double data = 3.14;
char buffer[20];

// Normal form:
dtostrf(data, length, precision, buffer);

// Exponential form:
dtostre(data, buffer, precision, DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE);

Parameter of floating point conversion:

  • Length is the complete length of the number (eg for 3.14 it is 4)
  • Precision defines the length to the right of the decimal point

Buffer size has to be adapted within the width of the number

The functions can be found in stdlib.h . There is a sample library here where they are implemented.

After conversion you can simply call

lcd_puts(buffer);

Maybe this helps

snprintf() is the answer!

Thank you all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM