Sunday, 31 May 2020

Introduction to Timers/Counters

Hi everyone, 

One of my student came across some problem in understanding the basic functionality of timer/counter, so I planned to explain this topic in the easiest words.

ok let's start..

friends you just need to understand few basic definition: 

What are Timer / Counter ?

so basically a Timer is a basic peripheral of many micro controller. These are basically a set of SFR (special function register) that can be used for the following functionality: 
  • keeping time and/or calculating the amount of time between events,
  • counting the events (events are nothing but pulse, explained below)
  • generating delay
If you are generating the delay then that functionality or application is called Timer but if you are counting the events then that functionality is called Counter.

Here i will be explaining, Timer/Counter in context of 8051 micro controller.
 
8051 has two timers: Timer0 (T0) and Timer1 (T1), both are 16-bit wide. Since 8051 has 8-bit architecture, each of these is accessed by two separate 8-bit registers as shown in the figure below. These registers are used to load timer count.


8051 Timer Registers


so the above given registers will be used either as a Timer or a Counter.

Now, before using them as Timer/Counter we need to know something more:

For Timer use : if the above given SFR is used as Timer then it is going to get clock signal from the crystal oscillator i.e. 11.0592 MHz as it is the main clock source for Timer. The internal circuitry in the 8051 micro controllers provides the clock source to the timers which is 1/12th of the frequency of crystal attached to the micro controller, also called as Machine cycle frequency.

8051 Timer Clock

For example, suppose we have crystal frequency of 11.0592 MHz then micro controller will provide 1/12th i.e.

Timer clock frequency = ( Xtal Osc.frequency )/12 
                                                 = ( 11.0592 MHz )/12 = 921.6 KHz

where, Time period (T) = 1/(921.6 KHz)=1.085 μS

here we can see, the Timer counts the internal clock pulse that is given to micro controller from the crystal oscillator.

For Counter use : Now if i am using the same timer peripheral as counter then Timer 0 will be receiving the clock signal from the T0 pin and Timer 1 will be receiving the clock signal from the T1 pin. Here we came to know that unlike timer, counter is not counting internal clock signal but counts the external clock pulses at their respective pins.

8051 has Timer Mode Register and Timer Control Register for selecting a mode of operation and controlling purpose.

Let's see these registers,

TMOD register

TMOD is an 8-bit register used to set timer mode of timer0 and timer1.

8051 TMOD Register

    Its lower 4 bits are used for Timer0 and upper 4 bits are used for Timer1
      Bit 7,3 – GATE:          
                 1 = Enable Timer/Counter only when the INT0/INT1 pin is high and 
                        TR0/TR1 is set i.e. hardware control.
                 0 = Enable Timer/Counter when TR0/TR1 is set i.e software control.

      Bit 6,2 - C/( Counter/Timer ): Timer or Counter select bit

                 1 = Use as Counter
                 0 = Use as Timer

      Bit 5:4 & 1:0 - M1:M0: Timer/Counter mode select bit
      These are Timer/Counter mode select bit as per below table :

       M1M0 Mode  Operation
       00 (13-bit timer mode)     13-bit timer/counter, 8-bit of THx & 5-bit of TLx  
       01 (16-bit timer mode)  16-bit timer/counter, THx cascaded with TLx
       12 (8-bit auto reload mode)  8-bit timer/counter (auto reload mode), TLx reload with value held by THx each time TLx overflow
       1 3 (split timer mode)Split 16-bit timerx into two 8-bit timer i.e. THx and TLx like two 8-bit timer

      TCON Register

      8051 TCON Register


      TCON is 8-bit control register and contains timer and interrupt flags.

      Bit 7 - TF1: Timer1 Overflow Flag

              1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to
                     zero).
              0 = Timer1 overflow not occurred.

      It is cleared through software. In Timer1 overflow interrupt service routine, this bit will get cleared automatically while exiting from ISR.

      Bit 6 - TR1: Timer1 Run Control Bit

               1 = Timer1 start.
               0 = Timer1 stop.

      It is set and cleared by software.

      Bit 5 – TF0: Timer0 Overflow Flag

              1 = Timer0 overflow occurred (i.e. Timer0 goes to its max and roll over back to 
                     zero).
              0 = Timer0 overflow not occurred.

      It is cleared through software. In Timer0 overflow interrupt service routine, this bit will get cleared automatically while exiting from ISR.
      Bit 4 – TR0: Timer0 Run Control Bit

              1 = Timer0 start.
              0 = Timer0 stop.

      It is set and cleared by software.
      Bit 3 - IE1: External Interrupt1 Edge Flag

              = External interrupt1 occurred.
              0 = External interrupt1 Processed.

      It is set and cleared by hardware.
      Bit 2 - IT1: External Interrupt1 Trigger Type Select Bit

             1 = Interrupt occur on falling edge at INT1 pin.
             0 = Interrupt occur on low level at INT1 pin.

      Bit 1 – IE0: External Interrupt0 Edge Flag

             = External interrupt0 occurred.
             0 = External interrupt0 Processed.

      It is set and cleared by hardware.
      Bit 0 – IT0: External Interrupt0 Trigger Type Select Bit

             1 = Interrupt occur on falling edge at INT0 pin.
             0 = Interrupt occur on low level at INT0 pin.




      Application of volatile in C A variable should be declared volatile when its value could be changed unexpectedly. In practice, you must decl...