EE Tips Resources

Translating Values: Number-to-ASCII Conversion (EE Tip #147)

There is a neat trick for performing the number-to-ASCII conversion. (We are not sure of this trick’s origin. We’d love to hear from anyone who saw it long ago and can us more about it.)ASCII-iStock_000007340231_Large

One starts by defining an array in the 8051 family microcontroller’s code segment using the following command:

HEX_NUM: DB ‘0123456789ABCDEF’

Following the conventions that now seem to be pretty standard for 8051 family assemblers, this command cause an array whose values are the ASCII values of the characters within the single quotes to be stored in the code segment (assuming that that is the segment in which you are working). To convert a number between 0 and 15 to the relevant ASCII value, you now need only store the value to be converted in the accumulator, A, and the location of the array HEX_NUM in the data pointer, DPTR. You then give the single command MOVC A, @A+DPTR. This command uses the current value of the accumulator, the value you are looking to translate into an ASCII value, as an offset and reads the relevant value of the array into A (thus overwriting the value you wanted to “translate” with the correct translation). Thus, if A starts off with a 5, it ends up with the sixth element of the array (as there is an element with a zero offset)—which is just the ASCII value of 5, and all of this takes place without any hard work on the part of the programmer.—Ahron Emanuel, Shlomo Engelberg, and Dvir Ophir (“Virtual Instrumentation, Circuit Cellar 294, 2015)


Don't miss out on upcoming issues of Circuit Cellar. Subscribe today!

 
 
Note: We’ve made the October 2017 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.


Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.
Website | + posts

Circuit Cellar's editorial team comprises professional engineers, technical editors, and digital media specialists. You can reach the Editorial Department at [email protected], @circuitcellar, and facebook.com/circuitcellar