Friday 13 January 2017

Delay using Timers in 8051 UC (Description)

In Embedded systems Timers is one of the main concept. These timers are useful to develop a delay functions. Before going to Delay, First of all we known about some registers in 8051 these are used for timers / counters and have different modes of operation. These registers are

TMOD :- Used for Timer Mode of operation as you seen below. Upper nibble (4-bits) for Timer-1                        and lower nibble is for Timer-0.

Gate :- It is used to enable the corresponding Timer. 

Enable or disable the Timer-1 using Gate-1 
            Logic-1 - Timer-1 operates only if INT1 bits is set (P3.3) 
            Logic-0 - Timer-1 operates regardless of the logic state of the INT1. 

C/T :- Counter / Timer. Logic-1 activates Counter and Logic-0 will activate Times. 

M1 & M0 :- This bits are useful to select different modes of operation see the below table.




TCON :- It is useful to control the timer operation. Below image shows the TCON register.


TF1 :-  This bit is set to Logic-1 automatically on the Timer-1 overflow.
TR1 :- This bit is used to enable the timer 1. This pin must be high to enable the Timer-1.
TF0 :- Set to one automatically when Timer-0 overflows.
TR0 :- Place 1 in this bit to enable the Timer-0.

Before going to calculate Delay time we know machine cycle and clock frequency of 8051 mc.

1 Machine Cycle = 12 Clock cycles

In 8051 mc we use crystal oscillator if frequency 11.0592 MHz.

So from this we get
 Frequency = Clock  frequency / 1 Machine Cycle 
                                                         
                                                            = 11.059 MHz / 12
                 
                                                            = 921.6 KHz.
                                       
                                        Time period = 1/ Frequency
                                                           
                                                             = 1/921.6 KHz = 1.085us

Now Calculate the Delay Time period

For Example.  I want time delay = 1ms

Steps:- 
  1. Divide the 5ms with time period i.e   (1ms/1.085us) =  922
  2. Subtraction Value-x from 65536  i.e  (65536 - 922) = 64614  in decimal form
  3. Convert 64614 into Hexa decimal format which gives FC66.
  4. Load this value into TH-0/1 and TL-0/1 i.e TH-0xFC and TL-0x66.
We complete our process these calculations is very useful for coding.

In next post I will show you the virtual mode execution of Delay function using Timers registers in 8051 micro-controller. 

No comments: