In: Electrical Engineering
Referring 8051 User’s Manual ,study how to use timer when operating in mode 2.
Understand the manual (p. 112~)
Mode 2 Programming
Mode 2 programming mode is an 8 bit auto reload mode
1. Mode 2 is an 8 bit timer. This means that the timers register TH can be loaded with values from 00 to FFH.
2. As soon as TH is loaded, a copy of the same is is given to TL.
3. Now the timer has to be started. This is done by SETB TR0 or SETB TR1 based on which timer is selected.
4. Now that the timer is started, it starts to count up by incrementing the TL register. It counts till it reaches FFH. When it reaches FFH, it will roll over to 00H, and sets the timer flag high(TF).
5. When TL register rolls from FFH too 00 and the timer flag is set, TL is automatically reloaded with the original value kept in TH register. To repeat the process, we must simply clear the TF. We need not reload the original value. This is why it is called an auto reload mode.
Steps to Program in Mode 2
Suppose, we want to generate a time delay using timer's mode 2, we have to do the following steps.
Step1: Load the TMOD value register indicating which timer is to be used, either Timer 0 or Timer 1, and select TIMER mode as 2.
Step2: Load TH register with the initial count value
Step 3: Start the timer by using SETB TR0 or SETB TR1 depending on which timer you have selected.
Step 4: Keep monitoring the timer flag(TF) using the instruction JNB TF0, ADDRESS or JNB TF1, ADDRESS depending on the timer selected. This is to see whether the count has rolled over from FFH to 00.
Step 5: Clear the TF flag. This is done by the instruction CLR TF0 or CLR TF1 depending on the timer that you have selected.
Step 6. Go back to step 4.
Example:
This program just creates square wave at port P3.3
MOV TMOD, #20H
; Timer 1, Mode 2 (8 bit auto – reload)
MOV TH1, #00H ; Initial Value loaded
AGAIN: CPL P3.3 ; Complement the port pin 3.3
SETB TR1 ; Start Timer 1
HERE: JNB TF1, HERE ; Wait for the roll over from FFH to 00
CLR TF1 ; Clear Timer Flag
SJMP AGAIN ; Repeat
Now the code is burned to the 8051 in Proteus and the waveform is
observed at P3.3
Mode 2 is usually used in Serial Communication