Programming Counters in 8051 Microcontroller and displaying the counts in LCD

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
programming-counters-8051-micorocontroller-lcd-display
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.

DIFFERENCE BETWEEN TIMERS AND COUNTERS:

  • 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.

TMOD REGISTER:

timers-counters-register-TMOD-8051-microcontroller
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.

STEPS TO PROGRAM COUNTERS AND PRINT COUNTS IN LCD:

  • 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.

CODE:

#include<regx51.h>
#include<stdio.h>
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()
 {
   lcd(0x38,0);
   lcd(0x0c,0);
   lcd(0x80,0);
   TMOD=0x05;                              //selecting counter mode 
   counter();                                
 }
void delay()
 {
   int i;
   for(i=0;i<=2000;i++);               //software delay
 } 
void counter()
 {
   TL0=0;                                 //lodaing initial value
   TR0=1;                               //starting timer
   for(k=0;k<5;k++)
     {
       lcd(msg[k],1);
     }
   while(1)
    {
      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 
      for(k=0;k<5;k++)
        {
          lcd(ch[k],1);
        }
    }
  }
 void lcd(char a,int b)
   {
     P1=a;
     rs=b;
     rw=0;
     en=1;
     delay();
     en=0;
     delay();
   }


NOTE:

  • 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. 

Related Designs and Schematics

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

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

Notify of
avatar
Sort by:   newest | oldest | most voted
arman
Guest

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
Guest

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

Sheeba
Guest

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
Guest

Always welcome, keep visiting.

Sheeba
Guest

thnx a lot now i clearly understand it…

Frank Donald
Guest

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.

Sheeba
Guest

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
Guest

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 .

wpDiscuz