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.)
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)
Circuit Cellar's editorial team comprises professional engineers, technical editors, and digital media specialists. You can reach the Editorial Department at editorial@circuitcellar.com, @circuitcellar, and facebook.com/circuitcellar