September 14, 2012

Pic Microcontroller Timer Calculation

Do you get stuck doing Pic microcontroller timer calculation juggling in the middle of datasheet, calculator and your manufacture requirements?

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

Calculations

Typically you will need to focus on the end supervene which is usually to make the timer originate a frequency or portion a time period. This is fairly difficult when you first have to find the data in the datasheet and then list for prescalers and postscalers etc.

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 comprehensive interrupt period. The required period is 18ms so 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-143; // 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

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 dissimilar 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 convert prescaler, postscaler or register value using slider controls. This makes it easy to experiment with dissimilar values as the supervene 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 colse to until you get close to your desired Pic timer period and then adjust the timer value to home in on the literal, 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 Pic-Timer 0 calculator

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 Pic-Timer 1 calculator.

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)

Note: the period register lets you originate an output frequency with no further processing i.e. You don't have to update the timer register value in an interrupt disposition as you do with Timer 0 or Timer 1 - this makes it easy to use (although it's tricky to get right - unless you use the calculator scripts).

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)

Link to Pic-Timer 2 calculator.

Low Pressure Sensors HTC Sensation Screen Protector [DEL] Homemade Chocolate Truffles