Saturday, 20 April 2013

4.8051 Microcontroller III

In the previous topic we have discussed about Addressing Modes & Instruction set.Let us discuss about Timers and Counters.

        In general 8051 is having two timers/counters .Timers are used to generate a delay and counter are used to count the events.Timers are named as timer 0 & timer 1 and both are 16 bit timers.
Both timers can be accessed by different registers as low byte reg & high byte reg,

Low Byte Reg : TL0/TL1,
High byte Reg:  TH0/TH1

Example:
It can be accessed like any other registers
MOV TL0,#05eH
MOV R1,TH0



TMOD is the register which is used to set & select the timer modes.

 TMOD is a 8-bit register
 Lower 4 bits : Timer 0.
 Upper 4 bits: Timer 1.

As you can see in the above chart, four bits (two for each timer) are used to specify a mode of operation. The modes of operation are:

M1M0Timer ModeDescription of Mode
00013-bit Timer.
01116-bit Timer
1028-bit auto-reload
113Split timer mode



13-bit Timer Mode (mode 0):
Timer mode "0" is a 13-bit timer. Generally the 13-bit timer mode is not being used in new.

16-bit Timer Mode (mode 1):
Timer mode "1" is a 16-bit timer. This is a very commonly used mode.

TL0 and TL1 are incremented from 00h to FFh. When it reaches FFh,it resets to 0 and also TH0 or TH1 getting increment by 1.


To generate a time delay
1. Load the TMOD value register indicating
which timer (timer 0 or timer 1) is to be
used and which timer mode (0 or 1) is
selected
2. Load registers TL and TH with initial count
value
3. Start the timer
4. Keep monitoring the timer flag (TF) with
the JNB TFx,target instruction to see
if it is raised
 Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL
again

Example 1:

In the following program, we create a square wave of 50% duty cycle (with
equal portions high and low) on the P1.6 bit. Timer 0 is used to generate the
time delay. Analyze the program
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#02CH ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.6 ;toggle P1.5
ACALL DELAY
SJMP HERE
In the above program notice the following step.
1. TMOD is loaded.
2. FFF2H is loaded into TH0-TL0.
3. P1.5 is toggled for the high and low portions of the pulse.


In Example 2: 
calculate the amount of time delay in the DELAY
subroutine generated by the timer. Assume XTAL = 11.0592 MHz.
Solution:
The timer works with a clock frequency of 1/12 of the XTAL
frequency; therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the
timer frequency. As a result, each clock has a period of T =
1/921.6kHz = 1.085us. In other words, Timer 0 counts up each 1.085
us resulting in delay = number of counts × 1.085us.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13
decimal). However, we add one to 13 because of the extra clock
needed when it rolls over from FFFF to 0 and raise the TF flag. This
gives 14 × 1.085us = 15.19us for half the pulse. For the entire period it
is T = 2 × 15.19us = 30.38us as the time delay generated by the timer.




To calculate the values to be loaded
into the TL and TH registers, look at
the following example
 Assume XTAL = 11.0592 MHz, we can
use the following steps for finding the TH,
TL registers’ values
1. Divide the desired time delay by 1.085 us
2. Perform 65536 – n, where n is the decimal
value we got in Step1
3. Convert the result of Step2 to hex, where
yyxx is the initial hex value to be loaded into
the timer’s register
4. Set TL = xx and TH = yy

Example 3:

Assume that XTAL = 11.0592 MHz. What value do we need to load
the timer’s register if we want to have a time delay of 5 ms
(milliseconds)? Show the program for timer 0 to create a pulse width
of 5 ms on P2.3.
Solution:
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
This means that out of many 1.085 us intervals we must make a 5 ms
pulse. To get that, we divide one by the other. We need 5 ms / 1.085
us = 4608 clocks. To Achieve that we need to load into TL and TH
the value 65536 – 4608 = EE00H. Therefore, we have TH = EE and
TL = 00.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#0 ;TL0=0, the low byte
MOV TH0,#0EEH ;TH0=EE, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag.





8-bit Timer Mode (mode 2):
Timer mode "2" is an 8-bit auto-reload mode. When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx.

It is an 8-bit timer; therefore, it allows
only values of 00 to FFH to be loaded
into the timer’s register TH
2. After TH is loaded with the 8-bit value,the 8051 gives a copy of it to TL.Then the timer must be started. This is done by the instruction SETB TR0 for timer 0 and SETB TR1 for timer 1
3. After the timer is started, it starts to
count up by incrementing the TL register. It counts up until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets high
the TF (timer flag).


When the TL register rolls from FFH to 0
and TF is set to 1, TL is reloaded
automatically with the original value kept
by the TH register.
1) To repeat the process, we must simply clear
TF and let it go without any need by the
programmer to reload the original value
2)This makes mode 2 an auto-reload, in
contrast with mode 1 in which the
programmer has to reload TH and TL.


To generate a time delay
1. Load the TMOD value register indicating
which timer (timer 0 or timer 1) is to be
used, and the timer mode (mode 2) is
selected
2. Load the TH registers with the initial
count value
3. Start timer
4. Keep monitoring the timer flag (TF) with
the JNB TFx,target instruction to see
whether it is raised
Get out of the loop when TF goes high
5. Clear the TF flag
6. Go back to Step4, since mode 2 is autoreload


Example:

Assume XTAL = 11.0592 MHz, find the frequency of the square
wave generated on pin P1.0 in the following program
MOV TMOD,#20H ;T1/8-bit/auto reload
MOV TH1,#5 ;TH1 = 5
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to hi, lo
CLR TF1 ;clear Timer 1 flag
SJMP BACK ;mode 2 is auto-reload
Solution:
First notice the target address of SJMP. In mode 2 we do not need to
reload TH since it is auto-reload. Now (256 - 05) × 1.085 us =
251 × 1.085 us = 272.33 us is the high portion of the pulse. Since
it is a 50% duty cycle square wave, the period T is twice that; as
a result T = 2 × 272.33 us = 544.67 us and the frequency =
1.83597 kHz.




Split Timer Mode (mode 3):
Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0.
While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.
The only real use I can see of using split timer mode is if you need to have two separate timers and, additionally, a baud rate generator. In such case you can use the real Timer 1 as a baud rate generator and use TH0/TL0 as two separate timers.

Counters:

Timers can also be used as counters
counting events happening outside the
8051
1) When it is used as a counter, it is a pulse
outside of the 8051 that increments the
TH, TL registers
2) TMOD and TH, TL registers are the same
as for the timer discussed previously
3) Programming the timer in the last
section also applies to programming it
as a counter
4) Except the source of the frequency.

The C/T bit in the TMOD registers
decides the source of the clock for the
timer
1) When C/T = 1, the timer is used as a
counter and gets its pulses from outside
the 8051
2) The counter counts up as pulses are fed from
pins 14 and 15, these pins are called T0 (timer
0 input) and T1 (timer 1 input).

Example:

Assuming 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 TH1
SETB P3.5 ;make T1 input
AGAIN: SETB TR1 ;start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1,Back ;keep doing, if TF = 0
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it










---->Class End,Please your comments below<-----------








No comments:

Post a Comment