Image may be NSFW. Clik here to view. ![]() |
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 |
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:
|
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.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.![]()
Image may be NSFW.Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.