I am using timer 0 of the 8051 microcontroller as a counter to count pulses from external pin T0. How can I display the value of TL0 on an LCD? I am using the following method to display strings:

code unsigned char word_A[] = "testing";

lcd_string(word_A);   //Display on LCD

Is there a way to pass the value of TL0 to the lcd_string function?

  • \$\begingroup\$ Use sprintf to a string, and then display it. \$\endgroup\$ – Eugene Sh. Jun 1 '15 at 16:05

Two basic methods.

You can do as @Eugene Sh suggested and use a buffer array and sprintf(), or you can redefine the primitive function that printf() calls (something like putchar) so that it writes to the LCD.

If you use the sprintf method, be sure to leave room for the terminating \0.

  • \$\begingroup\$ Is it possible to declare an array, move the value of TL0 into the array, and than send the array to be displayed? Something like: ... code unsigned char counts[3]; ... counts = TL0; \$\endgroup\$ – dritech Jun 1 '15 at 18:20
  • \$\begingroup\$ The LCD takes ASCII characters and the value is in binary. To convert the binary to ASCII requires something like printf or a you can write a simple routine to do it (which might save space). Hint: use % (modulo) 10 repeatedly. \$\endgroup\$ – Spehro Pefhany Jun 1 '15 at 18:27

Many common LCD displays support a mode which displays data in right-to-left order. This can be very useful when outputting numbers. To output a number, one can set the cursor position to the location of the last digit, select right-to-left mode, and do something like:

void out_number(unsigned int n, unsigned char digits, unsigned char pad)
    lcd_dat(n % 10 + '0');
    n /= 10;
  } while(n && --digits);

This method will output a number using the specified number of digits and pad it with the indicated character. There's no need to pre-buffer the value as would be necessary using something like printf; the digits are generated in the same order they are output.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.