Wednesday, March 23, 2016

8051 Counter

8051 timers can also be used as counters to count the external events. When the timer/counter is used as a timer, the 8051′s crystal is used as the source of the frequency. When it is used as a counter, however, it is a pulse outside the 8051 that increments the TH, TL registers.

The C/T bit in the TMOD register decides the source of the clock for the timer. If C/T = 0, the timer gets pulses from the crystal. In contrast, when C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051. Therefore, when C/T = 1, the counter counts up as pulses are fed from pins 14 (Timer 0 input) and 15 (Timer 1 input). In the case of Timer 0, when C/T = 1, pin P3.4 provides the clock pulse and the counter counts up for each clock pulse coming from that pin. Similarly, for Timer 1, when C/T = 1 each clock pulse coming in from pin P3.5 makes the counter count up.

MAIN :MOV TMOD, #01100000B ; counter 1, mode 2
MOV TH1, #0 ; clear TH1
SETB P3.5 ; make T1 input
START : SETB TR1 ; start counter
LOOP :MOV A, TL1 ; get copy of count TL1
MOV P2, A ; display it on port 2
JNB TF1, LOOP ; keep doing it if TF = 0
CLR TR1 ; stop the counter
CLR TF1; clear TF1

Related topics:
8051 Loop   |   8051 Delay   |   8051 IO Port Programming   |   8051 Timer Programming   |   8051 Serial Port Programming   |   8051 Programming Timer Interrupt   |   8051 Programming Serial Interrupt   |   8051 Programming External Interrupt

List of topics: 8051

No comments:

Post a Comment