Programming Counters in 8051 Microcontroller and displaying the counts in LCD

Counter programming and displaying counts in LCD display

Programming Counters in 8051 is used to count the external events through the T0 & T1 pins in the controller. Counters counts the external clock source whereas the timers counts the clock source from the oscillator used. The concept of counters play an important role where the embedded designer needs to count the number of events taking place by feeding clock per events.


  • When using the timer/counter as a timer the registers THx and TLx increments for every machine cycle that is it obtains clock source from crystal which is connected to XTAL1 and XTAL2 pins of the Microcontroller.
  • Whereas Counters require external clock source to be fed into the T0 (P3.4) and T1 (P3.5) pins of the 8051 Microcontroller.
  • Rest of the operations are same for both counters and timers.


Timers/Counters Register

To activate  the counter in 8051 you need to load logic 1 to the C/T bit of the TMOD register. This will tell the controller the timer/counter should function as a Counter. Loading logic ‘0’ in the TMOD register will activate the timer mode in 8051 microcontroller.

In the above design we have used only ‘T0’ pin to count the external event and the pulse is supplied by means of a simple push button. The count value is then fed into the LCD for display.


  • Initialize the TMOD register to make it timer/counter function as a register.
  • We are using only mode 1 of Timer0 so our TMOD hex value will be 0x05.
  • Load the Initial value from where your counter needs to be start counting TL0=0.
  • Start the Timer 0 to start counting by feeding the logic 1 to it TR0=1.
  • To display the count values in LCD copy the TL0 values to a Integer, in our program we used ” val “.
  • The LCD cannot display a integer , so we need to convert the integer to character.
  • For that purpose we use ” sprintf(ch,”%u”,val);
  • This will convert the Integer ” val ” values to character “ch”.
  • Feed the Character “ch” for displaying it in LCD.


sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;
void lcd(char a,int b);                       //subroutine for lcd display
unsigned char msg[]="Count";
char ch[4];
void delay();                                   //sub routine for delay
void counter();                              //subroutine for counters
int k;
unsigned int val;
void main()
   TMOD=0x05;                              //selecting counter mode 
void delay()
   int i;
   for(i=0;i<=2000;i++);               //software delay
void counter()
   TL0=0;                                 //lodaing initial value
   TR0=1;                               //starting timer
      lcd(0x86,0);                          //setting fixed position
      val=TL0|TH0<<8;                 //shifting the values from TL0 to TH0
      sprintf(ch,"%u",val);            //conversion of integer to character 
 void lcd(char a,int b)


  • The above code is capable of counting values from 0 to 65535 since combined 16 bit register (TH0 and TL0) can hold values from 0 to FFFF.
  • To use a single register alter the line 37 of the code to val=TL0 , but while using this the register is capable of counting up to 256 only. 
The following two tabs change content below.
Frank Donald

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who loves building stuff in his free time. Video games, blogging and programming are the things he loves most

Ask your query

17 Comments on "Programming Counters in 8051 Microcontroller and displaying the counts in LCD"

newest oldest most voted
Notify of

Hello sir how to write a program for read two key then display the key value and display the quotient and division of the key value with l second delay


hello sir ,actualy i want to measure the pulses per how should i use this code? which changes i have to made?

kedar pednekar

hi sir , your code helped me alot , sir i want your help ..i want to count pulses per second using counter 0 , my code is not working . i actually want to display RPM over lcd and speed sensor is connected to T0 pin .my logic was counting pulses for 1 sec .please help .


Sir I want to display a string named water usage that’s all.

I am using flowmeter to count water.
Don’t worry about that sir. I know that well

Arman Dalvi

sir i want to increase string size so, what i need to do?
instead of count i want water usage


thank you for the clear explanation sir but can you suggest the code and circuit modification for counting peoples using this…plz help me

Frank Donald

No need to modify the code however you need to place the sensor where the trigger to the interrupt is fed by a switch. If you are doing project to count peoples then you can use this People/object counter circuit .


actually i was asked to do with microconctroller…can u tell which sensor i should use to turn it into a people counter..

Frank Donald

You can use the same Transmitter circuit which i provided you the link above and as far as receiver is concerned you just need single part TSOP 1738. Connect the 3rd pin/ output pin of the TSOP to P3.4/T0 of the Microcontroller, that’s the sensor part the code remains the same.

Hope it helps, let me know if you need further assistance.


thnx a lot now i clearly understand it…

Frank Donald

Always welcome, keep visiting.