Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Beginners guide to the PIC Microcontroller

Part 3

Internal PIC timers 

One of the most important tasks a microcontroller can do is...

...measure time.

The microcontroller can easily do this as it operates using a fixed frequency clock oscillator.

The clock oscillator sets the speed of the microcontroller operation - this can either be an external crystal (up to 20MHz) or for modern PIC devices it can be an internal RC oscillator (from 4MHz to 8MHz).

Note: Using the internal oscillator saves having to wire up external hardware but the clock is less accurate.

Since each instruction executes in a set period of time (for the PIC micro this is 4 cycles of the clock) you can time events by working out how many instructions have executed.

Note: for jump instructions an extra cycle is needed.

Assembler code loop timing

By creating a routine containing an assembler code loop and knowing the clock speed you can create exact delay times since you know the time taken for each instruction to execute (from the datasheet).  

If you repeatedly executed this routine and kept a count of the number of times it's called then you can measure time in multiples of the execution time of the routine.

There's two problems to this method.

1.The microcontroller is not doing
useful work while it is executing the delay routine.

2.Measuring the delay time of the code
is not always easy (especially for complex loops).

Internal Timer Peripherals

The advantage of the timer peripherals is that they make it a trivial task to measure time.  They are hardware modules that operate separately from the CPU part of the microcontroller.

This also means that they are capable of very high speed operation (you can make a 50MHz frequency counter using a timer (even for the obsolete 16F84 device).

In the modern microcontrollers there are three timers; Timer0 Timer1 and Timer2 and each has different operations and capabilities.

You can use the timers to count periods of the internal clock or you can count periods of an external clock (for a signal connected to the correct pin of the microcontroller) i.e. a timer input- for Timer 0 this would be T0CKI  -Timer0 ClocK Input)

Note: Some timers can only count the internal clock - check the datasheet.

N.B. When counting the internal clock it is divided by 4 first Fosc/4.

Using a timer

There are two ways to use a timer polled or interrupt.  

Polling

You can continuously read the timer values until you detect a value you want to measure e.g. to count to 1000 compare the timer registers to 1000 then do something.

Although it's simple the disadvantage is that the microcontroller can do nothing else while polling (or can do very little as it has to keep going back and looking at the timer registers).

Interrupt

When a timer overflows (passes from its maximum value to zero) it generates an interrupt signal which you can use to interrupt the CPU to tell it something has happened.

This means the CPU can go and do other processing tasks while the timer hardware is running in the background.

Although more complex to set up and use an interrupt driven timer is the better way to use a timer if you need to do other processing tasks.  

When you add in other peripheralsinterrupts are essential e.g. measuring time while servicing a serial interface.

Timer 0,1,2 summary

Timer 0

Timer 0 is an 8 bit timer with an 8 bit prescaler - in total it looks like a 16 bit timer and it was the original peripheral (actually the only peripheral in the 16F84!).  Its slightly tricky to use when starting out as you can not read the prescaler value (there are ways around this).  

So lets just look at the next timer:

Timer 1

Timer1 is a 16bit timer with a prescaler that can be set to 1,2,4 or 8 and it can take an input clock from a pin or use the internal oscillator (Fosc/4).

A prescaler is just a divider so if you had a clock frequency of 8MHz and the prescaler as set to 8 the output from the prescaler would be 1MHz.

Timer 2

Timer 2 is an 8 bit timer that can only count periods of the internal clock Fosc/4.  It has a 2 bit prescaler and a 4 bit postscaler - these make the range out output periods (or frequency) flexible.

In addition it has a period counting register that resets the counter when the counter reaches a programmed count (programmed by you).  This means Timer 2 can generate a wide range of output frequencies easily and this is used to generate the baud clock for the internal USART.

Timers use and limitation

A timer measures out set periods of time or counts periods but they are limited by the maximum count that they can store.

Maximum count

The maximum count is important as it sets a limit on the usefulness of the timer (without extra programming).

So for Timer1 prescaler set to 1 the maximum count is 65535.  So you could count up to a maximum of 65535 events.

To overcome this limit you can use a variable to store the number of times that the Timer overflows - the variable would then store the input event count in multiples of the Timer's maximum value.  

This makes it practical to measure any number of events.

Next time...

... Overview of other Internal Peripherals.

New! Comments

Have your say about what you just read! Leave me a comment in the box below.



Privacy Policy | Contact | About Me

Site Map | Terms of Use



Visit our Facebook Page:

   Click Here



Recent Articles

  1. [Arduino Tutorial] : How to use the 74HC595 shift register with shiftOut().

    The Essential Guide to the 74HC595; What it is and how you can easily use one in any of your projects.

    Read more

  2. Pic Programmer Types

    Which pic programmer do you need? This page discusses PIC programmers and gives some essential information on choosing or building your own programmer.

    Read more

  3. Arduino Pulsein: an easy way to measure pulse periods in microseconds.

    learn how to use Arduino pulseIn and pulseInLong to get the most accurate pulse measurement on an Arduino.

    Read more

  4. Arduino millis

    How to use Arduino millis() for deylays but still make the processor do work - Stop using delay()

    Read more

  5. Multiple MCP23017 Interrupt tutorial for operating multiple interrupts from multiple MCP23017s.

    This MCP23017 Interrupt tutorial shows you how to connect interrupt outputs from several MCP23017s to a single microcontroller interrupt pin.

    Read more

  6. Arduino delay() How to use it (and why you should not use it!)

    Arduino Delay() is a useful function but not for large programs - Find Out Why Here...

    Read more

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Back to Top