Continue to Site

# Timer calculation in any microcontroller.

Status
Not open for further replies.

#### kiranhatti

##### Newbie level 6
Hi,

Please let me know the basic timer value calculation in any microcontroler. Always i find difficult in timer calculation.

For example you take for 1 msec , crystal is 8MHz. prescalar is 1:4

you look in datasheet under "oscillator". In pic its usually one instruction for every 4 clock cycles.

What you ask for is not really practical. The answers you have received so far relate to the Microchip devices but there are many other manufacturers of microcontrollers out there and they can all implement timers however they like.
Even in the Microchip range, the way the timers in the early PIC18F family of devices differs from the way the PIC24, dsPIC33 and PIC32 families implement timers, and some of the later PIC18 devices have differing methods for timers even in the same chip.
For example, some timers have a single 8 or 16 bit register that will trigger interrupts or set flags when they roll over which means they need to be initialised each cycle to some value that it derived from the time required to count form the value up to the rollover value. Others have two sets of registers with one being the 'trigger' value and the other being the timed counter that starts at zero and increments to the trigger value (much easier to have automatically reset each cycle).
Each device will have a data sheet that tells you how to set up the timer. Data sheets are your friend and it really does pay to learn how to read them and to understand the information they contain.
Treez - what you say is true for the PIC12, PIC16 and PIC18 devices (in general - there are exceptions) but not true for the PIC24, dsPIC33 and PIC32 devices where they often use 2 cycles per instruction.
Susan

bigdogguru

### bigdogguru

Points: 2
Hi,

Calculation:

8 MHz, 1ms
8.000.000 x 0.001 = 8.000

The timer needs to timeout after 8000 clock cycles.
With a prescaler of 4 you need 8000 ÷ 4 = 2000 timer counts.

*****
You could use a pre scaler of 64 and get 125 timer counts.
But with higher value pre scaler you get non integer timer count values, this is not possible, therefore this would generate timing errors.y

**

I use prime factorisation to calculate the values.

Klaus

Status
Not open for further replies.