Continue to Site

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.

Selection of timer/counter

Status
Not open for further replies.

Djsarkar

Member level 3
Joined
Jul 27, 2020
Messages
55
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
301
Hi

I want to understand when to use 8 bit timer and 16 bit timer. 8 bit gives 256 counts where as 16 bit counter gives 65536 counts. I am assuming PIC16F877 with 20 MHz crystal.

In example, if I need 10 ms delay, obesiouly 16 bit timer will use to get 10ms delay.

Is it possible to get 10ms delay from 8 bit timer ? how can we get 10ms delay from 8 bit timer ?
 

Without checking the data sheet for exact figures, 10mS is well outside the range you can achieve with TMR0. It is probably also too long for TMR1 even with the prescaler at maximum.

There is an easy way to create longer delays though. Configure interrupts to occur at an appropriate shorter delay and inside the ISR increment a counter. The counter will total the number of times the ISR was called. So if you want 10mS you might count 100 times 0.1mS, a period you probably can count using an 8-bit register. Using this method is also non-blocking so you can create the delay in the background while still doing other things.

Brian.
 
Without checking the data sheet for exact figures, 10mS is well outside the range you can achieve with TMR0. It is probably also too long for TMR1 even with the prescaler at maximum.

Brian.
1ms = 1000 micro second's
10ms = 10,000 micro second's

If one count takes 1 micro second's than 65536 takes 65536 micro second's

But we need to count only 10000 counts for 10ms delay

So we need to load value 55536 into register of TMR1

Why do you think TMR1 is not best option to achieve 10ms delay?
 

TMR1 input frequency is fosc/4 or 5 MHz. Hence you can achieve TMR1 10 ms interval even with 1:1 prescaler. TMR0 can also achieve timing intervall above 10 ms, but not exactly 10 ms.
 

    Djsarkar

    Points: 2
    Helpful Answer Positive Rating
Hi,

a clean mathematical solution is to calculate using prime factorization.

10ms x 20MHz = 200,000 counts

this is: 2^6 x 5^5

often prescalers can just divide by 2^n. In this case 2^6 which is 64.
Now you still have 5^5 (= 3125) done with a counter.
an 8 bit counter can't do this, because it´s limited to 256.
--> so you may do it with a 16 bit counter.

other solutions are:
1)
using an 8 bit counter with 5^3 (= 125). This generates an interrupt every 0.4ms.
Now you may use a software counter for the remaining 5^2 (= 25) to get 10ms (0.4ms x 25 = 10ms)

2a)
using a more suitable XTAL frequency.
in this case: the popular 3.6864MHz.
prescaler: 256
8 bit counter: 144

2b) going to the limit:
prescaler: 256
8 bit counter: 256
makes a 6.5536MHz Quarz. Indeed Farnell does show them but not on stock.

2C)
16MHz
prescaler: 256
8 bit counter: 125 (generates 2ms timeout)
software counter: 5

Klaus
 

    Djsarkar

    Points: 2
    Helpful Answer Positive Rating
With 20MHz crystal, Timer 0 can give very close to 10ms (9.984ms).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top