Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer calculation in any microcontroller.

Status
Not open for further replies.

kiranhatti

Newbie level 6
Joined
Oct 18, 2013
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
110
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
 

T

treez

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

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,260
Helped
380
Reputation
760
Reaction score
376
Trophy points
83
Activity points
13,631
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
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,772
Helped
4,350
Reputation
8,709
Reaction score
4,313
Trophy points
1,393
Activity points
130,852
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top