PIC Microcontroller Timer Calculation

Summary of PIC Microcontroller Timer Calculation


This article addresses the difficulty of manual PIC timer calculations by introducing free TCL-based interactive scripts. These tools automate prescaler, postscaler, and register value adjustments to achieve precise frequencies or periods for PIC Timer 0, Timer 1, and Timer 2. Users can experiment with different clock sources and settings via sliders without needing a calculator, simplifying hardware design workflows.

Parts used in the PIC Timer Calculation Project:

  • PIC Timer 0
  • PIC Timer 1
  • PIC Timer 2
  • TCL (Tool Command Language) Scripts
  • Prescaler
  • Postscaler
  • Timer Register
  • Period Register

 

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.

Calculations

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

Quick Solutions to Questions related to PIC Timer Calculation:

  • How can I automate PIC timer calculations?
    You can use three online interactive module scripts written in TCL that function like Java applets.
  • What is the benefit of using these scripts over manual calculation?
    The scripts allow you to move sliders to adjust values and see results instantly without a calculator.
  • Which timers are supported by these calculation tools?
    The tools support PIC Timer 0, PIC Timer 1, and Timer 2.
  • Can I change the main clock frequency in the tool?
    Yes, you can set the main clock frequency to be either internal or an external crystal.
  • What is the bit size of the prescaler for PIC Timer 0?
    PIC Timer 0 has an 8-bit prescaler.
  • How does Timer 2 differ from Timer 0 and 1 regarding the period register?
    Timer 2 has a period register that lets you set frequency output without updating values in the timer interruption routine.
  • What is the prescaler configuration range for Timer 2?
    Timer 2 uses a 2-bit prescaler with ratios of 1:1, 1:4, and 1:16.
  • Does Timer 1 support external clock sources?
    Yes, Timer 1 can be driven from an external clock or low-speed sources like a 32kHz crystal.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top