Hello
I am working on the project where I need to send ASCII numbers to to LCD
any idea how to do convert it in main function
Code:void LCD (unsigned char *PTR )
{
//LCD code
}
main ()
{
send (0x31);
send (0x45);
send (0x61);
... so on
}
Printable View
Hello
I am working on the project where I need to send ASCII numbers to to LCD
any idea how to do convert it in main function
Code:void LCD (unsigned char *PTR )
{
//LCD code
}
main ()
{
send (0x31);
send (0x45);
send (0x61);
... so on
}
Convert the "number" to a string and then "send" each character of that string.
Why not just use sprintf() to convert the "number" to a string? By number I mean something like 100983.
Do you not understand that 41 (hex) already equals 65 (decimal) which is equal to the 'A' character?
Perhaps you should study the ASCII chart for more information.
"ASCII numbers" are just numbers. The only time the "ASCII" part matters is when you're interpreting that number as a character.
In C, a char or unsigned char value is just a number. What makes it a "character" is that putting a source code character in (') apostrophes for a char constant or inside (") quotes as part of a string constant will convert that character to the corresponding number. The number is what the C program deals with. In particular, the constant 'B' is an int constant in C, not a char. Assuming that ASCII is the compiler's character set, there is absolutely no difference between using 'B', 0x42, or 66 in a C program. They are all different ways of writing the same number.
If you want to display that value as a character, you usually send it to a device unchanged, as in send(66) or send('B'). If you want to draw those characters on an LCD panel that doesn't support it, that needs a chapter in a book rather than a Q&A answer.
If you want to send a numeric representation, convert the number to a string and send the characters one at a time. Here's one way to do that to "send" as character as two hexadecimal digits:
You can also use sprintf() to convert a number to a string in decimal, hexadecimal or octal format. That's usually best avoided in low-level code. (I assume that any code directly interfacing to an LCD device is low-level.) You usually want the smallest reasonable code size, and sprintf() is *not* small. Most C compilers implement _itoa(), but if you know you need precisely 3 decimal digits, it's probably best to just write the C code, something like:Code:void send_hex(int c)
{
const static char hextab[] = "0123456789ABCDEF";
send(hextab[c & 0xF]);
send(hextab[(c >> r) & 0xF]);
}
Code:send('0' + c/100%10);
send('0' + c/10%10);
send('0' + c%10);