0X30 and LCD

Thread Starter

beeson76

Joined Apr 19, 2010
211
I cant grasp the idea that I add 0x30 to a number to get it to display on the LCD. I am looking at the ASCII chart and see that 0 is 48 Decimal and 0x30 Hex. 1 is 49 Decimal and 0x31 Hex. I see that but I just dont understand it.

I think I understand the LCD only wants to see ASCII characters...and that there is a difference in the number 1 and character 1...but where does adding 0x30 change things?

Thanks for any help provided.
 

bretm

Joined Feb 6, 2012
152
"1" is just a character as far as the LCD is concerned. It's not a number. But the LCD wants you to send it the "code" for each character you want to display. The codes for characters "0" through "9" are 48 through 57. So just adding 48 to any single-digit number will give you the code for the character that corresponds to that digit.

Another example is adding 64 to a number from 1 to 26 will give you the upper-case character corresponding to that position in the alphabet. For example, the letter "M" is the 13th letter, so the code is 64 + 13.
 

spinnaker

Joined Oct 29, 2009
7,830
I cant grasp the idea that I add 0x30 to a number to get it to display on the LCD. I am looking at the ASCII chart and see that 0 is 48 Decimal and 0x30 Hex. 1 is 49 Decimal and 0x31 Hex. I see that but I just dont understand it.

I think I understand the LCD only wants to see ASCII characters...and that there is a difference in the number 1 and character 1...but where does adding 0x30 change things?

Thanks for any help provided.
Look at your ASCII table again and it will be obvious.

ASCII zero is equivalent to 0x30

so:

0 + 0x30 = 0x30 or '0'
1 + 0x30 = 0x31 or '1'
2 + 0x30 = 0x32 or '2'

and so on.

Of course this only works with BCD (binary coded decimal) numbers 0 - 9. So maybe that is where you confusion is.

11 + 0x30 <> '11'

You need to first convert the 11 (single byte) into two bytes 0x01 and 0x01.
 
Top