Printing Hexadecimal Numbers onto an LCD

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
1
14%
4
2
29%
5
4
57%
 
Total votes: 7

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Printing Hexadecimal Numbers onto an LCD

Post by Benj »

Image

On a microcontroller numbers can be represented by variables in memory. The size of memory allocated for storing the numbers are defined by the maximum size of the number you wish to use. A byte variable otherwise known as a char or 8-bit variable can hold a maximum number of 255. An integer variable otherwise known as a word or 16-bit variable can hold a maximum number of 65535. However Flowcode uses signed integer variables to allow for negative numbers so the range is actually -32768 to 32767.

Numbers can be inputted into the system as decimal, hexadecimal or binary format. On the microcontroller itself each of these formats are handled in binary so there is no potential pit falls or reasons to use one format over another. Therefore it is down to preference.

Image

Hexadecimal is popular as it is very easy to convert between hex and binary in your head. To convert from hex to binary simply look at a character at a time and associate a 4-bit binary pattern.

Image

For example:
A hexadecimal value of 0x5A represents binary 01011010.
A hexadecimal value of 0x5A5A represents binary 0101101001011010.

Now comes the slightly tricky bit : ASCII data.

ASCII is a means of assigning characters and control commands to a byte variable or 8-bit number. This is used in everyday items like computer keyboards to encapsulate the keystrokes into single byte data. It is important to note that an ASCII byte's data is exactly the same as a normal byte it is however the meaning that is different.

For example
Numeric Byte value of 48 decimal (0x30 hexadecimal) = ASCII Value '0'

For a complete list of ASCII codes please refer here: http://www.asciitable.com/

The LCD prints out characters as ASCII code so therefore we need a way of transferring the single byte variable into a string of ASCII data.

For example
Byte variable 255 needs to be converted into ASCII string "0xFF" - 4 bytes long
Int variable 65535 needs to be converted into ASCII string "0xFFFF" - 6 bytes long

Two software macros have been provided to allow for numbers to be converted from byte or integer variables into ASCII strings, which are then send out to a LCD display. The macros can be included into your programs by using the Macro -> Import menu. The LCD could however be replaced by an RS232 bus or similar for outputting hexadecimal strings.

Image
Attachments
Number to Hex.zip
(4.49 KiB) Downloaded 1137 times

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Re: Printing Hexadecimal Numbers onto an LCD

Post by Docara »

Hi Ben

I'm having trouble implementing the Byte2Hex function you posted years ago could you explain a little more how to use it in anger - I can import it then what

Thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Printing Hexadecimal Numbers onto an LCD

Post by mnf »

Use the NumberToHex$ function - see post in other thread....

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Re: Printing Hexadecimal Numbers onto an LCD

Post by Docara »

Found it, I'll have a play
Thanks

Post Reply