PIC Timer calculation

Do you get stuck doing PIC timer calculations,juggling between datasheet, calculator and your design requirements? If so then you need some tools to automate the process.

Calculations

Typically you will need to focus on the end result which is usually to make the timer generate a frequency or measure a time period. This is fairly difficult when you first have to find the information in the datasheet and then account for prescalers and postscalers etc.

The main point about timers is that an interrupt is generated when the timer count overflows i.e. for an 8 bit timer that means when the count value goes from 256 to zero.

PIC Timer 0 : Calculation example

Here is an example of the typical calculations for creating an 18ms interrupt repeat rate using PIC Timer 0.

Selecting a prescaler ratio of 1:128 gives the following interrupt period (with Fosc/4 or 4MHz/4 = 1MHz) and using the maximum overflow from Timer 0.

1/(1MHz/128/256) = 32.768ms

Obviously this is longer than you need but you can cut it down by changing the overflow point (in the interrupt). To do this you need the period of the frequency input to Timer 0 which is:

1/(1MHz/128) = 128us

This is the period of time for each count in Timer 0 i.e.
256 * 128us = 32.768ms

So by manipulating the overflow point you can set the overall interrupt period. The required period is 18ms so using some calculations:

18ms/128us = 140.625 (nearest integer value is 141)

This is the number of counts required after which the interrupt is generated. To use it Timer 0 it is loaded in the following manner:

TMR0 = 256-141+2; // Need 141 but Timer 0 looses 2 at load.

From this point on every 128us is counted by Timer 0 and it will overflow after 141 counts (or 18ms)

141 * 128us = 18ms(approx), The precise period is 18.048ms.

Note: "When writing to TMR0, two instruction clock cycles are lost. Often you have a specific time period you want to count, say 100 decimal. In that case you might put 156 into TMR0 (256 - 100 = 156). However, since two instruction cycles are lost when you write to TMR0 (for internal logic synchronization), you should actually write 158 to the timer".[source mid-range reference manual : DS31011]

Note: If you look at the Timer 0 block diagram in the datasheet the internal clock synchronization uses Fosc/4, so do not add 2 when calculating the period if you are not using the internal clock as a clock source! The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.

TCL Scripts for PIC Timer calculation

Every time you use any built in PIC timer you have to do these type of calculations and the hardware in each timer is different and you'll also have to do battle with interrupts.

To make the process easy you can use three on-line (free) interactive script modules written in TCL (Tool Command Language). They operate in a similar way to a Java applets.

Each of these scripts is geared towards the most typical use of each timer and lets you change prescaler, postscaler or register value using slider controls. This makes it easy to experiment with different values as the result is immediately displayed in the web page (frequency and period are displayed from each part of the timer e.g. after the prescaler, after the register, after the postscaler).

The above calculation is now trivial just move the sliders around until you get close to your desired PIC timer period and then adjust the timer value to home in on the correct answer.

So you can do what-if type operations (all without a calculator in sight) e.g. 'I need a 15ms repeat rate'.

From Timer 2 the closest I could get is 15.136 (took 1 minutes to test) - perhaps I'll try timer 1 - Ah that gives an exact 15ms (took 30 seconds to test). Just check with Timer 0 - this gives 15.040ms (took ~30 seconds to test).

Of course you can also set the main clock frequency (internal or external crystal) as well.

PIC Timer 0

This has an 8 bit prescaler and an 8 bit timer and can be driven from an external clock.
Prescaler : 8 bit
Timer register : 8 bit

Link to Timer 0 calculator

Note: In 18F Series devices Timer 0 is enhanced to be 16 bit capable (although it is backwards compatible with the 8 bit version).

PIC Timer 1

This has a 4 bit prescaler and an 16 bit timer and can be driven from an external clock. It can also be driven from a slow speed crystal e.g. 32kHz.
Prescaler : 4 bit
Timer register: 16 bit

Link to Timer 1 calculator.

Note: In 18F Series devices Timer1 has enhanced 16 readability (High byte is double buffered to allow easy, exact, capturing of the timer register value.

PIC Timer 2

This has a 4 bit prescaler and an 8 bit timer and an 8 bit period register and is only driven from the internal clock (Fosc/4)


Prescaler : 2 bit (1:1, 1:4, 1:16)
Timer register: 8 bit
Period register : 8 bit
Postscaler : 4 bit (1:1 to 1:16 inclusive)

Note: In 18F Series devices Timer 2 has the same structure as in 16F versions.


Link to Timer 2 calculator.

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





Subscribe to the MicroZine Newsletter and collect your free microcontroller Ebooks, download project code and more...


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. How to use a DS18B20 on the Arduino for easy temperature measurement

    How to Easily Use the DS18B20 Maxim One-Wire thermometer with Arduino example code showing external or parasitic power modes.

    Read more

  3. Secrets of Arduino millis: How it works and how to use it.

    How to use Arduino millis() to make delays, one-shots and simple schedulers plus simple analysis of arduino millis() code; Find out how it works.

    Read more

  4. The TCS230 Color Sensing Chip: How it works and how to use it.

    How to use the TCS230 (/TCS3200) Color detector chip and easily add it to any of your projects.

    Read more

  5. 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

  6. 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

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