PIC Microcontroller Timer Calculation


Do you get stuck doing PIC timer calculation of juggling between datasheet, calculator, and your design requirements?

If so then you need some tools to automate the process.


Usually you will need to focus on the end result is usually the timer generates a measure of frequency or time period. It is quite difficult when you have to find information in a datasheet, and then account for prescalers and postscalers etc.

PIC Timer 0: calculation example

Here is a typical example of the calculation to create a fine 18ms repeat rate using the PIC-timer 0

Selection of prescaler ratio of 1:128 provides following termination of the period (with the FOSC / 4 or 4MHz / 4 = 1MHz) and by the maximum gradient of the timer 0

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

Apparently this is more than you need, but you can cut by changing the point of excess (the fine).

To do this you need from the frequency of Timer 0 as:

1 / (1MHz/128) = 128us

This is the time for each point in the Timer 0, or

256 * 128us = 32.768ms

Therefore, by manipulating the point you can adjust the excess of total termination of the period. 18ms period is required to make some calculations:

18ms/128us = 140 625 (nearest whole number value is 141)

This is the number of counts that are needed after the break is generated. To use it Timer 0 to be loaded as follows:

TMR0 = 256-143, / / to 141 but lost two Timer 0 to load.

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

141 * 128us = 18ms

TCL script for PIC timer calculation

Each time you use any built in PIC timer you have to do these kinds of calculations and the hardware timer in each is different and you will also have to struggle with interrupted.

To make the process easy, you can use three on-line (free) interactive module scripts written in TCL (Tool Command Language). They work in a similar way as Java applets.

Each of these scripts is directed to the most typical use of each timer and allows you to change the prescaler, postscaler or register value using the slider control. This makes it easy to experiment with different values, the result is displayed in the Web site (frequency and period are shown in each part-timer, for example, after the prescaler, after the register, after postscaler).

Above calculation is now just a trivial move the sliders around until you get close to the desired period PIC timer and set a timer to the value of houses in the correct answer.

So, you can do “what if” type of operation (all without a calculator in sight), eg “I need a 15ms repeat rate.

Timer 2 of the closest I could get was 15,136 (was 1 minute to test) – I might try a timer – Ah, that gives an accurate 15ms (a 30-second test). Just check with timer 0 – this gives 15.040ms (the ~ 30 seconds for testing).

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

PIC Timer 0

This is an 8-bit prescaler and 8-bit timer and can be driven from an external clock.

Prescaler: 8 bits

Timer Register: 8-bit

Link to PIC Timer 0 calculator

PIC-Timer 1

It is a 4 bit prescaler and 16-bit timer and can be driven from an external clock. It can also be driven from low speed, for example, a 32kHz crystal.

Prescaler: 4 bits
Timer Register: 16 bit

Link to PIC Timer 1 calculator.

Timer 2 PIC

It is a 4 bit prescaler and 8-bit timer and 8-bit register and the time is only expelled from the internal clock (FOSC / 4)

Note: the period register lets you set the frequency output without further processing, or do not have to update the values in the register of the timer interruption routine as you do with timer 0 or 1

About The Author

Ibrar Ayyub

Ibrar Ayyub is an experienced technical writer with a Master's degree in computer science from BZU Multan University. He has written for various industries, mainly home automation, and engineering. He has a clear and simple writing style and is skilled in using infographics and diagrams. He is a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top