Quantcast
Viewing all articles
Browse latest Browse all 215

Programming Counters in 8051 Microcontroller and displaying the counts in LCD

Image may be NSFW.
Clik here to view.
count-value-display-counters-8051-micorocontroller-lcd-display
Counter programming and displaying counts in LCD display
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:

Image may be NSFW.
Clik here to view.
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:


  1. #include<regx51.h>
  2. #include<stdio.h>
  3. sbit rs=P3^5;
  4. sbit rw=P3^6;
  5. sbit en=P3^7;
  6. void lcd(char a,int b);                       //subroutine for lcd display
  7. unsigned char msg[]="Count";
  8. char ch[4];
  9. void delay();                                   //sub routine for delay
  10. void counter();                              //subroutine for counters
  11. int k;
  12. unsigned int val;
  13. void main()
  14.  {
  15.    lcd(0x38,0);
  16.    lcd(0x0c,0);
  17.    lcd(0x80,0);
  18.    TMOD=0x05;                              //selecting counter mode 
  19.    counter();                                
  20.  }
  21. void delay()
  22.  {
  23.    int i;
  24.    for(i=0;i<=2000;i++);               //software delay
  25.  } 
  26. void counter()
  27.  {
  28.    TL0=0;                                 //lodaing initial value
  29.    TR0=1;                               //starting timer
  30.    for(k=0;k<5;k++)
  31.      {
  32.        lcd(msg[k],1);
  33.      }
  34.    while(1)
  35.     {
  36.       lcd(0x86,0);                          //setting fixed position
  37.       val=TL0|TH0<<8;                 //shifting the values from TL0 to TH0
  38.       sprintf(ch,"%u",val);            //conversion of integer to character 
  39.       for(k=0;k<5;k++)
  40.         {
  41.           lcd(ch[k],1);
  42.         }
  43.     }
  44.   }
  45.  void lcd(char a,int b)
  46.    {
  47.      P1=a;
  48.      rs=b;
  49.      rw=0;
  50.      en=1;
  51.      delay();
  52.      en=0;
  53.      delay();
  54.    }

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. 
Like this tutorial on Counters in 8051, Share this with others through social sites and don't forget to follow us there. Thank you for visiting and Enjoy Coding.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles