Programming Counters in 8051 Microcontroller and displaying the counts in LCD

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. 

25 Comments

  1. Bhadra Sajikumar

    what is the code in assembly, I was told to use that and not c

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Bhadra,
      Unfortunately we don’t have assembly code

      Reply
  2. Sumit Gupta

    hello sir,
    I gone through this counter code and i was looking for the switch condition means the switch which controls the counter connected on P3^4 so which line in the code defines the switch condition? how it increases the counter after each press? which line in code support the following condition?

    Reply
    1. Frank DonaldFrank Donald (Post author)

      counter() subroutine in the above code takes care of reading the switch and counting how many times it was pressed. In this code timers of 8051 have been used as counter so with every switch press value in the timer register will increase by 1 and it will be displayed in the LCD.

      Reply
  3. SamRat005

    Sir I want to stop the microcontroller from my desired count number.or when microcontroller rech my desire number that time led is blinking.
    SO ,is that possible and what c code for this programma?

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Samrat,
      You may need to modify the code with your desired count say 50 and compare it against the variable “val” using if statement and write a routine to make the LED blink when this condition is met.Hope this helps, let me know if you need any further help.

      Reply
  4. Prajwal

    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

    Reply
  5. p.h.g.

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

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi,

      Refer to this timer tutorial https://www.gadgetronicx.com/timers-in-8051-microcontroller-tutorial/ . You need to program one timer to count time and another to count pulses. Write your code in such a way the counter counts the pulse for 5 seconds and then multiply it by 12 and you will get your pulse per minute.

      Reply
  6. 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 .

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi Kedar,
      I believe the code should work. When you mean speed sensor what exactly is that, can you give the sensor specs. In my code counters will count the incoming pulses and display it in the lcd when coming to working with RPM you need to do some calibration before printing the values. Specify the speed sensor spec i might be able to help you out.

      Reply
  7. arman

    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

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Arman,

      Replace the word “count” with “water” in the line 7 of the code.

      Reply
      1. arman

        i am facing problem to display character size more than 5 alphabets.
        i need to display character size of 11 alphabets.
        plz tell me respective changes in coding

        Reply
        1. Frank DonaldFrank Donald (Post author)

          Arman,

          If you want to display your 11 characters in the char msg, you don’t need to change anything just replace “count” with your 11 characters since the array size in not defined there. if you want to display using char ch change the size of array from [4] to [10]

          Reply
  8. Arman Dalvi

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

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Arman,

      Water usage? how are you planning to measure that?

      Reply
  9. Sheeba

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

    Reply
    1. 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 .

      Reply
      1. Shrishail

        I need your support

        Reply
        1. Frank DonaldFrank Donald (Post author)

          Shrishail,
          What help do you need ? Kindly post your query.

          Reply
    2. Sheeba

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

      Reply
    3. 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.

      Reply
    4. Sheeba

      thnx a lot now i clearly understand it…

      Reply
    5. Frank Donald

      Always welcome, keep visiting.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *