Understanding of Timers and Counters

Discussion in 'Embedded Systems and Microcontrollers' started by Allen_Lusy, Apr 2, 2017.

  1. Allen_Lusy

    Thread Starter New Member

    Dec 10, 2014
    I know this is repeated question,I referred many post, but i didn't get an clear answer. My understanding is most of the micro-controller will have a 16/32 bit timer, which increments its ticks based on oscillator and clock provided(main/peripheral clock, divider, prescaler) to it.

    When i started to work on simple 8051 micro-controller, I thought timer and counter are same. The reason of my thought after the simple below test i did in 8051.

    "8051 has a bit C/T in TMOD Register. If timer is started after a reset i see TH/TL register are incremented irrespective of C/T bit (If bit set to 0 or 1 result is same)."

    Later i worked on PIC microcontroller, I noticed that the timer and counter has different concept in this controller. If it is configured as Timer Mode, the register TMRnH/TMRnL(where n is 1 or 0) was incremented for every instruction cycle. But if it is configured as Counter, the register was not incremented for every instruction, Then i connected one switch in pin TnCkn. When i pressed that switch, the counter was incremented. With this i changed my perspective that the timer and counter are not same. Timer will increment for every instruction cycle(internal clock/signal) but counter will increment for every time either a raising edge or falling edge with external clock/signal.

    Now i am working on Freescale MPC controller, Here i noticed the term GPT(General purpose timer). There are 5 timer units(GPT) in this controller, each timer unit has a register called "Counter Register(TCNTn)". When i started a timer, this register has incremented for every instruction cycle. So i thought this controller has only Timer not a counter. But i perturb with two terms below

    1. Counter Register - If it is timer, why it is called Counter register in user manual of MPC.
    2. In User manual, there is a section called Timer/Counter under this section, there is a subtopic called GPT and there is no subtopic for counters separately. which means timer and counter are same?

    My question:

    1. Is really Timer and counter are same?
    2. If it is not same, why 8051 and MPC are working as same(act as timer and counter).
  2. MrChips


    Oct 2, 2009
    Timer and counter are the same.
    Both are registers that increment when an input pulse is applied.
    What is different is the application. You can count number of events or you can measure a time interval.
    absf likes this.
  3. Kjeldgaard


    Apr 7, 2016
    I'll call it all for Counters.

    And if we take the example of a frequency counter, so we can use two counters:
    • A Counter that has clock input external from what you want to measure the frequency from.
    • Another Counter with clock coming from an accurate X-Tal oscillator. This counter is set up to generate an interrupt every second, and show the result on a display.
    absf likes this.
  4. Allen_Lusy

    Thread Starter New Member

    Dec 10, 2014
    You mean it has same register?.Application has only one one register to read, it depends on how and where they are using it. But why it is not same in case of PIC microcontroller? if i configured as counter mode, register incremented only if external pulse is applied on pin TnCKn
  5. MrChips


    Oct 2, 2009
    No, not same register.
    Same word. You can change the word timer to counter. It just depends on the context and how you want to use the register.

    Some people call it register. Other people call it accumulator. Same difference.
  6. Stuntman

    Senior Member

    Mar 28, 2011
    Quite simply they are doing the same things, just using the information differently. I give you an everyday life analogy:

    1 You have a turnstyle at a ballpark. Every time someone walks through and rotates the crossbar, it generates a square pulse. Could you not hook this to a microcontroller and use a counter to count things like "How many people total people have entered since the counter was turned on?"

    2. Now, what if I motorized the turnstyle so that it rotated 100 times per second. Could I now take this input and use a counter to do something like :"Every 10 pulses, toggle an LED. At this point, the counter would allow to generate a time sensitive event, such as, flashing an LED at 10 hertz (100 pulses per second/event every 10 pulses). You are now using the counter as a timer.

    3. Moving on, if you used two turnstyles, one with people walking through, and another being spun at 100 revolutions per second, could you not combine the two scenarios mentioned to answer a question like "How many people (pulses) entered per 1/10 of a second?" Could you not change the timer in scenario 2 to count to 100, then answer the question "How many people (pulses) entered every second?

    So the counter counts voltage events (scenario 1), but becomes a timer if you have a time driven signal (scenario 2) driving it. Does this make sense?