The 8051 Microcontroller and Embedded

(lily) #1
Department of Computer Scien

ce and Information Engineering

National Cheng Kung University, TAIWAN
HANEL
COUNTER PROGRAMMINGC/T Bit in TMOD Register(cont’)

Example 9-18Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode 2 to count the pulses and display the state of the TL1 count on P2, which connects to 8 LEDs.Solution:


MOV TM0D,#01100000B ;counter 1, mode 2,

;C/T=1 external pulses

MOV TH1,#0 ;clear TH1SETB P3.5 ;make T1 input
AGAIN: SETB TR1 ;start the counterBACK: MOV A,TL1 ;get copy of TL

MOV P2,A ;display it on port 2JNB TF1,Back ;keep doing, if TF = 0CLR TR1 ;stop the counter 1 CLR TF1 ;make TF=0SJMP AGAIN ;keep doing it
Notice in the above program the role of the instruction

SETB P3.5

.

Since ports are set up for output when the 8051 is powered up, we make P3.5 an input port by making it high. In other words, we must configure (set high) the T1 pin (pin P3.5) to allow pulses to be fed into it.
Free download pdf